relax 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,25 +1,23 @@
1
1
  module Relax
2
2
  module Parsers
3
-
4
3
  class Base
5
-
6
4
  attr_reader :parent
7
5
  attr_reader :parameters
8
-
6
+
9
7
  def initialize(raw, parent)
10
- @parent = parent
8
+ @parent = parent
11
9
  @parameters = parent.class.instance_variable_get('@parameters')
12
10
  parse!
13
11
  end
14
-
12
+
15
13
  def parse!; end
16
-
14
+
17
15
  def root; end
18
16
  def is?(name); end
19
17
  def has?(name); end
20
18
  def element(name); end
21
19
  def elements(name); end
22
-
20
+
23
21
  def attribute(element, name); end
24
22
  def value(value); end
25
23
  def text_value(value); end
@@ -27,8 +25,6 @@ module Relax
27
25
  def float_value(value); end
28
26
  def date_value(value); end
29
27
  def time_value(value); end
30
-
31
28
  end
32
-
33
29
  end
34
30
  end
@@ -1,43 +1,29 @@
1
1
  module Relax
2
2
  module Parsers
3
-
4
- ##
5
3
  # Manages the Relax::Parsers in the library.
6
- #
7
4
  module Factory
8
-
9
5
  class << self
10
-
11
- ##
12
- # Returns the parser class which has been registered for the given
6
+ # Returns the parser class which has been registered for the given
13
7
  # +name+.
14
- #
15
8
  def get(name)
16
9
  @@parsers ||= {}
17
- @@parsers[name] || raise(UnrecognizedParser, "Given parser name not recognized: #{name.inspect}. Expected one of: #{@@parsers.keys.inspect}")
10
+ @@parsers[name] || raise(UnrecognizedParser, "Given parser name not recognized: #{name.inspect}. Expected one of: #{@@parsers.keys.inspect}")
18
11
  end
19
-
20
- ##
21
- # Registers a new parser with the factory. The +name+ should be unique,
12
+
13
+ # Registers a new parser with the factory. The +name+ should be unique,
22
14
  # but if not, it will override the previously defined parser for the
23
15
  # given +name+.
24
- #
25
16
  def register(name, klass)
26
17
  @@parsers ||= {}
27
18
  @@parsers[:default] = klass if @@parsers.empty?
28
19
  @@parsers[name] = klass
29
20
  end
30
-
31
- ##
21
+
32
22
  # Removes all registered parsers from the factory.
33
- #
34
23
  def clear!
35
24
  @@parsers = {}
36
25
  end
37
-
38
26
  end
39
-
40
27
  end
41
-
42
28
  end
43
29
  end
@@ -3,33 +3,30 @@ require 'hpricot'
3
3
 
4
4
  module Relax
5
5
  module Parsers
6
-
7
- ##
8
6
  # Parses the server's raw response using the Hpricot library.
9
- #
10
7
  class Hpricot < Base
11
-
12
8
  FACTORY_NAME = :hpricot
13
-
9
+
14
10
  def initialize(raw, parent)
15
11
  @xml = ::Hpricot.XML(raw)
16
12
  super(raw, parent)
17
13
  end
18
-
14
+
19
15
  def parse!
20
16
  if parameters
21
17
  parameters.each do |parameter, options|
22
18
  begin
23
19
  element = options[:element] || parameter
24
20
 
25
- if attribute = options[:attribute] and attribute == true
26
- node = attribute(root, element)
27
- elsif attribute
28
- node = attribute(element(element), attribute)
29
- elsif options[:collection]
30
- node = elements(element)
31
- else
32
- node = element(element)
21
+ node = case
22
+ when options[:attribute] && options[:attribute] == true
23
+ attribute(root, element)
24
+ when options[:attribute]
25
+ attribute(element(element), options[:attribute])
26
+ when options[:collection]
27
+ elements(element)
28
+ else
29
+ element(element)
33
30
  end
34
31
 
35
32
  if options[:collection]
@@ -37,25 +34,20 @@ module Relax
37
34
  options[:collection].new(element)
38
35
  end
39
36
  else
40
- case type = options[:type]
37
+ value = case type = options[:type]
41
38
  when Response
42
- value = type.new(node)
43
-
39
+ type.new(node)
44
40
  when :date
45
- value = date_value(node)
46
-
41
+ date_value(node)
47
42
  when :time
