party_resource 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
@@ -11,7 +11,9 @@ module PartyResource
|
|
11
11
|
end
|
12
12
|
|
13
13
|
def fetch(request)
|
14
|
-
|
14
|
+
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)
|
15
17
|
unless (200..399).include? response.code
|
16
18
|
raise PartyResource::Exceptions::ConnectionError.build(response)
|
17
19
|
end
|
@@ -20,6 +22,16 @@ module PartyResource
|
|
20
22
|
|
21
23
|
private
|
22
24
|
|
25
|
+
def log(message)
|
26
|
+
unless PartyResource.logger.nil?
|
27
|
+
if PartyResource.logger.is_a? Proc
|
28
|
+
PartyResource.logger.call message
|
29
|
+
else
|
30
|
+
PartyResource.logger.debug message
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
23
35
|
def options=(options)
|
24
36
|
@options = {}
|
25
37
|
@options[:base_uri] = HTTParty.normalize_base_uri(options[:base_uri]) if options.has_key?(:base_uri)
|
@@ -12,6 +12,17 @@ module PartyResource
|
|
12
12
|
end
|
13
13
|
|
14
14
|
# Add a new named connector
|
15
|
+
# @param [Symbol] name Name for new connector
|
16
|
+
# @param [Hash] options
|
17
|
+
# @option options [String] :base_uri ('') URI to append to all routes using this connector
|
18
|
+
# @option options [String] :username HTTP basic auth username
|
19
|
+
# @option options [String] :password HTTP basic auth password
|
20
|
+
# @option options [Boolean] :default (false) Set this connector as the default
|
21
|
+
# @example
|
22
|
+
# PartyResource::Connector.add(:other_connector, {:base_uri => 'http://otherserver/'})
|
23
|
+
# @example
|
24
|
+
# PartyResource::Connector.add(:my_connector, {:base_uri => 'http://myserver/path', :username => 'fred', :password => 'pass', :default => true})
|
25
|
+
|
15
26
|
def add(name, options)
|
16
27
|
repository.new_connector(name, options)
|
17
28
|
end
|
@@ -3,11 +3,60 @@ Hash.send(:include, ActiveSupport::CoreExtensions::Hash::IndifferentAccess) unle
|
|
3
3
|
|
4
4
|
module PartyResource
|
5
5
|
|
6
|
+
# Set logger to use
|
7
|
+
# @param logger One of:
|
8
|
+
#
|
9
|
+
# nil - no logging
|
10
|
+
#
|
11
|
+
# Proc - log using logger.call(message)
|
12
|
+
#
|
13
|
+
# Object - log using logger.debug(message)
|
14
|
+
def self.logger=(logger)
|
15
|
+
@logger = logger
|
16
|
+
end
|
17
|
+
|
18
|
+
# @private
|
19
|
+
def self.logger
|
20
|
+
@logger
|
21
|
+
end
|
22
|
+
|
6
23
|
module ClassMethods
|
7
24
|
include MethodDefine
|
8
25
|
|
9
26
|
# Connect a method call to a restful uri
|
27
|
+
# @param [Symbol] name for method
|
28
|
+
# @param [Hash] options the options to use to create the route
|
29
|
+
# @option options [String] :get/:put/:post/:delete URI to attach to (key provides the HTTP verb)
|
30
|
+
# @option options :as (:self) How to build data returned by the route
|
31
|
+
#
|
32
|
+
# :raw - raw data
|
33
|
+
#
|
34
|
+
# :self - self.new(data)
|
35
|
+
#
|
36
|
+
# class - class.new(data)
|
37
|
+
#
|
38
|
+
# Array(class, method_name) - class.method_name(data)
|
39
|
+
#
|
40
|
+
# lambda - lambda.call(data)
|
41
|
+
# @option options [Array/Symbol] :with ([]) List of parameter names
|
42
|
+
# @option options :on (:class) Where to attach the method (:class/:instance)
|
43
|
+
# @option options [Hash<Symbol, Symbol>] :including ({}) Hash of extra values to pass into object creation
|
44
|
+
# @option options [Hash<String, Object>] :rescue ({}) Hash of {Exceptions Exception} names to catch and the value to return
|
10
45
|
# @return [nil]
|
46
|
+
# @example
|
47
|
+
# connect :find, :get => '/find/:id.ext', :with => :id, :on => :class
|
48
|
+
# @example
|
49
|
+
# connect :update, :put => '/update/:var.ext', :on => :instance, :as => OtherClass
|
50
|
+
# @example
|
51
|
+
# connect :save, :post => '/save/file', :with => :data, :as => :raw
|
52
|
+
# @example
|
53
|
+
# connect :destroy, :delete => '/delete', :as => [OtherClass, :make_boolean]
|
54
|
+
# @example
|
55
|
+
# connect :foo, :get => '/foo', :with => :value, :as => lambda {|data| "New #{data} Improved" }
|
56
|
+
# @example
|
57
|
+
# connect :fetch_json, :get => '/big_data', :as => [:self, :from_json], :rescue => {'ResourceNotFound' => nil}
|
58
|
+
# @example
|
59
|
+
# connect :include, :get => '/include', :on => :instance, :as => OtherClass, :including => {:thing => :value2}
|
11
60
|
def connect(name, options={})
|
12
61
|
level = options.delete(:on)
|
13
62
|
options = {:as => :self, :connector => @party_connector}.merge(options)
|
@@ -44,6 +93,18 @@ module PartyResource
|
|
44
93
|
# symbol - name
|
45
94
|
#
|
46
95
|
# array - list of nested hash keys
|
96
|
+
# @example
|
97
|
+
# property :value, :from => :input_name
|
98
|
+
# @example
|
99
|
+
# property :value2, :value3
|
100
|
+
# @example
|
101
|
+
# property :nested_value, :from => [:block, :var]
|
102
|
+
# @example
|
103
|
+
# property :other, :as => OtherClass
|
104
|
+
# @example
|
105
|
+
# property :processed, :as => lambda { |data| "Processed: #{data}" }, :to => :output_name
|
106
|
+
# @example
|
107
|
+
# property :child, :as => OtherPartyClass
|
47
108
|
# @return [nil]
|
48
109
|
def property(*names)
|
49
110
|
options = names.pop if names.last.is_a?(Hash)
|
@@ -59,6 +120,9 @@ module PartyResource
|
|
59
120
|
end
|
60
121
|
|
61
122
|
# Set the name of the connector to use for this class
|
123
|
+
# @param [Symbol] name Name of connector
|
124
|
+
# @example
|
125
|
+
# party_connector :my_shiny_connector
|
62
126
|
# @return [nil]
|
63
127
|
def party_connector(name)
|
64
128
|
@party_connector = name
|
@@ -76,7 +140,9 @@ module PartyResource
|
|
76
140
|
end
|
77
141
|
end
|
78
142
|
|
143
|
+
@private
|
79
144
|
module ParameterValues
|
145
|
+
@private
|
80
146
|
def parameter_values(list)
|
81
147
|
list.inject({}) do |out, var|
|
82
148
|
begin
|
@@ -7,6 +7,10 @@ describe TestClass do
|
|
7
7
|
|
8
8
|
let(:object) {TestClass.new(:foo)}
|
9
9
|
|
10
|
+
after do
|
11
|
+
PartyResource.logger = nil
|
12
|
+
end
|
13
|
+
|
10
14
|
describe 'class level call' do
|
11
15
|
it 'raises an argument error when called with the wrong number of arguments' do
|
12
16
|
lambda { TestClass.find }.should raise_error(ArgumentError)
|
@@ -170,4 +174,40 @@ describe TestClass do
|
|
170
174
|
obj.to_properties_hash.should == {:input_name => 'v1', :value2 => 'v2', :block => {:var => 'nv'}, :output_name => 'Processed: Milk', :child => {:thing => 'Happiness'}}
|
171
175
|
end
|
172
176
|
end
|
177
|
+
|
178
|
+
describe 'logging' do
|
179
|
+
context 'with no logger' do
|
180
|
+
it 'does not fail' do
|
181
|
+
stub_request(:get, "http://fred:pass@myserver/path/find/99.ext").to_return(:body => 'some data')
|
182
|
+
TestClass.find(99)
|
183
|
+
end
|
184
|
+
end
|
185
|
+
|
186
|
+
context 'with a logger object' do
|
187
|
+
before do
|
188
|
+
@logger = mock(:logger)
|
189
|
+
PartyResource.logger = @logger
|
190
|
+
end
|
191
|
+
|
192
|
+
it 'logs all api calls to debug' do
|
193
|
+
stub_request(:get, "http://fred:pass@myserver/path/find/99.ext").to_return(:body => 'some data')
|
194
|
+
@logger.should_receive(:debug).with('** PartyResource GET call to /find/99.ext with {:basic_auth=>{:username=>"fred", :password=>"pass"}, :base_uri=>"http://myserver/path"}')
|
195
|
+
TestClass.find(99)
|
196
|
+
end
|
197
|
+
end
|
198
|
+
|
199
|
+
context 'with a logger lambda' do
|
200
|
+
before do
|
201
|
+
@logger = mock(:logger)
|
202
|
+
PartyResource.logger = lambda {|message| @logger.log(message) }
|
203
|
+
end
|
204
|
+
|
205
|
+
it 'logs all api calls to debug' do
|
206
|
+
stub_request(:get, "http://fred:pass@myserver/path/find/99.ext").to_return(:body => 'some data')
|
207
|
+
@logger.should_receive(:log).with('** PartyResource GET call to /find/99.ext with {:basic_auth=>{:username=>"fred", :password=>"pass"}, :base_uri=>"http://myserver/path"}')
|
208
|
+
TestClass.find(99)
|
209
|
+
end
|
210
|
+
end
|
211
|
+
|
212
|
+
end
|
173
213
|
end
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
version: 0.0.
|
8
|
+
- 3
|
9
|
+
version: 0.0.3
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Tristan Harris
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2010-
|
18
|
+
date: 2010-06-02 00:00:00 +01:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|