Class: HttpMagic::Request
- Inherits:
-
Object
- Object
- HttpMagic::Request
- Defined in:
- lib/http_magic/request.rb
Overview
Encapsulating class to hold all of the infrastructure to make the actual requests to the api. It receives at a minimum a HttpMagic::Uri object which manages the url building.
Example
uri_object = HttpMagic::Uri.new('http://example.com')
request = Request.new(uri_object)
request.get
=> { 'name' => 'Foo Bar' }
Instance Method Summary collapse
-
#delete ⇒ Object
Makes a DELETE request to the url provided by the Uri object and returns the response parsed according to the content type specified in the repsonse.
-
#get ⇒ Object
Makes a GET request to the url provided by the Uri object and returns the response parsed according to the content type specified in the repsonse.
-
#initialize(uri, options = {}) ⇒ Request
constructor
A new instance of Request.
-
#post ⇒ Object
Makes a POST request to the url provided by the Uri object and returns the response parsed according to the content type specified in the repsonse.
-
#put ⇒ Object
Makes a PUT request to the url provided by the Uri object and returns the response parsed according to the content type specified in the repsonse.
Constructor Details
#initialize(uri, options = {}) ⇒ Request
Returns a new instance of Request.
18 19 20 21 22 23 24 |
# File 'lib/http_magic/request.rb', line 18 def initialize(uri, = {}) @uri = uri @data = [:data] || {} = { headers: [:headers] || {} } end |
Instance Method Details
#delete ⇒ Object
Makes a DELETE request to the url provided by the Uri object and returns the response parsed according to the content type specified in the repsonse.
Example
uri_object = HttpMagic::Uri.new('http://example.com')
request = Request.new(uri_object)
request.delete
=> { 'name' => 'Foo Bar' }
38 39 40 |
# File 'lib/http_magic/request.rb', line 38 def delete parse_response http.delete(@uri.urn, [:headers]) end |
#get ⇒ Object
Makes a GET request to the url provided by the Uri object and returns the response parsed according to the content type specified in the repsonse.
Example
uri_object = HttpMagic::Uri.new('http://example.com')
request = Request.new(uri_object)
request.get
=> { 'name' => 'Foo Bar' }
53 54 55 |
# File 'lib/http_magic/request.rb', line 53 def get parse_response http.request_get(@uri.urn, [:headers]) end |
#post ⇒ Object
Makes a POST request to the url provided by the Uri object and returns the response parsed according to the content type specified in the repsonse. If data was provided as an optional initialization parameter, then that is also POSTed.
Example
uri_object = HttpMagic::Uri.new('http://example.com')
request = Request.new(uri_object, data: { name: 'New Foo' })
request.post
69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/http_magic/request.rb', line 69 def post if !@data.empty? [:headers].merge!( 'Content-Type' => 'application/json' ) end parse_response http.request_post( @uri.urn, @data.to_json, [:headers] ) end |
#put ⇒ Object
Makes a PUT request to the url provided by the Uri object and returns the response parsed according to the content type specified in the repsonse. If data was provided as an optional initialization parameter, then that is also PUTed.
Example
uri_object = HttpMagic::Uri.new('http://example.com')
request = Request.new(uri_object, data: { name: 'New Foo' })
request.put
93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/http_magic/request.rb', line 93 def put if !@data.empty? [:headers].merge!( 'Content-Type' => 'application/json' ) end parse_response http.request_put( @uri.urn, @data.to_json, [:headers] ) end |