Class: Instamsg::Request
- Inherits:
-
Object
- Object
- Instamsg::Request
- Defined in:
- lib/instamsg/request.rb
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#params ⇒ Object
readonly
Returns the value of attribute params.
Instance Method Summary collapse
- #download_em_http(urls, concurrency) ⇒ Object
-
#initialize(client, verb, uri, token, params, body = nil) ⇒ Request
constructor
A new instance of Request.
- #send_async ⇒ Object
- #send_sync ⇒ Object
- #upload_sync ⇒ Object
Constructor Details
#initialize(client, verb, uri, token, params, body = nil) ⇒ Request
Returns a new instance of Request.
6 7 8 9 10 11 12 13 14 15 |
# File 'lib/instamsg/request.rb', line 6 def initialize(client, verb, uri, token, params, body = nil) @client, @verb, @uri = client, verb, uri @head = {} @body = body if @body @head['Content-Type'] = 'application/json' end @head['Authorization'] = token @params = params end |
Instance Attribute Details
#body ⇒ Object (readonly)
Returns the value of attribute body.
4 5 6 |
# File 'lib/instamsg/request.rb', line 4 def body @body end |
#params ⇒ Object (readonly)
Returns the value of attribute params.
4 5 6 |
# File 'lib/instamsg/request.rb', line 4 def params @params end |
Instance Method Details
#download_em_http(urls, concurrency) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/instamsg/request.rb', line 81 def download_em_http(urls, concurrency) EventMachine.run do multi = EventMachine::MultiRequest.new EM::Iterator.new(urls, concurrency).each do |url, iterator| req = EventMachine::HttpRequest.new(url).get req.callback do write_file url, req.response iterator.next end multi.add url, req multi.callback { EventMachine.stop } if url == urls.last end end end |
#send_async ⇒ Object
33 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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/instamsg/request.rb', line 33 def send_async if defined?(EventMachine) && EventMachine.reactor_running? http_client = @client.http_async_client(@uri) deferrable = EM::DefaultDeferrable.new http = case @verb when :post http_client.post({ :query => @params, :body => @body, :head => @head }) when :get http_client.get({ :query => @params, :head => @head }) when :put http_client.put({ :query => @params, :body => @body, :head => @head }) when :delete http_client.delete({ :query => @params, :head => @head }) else raise "Unsupported verb" end http.callback { begin deferrable.succeed(handle_response(http.response_header.status, http.response.chomp)) rescue => e deferrable.fail(e) end } http.errback { |e| = "Instamsg connection error : (#{http.error})" Instamsg.logger.debug() deferrable.fail(Error.new()) } return deferrable else http = @client.sync_http_client return http.request_async(@verb, @uri, @params, @body, @head) end end |
#send_sync ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/instamsg/request.rb', line 17 def send_sync http = @client.http_sync_client http.ssl_config.verify_mode = OpenSSL::SSL::VERIFY_NONE begin response = http.request(@verb, @uri, @params, @body, @head) rescue HTTPClient::BadResponseError, HTTPClient::TimeoutError, SocketError, HTTPClient::ReceiveTimeoutError, Errno::ECONNREFUSED => e error = Instamsg::HTTPError.new("#{e.message} (#{e.class})") error.original_error = e raise error end body = response.body ? response.body.chomp : nil return handle_response(response.code.to_i, body) end |
#upload_sync ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/instamsg/request.rb', line 97 def upload_sync http = @client.http_sync_client http.ssl_config.verify_mode = OpenSSL::SSL::VERIFY_NONE begin file = @params["file"] || @params[:file] if file.present? File.open(@params["file"]) do |file| @body = { :file => file} response = http.request(@verb, @uri, @params, @body, @head) body = response.body ? response.body.chomp : nil return handle_response(response.code.to_i, body) end else return handle_response("500", "You have not passed file object.") end rescue HTTPClient::BadResponseError, HTTPClient::TimeoutError, SocketError, HTTPClient::ReceiveTimeoutError, Errno::ECONNREFUSED => e error = Instamsg::HTTPError.new("#{e.message} (#{e.class})") error.original_error = e raise error end end |