1- require 'cgi'
21require 'net/http'
32require 'net/https'
43require 'json'
54
65module DetectLanguage
76 class Client
8- attr_reader :configuration
7+ attr_reader :config
98
10- def initialize ( configuration )
11- @configuration = configuration
9+ def initialize ( config )
10+ @config = config
1211 end
1312
14- def post ( method , params = { } )
15- execute ( method , params , :http_method => Net ::HTTP ::Post )
13+ def post ( path , params = { } )
14+ execute ( Net ::HTTP ::Post , path , params )
1615 end
1716
18- def get ( method , params = { } )
19- execute ( method , params , :http_method => Net ::HTTP ::Get )
17+ def get ( path , params = { } )
18+ execute ( Net ::HTTP ::Get , path , params )
2019 end
2120
2221 private
2322
24- def execute ( method , params , options )
25- http = setup_http_connection
26- http_method = options [ :http_method ]
27- request = http_method . new ( request_uri ( method ) )
23+ def execute ( method , path , params )
24+ uri = URI . parse ( config . base_url )
25+ http = setup_http_connection ( uri )
26+ request = method . new ( uri . path + path . to_s )
27+ request . set_form_data ( params )
2828
29- if RUBY_VERSION == '1.8.7'
30- set_form_data_18 ( request , params )
31- else
32- request . set_form_data ( params )
33- end
34-
35- request [ 'Authorization' ] = 'Bearer ' + configuration . api_key . to_s
36- request [ 'User-Agent' ] = configuration . user_agent
29+ request [ 'Authorization' ] = 'Bearer ' + config . api_key . to_s
30+ request [ 'User-Agent' ] = config . user_agent
3731
3832 response = http . request ( request )
3933
@@ -55,44 +49,22 @@ def parse_response(response_body)
5549 end
5650 end
5751
58- def request_uri ( method )
59- "/#{ configuration . api_version } /#{ method } "
60- end
61-
62- def setup_http_connection
63- http =
64- Net ::HTTP ::Proxy ( configuration . proxy_host , configuration . proxy_port , configuration . proxy_user ,
65- configuration . proxy_pass ) .
66- new ( configuration . host , configuration . port )
52+ def setup_http_connection ( uri )
53+ http = Net ::HTTP ::Proxy (
54+ config . proxy_host , config . proxy_port , config . proxy_user , config . proxy_pass
55+ ) . new ( uri . host , uri . port )
6756
68- http . read_timeout = configuration . http_read_timeout
69- http . open_timeout = configuration . http_open_timeout
57+ http . read_timeout = config . http_read_timeout
58+ http . open_timeout = config . http_open_timeout
7059
71- if configuration . secure?
72- http . use_ssl = true
73- http . verify_mode = OpenSSL ::SSL ::VERIFY_PEER
60+ if uri . scheme == 'https'
61+ http . use_ssl = true
62+ http . verify_mode = OpenSSL ::SSL ::VERIFY_PEER
7463 else
75- http . use_ssl = false
64+ http . use_ssl = false
7665 end
7766
7867 http
7968 end
80-
81- def set_form_data_18 ( request , params , sep = '&' )
82- request . body = params . map { |k , v |
83- if v . instance_of? ( Array )
84- v . map { |e | "#{ urlencode ( k . to_s ) } =#{ urlencode ( e . to_s ) } " } . join ( sep )
85- else
86- "#{ urlencode ( k . to_s ) } =#{ urlencode ( v . to_s ) } "
87- end
88- } . join ( sep )
89-
90- request . content_type = 'application/x-www-form-urlencoded'
91- end
92-
93- def urlencode ( str )
94- CGI ::escape ( str )
95- end
96-
9769 end
9870end
0 commit comments