Class: Grackle::Transport
- Inherits:
-
Object
- Object
- Grackle::Transport
- Defined in:
- lib/grackle/transport.rb
Constant Summary collapse
- CRLF =
"\r\n"
Instance Attribute Summary collapse
-
#debug ⇒ Object
Returns the value of attribute debug.
Instance Method Summary collapse
- #execute_request(method, url, options = {}) ⇒ Object
- #query_string(params) ⇒ Object
- #req_class(method) ⇒ Object
-
#request(method, string_url, options = {}) ⇒ Object
Options are one of - :params - a hash of parameters to be sent with the request.
Instance Attribute Details
#debug ⇒ Object
Returns the value of attribute debug.
16 17 18 |
# File 'lib/grackle/transport.rb', line 16 def debug @debug end |
Instance Method Details
#execute_request(method, url, options = {}) ⇒ Object
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 |
# File 'lib/grackle/transport.rb', line 48 def execute_request(method,url,={}) conn = Net::HTTP.new(url.host, url.port) conn.use_ssl = (url.scheme == 'https') conn.start do |http| req = req_class(method).new(url.request_uri) http.read_timeout = [:timeout] add_headers(req,[:headers]) if file_param?([:params]) add_multipart_data(req,[:params]) else add_form_data(req,[:params]) end if .has_key? :auth if [:auth][:type] == :basic add_basic_auth(req,[:auth]) elsif [:auth][:type] == :oauth add_oauth(http,req,[:auth]) end end dump_request(req) if debug res = http.request(req) dump_response(res) if debug Response.new(method,url.to_s,res.code.to_i,res.body) end end |
#query_string(params) ⇒ Object
74 75 76 77 78 79 80 81 82 83 |
# File 'lib/grackle/transport.rb', line 74 def query_string(params) query = case params when Hash then params.map{|key,value| url_encode_param(key,value) }.join("&") else url_encode(params.to_s) end if !(query == nil || query.length == 0) && query[0,1] != '?' query = "?#{query}" end query end |
#req_class(method) ⇒ Object
20 21 22 23 24 25 26 27 |
# File 'lib/grackle/transport.rb', line 20 def req_class(method) case method when :get then Net::HTTP::Get when :post then Net::HTTP::Post when :put then Net::HTTP::Put when :delete then Net::HTTP::Delete end end |
#request(method, string_url, options = {}) ⇒ Object
Options are one of
-
:params - a hash of parameters to be sent with the request. If a File is a parameter value, \
a multipart request will be sent. If a Time is included, .httpdate will be called on it.
-
:headers - a hash of headers to send with the request
-
:auth - a hash of authentication parameters for either basic or oauth
-
:timeout - timeout for the http request in seconds
35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/grackle/transport.rb', line 35 def request(method, string_url, ={}) params = stringify_params([:params]) if method == :get && params string_url << query_string(params) end url = URI.parse(string_url) begin execute_request(method,url,) rescue Timeout::Error raise "Timeout while #{method}ing #{url.to_s}" end end |