Class: ItgTest::HTTPClient
- Inherits:
-
Object
- Object
- ItgTest::HTTPClient
- Defined in:
- lib/itg_test/http_client.rb
Instance Attribute Summary collapse
-
#cookie ⇒ Object
Returns the value of attribute cookie.
Instance Method Summary collapse
- #_get(path, limit = 10) ⇒ Object
- #get(path, query = '') ⇒ Object
- #handle_res(res, limit = 10) ⇒ Object
-
#initialize(url) ⇒ HTTPClient
constructor
A new instance of HTTPClient.
- #post(path, params) ⇒ Object
- #session ⇒ Object
- #to_query(params, namespace = nil) ⇒ Object
Constructor Details
#initialize(url) ⇒ HTTPClient
Returns a new instance of HTTPClient.
10 11 12 13 14 15 16 17 18 |
# File 'lib/itg_test/http_client.rb', line 10 def initialize url new_url = if %r|^http://.+$| =~ url || %r|^https://.+$| =~ url url else "http://" + url end @cookie = "" @uri = URI(new_url) end |
Instance Attribute Details
#cookie ⇒ Object
Returns the value of attribute cookie.
9 10 11 |
# File 'lib/itg_test/http_client.rb', line 9 def @cookie end |
Instance Method Details
#_get(path, limit = 10) ⇒ Object
36 37 38 39 40 41 42 43 44 45 |
# File 'lib/itg_test/http_client.rb', line 36 def _get path, limit = 10 raise ArgumentError, 'too many HTTP redirects' if limit == 0 res = session.get(path, {'Cookie'=>@cookie}) = res.get_fields('set-cookie') @cookie = .collect{|ea| ea[/^.*?;/]}.join if final_res = handle_res res, limit JSON.parse final_res rescue JSON::ParserError final_res end |
#get(path, query = '') ⇒ Object
26 27 28 29 30 31 32 33 34 |
# File 'lib/itg_test/http_client.rb', line 26 def get path, query='' path = path[0...-1] if path[-1] == "/" path = "/#{path}" unless path[0] == "/" if query == '' _get path else _get "#{path}?#{to_query(query)}" end end |
#handle_res(res, limit = 10) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/itg_test/http_client.rb', line 65 def handle_res res, limit=10 case res when Net::HTTPRedirection location = res['location'] warn "redirected to #{location}" if %r|^http://.+$| =~ location || %r|^https://.+$| =~ location @uri = URI(location) _get(@uri.path, limit - 1) else _get(location, limit - 1) end when Net::HTTPSuccess res.body else res.value end end |
#post(path, params) ⇒ Object
47 48 49 50 |
# File 'lib/itg_test/http_client.rb', line 47 def post path, params res = session.post path, to_query(params), {'Cookie'=>@cookie} handle_res res end |
#session ⇒ Object
20 21 22 23 24 |
# File 'lib/itg_test/http_client.rb', line 20 def session Net::HTTP.start(@uri.host, @uri.port, use_ssl: @uri.scheme == 'https') rescue Errno::ECONNREFUSED => e sleep(1); retry end |
#to_query(params, namespace = nil) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/itg_test/http_client.rb', line 52 def to_query params, namespace=nil if params.is_a?(Hash) params.collect do |key, value| to_query(value, namespace ? "#{namespace}[#{key}]" : key) end.sort * '&' elsif params.is_a?(Array) prefix = "#{namespace}[]" params.collect { |value| to_query(value, prefix) }.join '&' else "#{CGI.escape(namespace.to_s)}=#{CGI.escape(params.to_s)}" end end |