/
resource.rb
168 lines (153 loc) · 5.15 KB
/
resource.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
module RestClient
# A class that can be instantiated for access to a RESTful resource,
# including authentication.
#
# Example:
#
# resource = RestClient::Resource.new('http://some/resource')
# jpg = resource.get(:accept => 'image/jpg')
#
# With HTTP basic authentication:
#
# resource = RestClient::Resource.new('http://protected/resource', :user => 'user', :password => 'password')
# resource.delete
#
# With a timeout (seconds):
#
# RestClient::Resource.new('http://slow', :read_timeout => 10)
#
# With an open timeout (seconds):
#
# RestClient::Resource.new('http://behindfirewall', :open_timeout => 10)
#
# You can also use resources to share common headers. For headers keys,
# symbols are converted to strings. Example:
#
# resource = RestClient::Resource.new('http://some/resource', :headers => { :client_version => 1 })
#
# This header will be transported as X-Client-Version (notice the X prefix,
# capitalization and hyphens)
#
# Use the [] syntax to allocate subresources:
#
# site = RestClient::Resource.new('http://example.com', :user => 'adam', :password => 'mypasswd')
# site['posts/1/comments'].post 'Good article.', :content_type => 'text/plain'
#
class Resource
attr_reader :url, :options, :block
def initialize(url, options={}, backwards_compatibility=nil, &block)
@url = url
@block = block
if options.class == Hash
@options = options
else # compatibility with previous versions
@options = { :user => options, :password => backwards_compatibility }
end
end
def get(additional_headers={}, &block)
headers = (options[:headers] || {}).merge(additional_headers)
Request.execute(options.merge(
:method => :get,
:url => url,
:headers => headers), &(block || @block))
end
def head(additional_headers={}, &block)
headers = (options[:headers] || {}).merge(additional_headers)
Request.execute(options.merge(
:method => :head,
:url => url,
:headers => headers), &(block || @block))
end
def post(payload, additional_headers={}, &block)
headers = (options[:headers] || {}).merge(additional_headers)
Request.execute(options.merge(
:method => :post,
:url => url,
:payload => payload,
:headers => headers), &(block || @block))
end
def put(payload, additional_headers={}, &block)
headers = (options[:headers] || {}).merge(additional_headers)
Request.execute(options.merge(
:method => :put,
:url => url,
:payload => payload,
:headers => headers), &(block || @block))
end
def patch(payload, additional_headers={}, &block)
headers = (options[:headers] || {}).merge(additional_headers)
Request.execute(options.merge(
:method => :patch,
:url => url,
:payload => payload,
:headers => headers), &(block || @block))
end
def delete(additional_headers={}, &block)
headers = (options[:headers] || {}).merge(additional_headers)
Request.execute(options.merge(
:method => :delete,
:url => url,
:headers => headers), &(block || @block))
end
def to_s
url
end
def user
options[:user]
end
def password
options[:password]
end
def headers
options[:headers] || {}
end
def read_timeout
options[:read_timeout]
end
def open_timeout
options[:open_timeout]
end
# Construct a subresource, preserving authentication.
#
# Example:
#
# site = RestClient::Resource.new('http://example.com', 'adam', 'mypasswd')
# site['posts/1/comments'].post 'Good article.', :content_type => 'text/plain'
#
# This is especially useful if you wish to define your site in one place and
# call it in multiple locations:
#
# def orders
# RestClient::Resource.new('http://example.com/orders', 'admin', 'mypasswd')
# end
#
# orders.get # GET http://example.com/orders
# orders['1'].get # GET http://example.com/orders/1
# orders['1/items'].delete # DELETE http://example.com/orders/1/items
#
# Nest resources as far as you want:
#
# site = RestClient::Resource.new('http://example.com')
# posts = site['posts']
# first_post = posts['1']
# comments = first_post['comments']
# comments.post 'Hello', :content_type => 'text/plain'
#
def [](suburl, &new_block)
case
when block_given? then self.class.new(concat_urls(url, suburl), options, &new_block)
when block then self.class.new(concat_urls(url, suburl), options, &block)
else self.class.new(concat_urls(url, suburl), options)
end
end
def concat_urls(url, suburl) # :nodoc:
url = url.to_s
suburl = suburl.to_s
if url.slice(-1, 1) == '/' or suburl.slice(0, 1) == '/'
url + suburl
else
"#{url}/#{suburl}"
end
end
end
end