11'use strict' ;
22
33var debug = require ( 'debug' ) ( 'httpsnippet' ) ;
4- var mapper = require ( './mapper ' ) ;
4+ var reducer = require ( './reducer ' ) ;
55var qs = require ( 'querystring' ) ;
66var targets = require ( './targets' ) ;
77var url = require ( 'url' ) ;
@@ -37,21 +37,21 @@ var HTTPSnippet = function (req, lang) {
3737 if ( this . source . queryString && this . source . queryString . length ) {
3838 debug ( 'queryString found, constructing queryString pair map' ) ;
3939
40- this . source . queryString . map ( mapper ( this . source . queryObj ) ) ;
40+ this . source . queryObj = this . source . queryString . reduce ( reducer , { } ) ;
4141 }
4242
4343 // construct headers objects
4444 if ( this . source . headers && this . source . headers . length ) {
4545 debug ( 'headers found, constructing header pair map' ) ;
4646
47- this . source . headers . map ( mapper ( this . source . headersObj ) ) ;
47+ this . source . headersObj = this . source . headers . reduce ( reducer , { } ) ;
4848 }
4949
5050 // deconstruct the uri
5151 this . source . uriObj = url . parse ( this . source . url , true , true ) ;
5252
5353 // merge all possible queryString values
54- this . source . queryString = util . _extend ( this . source . uriObj . query , this . source . queryObj ) ;
54+ this . source . queryObj = this . source . queryString = util . _extend ( this . source . uriObj . query , this . source . queryObj ) ;
5555
5656 // reset uriObj values for a clean url
5757 this . source . uriObj . query = null ;
@@ -71,16 +71,12 @@ var HTTPSnippet = function (req, lang) {
7171 } . bind ( this ) ) ;
7272} ;
7373
74- HTTPSnippet . prototype . getSource = function ( ) {
75- return this . source ;
76- } ;
77-
78- HTTPSnippet . prototype . convert = function ( family , target , opts ) {
79- if ( ! opts && target ) {
80- opts = target ;
74+ HTTPSnippet . prototype . convert = function ( target , client , opts ) {
75+ if ( ! opts && client ) {
76+ opts = client ;
8177 }
8278
83- var func = this . _matchTarget ( family , target ) ;
79+ var func = this . _matchTarget ( target , client ) ;
8480
8581 if ( func ) {
8682 return func . call ( this , opts ) ;
@@ -89,107 +85,49 @@ HTTPSnippet.prototype.convert = function (family, target, opts) {
8985 return false ;
9086} ;
9187
92- HTTPSnippet . prototype . _matchTarget = function ( familyName , target ) {
88+ HTTPSnippet . prototype . _matchTarget = function ( target , client ) {
9389 // does it exist?
94- if ( targets [ familyName ] === undefined ) {
90+ if ( ! targets . hasOwnProperty ( target ) ) {
9591 return false ;
9692 }
9793
98- // isolate the family
99- var family = targets [ familyName ] ;
100-
101- // childless targets
102- if ( typeof family === 'function' ) {
103- return family ;
94+ if ( typeof targets [ target ] === 'function' ) {
95+ return targets [ target ] ;
10496 }
10597
106- // find the first default target
107- var defaultTarget = family . _familyInfo ( ) . default ;
108-
10998 // shorthand
110- if ( ! target || typeof target === 'object' ) {
111- target = defaultTarget ;
112- }
113-
114- // asking for a particular target
115- if ( typeof target === 'string' ) {
116- // attempt to call the first one we find
117- if ( typeof family [ target ] !== 'function' ) {
118- target = defaultTarget ;
119- }
120-
121- // last chance
122- if ( typeof family [ target ] === 'function' ) {
123- return family [ target ] ;
124- }
99+ if ( typeof client === 'string' && typeof targets [ target ] [ client ] === 'function' ) {
100+ return targets [ target ] [ client ] ;
125101 }
126102
127- return false ;
103+ // default target
104+ return targets [ target ] [ targets [ target ] . info . default ] ;
128105} ;
129106
130107// exports
131-
132108module . exports = HTTPSnippet ;
133109
134- module . exports . _targets = function ( ) {
135- return Object . keys ( targets ) ;
136- } ;
110+ module . exports . availableTargets = function ( ) {
111+ return Object . keys ( targets ) . map ( function ( key ) {
112+ var target = util . _extend ( { } , targets [ key ] . info ) ;
113+ var clients = Object . keys ( targets [ key ] )
137114
138- module . exports . _familyInfo = function ( family ) {
139- if ( targets [ family ] && targets [ family ] . _familyInfo ) {
140- return targets [ family ] . _familyInfo ( ) ;
141- }
115+ . filter ( function ( prop ) {
116+ return ! ~ [ 'info' , 'index' ] . indexOf ( prop ) ;
117+ } )
142118
143- return false ;
144- } ;
119+ . map ( function ( client ) {
120+ return targets [ key ] [ client ] . info ;
121+ } ) ;
145122
146- module . exports . info = function ( family , target ) {
147- if ( ! targets [ family ] ) {
148- return false ;
149- }
150-
151- if ( typeof targets [ family ] === 'function' ) {
152- return targets [ family ] . info ( ) ;
153- }
154-
155- // get all info for all family members
156- if ( ! target && typeof targets [ family ] === 'object' ) {
157- var results = {
158- family : family
159- } ;
160-
161- results . members = Object . keys ( targets [ family ] )
162- . filter ( function ( key ) {
163- return key !== '_familyInfo' ;
164- } )
165-
166- . map ( function ( target ) {
167- var info = targets [ family ] [ target ] . info ( ) ;
168-
169- delete info . family ;
170-
171- return info ;
172- } ) ;
173-
174- return results ;
175- }
123+ if ( clients . length ) {
124+ target . clients = clients ;
125+ }
176126
177- if ( typeof targets [ family ] === 'object' && typeof targets [ family ] [ target ] === 'function' ) {
178- return targets [ family ] [ target ] . info ( ) ;
179- }
127+ return target ;
128+ } ) ;
180129} ;
181130
182- module . exports . extname = function ( family , target ) {
183- if ( ! targets [ family ] ) {
184- return '' ;
185- }
186-
187- if ( typeof targets [ family ] === 'function' ) {
188- return targets [ family ] . info ( ) . extname ;
189- }
190-
191- // get all info for all family members
192- if ( ! target && typeof targets [ family ] === 'object' ) {
193- return targets [ family ] . _familyInfo ( ) . extname ;
194- }
131+ module . exports . extname = function ( target ) {
132+ return targets [ target ] ? targets [ target ] . info . extname : '' ;
195133} ;
0 commit comments