Class: MingleAPI::Http
- Inherits:
-
Object
- Object
- MingleAPI::Http
- Defined in:
- lib/mingle-api/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.
14 15 16 |
# File 'lib/mingle-api/http.rb', line 14 def initialize(credentials) @credentials = credentials end |
Instance Method Details
#get(url) ⇒ Object
18 19 20 |
# File 'lib/mingle-api/http.rb', line 18 def get(url) process(Net::HTTP::Get, url) end |
#post(url, params) ⇒ Object
22 23 24 |
# File 'lib/mingle-api/http.rb', line 22 def post(url, params) process(Net::HTTP::Post::Multipart, url, params) end |
#process(request_class, url, form_data = {}, headers = nil) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/mingle-api/http.rb', line 26 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[:hmac] ApiAuth.sign!(request, *@credentials[:hmac]) elsif @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
55 56 57 58 59 |
# File 'lib/mingle-api/http.rb', line 55 def to_canonical_response(response) headers = {} response.each_header {|key, value| headers[key] = value } [response.code.to_i, response.body, headers] end |