48
- value = time_value(node)
49
-
43
+ time_value(node)
50
44
  when :float
51
- value = float_value(node)
52
-
45
+ float_value(node)
53
46
  when :integer
54
- value = integer_value(node)
55
-
47
+ integer_value(node)
56
48
  when :text
57
49
  else
58
- value = text_value(node)
50
+ text_value(node)
59
51
  end
60
52
  end
61
53
 
@@ -66,28 +58,28 @@ module Relax
66
58
  end
67
59
  end
68
60
  end
69
-
61
+
70
62
  # Returns the root of the XML document.
71
63
  def root
72
64
  @xml.root
73
65
  end
74
-
66
+
75
67
  # Checks the name of the root node.
76
68
  def is?(name)
77
69
  root.name.gsub(/.*:(.*)/, '\1') == node_name(name)
78
70
  end
79
-
71
+
80
72
  # Returns a set of elements matching name.
81
73
  def elements(name)
82
74
  root.search(root_path(name))
83
75
  end
84
-
76
+
85
77
  # Returns an element of the specified name.
86
78
  def element(name)
87
79
  root.at(root_path(name))
88
80
  end
89
81
  alias :has? :element
90
-
82
+
91
83
  # Returns an attribute on an element.
92
84
  def attribute(element, name)
93
85
  element[name]
@@ -97,7 +89,7 @@ module Relax
97
89
  def value(value)
98
90
  value.is_a?(::Hpricot::Elem) ? value.inner_text : value.to_s
99
91
  end
100
-
92
+
101
93
  # Gets a text value.
102
94
  def text_value(value)
103
95
  value(value)
@@ -122,24 +114,20 @@ module Relax
122
114
  def time_value(value)
123
115
  Time.parse(value(value))
124
116
  end
125
-
126
-
127
- private
128
-
129
-
117
+
130
118
  # Converts a name to a node name.
131
119
  def node_name(name)
132
- name.to_s
120
+ @parent.node_name(name)
133
121
  end
122
+ private :node_name
134
123
 
135
124
  # Gets the XPath expression representing the root node.
136
125
  def root_path(name)
137
126
  "/#{node_name(name)}"
138
127
  end
139
-
128
+ private :root_path
140
129
  end
141
-
130
+
142
131
  Factory.register(Hpricot::FACTORY_NAME, Hpricot)
143
-
144
132
  end
145
133
  end
@@ -3,71 +3,63 @@ require 'rexml/document'
3
3
 
4
4
  module Relax
5
5
  module Parsers
6
-
7
- ##
8
6
  # Parsers the server's response using the REXML library.
9
- #
7
+ #
10
8
  # Benefits:
11
9
  #
12
10
  # * XML Namespace support (parameter :foo, :namespace => 'bar')
13
- #
11
+ #
14
12
  # Drawbacks:
15
13
  #
16
14
  # * Case sensitive field names (<Status>..</> != parameter :status)
17
- #
18
15
  class REXML < Base
19
-
20
16
  FACTORY_NAME = :rexml
21
-
17
+
22
18
  def initialize(raw, parent)
23
19
  @xml = ::REXML::Document.new(raw)
24
20
  super(raw, parent)
25
21
  end
26
-
22
+
27
23
  def parse!
28
24
  if parameters
29
25
  parameters.each do |parameter, options|
30
26
  begin
31
27
  element = options[:element] || parameter
32
28
  namespace = options[:namespace]
33
-
34
- if attribute = options[:attribute] and attribute == true
35
- node = attribute(root, element, namespace)
36
- elsif attribute
37
- node = attribute(element(element), attribute, namespace)
38
- elsif options[:collection]
39
- node = elements(element, namespace)
40
- else
41
- node = element(element, namespace)
29
+
30
+ node = case
31
+ when options[:attribute] && options[:attribute] == true
32
+ attribute(root, element, namespace)
33
+ when options[:attribute]
34
+ attribute(element(element), options[:attribute], namespace)
35
+ when options[:collection]
36
+ elements(element, namespace)
37
+ else
38
+ element(element, namespace)
42
39
  end
43
-
40
+
44
41
  if options[:collection]
45
42
  value = node.collect do |element|
46
- options[:collection].new(element.deep_clone)
43
+ options[:collection].new(element.to_s)
47
44
  end
