Class: Grackle::Transport
- Inherits:
-
Object
- Object
- Grackle::Transport
- Defined in:
- lib/grackle/transport.rb
Constant Summary collapse
- CRLF =
"\r\n"
- DEFAULT_REDIRECT_LIMIT =
5
Class Attribute Summary collapse
-
.ca_cert_file ⇒ Object
Returns the value of attribute ca_cert_file.
Instance Attribute Summary collapse
-
#debug ⇒ Object
Returns the value of attribute debug.
-
#proxy ⇒ Object
Returns the value of attribute proxy.
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.
Class Attribute Details
.ca_cert_file ⇒ Object
Returns the value of attribute ca_cert_file.
60 61 62 |
# File 'lib/grackle/transport.rb', line 60 def ca_cert_file @ca_cert_file end |
Instance Attribute Details
#debug ⇒ Object
Returns the value of attribute debug.
54 55 56 |
# File 'lib/grackle/transport.rb', line 54 def debug @debug end |
#proxy ⇒ Object
Returns the value of attribute proxy.
54 55 56 |
# File 'lib/grackle/transport.rb', line 54 def proxy @proxy end |
Instance Method Details
#execute_request(method, url, options = {}) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/grackle/transport.rb', line 87 def execute_request(method,url,={}) conn = http_class.new(url.host, url.port) conn.use_ssl = (url.scheme == 'https') if conn.use_ssl? configure_ssl(conn) end 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 redirect_limit = [:redirect_limit] || DEFAULT_REDIRECT_LIMIT if res.code.to_s =~ /^3\d\d$/ && redirect_limit > 0 && res['location'] execute_request(method,URI.parse(res['location']),.merge(:redirect_limit=>redirect_limit-1)) else headers = filter_headers([:response_headers],res) Response.new(method,url.to_s,res.code.to_i,res.body,headers) end end end |
#query_string(params) ⇒ Object
122 123 124 125 126 127 128 129 130 131 |
# File 'lib/grackle/transport.rb', line 122 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
63 64 65 |
# File 'lib/grackle/transport.rb', line 63 def req_class(method) Net::HTTP.const_get(method.to_s.capitalize) 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
-
:response_headers - a list of headers to return with the response
74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/grackle/transport.rb', line 74 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 |