Class: Rest::Wrappers::TyphoeusWrapper

Inherits:
BaseWrapper show all
Defined in:
lib/rest/wrappers/typhoeus_wrapper.rb

Instance Method Summary collapse

Methods inherited from BaseWrapper

#close, #post_file, #to_json_parts

Constructor Details

#initialize(client) ⇒ TyphoeusWrapper

Returns a new instance of TyphoeusWrapper.



36
37
38
# File 'lib/rest/wrappers/typhoeus_wrapper.rb', line 36

def initialize(client)
  @client = client
end

Instance Method Details

#default_typhoeus_optionsObject



40
41
42
43
44
45
46
47
48
49
# File 'lib/rest/wrappers/typhoeus_wrapper.rb', line 40

def default_typhoeus_options
  req_hash = {}
  # todo: should change this timeout to longer if it's for posting file
  req_hash[:connecttimeout] = 5000
  req_hash[:timeout] = 10000
  req_hash[:followlocation] = true
  req_hash[:maxredirs] = 3
  req_hash[:accept_encoding] = 'gzip'
  req_hash
end

#delete(url, req_hash = {}) ⇒ Object



99
100
101
102
103
104
# File 'lib/rest/wrappers/typhoeus_wrapper.rb', line 99

def delete(url, req_hash={})
  req_hash = default_typhoeus_options.merge(req_hash)
  response = Typhoeus::Request.delete(url, req_hash)
  response = handle_response(response)
  response
end

#get(url, req_hash = {}) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'lib/rest/wrappers/typhoeus_wrapper.rb', line 51

def get(url, req_hash={})
  req_hash = default_typhoeus_options.merge(req_hash)
  req_hash[:proxy] = @client.options[:http_proxy] if @client.options[:http_proxy]
  # puts "REQ_HASH=" + req_hash.inspect
  response = Typhoeus::Request.get(url, req_hash)
  #p response
  response = handle_response(response)
  response
end

#handle_response(response) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/rest/wrappers/typhoeus_wrapper.rb', line 61

def handle_response(response)
  r = TyphoeusResponseWrapper.new(response)
  if response.success?
    return r
  elsif response.timed_out?
    raise TyphoeusTimeoutError.new(response)
  elsif response.code == 0
    # Could not get an http response, something's wrong.
    raise Rest::RestError.new("Could not get a response. Curl error 0.")
  else
    raise Rest::HttpError.new(r, r.code)
  end
end

#patch(url, req_hash = {}) ⇒ Object



91
92
93
94
95
96
97
# File 'lib/rest/wrappers/typhoeus_wrapper.rb', line 91

def patch(url, req_hash={})
  req_hash = default_typhoeus_options.merge(req_hash)
  # puts "REQ_HASH=" + req_hash.inspect
  response = Typhoeus::Request.patch(url, req_hash)
  response = handle_response(response)
  response
end

#post(url, req_hash = {}) ⇒ Object



75
76
77
78
79
80
81
# File 'lib/rest/wrappers/typhoeus_wrapper.rb', line 75

def post(url, req_hash={})
  req_hash = default_typhoeus_options.merge(req_hash)
  to_json_parts(req_hash)
  response = Typhoeus::Request.post(url, req_hash)
  response = handle_response(response)
  response
end

#put(url, req_hash = {}) ⇒ Object



83
84
85
86
87
88
89
# File 'lib/rest/wrappers/typhoeus_wrapper.rb', line 83

def put(url, req_hash={})
  req_hash = default_typhoeus_options.merge(req_hash)
  # puts "REQ_HASH=" + req_hash.inspect
  response = Typhoeus::Request.put(url, req_hash)
  response = handle_response(response)
  response
end