48
45
  else
49
- case type = options[:type]
46
+ value = case type = options[:type]
50
47
  when Response
51
- value = type.new(node)
52
-
48
+ type.new(node)
53
49
  when :date
54
- value = date_value(node)
55
-
50
+ date_value(node)
56
51
  when :time
57
- value = time_value(node)
58
-
52
+ time_value(node)
59
53
  when :float
60
- value = float_value(node)
61
-
54
+ float_value(node)
62
55
  when :integer
63
- value = integer_value(node)
64
-
56
+ integer_value(node)
65
57
  when :text
66
58
  else
67
- value = text_value(node)
59
+ text_value(node)
68
60
  end
69
61
  end
70
-
62
+
71
63
  parent.instance_variable_set("@#{parameter}", value)
72
64
  rescue
73
65
  raise(Relax::MissingParameter) if node.nil? && options[:required]
@@ -75,30 +67,30 @@ module Relax
75
67
  end
76
68
  end
77
69
  end
78
-
70
+
79
71
  # Returns the root of the XML document.
80
72
  def root
81
73
  @xml.root
82
74
  end
83
-
75
+
84
76
  # Checks the name of the root node.
85
- def is?(name, namespace = nil)
77
+ def is?(name, namespace=nil)
86
78
  root.name == node_name(name, nil)
87
79
  end
88
-
80
+
89
81
  # Returns a set of elements matching name.
90
- def elements(name, namespace = nil)
82
+ def elements(name, namespace=nil)
91
83
  root.get_elements(node_path(name, namespace))
92
84
  end
93
-
85
+
94
86
  # Returns an element of the specified name.
95
- def element(name, namespace = nil)
87
+ def element(name, namespace=nil)
96
88
  root.elements[node_path(name, namespace)]
97
89
  end
98
90
  alias :has? :element
99
-
91
+
100
92
  # Returns an attribute on an element.
101
- def attribute(element, name, namespace = nil)
93
+ def attribute(element, name, namespace=nil)
102
94
  element.attribute(name)
103
95
  end
104
96
 
@@ -106,7 +98,7 @@ module Relax
106
98
  def value(value)
107
99
  value.is_a?(::REXML::Element) ? value.text : value.to_s
108
100
  end
109
-
101
+
110
102
  # Gets a text value.
111
103
  def text_value(value)
112
104
  value(value)
@@ -131,28 +123,25 @@ module Relax
131
123
  def time_value(value)
132
124
  Time.parse(value(value))
133
125
  end
134
-
135
-
136
- private
137
-
138
-
126
+
139
127
  # Converts a name to a node name.
140
- def node_name(name, namespace = nil)
141
- "#{namespace.to_s + ':' if namespace}#{name}"
128
+ def node_name(name, namespace=nil)
129
+ @parent.node_name(name, namespace)
142
130
  end
131
+ private :node_name
143
132
 
144
133
  # Gets the XPath expression representing the root node.
145
134
  def root_path(name)
146
135
  "/#{node_name(name)}"
147
136
  end
148
-
149
- def node_path(name, namespace = nil)
137
+ private :root_path
138
+
139
+ def node_path(name, namespace=nil)
150
140
  "#{node_name(name, namespace)}"
151
141
  end
152
-
142
+ private :node_path
153
143
  end
154
-
144
+
155
145
  Factory.register(REXML::FACTORY_NAME, REXML)
156
-
157
146
  end
158
147
  end
data/lib/relax/parsers.rb CHANGED
@@ -10,4 +10,4 @@ require 'relax/parsers/rexml'
10
10
  module Relax
11
11
  module Parsers
12
12
  end
13
- end
13
+ end
data/lib/relax/request.rb CHANGED
@@ -8,8 +8,8 @@ module Relax
8
8
 
9
9
  # New takes an optional hash of default parameter values. When passed,
10
10
  # the values will be set on the request if the key exists as a valid
11
- # parameter name.
12
- def initialize(defaults = {})
11
+ # parameter name.
12
+ def initialize(defaults={})
13
13
  # initialize default parameter values
14
14
  self.class.parameters.each do |parameter, options|
15
15
  if defaults.has_key?(parameter)
@@ -27,6 +27,7 @@ module Relax
27
27
  self.class.parameters.keys.inject(Query.new) do |query, key|
