party_resource 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -7,6 +7,10 @@ party_resource is a framework for building ruby objects which interact with a RE
7
7
  HTTParty is great for adding a couple of methods that fetch data from an HTTP api, but becomes cumbersome if you have
8
8
  lots of objects that all need to connect to several routes on the api. ActiveResource doesn't give you enough control.
9
9
 
10
+ ## Installation
11
+
12
+ gem install party_resource
13
+
10
14
  ## Usage
11
15
 
12
16
  For detailed usage instructions, please see the [API Documentation](http://yardoc.org/docs/edendevelopment-party_resource).
@@ -27,14 +27,14 @@ module PartyResource
27
27
  repository.new_connector(name, options)
28
28
  end
29
29
 
30
+ def repository
31
+ @repository ||= Repository.new
32
+ end
30
33
  private
31
34
  def default=(name)
32
35
  repository.default = name
33
36
  end
34
37
 
35
- def repository
36
- @repository ||= Repository.new
37
- end
38
38
  end
39
39
 
40
40
  class Repository
@@ -3,24 +3,38 @@ module PartyResource
3
3
  module Connector
4
4
 
5
5
  class Base
6
- attr :options
7
6
 
8
7
  def initialize(name, options)
9
- self.options = options
8
+ set_base_uri(options)
9
+ set_basic_auth(options)
10
+ set_headers(options)
10
11
  @name = name
11
12
  end
12
13
 
13
- def fetch(request)
14
+ def send_request(request)
14
15
  params = request.http_data(options)
15
- log "** PartyResource #{request.verb.to_s.upcase} call to #{request.path} with #{params.inspect}"
16
- response = HTTParty.send(request.verb, request.path, params)
17
- unless (200..399).include? response.code
18
- raise PartyResource::Exceptions::ConnectionError.build(response)
19
- end
20
- response
16
+ log_request(request, params)
17
+ HTTParty.send(request.verb, request.path, params)
18
+ end
19
+
20
+ def fetch(request)
21
+ response = send_request(request)
22
+ return response if successful_response?(response.code)
23
+ raise PartyResource::Exceptions::ConnectionError.build(response)
24
+ end
25
+
26
+ def options
27
+ @options ||= {}
21
28
  end
22
29
 
23
30
  private
31
+ def successful_response?(code)
32
+ (200..399).include? code
33
+ end
34
+
35
+ def log_request(request, params)
36
+ log "** PartyResource #{request.verb.to_s.upcase} call to #{request.path} with #{params.inspect}"
37
+ end
24
38
 
25
39
  def log(message)
26
40
  unless PartyResource.logger.nil?
@@ -32,15 +46,28 @@ module PartyResource
32
46
  end
33
47
  end
34
48
 
35
- def options=(options)
36
- @options = {}
37
- @options[:base_uri] = HTTParty.normalize_base_uri(options[:base_uri]) if options.has_key?(:base_uri)
49
+ def set_base_uri(options)
50
+ set_option(:base_uri, HTTParty.normalize_base_uri(options[:base_uri])) if options.has_key?(:base_uri)
51
+ end
38
52
 
39
- if options.has_key?(:username) || options.has_key?(:password)
40
- @options[:basic_auth] = {:username => options[:username], :password => options[:password]}
53
+ def set_basic_auth(options)
54
+ if requires_basic_auth(options)
55
+ set_option(:basic_auth, :username => options[:username], :password => options[:password])
41
56
  end
42
57
  end
43
58
 
59
+ def set_headers(options)
60
+ set_option(:headers, options[:headers]) if options.has_key?(:headers)
61
+ end
62
+
63
+ def requires_basic_auth(options)
64
+ options.has_key?(:username) || options.has_key?(:password)
65
+ end
66
+
67
+ def set_option(key, value)
68
+ self.options[key] = value
69
+ end
70
+
44
71
  end
45
72
 
46
73
  end
@@ -29,10 +29,10 @@ module PartyResource
29
29
 
30
30
  def self.build(data=nil)
31
31
  klass = case data.code
32
- when 404: ResourceNotFound
33
- when 422: ResourceInvalid
34
- when 400..499: ClientError
35
- when 500..599: ServerError
32
+ when 404 then ResourceNotFound
33
+ when 422 then ResourceInvalid
34
+ when 400..499 then ClientError
35
+ when 500..599 then ServerError
36
36
  else self
37
37
  end
38
38
  klass.new(data)
@@ -14,7 +14,7 @@ module PartyResource
14
14
  options = args.pop if args.last.is_a?(Hash) && args.size == expected_args.size + 1
15
15
  raise ArgumentError, "wrong number of arguments (#{args.size} for #{expected_args.size})" unless expected_args.size == args.size
16
16
  begin
17
- builder.call(connector.fetch(request(context, args, options)), context, included(context))
17
+ builder.call(parsed_response(context, args, options), context, included(context))
18
18
  rescue Exceptions::Error => e
19
19
  name = e.class.name.split(/::/).last
20
20
  return @options[:rescue][name] if @options[:rescue].has_key?(name)
@@ -22,6 +22,10 @@ module PartyResource
22
22
  end
23
23
  end
24
24
 
25
+ def parsed_response(context, args, options)
26
+ connector.fetch(request(context, args, options)).parsed_response
27
+ end
28
+
25
29
  def connector
26
30
  PartyResource::Connector.lookup(@options[:connector])
27
31
  end
metadata CHANGED
@@ -1,99 +1,266 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: party_resource
3
- version: !ruby/object:Gem::Version
4
- prerelease: false
5
- segments:
6
- - 0
7
- - 0
8
- - 3
9
- version: 0.0.3
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.4
5
+ prerelease:
10
6
  platform: ruby
11
- authors:
7
+ authors:
12
8
  - Tristan Harris
13
9
  - Steve Tooke
14
10
  autorequire:
15
11
  bindir: bin
16
12
  cert_chain: []
17
-
18
- date: 2010-06-02 00:00:00 +01:00
19
- default_executable:
20
- dependencies:
21
- - !ruby/object:Gem::Dependency
22
- requirement: &id001 !ruby/object:Gem::Requirement
23
- requirements:
24
- - - ">="
25
- - !ruby/object:Gem::Version
26
- segments:
27
- - 0
28
- - 5
29
- - 2
30
- version: 0.5.2
13
+ date: 2012-07-20 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
31
16
  name: httparty
32
- prerelease: false
17
+ requirement: !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ! '>='
21
+ - !ruby/object:Gem::Version
22
+ version: '0'
33
23
  type: :runtime
34
- version_requirements: *id001
35
- - !ruby/object:Gem::Dependency
36
- requirement: &id002 !ruby/object:Gem::Requirement
37
- requirements:
38
- - - ">="
39
- - !ruby/object:Gem::Version
40
- segments:
41
- - 2
42
- - 3
43
- - 5
44
- version: 2.3.5
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ! '>='
29
+ - !ruby/object:Gem::Version
30
+ version: '0'
31
+ - !ruby/object:Gem::Dependency
45
32
  name: activesupport
33
+ requirement: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ! '>='
37
+ - !ruby/object:Gem::Version
38
+ version: 3.0.1
39
+ type: :runtime
46
40
  prerelease: false
41
+ version_requirements: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ! '>='
45
+ - !ruby/object:Gem::Version
46
+ version: 3.0.1
47
+ - !ruby/object:Gem::Dependency
48
+ name: activesupport
49
+ requirement: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: 3.0.1
47
55
  type: :runtime
48
- version_requirements: *id002
49
- - !ruby/object:Gem::Dependency
50
- requirement: &id003 !ruby/object:Gem::Requirement
51
- requirements:
52
- - - ">="
53
- - !ruby/object:Gem::Version
54
- segments:
55
- - 1
56
- - 2
57
- - 9
58
- version: 1.2.9
59
- name: rspec
60
56
  prerelease: false
57
+ version_requirements: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ! '>='
61
+ - !ruby/object:Gem::Version
62
+ version: 3.0.1
63
+ - !ruby/object:Gem::Dependency
64
+ name: rake
65
+ requirement: !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ! '>='
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
61
71
  type: :development
62
- version_requirements: *id003
63
- - !ruby/object:Gem::Dependency
64
- requirement: &id004 !ruby/object:Gem::Requirement
65
- requirements:
66
- - - ">="
67
- - !ruby/object:Gem::Version
68
- segments:
69
- - 0
70
- version: "0"
71
- name: yard
72
72
  prerelease: false
73
+ version_requirements: !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ! '>='
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ - !ruby/object:Gem::Dependency
80
+ name: jeweler
81
+ requirement: !ruby/object:Gem::Requirement
82
+ none: false
83
+ requirements:
84
+ - - ! '>='
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
73
87
  type: :development
74
- version_requirements: *id004
75
- - !ruby/object:Gem::Dependency
76
- requirement: &id005 !ruby/object:Gem::Requirement
77
- requirements:
78
- - - ">="
79
- - !ruby/object:Gem::Version
80
- segments:
81
- - 0
82
- version: "0"
88
+ prerelease: false
89
+ version_requirements: !ruby/object:Gem::Requirement
90
+ none: false
91
+ requirements:
92
+ - - ! '>='
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ - !ruby/object:Gem::Dependency
83
96
  name: webmock
97
+ requirement: !ruby/object:Gem::Requirement
98
+ none: false
99
+ requirements:
100
+ - - ! '>='
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ type: :development
104
+ prerelease: false
105
+ version_requirements: !ruby/object:Gem::Requirement
106
+ none: false
107
+ requirements:
108
+ - - ! '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rspec
113
+ requirement: !ruby/object:Gem::Requirement
114
+ none: false
115
+ requirements:
116
+ - - ! '>='
117
+ - !ruby/object:Gem::Version
118
+ version: 2.0.0
119
+ type: :development
84
120
  prerelease: false
121
+ version_requirements: !ruby/object:Gem::Requirement
122
+ none: false
123
+ requirements:
124
+ - - ! '>='
125
+ - !ruby/object:Gem::Version
126
+ version: 2.0.0
127
+ - !ruby/object:Gem::Dependency
128
+ name: yard
129
+ requirement: !ruby/object:Gem::Requirement
130
+ none: false
131
+ requirements:
132
+ - - ! '>='
133
+ - !ruby/object:Gem::Version
134
+ version: '0'
85
135
  type: :development
86
- version_requirements: *id005
87
- description: party_resource is a framework for building ruby objects which interact with a REST api. Built on top of HTTParty.
88
- email: dev+party_resource@edendevelopment.co.uk
136
+ prerelease: false
137
+ version_requirements: !ruby/object:Gem::Requirement
138
+ none: false
139
+ requirements:
140
+ - - ! '>='
141
+ - !ruby/object:Gem::Version
142
+ version: '0'
143
+ - !ruby/object:Gem::Dependency
144
+ name: bluecloth
145
+ requirement: !ruby/object:Gem::Requirement
146
+ none: false
147
+ requirements:
148
+ - - ! '>='
149
+ - !ruby/object:Gem::Version
150
+ version: '0'
151
+ type: :development
152
+ prerelease: false
153
+ version_requirements: !ruby/object:Gem::Requirement
154
+ none: false
155
+ requirements:
156
+ - - ! '>='
157
+ - !ruby/object:Gem::Version
158
+ version: '0'
159
+ - !ruby/object:Gem::Dependency
160
+ name: metric_fu
161
+ requirement: !ruby/object:Gem::Requirement
162
+ none: false
163
+ requirements:
164
+ - - ! '>='
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
167
+ type: :development
168
+ prerelease: false
169
+ version_requirements: !ruby/object:Gem::Requirement
170
+ none: false
171
+ requirements:
172
+ - - ! '>='
173
+ - !ruby/object:Gem::Version
174
+ version: '0'
175
+ - !ruby/object:Gem::Dependency
176
+ name: httparty
177
+ requirement: !ruby/object:Gem::Requirement
178
+ none: false
179
+ requirements:
180
+ - - ! '>='
181
+ - !ruby/object:Gem::Version
182
+ version: 0.5.2
183
+ type: :runtime
184
+ prerelease: false
185
+ version_requirements: !ruby/object:Gem::Requirement
186
+ none: false
187
+ requirements:
188
+ - - ! '>='
189
+ - !ruby/object:Gem::Version
190
+ version: 0.5.2
191
+ - !ruby/object:Gem::Dependency
192
+ name: activesupport
193
+ requirement: !ruby/object:Gem::Requirement
194
+ none: false
195
+ requirements:
196
+ - - ! '>='
197
+ - !ruby/object:Gem::Version
198
+ version: 2.3.5
199
+ type: :runtime
200
+ prerelease: false
201
+ version_requirements: !ruby/object:Gem::Requirement
202
+ none: false
203
+ requirements:
204
+ - - ! '>='
205
+ - !ruby/object:Gem::Version
206
+ version: 2.3.5
207
+ - !ruby/object:Gem::Dependency
208
+ name: rspec
209
+ requirement: !ruby/object:Gem::Requirement
210
+ none: false
211
+ requirements:
212
+ - - ! '>='
213
+ - !ruby/object:Gem::Version
214
+ version: 1.2.9
215
+ type: :development
216
+ prerelease: false
217
+ version_requirements: !ruby/object:Gem::Requirement
218
+ none: false
219
+ requirements:
220
+ - - ! '>='
221
+ - !ruby/object:Gem::Version
222
+ version: 1.2.9
223
+ - !ruby/object:Gem::Dependency
224
+ name: yard
225
+ requirement: !ruby/object:Gem::Requirement
226
+ none: false
227
+ requirements:
228
+ - - ! '>='
229
+ - !ruby/object:Gem::Version
230
+ version: '0'
231
+ type: :development
232
+ prerelease: false
233
+ version_requirements: !ruby/object:Gem::Requirement
234
+ none: false
235
+ requirements:
236
+ - - ! '>='
237
+ - !ruby/object:Gem::Version
238
+ version: '0'
239
+ - !ruby/object:Gem::Dependency
240
+ name: webmock
241
+ requirement: !ruby/object:Gem::Requirement
242
+ none: false
243
+ requirements:
244
+ - - ! '>='
245
+ - !ruby/object:Gem::Version
246
+ version: '0'
247
+ type: :development
248
+ prerelease: false
249
+ version_requirements: !ruby/object:Gem::Requirement
250
+ none: false
251
+ requirements:
252
+ - - ! '>='
253
+ - !ruby/object:Gem::Version
254
+ version: '0'
255
+ description: party_resource is a framework for building ruby objects which interact
256
+ with a REST api. Built on top of HTTParty.
257
+ email: steve@heavi.es
89
258
  executables: []
90
-
91
259
  extensions: []
92
-
93
- extra_rdoc_files:
260
+ extra_rdoc_files:
94
261
  - LICENSE
95
262
  - README.md
96
- files:
263
+ files:
97
264
  - lib/party_resource.rb
98
265
  - lib/party_resource/buildable.rb
99
266
  - lib/party_resource/connector.rb
@@ -106,45 +273,31 @@ files:
106
273
  - lib/party_resource/route.rb
107
274
  - LICENSE
108
275
  - README.md
109
- has_rdoc: true
110
- homepage: http://github.com/edendevelopment/party_resource.git
276
+ homepage: http://github.com/tooky/party_resource.git
111
277
  licenses: []
112
-
113
278
  post_install_message:
114
- rdoc_options:
115
- - --charset=UTF-8
116
- require_paths:
279
+ rdoc_options: []
280
+ require_paths:
117
281
  - lib
118
- required_ruby_version: !ruby/object:Gem::Requirement
119
- requirements:
120
- - - ">="
121
- - !ruby/object:Gem::Version
122
- segments:
123
- - 0
124
- version: "0"
125
- required_rubygems_version: !ruby/object:Gem::Requirement
126
- requirements:
127
- - - ">="
128
- - !ruby/object:Gem::Version
129
- segments:
282
+ required_ruby_version: !ruby/object:Gem::Requirement
283
+ none: false
284
+ requirements:
285
+ - - ! '>='
286
+ - !ruby/object:Gem::Version
287
+ version: '0'
288
+ segments:
130
289
  - 0
131
- version: "0"
290
+ hash: 3850846949041232627
291
+ required_rubygems_version: !ruby/object:Gem::Requirement
292
+ none: false
293
+ requirements:
294
+ - - ! '>='
295
+ - !ruby/object:Gem::Version
296
+ version: '0'
132
297
  requirements: []
133
-
134
298
  rubyforge_project:
135
- rubygems_version: 1.3.6
299
+ rubygems_version: 1.8.23
136
300
  signing_key:
137
301
  specification_version: 3
138
302
  summary: Simple REST interface for ruby objects.
139
- test_files:
140
- - spec/fixtures/other_class.rb
141
- - spec/fixtures/test_base_class.rb
142
- - spec/fixtures/test_class.rb
143
- - spec/integration/integration_spec.rb
144
- - spec/spec_helper.rb
145
- - spec/unit/connector/base_spec.rb
146
- - spec/unit/connector_spec.rb
147
- - spec/unit/exceptions_spec.rb
148
- - spec/unit/party_resource_spec.rb
149
- - spec/unit/request_spec.rb
150
- - spec/unit/route_spec.rb
303
+ test_files: []