Class: GoAPI::Http
- Inherits:
-
Object
- Object
- GoAPI::Http
- Defined in:
- lib/goapi/http.rb
Defined Under Namespace
Classes: Error
Instance Method Summary collapse
- #get(url) ⇒ Object
-
#initialize(credentials) ⇒ Http
constructor
A new instance of Http.
- #post(url, params) ⇒ Object
- #process(request_class, url, form_data = {}, headers = nil) ⇒ Object
- #to_canonical_response(response) ⇒ Object
Constructor Details
#initialize(credentials) ⇒ Http
Returns a new instance of Http.
12 13 14 |
# File 'lib/goapi/http.rb', line 12 def initialize(credentials) @credentials = credentials || {} end |
Instance Method Details
#get(url) ⇒ Object
16 17 18 |
# File 'lib/goapi/http.rb', line 16 def get(url) process(Net::HTTP::Get, url) end |
#post(url, params) ⇒ Object
20 21 22 |
# File 'lib/goapi/http.rb', line 20 def post(url, params) process(Net::HTTP::Post, url, params) end |
#process(request_class, url, form_data = {}, headers = nil) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/goapi/http.rb', line 24 def process(request_class, url, form_data={}, headers=nil) uri = URI(url) http = Net::HTTP.new(uri.host, uri.port) if uri.scheme == 'https' http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_NONE end request = request_class.new(uri.request_uri, form_data) if headers headers.each do |key, value| request[key] = value end end if @credentials[:basic_auth] request.basic_auth *@credentials[:basic_auth] end response = http.request(request) raise Error.new(request_class, url, response) if response.code.to_i >= 300 to_canonical_response(response) end |
#to_canonical_response(response) ⇒ Object
51 52 53 54 55 |
# File 'lib/goapi/http.rb', line 51 def to_canonical_response(response) headers = {} response.each_header {|key, value| headers[key] = value } [response.code.to_i, response.body, headers] end |