28
28
  value = send(key)
29
29
  options = self.class.parameters[key]
30
+
30
31
  if value && !options[:type]
31
32
  query[convert_key(key)] = value if value
32
33
  elsif options[:type]
@@ -34,6 +35,7 @@ module Relax
34
35
  query[convert_complex_key(key, parameter)] = value.send(parameter) if value
35
36
  end
36
37
  end
38
+
37
39
  query
38
40
  end
39
41
  end
@@ -51,7 +53,7 @@ module Relax
51
53
  key
52
54
  end
53
55
  protected :convert_key
54
-
56
+
55
57
  # Converts a complex key (i.e. a parameter with a custom type) when the
56
58
  # Request is converted to a query. By default, this means the key name and
57
59
  # the parameter name separated by two underscores. This method can be
@@ -82,7 +84,7 @@ module Relax
82
84
  # Adds a template value to a request class. Equivalent to creating a
83
85
  # parameter with a default value.
84
86
  def []=(key, value)
85
- parameter(key, {:value => value})
87
+ parameter(key, :value => value)
86
88
  end
87
89
 
88
90
  # Returns a hash of all of the parameters for this request, including
@@ -7,20 +7,24 @@ module Relax
7
7
  # #element and #attribute.
8
8
  class Response
9
9
  attr_accessor :raw
10
-
10
+
11
11
  # New takes in and parses the raw response.
12
12
  #
13
13
  # This will raise a MissingParameter error if a parameterd marked as
14
14
  # required is not present in the parsed response.
15
15
  def initialize(xml)
16
- @raw = xml
16
+ @raw = xml
17
17
  @parser = Relax::Parsers::Factory.get(parser_name).new(xml.to_s, self)
18
18
  end
19
-
20
- def parser_name
19
+
20
+ def parser_name #:nodoc:
21
21
  self.class.instance_variable_get('@parser') || :default
22
22
  end
23
-
23
+
24
+ def node_name(name, namespace=nil) #:nodoc:
25
+ "#{namespace.to_s + ':' if namespace}#{name}"
26
+ end
27
+
24
28
  def method_missing(method, *args) #:nodoc:
25
29
  if @parser.respond_to?(method)
26
30
  @parser.__send__(method, *args)
@@ -39,6 +43,7 @@ module Relax
39
43
  @parameters.each do |name, options|
40
44
  subclass.parameter(name, options)
41
45
  end if @parameters
46
+
42
47
  subclass.parser(@parser) if @parser
43
48
  end
44
49
 
@@ -54,16 +59,16 @@ module Relax
54
59
  # - <tt>:object</tt>: A class used to instantiate an element.
55
60
  # - <tt>:type</tt>: The type of the parameter. Should be one of
56
61
  # <tt>:text</tt>, <tt>:integer</tt>, <tt>:float</tt>, or <tt>:date</tt>.
57
- def parameter(name, options = {})
62
+ def parameter(name, options={})
58
63
  attr_accessor name
59
64
  @parameters ||= {}
60
65
  @parameters[name] = options
61
66
  end
62
-
63
- # Specifies the parser to use when decoding the server response. If
64
- # no parser is specified for the response, then the default parser will
65
- # be used.
66
- #
67
+
68
+ # Specifies the parser to use when decoding the server response. If no
69
+ # parser is specified for the response, then the default parser will be
70
+ # used.
71
+ #
67
72
  # See Relax::Parsers for a list of available parsers.
68
73
  def parser(name)
69
74
  @parser ||= name
@@ -73,6 +78,5 @@ module Relax
73
78
  response.is_a?(Class) ? response.ancestors.include?(self) : super
74
79
  end
75
80
  end
76
-
77
81
  end
78
82
  end
@@ -66,14 +66,14 @@ module Relax
66
66
  alias :has_key? :key?
67
67
  alias :member? :key?
68
68
 
69
- protected
70
-
71
69
  def convert_key(key)
72
70
  !key.kind_of?(Symbol) ? key.to_sym : key
73
71
  end
72
+ protected :convert_key
74
73
 
75
74
  def convert_value(value)
76
75
  value
77
76
  end
77
+ protected :convert_value
78
78
  end
79
79
  end
@@ -1,7 +1,6 @@
1
1
  require File.dirname(__FILE__) + '/../spec_helper'
