kelredd-resourceful 0.7.24 → 0.7.25
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.
- data/Rakefile +1 -0
- data/lib/resourceful.rb +1 -1
- data/lib/resourceful/agent/base.rb +59 -59
- data/lib/resourceful/agent/mechanize.rb +33 -33
- data/lib/resourceful/agent/rest_client.rb +47 -47
- data/lib/resourceful/exceptions.rb +20 -20
- data/lib/resourceful/extensions.rb +34 -182
- data/lib/resourceful/model/activerecord_associations.rb +77 -78
- data/lib/resourceful/model/attribute_types.rb +34 -35
- data/lib/resourceful/model/base.rb +164 -164
- data/lib/resourceful/model/embedded_associations.rb +56 -57
- data/lib/resourceful/model/external_associations.rb +77 -78
- data/lib/resourceful/model/findable.rb +93 -94
- data/lib/resourceful/model/json.rb +62 -62
- data/lib/resourceful/model/xml.rb +70 -70
- data/lib/resourceful/resource/cache.rb +37 -42
- data/lib/resourceful/resource/format.rb +43 -47
- data/lib/resourceful/shoulda/test_unit.rb +90 -112
- data/lib/resourceful/version.rb +1 -1
- metadata +12 -2
@@ -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
|
-
|
7
|
-
|
8
|
-
attr_reader :json
|
5
|
+
module Resourceful::Model
|
9
6
|
|
10
|
-
|
11
|
-
|
12
|
-
|
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
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
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
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
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
|
-
|
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
|
-
|
7
|
-
|
8
|
-
attr_reader :xml
|
5
|
+
module Resourceful::Model
|
9
6
|
|
10
|
-
|
11
|
-
|
12
|
-
|
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
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
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
|
-
|
45
|
-
|
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
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
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
|
-
|
82
|
+
|
83
|
+
end
|
@@ -1,51 +1,46 @@
|
|
1
|
-
module Resourceful
|
2
|
-
|
1
|
+
module Resourceful; end
|
2
|
+
|
3
|
+
module Resourceful::Resource
|
3
4
|
|
4
|
-
|
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
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
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
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
15
|
+
def initialize(expiration=EXPIRY_SECS)
|
16
|
+
@expiration = (expiration && expiration.kind_of?(::Fixnum)) ? expiration : EXPIRY_SECS
|
17
|
+
@store = {}
|
18
|
+
end
|
23
19
|
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
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
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
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
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
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
|
-
|
24
|
-
|
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
|
-
|
37
|
-
|
35
|
+
module Resourceful::Resource
|
36
|
+
class Json < Resourceful::Resource::Format
|
37
|
+
|
38
|
+
def self.to_s
|
39
|
+
"json"
|
40
|
+
end
|
38
41
|
|
39
|
-
|
40
|
-
|
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
|
-
|
53
|
-
|
49
|
+
module Resourceful::Resource
|
50
|
+
class Xml < Resourceful::Resource::Format
|
51
|
+
|
52
|
+
def self.to_s
|
53
|
+
"xml"
|
54
|
+
end
|
54
55
|
|
55
|
-
|
56
|
-
|
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
|