kelredd-resourceful 0.7.24 → 0.7.25

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,74 +1,74 @@
1
1
  require "resourceful/model/base"
2
2
 
3
- module Resourceful
4
- module Model
3
+ module Resourceful; end
5
4
 
6
- class Json < Resourceful::Model::Base
7
-
8
- attr_reader :json
5
+ module Resourceful::Model
9
6
 
10
- def self.get(path, params, search=nil, force=false, &block)
11
- opts = {
12
- :format => 'json',
13
- :params => params || {},
14
- :force => force,
15
- :on_response => block
16
- }
17
- new(get_result_data(super(path, opts), search))
18
- end
19
- def self.get_collection(path, params, search=nil, force=false, &block)
20
- opts = {
21
- :format => 'json',
22
- :params => params || {},
23
- :force => force,
24
- :on_response => block
25
- }
26
- super(path, opts) do |data|
27
- data.collect {|item| get_result_data(item, search) }
28
- end
29
- end
7
+ class Json < Resourceful::Model::Base
8
+
9
+ attr_reader :json
30
10
 
31
- def initialize(json=nil)
32
- raise Resourceful::Exceptions::ModelError, "trying to initialize a Resourceful::Model::Json model with '#{json.class.name}' data" unless json.nil? || json.kind_of?(::Hash)
33
- @data = json
34
- end
35
-
36
- protected
37
-
38
- def push_data(verb, path, opts, data, search=nil)
39
- self.class.get_result_data(super(verb, path, opts, data), search)
40
- end
41
-
42
- def self.get_result_data(result, search)
43
- hsh_keys = search.kind_of?(String) ? search.split(/\s+/) : nil
44
- result_data = !hsh_keys.nil? && !hsh_keys.empty? && result.respond_to?(:get_value) ? (result.get_value(hsh_keys) || result) : result
45
- end
46
-
47
- def self.format
48
- Resourceful::Resource::Json.to_s
11
+ def self.get(path, params, search=nil, force=false, &block)
12
+ opts = {
13
+ :format => 'json',
14
+ :params => params || {},
15
+ :force => force,
16
+ :on_response => block
17
+ }
18
+ new(get_result_data(super(path, opts), search))
19
+ end
20
+ def self.get_collection(path, params, search=nil, force=false, &block)
21
+ opts = {
22
+ :format => 'json',
23
+ :params => params || {},
24
+ :force => force,
25
+ :on_response => block
26
+ }
27
+ super(path, opts) do |data|
28
+ data.collect {|item| get_result_data(item, search) }
49
29
  end
50
-
51
- def attribute(config)
52
- begin
53
- get_node(config[:path])
54
- rescue Exception => err
55
- nil
56
- end
57
- end
30
+ end
58
31
 
59
- def child(config)
60
- attribute(config)
61
- end
62
-
63
- def self.get_node(json, path_config)
64
- paths = path_config.to_s.split('/')
65
- paths.inject(json) { |val,path| val.fetch(path, nil) rescue nil }
66
- end
67
- def get_node(path_config)
68
- self.class.get_node(@data, path_config)
32
+ def initialize(json=nil)
33
+ raise Resourceful::Exceptions::ModelError, "trying to initialize a Resourceful::Model::Json model with '#{json.class.name}' data" unless json.nil? || json.kind_of?(::Hash)
34
+ @data = json
35
+ end
36
+
37
+ protected
38
+
39
+ def push_data(verb, path, opts, data, search=nil)
40
+ self.class.get_result_data(super(verb, path, opts, data), search)
41
+ end
42
+
43
+ def self.get_result_data(result, search)
44
+ hsh_keys = search.kind_of?(String) ? search.split(/\s+/) : nil
45
+ result_data = !hsh_keys.nil? && !hsh_keys.empty? && result.respond_to?(:get_value) ? (result.get_value(hsh_keys) || result) : result
46
+ end
47
+
48
+ def self.format
49
+ Resourceful::Resource::Json.to_s
50
+ end
51
+
52
+ def attribute(config)
53
+ begin
54
+ get_node(config[:path])
55
+ rescue Exception => err
56
+ nil
69
57
  end
58
+ end
70
59
 
60
+ def child(config)
61
+ attribute(config)
62
+ end
63
+
64
+ def self.get_node(json, path_config)
65
+ paths = path_config.to_s.split('/')
66
+ paths.inject(json) { |val,path| val.fetch(path, nil) rescue nil }
67
+ end
68
+ def get_node(path_config)
69
+ self.class.get_node(@data, path_config)
71
70
  end
72
71
 
73
72
  end
74
- end
73
+
74
+ end
@@ -1,83 +1,83 @@
1
1
  require "resourceful/model/base"
2
2
 
3
- module Resourceful
4
- module Model
3
+ module Resourceful; end
5
4
 
6
- class Xml < Resourceful::Model::Base
7
-
8
- attr_reader :xml
5
+ module Resourceful::Model
9
6
 
