forked from jnunemaker/httparty
/
httparty.rb
203 lines (175 loc) · 5.65 KB
/
httparty.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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
81
82
83
84
85
86
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
$:.unshift(File.dirname(__FILE__))
require 'net/http'
require 'net/https'
require 'httparty/module_inheritable_attributes'
require 'rubygems'
module HTTParty
AllowedFormats = {
'text/xml' => :xml,
'application/xml' => :xml,
'application/json' => :json,
'text/json' => :json,
'application/javascript' => :json,
'text/javascript' => :json,
'text/html' => :html,
'application/x-yaml' => :yaml,
'text/yaml' => :yaml,
'text/plain' => :plain
} unless defined?(AllowedFormats)
def self.included(base)
base.extend ClassMethods
base.send :include, HTTParty::ModuleInheritableAttributes
base.send(:mattr_inheritable, :default_options)
base.instance_variable_set("@default_options", {})
end
module ClassMethods
# Allows setting http proxy information to be used
#
# class Foo
# include HTTParty
# http_proxy 'http://foo.com', 80
# end
def http_proxy(addr=nil, port = nil)
default_options[:http_proxyaddr] = addr
default_options[:http_proxyport] = port
end
# Allows setting a base uri to be used for each request.
# Will normalize uri to include http, etc.
#
# class Foo
# include HTTParty
# base_uri 'twitter.com'
# end
def base_uri(uri=nil)
return default_options[:base_uri] unless uri
default_options[:base_uri] = HTTParty.normalize_base_uri(uri)
end
# Allows setting basic authentication username and password.
#
# class Foo
# include HTTParty
# basic_auth 'username', 'password'
# end
def basic_auth(u, p)
default_options[:basic_auth] = {:username => u, :password => p}
end
# Allows setting default parameters to be appended to each request.
# Great for api keys and such.
#
# class Foo
# include HTTParty
# default_params :api_key => 'secret', :another => 'foo'
# end
def default_params(h={})
raise ArgumentError, 'Default params must be a hash' unless h.is_a?(Hash)
default_options[:default_params] ||= {}
default_options[:default_params].merge!(h)
end
# Allows setting a base uri to be used for each request.
#
# class Foo
# include HTTParty
# headers 'Accept' => 'text/html'
# end
def headers(h={})
raise ArgumentError, 'Headers must be a hash' unless h.is_a?(Hash)
default_options[:headers] ||= {}
default_options[:headers].merge!(h)
end
def cookies(h={})
raise ArgumentError, 'Cookies must be a hash' unless h.is_a?(Hash)
default_options[:cookies] ||= CookieHash.new
default_options[:cookies].add_cookies(h)
end
# Allows setting the format with which to parse.
# Must be one of the allowed formats ie: json, xml
#
# class Foo
# include HTTParty
# format :json
# end
def format(f)
raise UnsupportedFormat, "Must be one of: #{AllowedFormats.values.uniq.join(', ')}" unless AllowedFormats.value?(f)
default_options[:format] = f
end
# Allows making a get request to a url.
#
# class Foo
# include HTTParty
# end
#
# # Simple get with full url
# Foo.get('http://foo.com/resource.json')
#
# # Simple get with full url and query parameters
# # ie: http://foo.com/resource.json?limit=10
# Foo.get('http://foo.com/resource.json', :query => {:limit => 10})
def get(path, options={})
perform_request Net::HTTP::Get, path, options
end
# Allows making a post request to a url.
#
# class Foo
# include HTTParty
# end
#
# # Simple post with full url and setting the body
# Foo.post('http://foo.com/resources', :body => {:bar => 'baz'})
#
# # Simple post with full url using :query option,
# # which gets set as form data on the request.
# Foo.post('http://foo.com/resources', :query => {:bar => 'baz'})
def post(path, options={})
perform_request Net::HTTP::Post, path, options
end
def put(path, options={})
perform_request Net::HTTP::Put, path, options
end
def delete(path, options={})
perform_request Net::HTTP::Delete, path, options
end
def default_options #:nodoc:
@default_options
end
private
def perform_request(http_method, path, options) #:nodoc:
process_cookies(options)
Request.new(http_method, path, default_options.dup.merge(options)).perform
end
def process_cookies(options) #:nodoc:
return unless options[:cookies] || default_options[:cookies]
options[:headers] ||= {}
options[:headers]["cookie"] = cookies(options[:cookies] || {}).to_cookie_string
default_options.delete(:cookies)
options.delete(:cookies)
end
end
def self.normalize_base_uri(url) #:nodoc:
normalized_url = url.dup
use_ssl = (normalized_url =~ /^https/) || normalized_url.include?(':443')
ends_with_slash = normalized_url =~ /\/$/
normalized_url.chop! if ends_with_slash
normalized_url.gsub!(/^https?:\/\//i, '')
"http#{'s' if use_ssl}://#{normalized_url}"
end
class Basement #:nodoc:
include HTTParty
end
def self.get(*args)
Basement.get(*args)
end
def self.post(*args)
Basement.post(*args)
end
def self.put(*args)
Basement.put(*args)
end
def self.delete(*args)
Basement.delete(*args)
end
end
require 'httparty/cookie_hash'
require 'httparty/core_extensions'
require 'httparty/exceptions'
require 'httparty/request'
require 'httparty/response'