2
2
  require File.dirname(__FILE__) + '/../parser_helper'
3
3
 
4
-
5
4
  class HpricotTestResponse < Relax::Response
6
5
  class Token < Relax::Response
7
6
  parser :hpricot
@@ -14,7 +13,7 @@ class HpricotTestResponse < Relax::Response
14
13
  parameter :code, :type => :integer
15
14
  parameter :message
16
15
  end
17
-
16
+
18
17
  parser :hpricot
19
18
  parameter :status, :required => true
20
19
  parameter :request_id, :element => :requestid, :type => :integer
@@ -23,13 +22,10 @@ class HpricotTestResponse < Relax::Response
23
22
  parameter :error, :type => Error
24
23
  end
25
24
 
26
-
27
25
  describe 'an Hpricot parser' do
28
-
29
26
  before(:each) do
30
27
  @response = HpricotTestResponse.new(XML)
31
28
  end
32
-
29
+
33
30
  it_should_behave_like 'a successfully parsed response'
34
-
35
- end
31
+ end
@@ -1,40 +1,36 @@
1
1
  require File.dirname(__FILE__) + '/../spec_helper'
2
2
  require File.dirname(__FILE__) + '/../parser_helper'
3
3
 
4
-
5
4
  class RexmlTestResponse < Relax::Response
6
5
  class Token < Relax::Response
7
6
  parser :rexml
8
- parameter :token_id, :element => 'TokenId'
9
- parameter :status, :element => 'Status'
7
+ parameter :token_id, :element => 'TokenId'
8
+ parameter :status, :element => 'Status'
10
9
  end
11
-
10
+
12
11
  class Error < Relax::Response
13
12
  parser :rexml
14
- parameter :code, :element => 'Code', :type => :integer
15
- parameter :message, :element => 'Message'
13
+ parameter :code, :element => 'Code', :type => :integer
14
+ parameter :message, :element => 'Message'
16
15
  end
17
-
16
+
18
17
  parser :rexml
19
- parameter :status, :element => 'Status', :required => true
20
- parameter :request_id, :element => 'RequestId', :type => :integer
21
- parameter :valid_request, :element => 'RequestId', :attribute => :valid
22
- parameter :namespace, :element => 'Namespace', :namespace => 'ns1'
23
- parameter :tokens, :element => 'Tokens', :collection => Token
24
- parameter :error, :element => 'Error', :type => Error
18
+ parameter :status, :element => 'Status', :required => true
19
+ parameter :request_id, :element => 'RequestId', :type => :integer
20
+ parameter :valid_request, :element => 'RequestId', :attribute => :valid
21
+ parameter :namespace, :element => 'Namespace', :namespace => 'ns1'
22
+ parameter :tokens, :element => 'Tokens', :collection => Token
23
+ parameter :error, :element => 'Error', :type => Error
25
24
  end
26
25
 
27
-
28
26
  describe 'a REXML parser' do
29
-
30
27
  before(:each) do
31
28
  @response = RexmlTestResponse.new(XML)
32
29
  end
33
-
30
+
34
31
  it_should_behave_like 'a successfully parsed response'
35
-
32
+
36
33
  it 'should parse namespaced parameters' do
37
34
  @response.namespace.should eql('Passed')
38
35
  end
39
-
40
- end
36
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: relax
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tyler Hunt
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-10-20 00:00:00 -04:00
12
+ date: 2009-02-03 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -45,13 +45,6 @@ files:
45
45
  - lib/relax/service.rb
46
46
  - lib/relax/symbolic_hash.rb
47
47
  - lib/relax.rb
48
- - spec/parsers/factory_spec.rb
49
- - spec/parsers/hpricot_spec.rb
50
- - spec/parsers/rexml_spec.rb
51
- - spec/query_spec.rb
52
- - spec/request_spec.rb
53
- - spec/response_spec.rb
54
- - spec/symbolic_hash_spec.rb
55
48
  - README
56
49
  - LICENSE
57
50
  has_rdoc: true
@@ -76,7 +69,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
76
69
  requirements: []
77
70
 
78
71
  rubyforge_project: relax
79
- rubygems_version: 1.2.0
72
+ rubygems_version: 1.3.1
80
73
  signing_key:
81
74
  specification_version: 2
82
75
  summary: A simple library for creating REST consumers.