kelredd-resourceful 0.4.5 → 0.4.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -37,11 +37,10 @@ module Resourceful
37
37
  protected
38
38
 
39
39
  def check_config(path, opts) # :nodoc:
40
- raise Resourceful::Exceptions::ConfigurationError, "invalid Mechanize agent" unless agent && agent_url && !agent_url.empty?
40
+ raise Resourceful::Exceptions::ConfigurationError, "invalid agent" unless agent && agent_url && !agent_url.empty?
41
41
  opts ||= {}
42
42
  opts[:force] ||= false
43
43
  if path =~ /^(.+)\.(.+)$/
44
- path = $1
45
44
  opts[:format] ||= $2
46
45
  end
47
46
  opts[:format] ||= Resourceful::Resource::Json.to_s
@@ -50,7 +49,7 @@ module Resourceful
50
49
  end
51
50
 
52
51
  def self.resource_path(path, format, params) # :nodoc:
53
- "#{path}.#{format}#{params.to_http_query_str unless params.empty?}"
52
+ "#{path}#{params.to_http_query_str unless params.empty?}"
54
53
  end
55
54
 
56
55
  def summary(verb, full_resource_path) # :nodoc:
@@ -5,21 +5,30 @@ module Resourceful
5
5
  module Agent
6
6
  class Mechanize < Resourceful::Agent::Base
7
7
 
8
+ ATTRS = [:agent_alias]
9
+ ATTRS.each { |a| attr_reader a }
10
+
8
11
  def initialize(args={})
12
+ ATTRS.each { |a| instance_variable_set("@#{a.to_s}", args.delete(a)) }
9
13
  super(args)
10
14
  self.log = yield if block_given?
11
- @mechanize = ::WWW::Mechanize.new { |obj| obj.log = @logger }
15
+ @mechanize = ::WWW::Mechanize.new do |obj|
16
+ obj.log = @logger
17
+ obj.user_agent_alias = @agent_alias unless @agent_alias.nil?
18
+ end
12
19
  end
13
20
 
14
21
  def get(path, opts={})
15
22
  super(path, opts) do |path|
23
+ resp = ""
16
24
  @mechanize.get("#{@host}#{path}") do |page|
17
- if block_given?
25
+ resp = if block_given?
18
26
  yield page
19
27
  else
20
28
  page
21
29
  end
22
- end.body
30
+ end
31
+ resp.body
23
32
  end
24
33
  end
25
34
 
@@ -1,3 +1,25 @@
1
+ module Resourceful
2
+ module Extensions
3
+ module NilClass
4
+
5
+ module ClassMethods; end
6
+ def self.included(klass)
7
+ klass.extend(ClassMethods) if klass.kind_of?(Class)
8
+ end
9
+
10
+ module ClassMethods
11
+ end
12
+
13
+ unless nil.respond_to?(:to_boolean)
14
+ def to_boolean
15
+ !!self
16
+ end
17
+ end
18
+
19
+ end
20
+ end
21
+ end
22
+
1
23
  module Resourceful
2
24
  module Extensions
3
25
  module String
@@ -24,7 +46,7 @@ module Resourceful
24
46
 
25
47
  unless "".respond_to?(:to_boolean)
26
48
  def to_boolean
27
- self =~ /^(true|1)$/i ? true : false
49
+ self.nil? || self.empty? || self =~ /^(false|0)$/i ? false : true
28
50
  end
29
51
  end
30
52
 
@@ -95,6 +117,10 @@ module Resourceful
95
117
  end
96
118
  end
97
119
 
120
+ class NilClass
121
+ include Resourceful::Extensions::NilClass
122
+ end
123
+
98
124
  class String
99
125
  include Resourceful::Extensions::String
100
126
  end
@@ -9,20 +9,30 @@ module Resourceful
9
9
  end
10
10
 
11
11
  def self.get(path, opts={})
12
- get_proc = opts.delete(:get_proc)
13
- set_agent.get(path, opts, &get_proc)
12
+ block = opts.delete(:on_response)
13
+ set_agent.get(path, opts, &block)
14
14
  end
15
15
  def self.get_collection(path, opts={})
16
- get_proc = opts.delete(:get_proc)
17
- (yield set_agent.get(path, opts, &get_proc)).collect{|data| new(data)}
16
+ block = opts.delete(:on_response)
17
+ (yield set_agent.get(path, opts, &block)).collect{|data| new(data)}
18
18
  end
19
19
 
20
20
  def initialize(data)
21
21
  end
22
22
 
23
+ def data
24
+ @data
25
+ end
26
+
27
+ def attributes
28
+ @attributes ||= @@attributes[self.class.name].inject({}) { |hsh, key| hsh[key] = self.send(key); hsh }
29
+ end
30
+
23
31
  protected
24
32
 
25
33
  def self.attribute(name, type, config={})
34
+ clean_name = name.to_s.gsub(/\W/,'')
35
+ add_to_attributes(name.to_s)
26
36
  config ||= {}
27
37
  config[:path] ||= name
28
38
  content_method = case type.to_sym
