Class: RestClient::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/vendor/rest-client/lib/rest_client.rb,
lib/vendor/rest-client/lib/rest_client/request_errors.rb

Overview

backwards compatibility

Constant Summary collapse

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Request

Returns a new instance of Request.



72
73
74
75
76
77
78
79
# File 'lib/vendor/rest-client/lib/rest_client.rb', line 72

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

Instance Attribute Details

#headersObject (readonly)

Returns the value of attribute headers.



66
67
68
# File 'lib/vendor/rest-client/lib/rest_client.rb', line 66

def headers
  @headers
end

#methodObject (readonly)

Returns the value of attribute method.



66
67
68
# File 'lib/vendor/rest-client/lib/rest_client.rb', line 66

def method
  @method
end

#passwordObject (readonly)

Returns the value of attribute password.



66
67
68
# File 'lib/vendor/rest-client/lib/rest_client.rb', line 66

def password
  @password
end

#urlObject (readonly)

Returns the value of attribute url.



66
67
68
# File 'lib/vendor/rest-client/lib/rest_client.rb', line 66

def url
  @url
end

#userObject (readonly)

Returns the value of attribute user.



66
67
68
# File 'lib/vendor/rest-client/lib/rest_client.rb', line 66

def user
  @user
end

Class Method Details

.execute(args, &b) ⇒ Object



68
69
70
# File 'lib/vendor/rest-client/lib/rest_client.rb', line 68

def self.execute(args, &b)
  new(args).execute(&b)
end

Instance Method Details

#default_headersObject



184
185
186
# File 'lib/vendor/rest-client/lib/rest_client.rb', line 184

def default_headers
  @payload.headers.merge({ :accept => 'application/xml' })
end

#execute(&b) ⇒ Object



81
82
83
84
85
86
# File 'lib/vendor/rest-client/lib/rest_client.rb', line 81

def execute(&b)
  execute_inner(&b)
rescue Redirect => e
  @url = e.url
  execute(&b)
end

#execute_inner(&b) ⇒ Object



88
89
90
91
# File 'lib/vendor/rest-client/lib/rest_client.rb', line 88

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

#make_headers(user_headers) ⇒ Object



93
94
95
96
97
98
99
100
# File 'lib/vendor/rest-client/lib/rest_client.rb', line 93

def make_headers(user_headers)
  final = {}
  merged = default_headers.merge(user_headers)
  merged.keys.each do |key|
    final[key.to_s.gsub(/_/, '-').capitalize] = merged[key]
  end
  final
end

#net_http_class(method) ⇒ Object



102
103
104
# File 'lib/vendor/rest-client/lib/rest_client.rb', line 102

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

#parse_url(url) ⇒ Object



106
107
108
109
# File 'lib/vendor/rest-client/lib/rest_client.rb', line 106

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

#parse_url_with_auth(url) ⇒ Object



111
112
113
114
115
116
# File 'lib/vendor/rest-client/lib/rest_client.rb', line 111

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

#payloadObject



180
181
182
# File 'lib/vendor/rest-client/lib/rest_client.rb', line 180

def payload
  @payload
end

#process_payload(p = nil, parent_key = nil) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/vendor/rest-client/lib/rest_client.rb', line 118

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

#process_result(res, &b) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/vendor/rest-client/lib/rest_client.rb', line 158

def process_result(res, &b)
  if %w(200 201 202).include? res.code
    return res.body unless b
  elsif %w(301 302 303).include? res.code
    url = res.header['Location']

    if url !~ /^http/
      uri = URI.parse(@url)
      uri.path = "/#{url}".squeeze('/')
      url = uri.to_s
    end

    raise Redirect, url
  elsif res.code == "401"
    raise Unauthorized
  elsif res.code == "404"
    raise ResourceNotFound
  else
    raise RequestFailed, res
  end
end

#setup_credentials(req) ⇒ Object



154
155
156
# File 'lib/vendor/rest-client/lib/rest_client.rb', line 154

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

#transmit(uri, req, payload, &b) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/vendor/rest-client/lib/rest_client.rb', line 135

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

  net = Net::HTTP.new(uri.host, uri.port)
  net.use_ssl = uri.is_a?(URI::HTTPS)
  net.start do |http|
    ## Ok. I know this is weird but it's a hack for now

    ## this lets process_result determine if it should read the body

    ## into memory or not

    process_result(http.request(req, payload || "", &b), &b)
  end
rescue EOFError
  raise RestClient::ServerBrokeConnection
rescue Timeout::Error
  raise RestClient::RequestTimeout
ensure
  payload.close
end