relax 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -11,50 +11,65 @@ module Relax
11
11
  # stores an XML document, and provides access to it through methods like
12
12
  # #element and #attribute.
13
13
  class Response
14
+ attr_accessor :raw
14
15
  attr_accessor :xml
15
16
 
16
17
  # New takes in the XML from the response. For the initial response, this
17
18
  # will be the root element, but child elements may also be passed into
18
19
  # Response objects.
20
+ #
21
+ # This will raise a MissingParameter error if a parameterd marked as
22
+ # required is not present in the XML response.
19
23
  def initialize(xml)
24
+ @raw = xml
20
25
  @xml = Hpricot.XML(xml.to_s)
21
26
 
22
- if parameter = self.class.instance_variable_get('@parameters')
23
- parameter.each do |parameter, options|
24
- element = options[:element] ? options[:element] : parameter
25
-
26
- if attribute = options[:attribute] and attribute == true
27
- node = attribute(root, element)
28
- elsif attribute
29
- node = attribute(element(element), attribute)
30
- elsif options[:collection]
31
- node = elements(element)
32
- else
33
- node = element(element)
34
- end
35
-
36
- if options[:collection]
37
- value = node.collect do |element|
38
- options[:collection].new(element)
27
+ if parameters = self.class.instance_variable_get('@parameters')
28
+ parameters.each do |parameter, options|
29
+ begin
30
+ element = options[:element] || parameter
31
+
32
+ if attribute = options[:attribute] and attribute == true
33
+ node = attribute(root, element)
34
+ elsif attribute
35
+ node = attribute(element(element), attribute)
36
+ elsif options[:collection]
37
+ node = elements(element)
38
+ else
39
+ node = element(element)
39
40
  end
40
- else
41
- case type = options[:type]
42
- when Response
43
- value = type.new(node)
44
41
 
45
- when :float
46
- value = float_value(node)
42
+ if options[:collection]
43
+ value = node.collect do |element|
44
+ options[:collection].new(element)
45
+ end
46
+ else
47
+ case type = options[:type]
48
+ when Response
49
+ value = type.new(node)
50
+
51
+ when :date
52
+ value = date_value(node)
47
53
 
48
- when :integer
49
- value = integer_value(node)
54
+ when :time
55
+ value = time_value(node)
50
56
 
51
- when :text
52
- else
53
- value = text_value(node)
57
+ when :float
58
+ value = float_value(node)
59
+
60
+ when :integer
61
+ value = integer_value(node)
62
+
63
+ when :text
64
+ else
65
+ value = text_value(node)
66
+ end
54
67
  end
55
- end
56
68
 
57
- instance_variable_set("@#{parameter}", value)
69
+ instance_variable_set("@#{parameter}", value)
70
+ rescue Hpricot::Error
71
+ raise MissingParameter if options[:required]
72
+ end
58
73
  end
59
74
  end
60
75
  end
@@ -110,7 +125,23 @@ module Relax
110
125
  Date.parse(value(value))
111
126
  end
112
127
 
128
+ # Gets a time value.
129
+ def time_value(value)
130
+ Time.parse(value(value))
131
+ end
132
+
113
133
  class << self
134
+ # When a Response is extended, the superclasses parameters are copied
135
+ # into the new class. This behavior has the following side-effect: if
136
+ # parameters are added to the superclass after it has been extended,
137
+ # those new paramters won't be passed on to its children. This shouldn't
138
+ # be a problem in most cases.
139
+ def inherited(subclass)
140
+ @parameters.each do |name, options|
141
+ subclass.parameter(name, options)
142
+ end if @parameters
143
+ end
144
+
114
145
  # Specifes a parameter that will be automatically parsed when the
115
146
  # Response is instantiated.
116
147
  #
data/lib/relax.rb CHANGED
@@ -5,3 +5,7 @@ require 'relax/request'
5
5
  require 'relax/response'
6
6
  require 'relax/service'
7
7
  require 'relax/symbolic_hash'
8
+
9
+ module Relax
10
+ class MissingParameter < ArgumentError ; end
11
+ end
@@ -22,7 +22,12 @@ XML = <<EOF
22
22
  </RESTResponse>
23
23
  EOF
24
24
 
25
- class TestResponse < Relax::Response
25
+ class BaseResponse < Relax::Response
26
+ parameter :status, :required => true
27
+ parameter :request_id, :element => :requestid, :type => :integer
28
+ end
29
+
30
+ class TestResponse < BaseResponse
26
31
  class Token < Relax::Response
27
32
  parameter :token_id, :element => :tokenid
28
33
  parameter :status
@@ -33,8 +38,6 @@ class TestResponse < Relax::Response
33
38
  parameter :message
34
39
  end
35
40
 
36
- parameter :status
37
- parameter :request_id, :element => :requestid, :type => :integer
38
41
  parameter :valid_request, :element => :requestid, :attribute => :valid
39
42
  parameter :tokens, :collection => Token
40
43
  parameter :error, :type => Error
@@ -83,8 +86,6 @@ describe 'a response' do
83
86
 
84
87
  it 'should automatically pull parameters from the XML' do
85
88
  response = TestResponse.new(XML)
86
- response.status.should eql('Success')
87
- response.request_id.should eql(44287)
88
89
  response.valid_request.should eql('true')
89
90
  response.tokens.length.should eql(2)
90
91
  response.tokens.first.status.should eql('Active')
@@ -92,7 +93,17 @@ describe 'a response' do
92
93
  response.error.message.should eql('Failed')
93
94
  end
94
95
 
96
+ it "should automatically pull its parent's parameters from the XML" do
97
+ response = TestResponse.new(XML)
98
+ response.status.should eql('Success')
99
+ response.request_id.should eql(44287)
100
+ end
101
+
95
102
  it 'should be relationally equivalent to its children' do
96
103
  (Relax::Response === TestResponse).should be_true
97
104
  end
105
+
106
+ it 'should raise MissingParameter if required parameters are missing' do
107
+ proc { TestResponse.new('') }.should raise_error(Relax::MissingParameter)
108
+ end
98
109
  end
metadata CHANGED
@@ -1,10 +1,10 @@
1
1
  --- !ruby/object:Gem::Specification
2
- rubygems_version: 0.9.2
2
+ rubygems_version: 0.9.0
3
3
  specification_version: 1
4
4
  name: relax
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.0.2
7
- date: 2007-10-10 00:00:00 -04:00
6
+ version: 0.0.3
7
+ date: 2007-11-01 00:00:00 -04:00
8
8
  summary: A simple library for creating REST consumers.
9
9
  require_paths:
10
10
  - lib
@@ -29,20 +29,20 @@ post_install_message:
29
29
  authors:
30
30
  - Tyler Hunt
31
31
  files:
32
- - lib/relax.rb
33
32
  - lib/relax
34
- - lib/relax/response.rb
33
+ - lib/relax.rb
34
+ - lib/relax/query.rb
35
35
  - lib/relax/request.rb
36
+ - lib/relax/response.rb
36
37
  - lib/relax/service.rb
37
- - lib/relax/query.rb
38
38
  - lib/relax/symbolic_hash.rb
39
39
  - README
40
40
  - LICENSE
41
41
  test_files:
42
- - spec/request_spec.rb
43
- - spec/symbolic_hash_spec.rb
44
42
  - spec/query_spec.rb
43
+ - spec/request_spec.rb
45
44
  - spec/response_spec.rb
45
+ - spec/symbolic_hash_spec.rb
46
46
  rdoc_options: []
47
47
 
48
48
  extra_rdoc_files: