Class: Grackle::Transport

Inherits:
Object
  • Object
show all
Defined in:
lib/grackle/transport.rb

Constant Summary

CRLF =
"\r\n"
DEFAULT_REDIRECT_LIMIT =
5

Class Attribute Summary (collapse)

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Class Attribute Details

+ (Object) ca_cert_file

Returns the value of attribute ca_cert_file



22
23
24
# File 'lib/grackle/transport.rb', line 22

def ca_cert_file
  @ca_cert_file
end

Instance Attribute Details

- (Object) debug

Returns the value of attribute debug



16
17
18
# File 'lib/grackle/transport.rb', line 16

def debug
  @debug
end

- (Object) proxy

Returns the value of attribute proxy



16
17
18
# File 'lib/grackle/transport.rb', line 16

def proxy
  @proxy
end

Instance Method Details

- (Object) execute_request(method, url, options = {})



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
80
# File 'lib/grackle/transport.rb', line 48

def execute_request(method,url,options={})
  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 = options[:timeout]
    add_headers(req,options[:headers])
    if file_param?(options[:params])
      add_multipart_data(req,options[:params])
    else
      add_form_data(req,options[:params])
    end
    if options.has_key? :auth
      if options[:auth][:type] == :basic
        add_basic_auth(req,options[:auth])
      elsif options[:auth][:type] == :oauth
        add_oauth(http,req,options[:auth])
      end
    end
    dump_request(req) if debug
    res = http.request(req)
    dump_response(res) if debug
    redirect_limit = options[: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']),options.merge(:redirect_limit=>redirect_limit-1))
    else 
      Response.new(method,url.to_s,res.code.to_i,res.body)
    end
  end
end

- (Object) query_string(params)



82
83
84
85
86
87
88
89
90
91
# File 'lib/grackle/transport.rb', line 82

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

- (Object) req_class(method)



25
26
27
# File 'lib/grackle/transport.rb', line 25

def req_class(method)
  Net::HTTP.const_get(method.to_s.capitalize)
end

- (Object) request(method, string_url, options = {})

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, options={})
  params = stringify_params(options[:params])
  if method == :get && params
    string_url << query_string(params)
  end
  url = URI.parse(string_url)
  begin
    execute_request(method,url,options)
  rescue Timeout::Error
    raise "Timeout while #{method}ing #{url.to_s}"
  end
end