dbalatero-relax 0.0.7.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/relax.rb +13 -0
- data/lib/relax/parsers.rb +13 -0
- data/lib/relax/parsers/base.rb +30 -0
- data/lib/relax/parsers/factory.rb +29 -0
- data/lib/relax/parsers/hpricot.rb +133 -0
- data/lib/relax/parsers/rexml.rb +147 -0
- data/lib/relax/query.rb +46 -0
- data/lib/relax/request.rb +107 -0
- data/lib/relax/response.rb +82 -0
- data/lib/relax/service.rb +102 -0
- data/lib/relax/symbolic_hash.rb +79 -0
- data/spec/parser_helper.rb +49 -0
- data/spec/parsers/factory_spec.rb +29 -0
- data/spec/parsers/hpricot_spec.rb +31 -0
- data/spec/parsers/rexml_spec.rb +36 -0
- data/spec/query_spec.rb +60 -0
- data/spec/request_spec.rb +114 -0
- data/spec/response_spec.rb +98 -0
- data/spec/spec_helper.rb +27 -0
- data/spec/symbolic_hash_spec.rb +67 -0
- metadata +72 -0
@@ -0,0 +1,114 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper'
|
2
|
+
|
3
|
+
require 'relax/request'
|
4
|
+
|
5
|
+
class Amount < Relax::Request
|
6
|
+
parameter :amount
|
7
|
+
parameter :currency
|
8
|
+
end
|
9
|
+
|
10
|
+
class TestRequest < Relax::Request
|
11
|
+
parameter :action, :required => true
|
12
|
+
parameter :token_id
|
13
|
+
parameter :user_id
|
14
|
+
parameter :amount, :type => Amount
|
15
|
+
end
|
16
|
+
|
17
|
+
class ChildRequest < TestRequest
|
18
|
+
parameter :child_id
|
19
|
+
end
|
20
|
+
|
21
|
+
describe 'an option initialized request', :shared => true do
|
22
|
+
it 'should have its values set by the options hash' do
|
23
|
+
request = TestRequest.new(:action => 'FetchAll', :token_id => 123)
|
24
|
+
request.action.should eql('FetchAll')
|
25
|
+
request.token_id.should eql(123)
|
26
|
+
request.user_id.should be_nil
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
describe 'a request that converts to a query', :shared => true do
|
31
|
+
before(:each) do
|
32
|
+
@query = TestRequest.new(:action => 'Search', :token_id => 123).to_query
|
33
|
+
end
|
34
|
+
|
35
|
+
it 'should include its parameters in the query' do
|
36
|
+
@query[:action].should eql('Search')
|
37
|
+
@query[:token_id].should eql('123')
|
38
|
+
@query[:user_id].should be_nil
|
39
|
+
@query[:amount].should be_nil
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'should only include parameters in the query if they are set' do
|
43
|
+
@query.key?(:action).should be_true
|
44
|
+
@query.key?(:token_id).should be_true
|
45
|
+
@query.key?(:user_id).should be_false
|
46
|
+
@query.key?(:amount).should be_false
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
describe 'a normal request' do
|
51
|
+
it_should_behave_like 'a request that converts to a query'
|
52
|
+
it_should_behave_like 'an option initialized request'
|
53
|
+
|
54
|
+
it 'should raise an exception if a required parameter is missing' do
|
55
|
+
lambda {
|
56
|
+
TestRequest.new(:token_id => 123).valid?
|
57
|
+
}.should raise_error(Relax::MissingParameter)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
describe 'a template request' do
|
62
|
+
it_should_behave_like 'a request that converts to a query'
|
63
|
+
it_should_behave_like 'an option initialized request'
|
64
|
+
|
65
|
+
before(:each) do
|
66
|
+
# this syntax may need to go away unless we can find a way to make it work
|
67
|
+
TestRequest[:api_key] = '123456'
|
68
|
+
TestRequest[:secret] = 'shhh!'
|
69
|
+
end
|
70
|
+
|
71
|
+
it 'should always have the template values in its query' do
|
72
|
+
request = TestRequest.new
|
73
|
+
request.api_key.should eql('123456')
|
74
|
+
request.secret.should eql('shhh!')
|
75
|
+
end
|
76
|
+
|
77
|
+
it 'should allow its template variables to be overridden' do
|
78
|
+
request = TestRequest.new(:secret => 'abracadabra')
|
79
|
+
request.api_key.should eql('123456')
|
80
|
+
request.secret.should eql('abracadabra')
|
81
|
+
end
|
82
|
+
|
83
|
+
it 'should pass its template on to its children' do
|
84
|
+
request = ChildRequest.new
|
85
|
+
request.api_key.should eql('123456')
|
86
|
+
request.secret.should eql('shhh!')
|
87
|
+
end
|
88
|
+
|
89
|
+
it 'should allow template parameters on its children that are additive' do
|
90
|
+
ChildRequest[:query] = '1a2b3c'
|
91
|
+
child = ChildRequest.new
|
92
|
+
child.api_key.should eql('123456')
|
93
|
+
child.secret.should eql('shhh!')
|
94
|
+
child.query.should eql('1a2b3c')
|
95
|
+
|
96
|
+
parent = TestRequest.new
|
97
|
+
parent.api_key.should eql('123456')
|
98
|
+
parent.secret.should eql('shhh!')
|
99
|
+
parent.respond_to?(:query).should be_false
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
describe 'a request with a custom type' do
|
104
|
+
before(:each) do
|
105
|
+
request = TestRequest.new(:action => 'Pay', :token_id => 123)
|
106
|
+
request.amount = Amount.new(:amount => 3.50, :currency => 'USD')
|
107
|
+
@query = request.to_query
|
108
|
+
end
|
109
|
+
|
110
|
+
it 'should add the type parameters to the query' do
|
111
|
+
@query.key?(:"amount.amount").should be_true
|
112
|
+
@query.key?(:"amount.currency").should be_true
|
113
|
+
end
|
114
|
+
end
|
@@ -0,0 +1,98 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper'
|
2
|
+
|
3
|
+
|
4
|
+
class BaseResponse < Relax::Response
|
5
|
+
parameter :status, :required => true
|
6
|
+
parameter :request_id, :element => :requestid, :type => :integer
|
7
|
+
end
|
8
|
+
|
9
|
+
class TestResponse < BaseResponse
|
10
|
+
class Token < Relax::Response
|
11
|
+
parameter :token_id, :element => :tokenid
|
12
|
+
parameter :status
|
13
|
+
end
|
14
|
+
|
15
|
+
class Error < Relax::Response
|
16
|
+
parser :hpricot
|
17
|
+
parameter :code, :type => :integer
|
18
|
+
parameter :message
|
19
|
+
end
|
20
|
+
|
21
|
+
parameter :valid_request, :element => :requestid, :attribute => :valid
|
22
|
+
parameter :tokens, :collection => Token
|
23
|
+
parameter :error, :type => Error
|
24
|
+
end
|
25
|
+
|
26
|
+
|
27
|
+
describe 'a response' do
|
28
|
+
before(:each) do
|
29
|
+
@response = Relax::Response.new(XML)
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'should allow access to the root' do
|
33
|
+
root = @response.root
|
34
|
+
root.should be_an_instance_of(Hpricot::Elem)
|
35
|
+
root.name.should eql('RESTResponse')
|
36
|
+
end
|
37
|
+
|
38
|
+
it 'should be checkable by the name of its root' do
|
39
|
+
@response.is?(:RESTResponse).should be_true
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'should allow access to an element by its name' do
|
43
|
+
@response.element(:RequestId).should be_an_instance_of(Hpricot::Elem)
|
44
|
+
end
|
45
|
+
|
46
|
+
it 'should allow access to an element\'s elements by its name' do
|
47
|
+
tokens = @response.elements(:Tokens)
|
48
|
+
tokens.should be_an_instance_of(Hpricot::Elements)
|
49
|
+
tokens.should_not be_empty
|
50
|
+
end
|
51
|
+
|
52
|
+
it 'should allow access to an element\'s value by its name' do
|
53
|
+
token = Relax::Response.new(@response.elements(:Tokens).first)
|
54
|
+
token.element(:TokenId).inner_text.should eql('JPMQARDVJK')
|
55
|
+
token.element(:Status).inner_text.should eql('Active')
|
56
|
+
end
|
57
|
+
|
58
|
+
it 'should have a means of checking for the existence of a node' do
|
59
|
+
@response.has?(:Status).should_not be_nil
|
60
|
+
@response.has?(:Errors).should be_nil
|
61
|
+
end
|
62
|
+
|
63
|
+
it 'should be able to define children of Response without modifying parent' do
|
64
|
+
Relax::Response.new(XML).respond_to?(:status).should be_false
|
65
|
+
TestResponse.new(XML).respond_to?(:status).should be_true
|
66
|
+
end
|
67
|
+
|
68
|
+
it 'should automatically pull parameters from the XML' do
|
69
|
+
response = TestResponse.new(XML)
|
70
|
+
response.valid_request.should eql('true')
|
71
|
+
response.tokens.length.should eql(2)
|
72
|
+
response.tokens.first.status.should eql('Active')
|
73
|
+
response.error.code.should eql(1)
|
74
|
+
response.error.message.should eql('Failed')
|
75
|
+
end
|
76
|
+
|
77
|
+
it "should automatically pull its parent's parameters from the XML" do
|
78
|
+
response = TestResponse.new(XML)
|
79
|
+
response.status.should eql('Success')
|
80
|
+
response.request_id.should eql(44287)
|
81
|
+
end
|
82
|
+
|
83
|
+
it 'should be relationally equivalent to its children' do
|
84
|
+
(Relax::Response === TestResponse).should be_true
|
85
|
+
end
|
86
|
+
|
87
|
+
it 'should raise MissingParameter if required parameters are missing' do
|
88
|
+
proc { TestResponse.new('') }.should raise_error(Relax::MissingParameter)
|
89
|
+
end
|
90
|
+
|
91
|
+
it 'should use the default parser when undefined' do
|
92
|
+
TestResponse::Token.new('').parser_name.should ==:default
|
93
|
+
end
|
94
|
+
|
95
|
+
it 'should use the defined parser when given' do
|
96
|
+
TestResponse::Error.new('').parser_name.should ==:hpricot
|
97
|
+
end
|
98
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'spec'
|
3
|
+
|
4
|
+
require File.dirname(__FILE__) + '/../lib/relax'
|
5
|
+
|
6
|
+
unless defined?(XML)
|
7
|
+
XML = <<-EOF
|
8
|
+
<?xml version="1.0"?>
|
9
|
+
<RESTResponse xmlns:ns1="http://namespace.url">
|
10
|
+
<Tokens>
|
11
|
+
<TokenId>JPMQARDVJK</TokenId>
|
12
|
+
<Status>Active</Status>
|
13
|
+
</Tokens>
|
14
|
+
<Tokens>
|
15
|
+
<TokenId>RDVJKJPMQA</TokenId>
|
16
|
+
<Status>Inactive</Status>
|
17
|
+
</Tokens>
|
18
|
+
<Status>Success</Status>
|
19
|
+
<RequestId valid="true">44287</RequestId>
|
20
|
+
<ns1:Namespace>Passed</ns1:Namespace>
|
21
|
+
<Error>
|
22
|
+
<Code>1</Code>
|
23
|
+
<Message>Failed</Message>
|
24
|
+
</Error>
|
25
|
+
</RESTResponse>
|
26
|
+
EOF
|
27
|
+
end
|
@@ -0,0 +1,67 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper'
|
2
|
+
|
3
|
+
require 'relax/symbolic_hash'
|
4
|
+
|
5
|
+
describe 'a symbolic hash' do
|
6
|
+
before(:each) do
|
7
|
+
@url = 'http://example.com/'
|
8
|
+
@query = Relax::SymbolicHash.new
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'should be accessible via string or symbol keys' do
|
12
|
+
@query[:amount] = 10
|
13
|
+
@query[:amount].should == 10
|
14
|
+
@query['amount'].should == 10
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'should convert keys to symbols' do
|
18
|
+
@query['symbol'] = 'aleph'
|
19
|
+
@query[:symbol].should == 'aleph'
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'should convert keys to symbols' do
|
23
|
+
@query['symbol'] = 'aleph'
|
24
|
+
@query[:symbol].should == 'aleph'
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'should test for keys by symbol' do
|
28
|
+
@query[:symbol] = 'aleph'
|
29
|
+
@query.key?('symbol').should be_true
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'should delete values with a symbolic key' do
|
33
|
+
@query[:symbol] = 'aleph'
|
34
|
+
@query.delete('symbol')
|
35
|
+
@query.key?(:symbol).should be_false
|
36
|
+
end
|
37
|
+
|
38
|
+
it 'should be mergeable' do
|
39
|
+
@query[:one] = 2
|
40
|
+
merged_query = @query.merge({ :one => 1, :two => 2 })
|
41
|
+
merged_query[:one].should == 1
|
42
|
+
merged_query[:two].should == 2
|
43
|
+
end
|
44
|
+
|
45
|
+
it 'should be able to duplicate itself' do
|
46
|
+
@query[:one] = 'uno'
|
47
|
+
@query[:two] = 'dos'
|
48
|
+
new_query = @query.dup
|
49
|
+
new_query[:one].should == 'uno'
|
50
|
+
new_query[:two].should == 'dos'
|
51
|
+
|
52
|
+
@query[:three] == 'tres'
|
53
|
+
new_query.key?(:three).should be_false
|
54
|
+
end
|
55
|
+
|
56
|
+
it 'should be able to get multiple values by symbol' do
|
57
|
+
@query[:one] = 1
|
58
|
+
@query[:two] = 2
|
59
|
+
@query.values_at(:one, :two).should == [1, 2]
|
60
|
+
end
|
61
|
+
|
62
|
+
it 'should be instantiable with a hash' do
|
63
|
+
query = Relax::SymbolicHash.new({ :one => 1, :two => 2 })
|
64
|
+
query[:one].should == 1
|
65
|
+
query[:two].should == 2
|
66
|
+
end
|
67
|
+
end
|
metadata
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: dbalatero-relax
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.7.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Tyler Hunt
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-03-03 00:00:00 -08:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: A simple library for creating REST consumers.
|
17
|
+
email:
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files: []
|
23
|
+
|
24
|
+
files:
|
25
|
+
- lib/relax/parsers/base.rb
|
26
|
+
- lib/relax/parsers/factory.rb
|
27
|
+
- lib/relax/parsers/hpricot.rb
|
28
|
+
- lib/relax/parsers/rexml.rb
|
29
|
+
- lib/relax/parsers.rb
|
30
|
+
- lib/relax/query.rb
|
31
|
+
- lib/relax/request.rb
|
32
|
+
- lib/relax/response.rb
|
33
|
+
- lib/relax/service.rb
|
34
|
+
- lib/relax/symbolic_hash.rb
|
35
|
+
- lib/relax.rb
|
36
|
+
- spec/parser_helper.rb
|
37
|
+
- spec/parsers/factory_spec.rb
|
38
|
+
- spec/parsers/hpricot_spec.rb
|
39
|
+
- spec/parsers/rexml_spec.rb
|
40
|
+
- spec/query_spec.rb
|
41
|
+
- spec/request_spec.rb
|
42
|
+
- spec/response_spec.rb
|
43
|
+
- spec/spec_helper.rb
|
44
|
+
- spec/symbolic_hash_spec.rb
|
45
|
+
has_rdoc: false
|
46
|
+
homepage: http://github.com/tylerhunt/relax/tree/master
|
47
|
+
post_install_message:
|
48
|
+
rdoc_options: []
|
49
|
+
|
50
|
+
require_paths:
|
51
|
+
- lib
|
52
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: "0"
|
57
|
+
version:
|
58
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: "0"
|
63
|
+
version:
|
64
|
+
requirements: []
|
65
|
+
|
66
|
+
rubyforge_project:
|
67
|
+
rubygems_version: 1.2.0
|
68
|
+
signing_key:
|
69
|
+
specification_version: 2
|
70
|
+
summary: AWS FPS Ruby API
|
71
|
+
test_files: []
|
72
|
+
|