@@ -32,6 +42,8 @@ module Resourceful
32
42
  'to_i'
33
43
  when :float
34
44
  'to_f'
45
+ when :currency
46
+ 'from_currency_to_f'
35
47
  when :date
36
48
  'to_date'
37
49
  when :datetime
@@ -42,10 +54,10 @@ module Resourceful
42
54
  'to_s'
43
55
  end
44
56
  define_method(name) do
45
- instance_variable_get("@#{name}") || \
46
- instance_variable_set("@#{name}", \
57
+ instance_variable_get("@#{clean_name}") || \
58
+ instance_variable_set("@#{clean_name}", \
47
59
  if ((a = attribute(config)) && a.kind_of?(String))
48
- a.send(content_method)
60
+ self.class.get_value(a, config).send(content_method)
49
61
  else
50
62
  a
51
63
  end
@@ -102,6 +114,21 @@ module Resourceful
102
114
 
103
115
  private
104
116
 
117
+ def self.add_to_attributes(name)
118
+ @@attributes ||= {}
119
+ klass_name = self.name
120
+ @@attributes[klass_name] ||= []
121
+ @@attributes[klass_name] << name
122
+ end
123
+
124
+ def self.get_value(attribute, opts={})
125
+ if opts[:value]
126
+ (attribute =~ opts[:value]) ? ($1 || $&) : nil
127
+ else
128
+ attribute
129
+ end
130
+ end
131
+
105
132
  def self.set_agent
106
133
  unless @@agent.kind_of?(Resourceful::Agent::Base)
107
134
  @@agent = @@agent.call
@@ -5,19 +5,21 @@ module Resourceful
5
5
 
6
6
  attr_reader :json
7
7
 
8
- def self.get(path, params, force=false)
8
+ def self.get(path, params, force=false, &block)
9
9
  opts = {
10
10
  :format => 'json',
11
11
  :params => params || {},
12
- :force => force
12
+ :force => force,
13
+ :on_response => block
13
14
  }
14
15
  new(super(path, opts))
15
16
  end
16
- def self.get_collection(path, params, force=false)
17
+ def self.get_collection(path, params, force=false, &block)
17
18
  opts = {
18
19
  :format => 'json',
19
20
  :params => params || {},
20
- :force => force
21
+ :force => force,
22
+ :on_response => block
21
23
  }
22
24
  super(path, opts) do |data|
23
25
  data
@@ -29,9 +31,9 @@ module Resourceful
29
31
  @data = json
30
32
  end
31
33
 
32
- def attributes
33
- @data
34
- end
34
+ #def attributes
35
+ # @data
36
+ #end
35
37
 
36
38
  protected
37
39
 
@@ -5,22 +5,24 @@ module Resourceful
5
5
 
6
6
  attr_reader :xml
7
7
 
8
- def self.get(path, params, xpath, force=false)
8
+ def self.get(path, params, search, force=false, &block)
9
9
  opts = {
10
10
  :format => 'xml',
11
11
  :params => params || {},
12
- :force => force
12
+ :force => force,
13
+ :on_response => block
13
14
  }
14
- new(super(path, opts).xpath(xpath))
15
+ new(super(path, opts).search(search))
15
16
  end
16
- def self.get_collection(path, params, xpath, force=false)
17
+ def self.get_collection(path, params, search, force=false, &block)
17
18
  opts = {
18
19
  :format => 'xml',
19
20
  :params => params || {},
20
- :force => force
21
+ :force => force,
22
+ :on_response => block
21
23
  }
22
24
  super(path, opts) do |data|
23
- data.xpath(xpath)
25
+ data.search(search)
24
26
  end
25
27
  end
26
28
 
@@ -33,7 +35,7 @@ module Resourceful
33
35
 
34
36
  def attribute(config)
35
37
  begin
36
- get_node("./#{config[:path]}").content
38
+ get_node("#{config[:path]}").send((config[:content] || 'content').to_s)
37
39
  rescue Exception => err
38
40
  nil
39
41
  end
@@ -41,14 +43,14 @@ module Resourceful
41
43
 
42
44
  def child(config)
43
45
  begin
44
- get_node("./#{config[:path]}")
46
+ get_node("#{config[:path]}")
45
47
  rescue Exception => err
46
48
  nil
47
49
  end
48
50
  end
49
51
 
50
52
  def self.get_node(xml, path)
51
- xml.xpath(path.to_s).first
53
+ xml.search(path.to_s).first
52
54
  end
53
55
  def get_node(path)
54
56
  self.class.get_node(@data, path)
@@ -3,7 +3,7 @@ module Resourceful
3
3
 
4
4
  MAJOR = 0
5
5
  MINOR = 4
6
- TINY = 5
6
+ TINY = 6
7
7
 
8
8
  def self.to_s # :nodoc:
9
9
  [MAJOR, MINOR, TINY].join('.')
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kelredd-resourceful
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.5
4
+ version: 0.4.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kelly Redding
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-08-04 00:00:00 -07:00
12
+ date: 2009-08-06 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency