|
df29a552
»
|
jnunemaker |
2008-07-27 |
Initial commit |
1 |
require 'net/http' |
| |
2 |
require 'net/https' |
| |
3 |
require 'uri' |
|
de9b4fb6
»
|
jnunemaker |
2008-07-27 |
Put in first wave of parsin...  |
4 |
require 'ostruct' |
|
df29a552
»
|
jnunemaker |
2008-07-27 |
Initial commit |
5 |
require 'rubygems' |
| |
6 |
require 'active_support' |
| |
7 |
|
|
fda012a3
»
|
jnunemaker |
2008-11-08 |
Removed stupid core extensi... |
8 |
directory = File.dirname(__FILE__) |
| |
9 |
$:.unshift(directory) unless $:.include?(directory) || $:.include?(File.expand_path(directory)) |
|
ae7ce309
»
|
evri |
2008-09-19 |
Add the ability to automati... |
10 |
|
|
7bdca06b
»
|
reinh |
2008-11-08 |
Moving send_request and fri... |
11 |
require 'httparty/request' |
| |
12 |
|
|
1d48da03
»
|
jnunemaker |
2008-07-28 |
Renamed to HTTParty which i... |
13 |
module HTTParty |
|
76601ba0
»
|
jnunemaker |
2008-08-22 |
Changed format detection fr... |
14 |
class UnsupportedFormat < StandardError; end |
|
ae7ce309
»
|
evri |
2008-09-19 |
Add the ability to automati... |
15 |
class RedirectionTooDeep < StandardError; end |
|
7bdca06b
»
|
reinh |
2008-11-08 |
Moving send_request and fri... |
16 |
|
| |
17 |
AllowedFormats = {:xml => 'text/xml', :json => 'application/json'} |
|
76601ba0
»
|
jnunemaker |
2008-08-22 |
Changed format detection fr... |
18 |
|
|
df29a552
»
|
jnunemaker |
2008-07-27 |
Initial commit |
19 |
def self.included(base) |
| |
20 |
base.extend ClassMethods |
| |
21 |
end |
| |
22 |
|
|
99a07b58
»
|
jnunemaker |
2008-11-11 |
Removed some crappy documen... |
23 |
module ClassMethods |
|
7bdca06b
»
|
reinh |
2008-11-08 |
Moving send_request and fri... |
24 |
def default_options |
| |
25 |
@@default_options ||= {} |
| |
26 |
end |
| |
27 |
|
|
b0f1cc2a
»
|
francxk |
2008-08-18 |
Raises exception when http ... |
28 |
# |
| |
29 |
# Set an http proxy |
| |
30 |
# |
| |
31 |
# class Twitter |
| |
32 |
# include HTTParty |
|
99a07b58
»
|
jnunemaker |
2008-11-11 |
Removed some crappy documen... |
33 |
# http_proxy 'http://myProxy', 1080 |
|
b0f1cc2a
»
|
francxk |
2008-08-18 |
Raises exception when http ... |
34 |
# .... |
| |
35 |
def http_proxy(addr=nil, port = nil) |
|
7bdca06b
»
|
reinh |
2008-11-08 |
Moving send_request and fri... |
36 |
default_options[:http_proxyaddr] = addr |
| |
37 |
default_options[:http_proxyport] = port |
|
b0f1cc2a
»
|
francxk |
2008-08-18 |
Raises exception when http ... |
38 |
end |
| |
39 |
|
|
7bdca06b
»
|
reinh |
2008-11-08 |
Moving send_request and fri... |
40 |
def base_uri(uri=nil) |
| |
41 |
return default_options[:base_uri] unless uri |
| |
42 |
default_options[:base_uri] = normalize_base_uri(uri) |
|
8e143785
»
|
jnunemaker |
2008-07-27 |
get, post, put and delete n... |
43 |
end |
|
7bdca06b
»
|
reinh |
2008-11-08 |
Moving send_request and fri... |
44 |
|
|
8e143785
»
|
jnunemaker |
2008-07-27 |
get, post, put and delete n... |
45 |
def basic_auth(u, p) |
|
7bdca06b
»
|
reinh |
2008-11-08 |
Moving send_request and fri... |
46 |
default_options[:basic_auth] = {:username => u, :password => p} |
|
df29a552
»
|
jnunemaker |
2008-07-27 |
Initial commit |
47 |
end |
| |
48 |
|
|
236b3ad4
»
|
jnunemaker |
2008-07-28 |
Made it so default_params a... |
49 |
def default_params(h={}) |
| |
50 |
raise ArgumentError, 'Default params must be a hash' unless h.is_a?(Hash) |
|
7bdca06b
»
|
reinh |
2008-11-08 |
Moving send_request and fri... |
51 |
default_options[:default_params] ||= {} |
| |
52 |
default_options[:default_params].merge!(h) |
|
0c05dcda
»
|
jnunemaker |
2008-07-28 |
Added #default_params metho... |
53 |
end |
| |
54 |
|
| |
55 |
def headers(h={}) |
| |
56 |
raise ArgumentError, 'Headers must be a hash' unless h.is_a?(Hash) |
|
7bdca06b
»
|
reinh |
2008-11-08 |
Moving send_request and fri... |
57 |
default_options[:headers] ||= {} |
| |
58 |
default_options[:headers].merge!(h) |
|
0c05dcda
»
|
jnunemaker |
2008-07-28 |
Added #default_params metho... |
59 |
end |
| |
60 |
|
|
236b3ad4
»
|
jnunemaker |
2008-07-28 |
Made it so default_params a... |
61 |
def format(f) |
|
76601ba0
»
|
jnunemaker |
2008-08-22 |
Changed format detection fr... |
62 |
raise UnsupportedFormat, "Must be one of: #{AllowedFormats.keys.join(', ')}" unless AllowedFormats.key?(f) |
|
7bdca06b
»
|
reinh |
2008-11-08 |
Moving send_request and fri... |
63 |
default_options[:format] = f |
|
236b3ad4
»
|
jnunemaker |
2008-07-28 |
Made it so default_params a... |
64 |
end |
| |
65 |
|
|
8e143785
»
|
jnunemaker |
2008-07-27 |
get, post, put and delete n... |
66 |
def get(path, options={}) |
|
67cb5725
»
|
jnunemaker |
2008-11-11 |
Switched from send to perfo... |
67 |
perform_request Net::HTTP::Get, path, options |
|
8e143785
»
|
jnunemaker |
2008-07-27 |
get, post, put and delete n... |
68 |
end |
|
236b3ad4
»
|
jnunemaker |
2008-07-28 |
Made it so default_params a... |
69 |
|
|
8e143785
»
|
jnunemaker |
2008-07-27 |
get, post, put and delete n... |
70 |
def post(path, options={}) |
|
67cb5725
»
|
jnunemaker |
2008-11-11 |
Switched from send to perfo... |
71 |
perform_request Net::HTTP::Post, path, options |
|
8e143785
»
|
jnunemaker |
2008-07-27 |
get, post, put and delete n... |
72 |
end |
|
236b3ad4
»
|
jnunemaker |
2008-07-28 |
Made it so default_params a... |
73 |
|
|
8e143785
»
|
jnunemaker |
2008-07-27 |
get, post, put and delete n... |
74 |
def put(path, options={}) |
|
67cb5725
»
|
jnunemaker |
2008-11-11 |
Switched from send to perfo... |
75 |
perform_request Net::HTTP::Put, path, options |
|
8e143785
»
|
jnunemaker |
2008-07-27 |
get, post, put and delete n... |
76 |
end |
|
236b3ad4
»
|
jnunemaker |
2008-07-28 |
Made it so default_params a... |
77 |
|
|
8e143785
»
|
jnunemaker |
2008-07-27 |
get, post, put and delete n... |
78 |
def delete(path, options={}) |
|
67cb5725
»
|
jnunemaker |
2008-11-11 |
Switched from send to perfo... |
79 |
perform_request Net::HTTP::Delete, path, options |
|
8e143785
»
|
jnunemaker |
2008-07-27 |
get, post, put and delete n... |
80 |
end |
|
7bdca06b
»
|
reinh |
2008-11-08 |
Moving send_request and fri... |
81 |
|
|
df29a552
»
|
jnunemaker |
2008-07-27 |
Initial commit |
82 |
private |
|
67cb5725
»
|
jnunemaker |
2008-11-11 |
Switched from send to perfo... |
83 |
def perform_request(http_method, path, options) |
|
b6340ed9
»
|
jnunemaker |
2008-11-11 |
First pass of refactoring t... |
84 |
Request.new(http_method, path, default_options.merge(options)).perform |
|
8e143785
»
|
jnunemaker |
2008-07-27 |
get, post, put and delete n... |
85 |
end |
| |
86 |
|
|
df29a552
»
|
jnunemaker |
2008-07-27 |
Initial commit |
87 |
# Makes it so uri is sure to parse stuff like google.com with the http |
|
bcc38bcf
»
|
jnunemaker |
2008-11-08 |
Cleaned up base uri normali... |
88 |
def normalize_base_uri(url) #:nodoc: |
| |
89 |
use_ssl = (url =~ /^https/) || url.include?(':443') |
| |
90 |
url.chop! if url.ends_with?('/') |
| |
91 |
url.gsub!(/^https?:\/\//i, '') |
| |
92 |
"http#{'s' if use_ssl}://#{url}" |
|
df29a552
»
|
jnunemaker |
2008-07-27 |
Initial commit |
93 |
end |
| |
94 |
end |
|
a2fd0925
»
|
reinh |
2008-11-08 |
Replacing string parameters... |
95 |
end |