Class: RestClient::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/rest_client.rb,
lib/request_errors.rb

Overview

backwards compatibility

Constant Summary collapse

Redirect =
RestClient::Redirect
Unauthorized =
RestClient::Unauthorized
RequestFailed =
RestClient::RequestFailed

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Request

Returns a new instance of Request.



52
53
54
55
56
57
58
59
# File 'lib/rest_client.rb', line 52

def initialize(args)
  @method = args[:method] or raise ArgumentError, "must pass :method"
  @url = args[:url] or raise ArgumentError, "must pass :url"
  @headers = args[:headers] || {}
  @payload = process_payload(args[:payload])
  @user = args[:user]
  @password = args[:password]
end

Class Attribute Details

.default_headersObject

Returns the value of attribute default_headers.



38
39
40
# File 'lib/rest_client.rb', line 38

def default_headers
  @default_headers
end

.timeout_thresholdObject

Returns the value of attribute timeout_threshold.



38
39
40
# File 'lib/rest_client.rb', line 38

def timeout_threshold
  @timeout_threshold
end

Instance Attribute Details

#headersObject (readonly)

Returns the value of attribute headers.



46
47
48
# File 'lib/rest_client.rb', line 46

def headers
  @headers
end

#methodObject (readonly)

Returns the value of attribute method.



46
47
48
# File 'lib/rest_client.rb', line 46

def method
  @method
end

#passwordObject (readonly)

Returns the value of attribute password.



46
47
48
# File 'lib/rest_client.rb', line 46

def password
  @password
end

#payloadObject (readonly)

Returns the value of attribute payload.



46
47
48
# File 'lib/rest_client.rb', line 46

def payload
  @payload
end

#urlObject (readonly)

Returns the value of attribute url.



46
47
48
# File 'lib/rest_client.rb', line 46

def url
  @url
end

#userObject (readonly)

Returns the value of attribute user.



46
47
48
# File 'lib/rest_client.rb', line 46

def user
  @user
end

Class Method Details

.execute(args) ⇒ Object



48
49
50
# File 'lib/rest_client.rb', line 48

def self.execute(args)
  new(args).execute
end

Instance Method Details

#executeObject



61
62
63
64
65
66
# File 'lib/rest_client.rb', line 61

def execute
  execute_inner
rescue Redirect => e
  @url = e.message
  execute
end

#execute_innerObject



68
69
70
71
# File 'lib/rest_client.rb', line 68

def execute_inner
  uri = parse_url_with_auth(url)
  transmit uri, net_http_class(method).new(uri.request_uri, make_headers(headers)), payload
end

#make_headers(user_headers) ⇒ Object



73
74
75
76
77
78
# File 'lib/rest_client.rb', line 73

def make_headers(user_headers)
  merged = self.class.default_headers.merge(user_headers)
  merged.inject({}) do |final, (key, value)|
    final.update(key.to_s.gsub(/_/, '-').capitalize => value)
  end
end

#net_http_class(method) ⇒ Object



80
81
82
# File 'lib/rest_client.rb', line 80

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

#parse_url(url) ⇒ Object



84
85
86
87
# File 'lib/rest_client.rb', line 84

def parse_url(url)
  url = "http://#{url}" unless url.match(/^http/)
  URI.parse(url)
end

#parse_url_with_auth(url) ⇒ Object



89
90
91
92
93
94
# File 'lib/rest_client.rb', line 89

def parse_url_with_auth(url)
  uri = parse_url(url)
  @user = uri.user if uri.user
  @password = uri.password if uri.password
  uri
end

#process_payload(p = nil) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
# File 'lib/rest_client.rb', line 96

def process_payload(p=nil)
  unless p.is_a?(Hash)
    p
  else
    @headers[:content_type] = 'application/x-www-form-urlencoded'
    p.keys.map do |k|
      v = URI.escape(p[k].to_s, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))
      "#{k}=#{v}"
    end.join("&")
  end
end

#process_result(res) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/rest_client.rb', line 128

def process_result(res)
  if %w(200 201 202).include? res.code
    res.body
  elsif %w(301 302 303).include? res.code
    raise Redirect, res.header['Location']
  elsif res.code == "401"
    raise Unauthorized
  elsif res.code == "404"
    raise ResourceNotFound
  else
    raise RequestFailed, res
  end
end

#setup_credentials(req) ⇒ Object



124
125
126
# File 'lib/rest_client.rb', line 124

def setup_credentials(req)
  req.basic_auth(user, password) if user
end

#transmit(uri, req, payload) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/rest_client.rb', line 108

def transmit(uri, req, payload)
  setup_credentials(req)

  net = Net::HTTP.new(uri.host, uri.port)
  net.read_timeout = net.open_timeout = self.class.timeout_threshold if self.class.timeout_threshold
  net.use_ssl = uri.is_a?(URI::HTTPS)

  net.start do |http|
    process_result http.request(req, payload || "")
  end
rescue EOFError
  raise RestClient::ServerBrokeConnection
rescue Timeout::Error
  raise RestClient::RequestTimeout
end