tylerhunt-relax 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +20 -0
- data/README +171 -0
- data/lib/relax/parsers/base.rb +34 -0
- data/lib/relax/parsers/factory.rb +43 -0
- data/lib/relax/parsers/hpricot.rb +145 -0
- data/lib/relax/parsers/rexml.rb +158 -0
- data/lib/relax/parsers.rb +13 -0
- data/lib/relax/query.rb +46 -0
- data/lib/relax/request.rb +95 -0
- data/lib/relax/response.rb +78 -0
- data/lib/relax/service.rb +101 -0
- data/lib/relax/symbolic_hash.rb +79 -0
- data/lib/relax.rb +13 -0
- data/spec/parsers/factory_spec.rb +29 -0
- data/spec/parsers/hpricot_spec.rb +35 -0
- data/spec/parsers/rexml_spec.rb +40 -0
- data/spec/query_spec.rb +60 -0
- data/spec/request_spec.rb +108 -0
- data/spec/response_spec.rb +98 -0
- data/spec/symbolic_hash_spec.rb +67 -0
- metadata +89 -0
@@ -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
|
@@ -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,89 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: tylerhunt-relax
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.5
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Tyler Hunt
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2008-10-20 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: hpricot
|
17
|
+
version_requirement:
|
18
|
+
version_requirements: !ruby/object:Gem::Requirement
|
19
|
+
requirements:
|
20
|
+
- - ">="
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: "0.6"
|
23
|
+
version:
|
24
|
+
description:
|
25
|
+
email: tyler@tylerhunt.com
|
26
|
+
executables: []
|
27
|
+
|
28
|
+
extensions: []
|
29
|
+
|
30
|
+
extra_rdoc_files:
|
31
|
+
- README
|
32
|
+
- LICENSE
|
33
|
+
files:
|
34
|
+
- lib/relax
|
35
|
+
- lib/relax/parsers
|
36
|
+
- lib/relax/parsers/base.rb
|
37
|
+
- lib/relax/parsers/factory.rb
|
38
|
+
- lib/relax/parsers/hpricot.rb
|
39
|
+
- lib/relax/parsers/rexml.rb
|
40
|
+
- lib/relax/parsers.rb
|
41
|
+
- lib/relax/query.rb
|
42
|
+
- lib/relax/request.rb
|
43
|
+
- lib/relax/response.rb
|
44
|
+
- lib/relax/service.rb
|
45
|
+
- lib/relax/symbolic_hash.rb
|
46
|
+
- lib/relax.rb
|
47
|
+
- spec/parsers/factory_spec.rb
|
48
|
+
- spec/parsers/hpricot_spec.rb
|
49
|
+
- spec/parsers/rexml_spec.rb
|
50
|
+
- spec/query_spec.rb
|
51
|
+
- spec/request_spec.rb
|
52
|
+
- spec/response_spec.rb
|
53
|
+
- spec/symbolic_hash_spec.rb
|
54
|
+
- README
|
55
|
+
- LICENSE
|
56
|
+
has_rdoc: true
|
57
|
+
homepage: http://tylerhunt.com/
|
58
|
+
post_install_message:
|
59
|
+
rdoc_options: []
|
60
|
+
|
61
|
+
require_paths:
|
62
|
+
- lib
|
63
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: "0"
|
68
|
+
version:
|
69
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
70
|
+
requirements:
|
71
|
+
- - ">="
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: "0"
|
74
|
+
version:
|
75
|
+
requirements: []
|
76
|
+
|
77
|
+
rubyforge_project: relax
|
78
|
+
rubygems_version: 1.2.0
|
79
|
+
signing_key:
|
80
|
+
specification_version: 2
|
81
|
+
summary: A simple library for creating REST consumers.
|
82
|
+
test_files:
|
83
|
+
- spec/parsers/factory_spec.rb
|
84
|
+
- spec/parsers/hpricot_spec.rb
|
85
|
+
- spec/parsers/rexml_spec.rb
|
86
|
+
- spec/query_spec.rb
|
87
|
+
- spec/request_spec.rb
|
88
|
+
- spec/response_spec.rb
|
89
|
+
- spec/symbolic_hash_spec.rb
|