10
- def self.get(path, params, search, force=false, &block)
11
- opts = {
12
- :format => 'xml',
13
- :params => params || {},
14
- :force => force,
15
- :on_response => block
16
- }
17
- data = super(path, opts)
18
- new(data.search(search) || data)
19
- end
20
- def self.get_collection(path, params, search, force=false, &block)
21
- opts = {
22
- :format => 'xml',
23
- :params => params || {},
24
- :force => force,
25
- :on_response => block
26
- }
27
- super(path, opts) do |data|
28
- data.search(search) || data
29
- end
30
- end
7
+ class Xml < Resourceful::Model::Base
8
+
9
+ attr_reader :xml
31
10
 
32
- def initialize(xml=nil)
33
- raise Resourceful::Exceptions::ModelError, "trying to initialize a Resourceful::Model::Xml model with '#{xml.class.name}' data" unless xml.nil? || xml.kind_of?(Nokogiri::XML::NodeSet) || xml.kind_of?(Nokogiri::XML::Element)
34
- @data = xml
35
- end
36
-
37
- protected
38
-
39
- def push_data(verb, path, opts, data, search=nil)
40
- result = super(verb, path, opts, data)
41
- search.nil? ? result : (result.search(search) || result)
11
+ def self.get(path, params, search, force=false, &block)
12
+ opts = {
13
+ :format => 'xml',
14
+ :params => params || {},
15
+ :force => force,
16
+ :on_response => block
17
+ }
18
+ data = super(path, opts)
19
+ new(data.search(search) || data)
20
+ end
21
+ def self.get_collection(path, params, search, force=false, &block)
22
+ opts = {
23
+ :format => 'xml',
24
+ :params => params || {},
25
+ :force => force,
26
+ :on_response => block
27
+ }
28
+ super(path, opts) do |data|
29
+ data.search(search) || data
42
30
  end
31
+ end
32
+
33
+ def initialize(xml=nil)
34
+ raise Resourceful::Exceptions::ModelError, "trying to initialize a Resourceful::Model::Xml model with '#{xml.class.name}' data" unless xml.nil? || xml.kind_of?(Nokogiri::XML::NodeSet) || xml.kind_of?(Nokogiri::XML::Element)
35
+ @data = xml
36
+ end
37
+
38
+ protected
39
+
40
+ def push_data(verb, path, opts, data, search=nil)
41
+ result = super(verb, path, opts, data)
42
+ search.nil? ? result : (result.search(search) || result)
43
+ end
44
+
45
+ def self.format
46
+ Resourceful::Resource::Xml.to_s
47
+ end
48
+
49
+ def attribute(config)
50
+ begin
51
+ node = get_node(config[:path])
52
+ node = node.first if node.respond_to?('first')
53
+ node.send((config[:content] || 'content').to_s)
54
+ rescue Exception => err
55
+ nil
56
+ end
57
+ end
43
58
 
44
- def self.format
45
- Resourceful::Resource::Xml.to_s
59
+ def child(config)
60
+ begin
61
+ get_node(config[:path])
62
+ rescue Exception => err
63
+ nil
46
64
  end
65
+ end
47
66
 
48
- def attribute(config)
49
- begin
50
- node = get_node(config[:path])
51
- node = node.first if node.respond_to?('first')
52
- node.send((config[:content] || 'content').to_s)
53
- rescue Exception => err
54
- nil
55
- end
56
- end
57
-
58
- def child(config)
59
- begin
60
- get_node(config[:path])
61
- rescue Exception => err
62
- nil
63
- end
64
- end
65
-
66
- def self.get_node(xml, path)
67
- path.to_s.empty? ? xml : xml.search(path.to_s)
68
- end
69
- def get_node(path)
70
- self.class.get_node(@data, path)
71
- end
72
-
73
- def self.xml_root_name(xml)
74
- xml.root.name
75
- end
76
- def xml_root_name
77
- self.class.xml_root_name(@data)
78
- end
67
+ def self.get_node(xml, path)
68
+ path.to_s.empty? ? xml : xml.search(path.to_s)
69
+ end
70
+ def get_node(path)
71
+ self.class.get_node(@data, path)
72
+ end
79
73
 
74
+ def self.xml_root_name(xml)
75
+ xml.root.name
76
+ end
77
+ def xml_root_name
78
+ self.class.xml_root_name(@data)
80
79
  end
81
80
 
82
81
  end
83
- end
82
+
83
+ end
@@ -1,51 +1,46 @@
1
- module Resourceful
2
- module Resource
1
+ module Resourceful; end
2
+
3
+ module Resourceful::Resource
3
4
 
4
- # The idea here is to put Resourceful into an eager loading "mode" while yielding to a block
5
- # => while in this mode, resourceful models will try to load and cache data in batches
6
- #def self.eager_load
7
- # @@
8
- #end
9
- class Cache
5
+ class Cache
10
6
 
11
- attr_reader :store, :expiration
12
-
13
- EXPIRY_SECS = 60
14
-
15
- def self.key(host, verb, resource)
16
- "#{host}_#{verb}_#{resource}"
17
- end
7
+ attr_reader :store, :expiration
8
+
9
+ EXPIRY_SECS = 60
10
+
11
+ def self.key(host, verb, resource)
12
+ "#{host}_#{verb}_#{resource}"
13
+ end
18
14
 
