Class: Shove::Http::Request
- Inherits:
-
Object
- Object
- Shove::Http::Request
- Includes:
- EventMachine::HttpEncoding
- Defined in:
- lib/shove/http/request.rb
Instance Attribute Summary collapse
-
#headers ⇒ Object
Returns the value of attribute headers.
-
#key ⇒ Object
Returns the value of attribute key.
-
#url ⇒ Object
Returns the value of attribute url.
Instance Method Summary collapse
-
#delete(&block) ⇒ Object
HTTP Delete request.
-
#exec(method, params = {}, &block) ⇒ Object
exec a HTTP request, and callback with a response via block.
- #exec_async(method, params = {}, &block) ⇒ Object
- #exec_sync(method, params = {}, &block) ⇒ Object
-
#get(&block) ⇒ Object
HTTP Get request.
-
#initialize(url, app) ⇒ Request
constructor
A new instance of Request.
- #normalize_body(body) ⇒ Object
-
#post(params = {}, &block) ⇒ Object
HTTP Post request for new content.
-
#put(params = {}, &block) ⇒ Object
HTTP Put request for updates.
Constructor Details
#initialize(url, app) ⇒ Request
Returns a new instance of Request.
9 10 11 12 |
# File 'lib/shove/http/request.rb', line 9 def initialize url, app @url = url @key = app.app_key end |
Instance Attribute Details
#headers ⇒ Object
Returns the value of attribute headers.
7 8 9 |
# File 'lib/shove/http/request.rb', line 7 def headers @headers end |
#key ⇒ Object
Returns the value of attribute key.
7 8 9 |
# File 'lib/shove/http/request.rb', line 7 def key @key end |
#url ⇒ Object
Returns the value of attribute url.
7 8 9 |
# File 'lib/shove/http/request.rb', line 7 def url @url end |
Instance Method Details
#delete(&block) ⇒ Object
HTTP Delete request
15 16 17 |
# File 'lib/shove/http/request.rb', line 15 def delete &block exec :delete, &block end |
#exec(method, params = {}, &block) ⇒ Object
exec a HTTP request, and callback with a response via block
86 87 88 89 90 91 92 |
# File 'lib/shove/http/request.rb', line 86 def exec method, params={}, &block if EM.reactor_running? exec_async(method, params, &block) else exec_sync(method, params, &block) end end |
#exec_async(method, params = {}, &block) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/shove/http/request.rb', line 60 def exec_async method, params={}, &block http = EventMachine::HttpRequest.new(url).send(method, { :body => params, :head => { :authorization => ["", key] } }) # handle error http.errback { if block block.call(Response.new(http.response_header.status, http.response, true)) end } # handle success http.callback { status = http.response_header.status if block block.call(Response.new(status, http.response, status >= 400)) end } end |
#exec_sync(method, params = {}, &block) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/shove/http/request.rb', line 34 def exec_sync method, params={}, &block uri = URI.parse(url) req = { :post => Net::HTTP::Post, :put => Net::HTTP::Put, :get => Net::HTTP::Get, :delete => Net::HTTP::Delete }[method].new(uri.path + (uri.query ? "?#{uri.query}" : "")) req.basic_auth "", key http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = uri.scheme == "https" http.verify_mode = OpenSSL::SSL::VERIFY_NONE res = http.request(req, normalize_body(params)) result = Response.new(res.code, res.body, res.code.to_i >= 400) if block_given? block.call(result) end result end |
#get(&block) ⇒ Object
HTTP Get request
30 31 32 |
# File 'lib/shove/http/request.rb', line 30 def get &block exec :get, &block end |
#normalize_body(body) ⇒ Object
94 95 96 |
# File 'lib/shove/http/request.rb', line 94 def normalize_body(body) body.is_a?(Hash) ? form_encode_body(body) : body end |
#post(params = {}, &block) ⇒ Object
HTTP Post request for new content
20 21 22 |
# File 'lib/shove/http/request.rb', line 20 def post params={}, &block exec :post, params, &block end |
#put(params = {}, &block) ⇒ Object
HTTP Put request for updates
25 26 27 |
# File 'lib/shove/http/request.rb', line 25 def put params={}, &block exec :put, params, &block end |