relax 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/relax/request.rb +57 -24
- data/lib/relax/response.rb +1 -1
- data/spec/query_spec.rb +6 -6
- data/spec/request_spec.rb +22 -0
- metadata +4 -4
data/lib/relax/request.rb
CHANGED
@@ -4,21 +4,37 @@ module Relax
|
|
4
4
|
# Request is intended to be a parent class for requests passed to
|
5
5
|
# Service#call.
|
6
6
|
class Request
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
7
|
+
@parameters = {}
|
8
|
+
|
9
|
+
# New takes an optional hash of default parameter values. When passed,
|
10
|
+
# the values will be set on the request if the key exists as a valid
|
11
|
+
# parameter name.
|
12
|
+
def initialize(defaults = {})
|
13
|
+
# initialize default parameter values
|
14
|
+
self.class.parameters.each do |parameter, options|
|
15
|
+
if defaults.has_key?(parameter)
|
16
|
+
value = defaults[parameter]
|
17
|
+
elsif options[:value]
|
18
|
+
value = options[:value]
|
19
|
+
end
|
11
20
|
|
12
|
-
|
13
|
-
instance_variable_set "@#{key}", value
|
21
|
+
instance_variable_set("@#{parameter}", value) if value
|
14
22
|
end
|
15
23
|
end
|
16
24
|
|
17
25
|
# Converts this request into a Query object.
|
18
26
|
def to_query
|
19
|
-
keys.inject(Query.new) do |
|
20
|
-
|
21
|
-
parameters
|
27
|
+
self.class.parameters.keys.inject(Query.new) do |query, key|
|
28
|
+
value = send(key)
|
29
|
+
options = self.class.parameters[key]
|
30
|
+
if value && !options[:type]
|
31
|
+
query[convert_key(key)] = value if value
|
32
|
+
elsif options[:type]
|
33
|
+
options[:type].parameters.each do |parameter, options|
|
34
|
+
query[convert_complex_key(key, parameter)] = value.send(parameter) if value
|
35
|
+
end
|
36
|
+
end
|
37
|
+
query
|
22
38
|
end
|
23
39
|
end
|
24
40
|
|
@@ -27,14 +43,40 @@ module Relax
|
|
27
43
|
to_query.to_s
|
28
44
|
end
|
29
45
|
|
46
|
+
# Converts a key when the Request is converted to a query. By default, no
|
47
|
+
# conversion actually takes place, but this method can be overridden by
|
48
|
+
# child classes to perform standard manipulations, such as replacing
|
49
|
+
# underscores.
|
50
|
+
def convert_key(key)
|
51
|
+
key
|
52
|
+
end
|
53
|
+
protected :convert_key
|
54
|
+
|
55
|
+
# Converts a complex key (i.e. a parameter with a custom type) when the
|
56
|
+
# Request is converted to a query. By default, this means the key name and
|
57
|
+
# the parameter name separated by two underscores. This method can be
|
58
|
+
# overridden by child classes.
|
59
|
+
def convert_complex_key(key, parameter)
|
60
|
+
"#{key}.#{parameter}"
|
61
|
+
end
|
62
|
+
protected :convert_complex_key
|
63
|
+
|
30
64
|
class << self
|
65
|
+
# Create the parameters hash for the subclass.
|
66
|
+
def inherited(subclass) #:nodoc:
|
67
|
+
subclass.instance_variable_set('@parameters', {})
|
68
|
+
end
|
69
|
+
|
31
70
|
# Specifies a parameter to create on the request class.
|
32
71
|
#
|
33
72
|
# Options:
|
73
|
+
# - <tt>:type</tt>: An optional custom data type for the parameter.
|
74
|
+
# This must be a class that is a descendent of Request.
|
34
75
|
# - <tt>:value</tt>: The default value for this parameter.
|
35
76
|
def parameter(name, options = {})
|
36
77
|
attr_accessor name
|
37
|
-
|
78
|
+
options = @parameters[name].merge(options) if @parameters.has_key?(name)
|
79
|
+
@parameters[name] = options
|
38
80
|
end
|
39
81
|
|
40
82
|
# Adds a template value to a request class. Equivalent to creating a
|
@@ -42,21 +84,12 @@ module Relax
|
|
42
84
|
def []=(key, value)
|
43
85
|
parameter(key, {:value => value})
|
44
86
|
end
|
45
|
-
end
|
46
|
-
|
47
|
-
protected
|
48
87
|
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
# Converts a key when the Request is converted to a query. By default, no
|
55
|
-
# conversion actually takes place, but this method can be overridden by
|
56
|
-
# child classes to perform standard manipulations, such as replacing
|
57
|
-
# underscores.
|
58
|
-
def convert_key(key)
|
59
|
-
key
|
88
|
+
# Returns a hash of all of the parameters for this request, including
|
89
|
+
# those that are inherited.
|
90
|
+
def parameters #:nodoc:
|
91
|
+
(superclass.respond_to?(:parameters) ? superclass.parameters : {}).merge(@parameters)
|
92
|
+
end
|
60
93
|
end
|
61
94
|
end
|
62
95
|
end
|
data/lib/relax/response.rb
CHANGED
@@ -131,7 +131,7 @@ module Relax
|
|
131
131
|
end
|
132
132
|
|
133
133
|
class << self
|
134
|
-
# When a Response is extended, the
|
134
|
+
# When a Response is extended, the superclass's parameters are copied
|
135
135
|
# into the new class. This behavior has the following side-effect: if
|
136
136
|
# parameters are added to the superclass after it has been extended,
|
137
137
|
# those new paramters won't be passed on to its children. This shouldn't
|
data/spec/query_spec.rb
CHANGED
@@ -11,13 +11,13 @@ describe 'a query' do
|
|
11
11
|
it 'should convert to a query string' do
|
12
12
|
@query[:action] = 'Search'
|
13
13
|
@query[:query] = 'strings'
|
14
|
-
@query.to_s.should
|
14
|
+
@query.to_s.should eql('action=Search&query=strings')
|
15
15
|
end
|
16
16
|
|
17
17
|
it 'should convert its values to strings' do
|
18
18
|
date = Date.today
|
19
19
|
@query[:date] = date
|
20
|
-
@query.to_s.should
|
20
|
+
@query.to_s.should eql("date=#{date.to_s}")
|
21
21
|
end
|
22
22
|
|
23
23
|
it 'should escape its values using "+" instead of "%20"' do
|
@@ -28,18 +28,18 @@ describe 'a query' do
|
|
28
28
|
@query[:charlie] = 3
|
29
29
|
@query[:alpha] = 1
|
30
30
|
@query[:bravo] = 2
|
31
|
-
@query.to_s.should
|
31
|
+
@query.to_s.should eql('alpha=1&bravo=2&charlie=3')
|
32
32
|
end
|
33
33
|
|
34
34
|
it 'should encode its parameter values' do
|
35
35
|
@query[:spaces] = 'two words'
|
36
36
|
@query[:url] = 'http://example.com/'
|
37
|
-
@query.to_s.should
|
37
|
+
@query.to_s.should eql('spaces=two+words&url=http%3A%2F%2Fexample.com%2F')
|
38
38
|
end
|
39
39
|
|
40
40
|
it 'should be able to parse query strings' do
|
41
41
|
parsed_query = Relax::Query.parse(@uri)
|
42
|
-
parsed_query[:action].should
|
43
|
-
parsed_query[:query].should
|
42
|
+
parsed_query[:action].should eql('search')
|
43
|
+
parsed_query[:query].should eql('keyword')
|
44
44
|
end
|
45
45
|
end
|
data/spec/request_spec.rb
CHANGED
@@ -2,10 +2,16 @@ require File.dirname(__FILE__) + '/spec_helper'
|
|
2
2
|
|
3
3
|
require 'relax/request'
|
4
4
|
|
5
|
+
class Amount < Relax::Request
|
6
|
+
parameter :amount
|
7
|
+
parameter :currency
|
8
|
+
end
|
9
|
+
|
5
10
|
class TestRequest < Relax::Request
|
6
11
|
parameter :action
|
7
12
|
parameter :token_id
|
8
13
|
parameter :user_id
|
14
|
+
parameter :amount, :type => Amount
|
9
15
|
end
|
10
16
|
|
11
17
|
class ChildRequest < TestRequest
|
@@ -30,12 +36,14 @@ describe 'a request that converts to a query', :shared => true do
|
|
30
36
|
@query[:action].should eql('Search')
|
31
37
|
@query[:token_id].should eql('123')
|
32
38
|
@query[:user_id].should be_nil
|
39
|
+
@query[:amount].should be_nil
|
33
40
|
end
|
34
41
|
|
35
42
|
it 'should only include parameters in the query if they are set' do
|
36
43
|
@query.key?(:action).should be_true
|
37
44
|
@query.key?(:token_id).should be_true
|
38
45
|
@query.key?(:user_id).should be_false
|
46
|
+
@query.key?(:amount).should be_false
|
39
47
|
end
|
40
48
|
end
|
41
49
|
|
@@ -49,6 +57,7 @@ describe 'a template request' do
|
|
49
57
|
it_should_behave_like 'an option initialized request'
|
50
58
|
|
51
59
|
before(:each) do
|
60
|
+
# this syntax may need to go away unless we can find a way to make it work
|
52
61
|
TestRequest[:api_key] = '123456'
|
53
62
|
TestRequest[:secret] = 'shhh!'
|
54
63
|
end
|
@@ -84,3 +93,16 @@ describe 'a template request' do
|
|
84
93
|
parent.respond_to?(:query).should be_false
|
85
94
|
end
|
86
95
|
end
|
96
|
+
|
97
|
+
describe 'a request with a custom type' do
|
98
|
+
before(:each) do
|
99
|
+
request = TestRequest.new(:action => 'Pay', :token_id => 123)
|
100
|
+
request.amount = Amount.new(:amount => 3.50, :currency => 'USD')
|
101
|
+
@query = request.to_query
|
102
|
+
end
|
103
|
+
|
104
|
+
it 'should add the type parameters to the query' do
|
105
|
+
@query.key?(:"amount.amount").should be_true
|
106
|
+
@query.key?(:"amount.currency").should be_true
|
107
|
+
end
|
108
|
+
end
|
metadata
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
|
-
rubygems_version: 0.9.
|
2
|
+
rubygems_version: 0.9.4
|
3
3
|
specification_version: 1
|
4
4
|
name: relax
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 0.0.
|
7
|
-
date: 2007-11-
|
6
|
+
version: 0.0.4
|
7
|
+
date: 2007-11-17 00:00:00 -05:00
|
8
8
|
summary: A simple library for creating REST consumers.
|
9
9
|
require_paths:
|
10
10
|
- lib
|
@@ -30,12 +30,12 @@ authors:
|
|
30
30
|
- Tyler Hunt
|
31
31
|
files:
|
32
32
|
- lib/relax
|
33
|
-
- lib/relax.rb
|
34
33
|
- lib/relax/query.rb
|
35
34
|
- lib/relax/request.rb
|
36
35
|
- lib/relax/response.rb
|
37
36
|
- lib/relax/service.rb
|
38
37
|
- lib/relax/symbolic_hash.rb
|
38
|
+
- lib/relax.rb
|
39
39
|
- README
|
40
40
|
- LICENSE
|
41
41
|
test_files:
|