19
- def initialize(expiration=EXPIRY_SECS)
20
- @expiration = (expiration && expiration.kind_of?(::Fixnum)) ? expiration : EXPIRY_SECS
21
- @store = {}
22
- end
15
+ def initialize(expiration=EXPIRY_SECS)
16
+ @expiration = (expiration && expiration.kind_of?(::Fixnum)) ? expiration : EXPIRY_SECS
17
+ @store = {}
18
+ end
23
19
 
24
- def clear(key=nil)
25
- if key
26
- @store[key] = nil
27
- else
28
- @store = {}
29
- end
30
- end
31
-
32
- def read(key)
33
- entry = @store[key]
34
- expired?(entry) ? nil : entry[:value]
35
- end
36
-
37
- def write(key, value)
38
- @store[key] = {:value => value, :expires => Time.now.to_i + @expiration}
39
- value
20
+ def clear(key=nil)
21
+ if key
22
+ @store[key] = nil
23
+ else
24
+ @store = {}
40
25
  end
26
+ end
27
+
28
+ def read(key)
29
+ entry = @store[key]
30
+ expired?(entry) ? nil : entry[:value]
31
+ end
32
+
33
+ def write(key, value)
34
+ @store[key] = {:value => value, :expires => Time.now.to_i + @expiration}
35
+ value
36
+ end
37
+
38
+ private
39
+
40
+ def expired?(entry)
41
+ (entry.nil? || !entry.kind_of?(::Hash) || entry[:expires] < Time.now.to_i)
41
42
 
42
- private
43
-
44
- def expired?(entry)
45
- (entry.nil? || !entry.kind_of?(::Hash) || entry[:expires] < Time.now.to_i)
46
-
47
- end
48
-
49
43
  end
44
+
50
45
  end
51
46
  end
@@ -1,65 +1,61 @@
1
1
  require 'nokogiri'
2
2
  require 'json'
3
3
 
4
- module Resourceful
5
- module Resource
6
- class Format
7
-
8
- SUPPORTED = ['json', 'xml']
9
-
10
- def self.get(format)
11
- raise Resourceful::Exceptions::FormatError, "the format '#{format}' is not supported" unless SUPPORTED.include?(format.to_s)
12
- self.send(format)
13
- end
4
+ module Resourceful; end
5
+
6
+ module Resourceful::Resource
7
+ class Format
8
+
9
+ SUPPORTED = ['json', 'xml']
10
+
11
+ def self.get(format)
12
+ raise Resourceful::Exceptions::FormatError, "the format '#{format}' is not supported" unless SUPPORTED.include?(format.to_s)
13
+ self.send(format)
14
+ end
15
+
16
+ def self.json
17
+ Resourceful::Resource::Json
18
+ end
14
19
 
15
- def self.json
16
- Resourceful::Resource::Json
17
- end
18
-
19
- def self.xml
20
- Resourceful::Resource::Xml
21
- end
20
+ def self.xml
21
+ Resourceful::Resource::Xml
22
+ end
23
+
24
+ def self.to_s
25
+ raise 'method not implemented by this format'
26
+ end
22
27
 
23
- def self.to_s
24
- raise 'method not implemented by this format'
25
- end
26
-
27
- def self.build(str)
28
- raise 'method not implemented by this format'
29
- end
30
-
28
+ def self.build(str)
29
+ raise 'method not implemented by this format'
31
30
  end
31
+
32
32
  end
33
33
  end
34
34
 
35
- module Resourceful
36
- module Resource
37
- class Json < Resourceful::Resource::Format
35
+ module Resourceful::Resource
36
+ class Json < Resourceful::Resource::Format
37
+
38
+ def self.to_s
39
+ "json"
40
+ end
38
41
 
39
- def self.to_s
40
- "json"
41
- end
42
-
43
- def self.build(json_str)
44
- !json_str.nil? && !json_str.to_s.empty? && (json_str.to_s != 'null') ? JSON.parse(json_str.to_s) : nil
45
- end
46
-
42
+ def self.build(json_str)
43
+ !json_str.nil? && !json_str.to_s.empty? && (json_str.to_s != 'null') ? JSON.parse(json_str.to_s) : nil
47
44
  end
45
+
48
46
  end
49
47
  end
50
48
 
51
- module Resourceful
52
- module Resource
53
- class Xml < Resourceful::Resource::Format
49
+ module Resourceful::Resource
50
+ class Xml < Resourceful::Resource::Format
51
+
52
+ def self.to_s
53
+ "xml"
54
+ end
54
55
 
55
- def self.to_s
56
- "xml"
57
- end
58
-
59
- def self.build(xml_str)
60
- Nokogiri::XML(xml_str.to_s)
61
- end
62
-
56
+ def self.build(xml_str)
57
+ Nokogiri::XML(xml_str.to_s)
63
58
  end
59
+
64
60
  end
65
61
  end