kelredd-resourceful 0.4.2 → 0.4.3
Sign up to get free protection for your applications and to get access to all the features.
@@ -6,15 +6,61 @@ module Resourceful
|
|
6
6
|
class Base
|
7
7
|
|
8
8
|
attr_reader :logger, :cache
|
9
|
+
|
10
|
+
ATTRS = [:host, :user, :password]
|
11
|
+
ATTRS.each { |a| attr_reader a }
|
9
12
|
|
10
13
|
def initialize(args={})
|
14
|
+
ATTRS.each { |a| instance_variable_set("@#{a.to_s}", args.delete(a)) }
|
11
15
|
@cache = Resourceful::Resource::Cache.new
|
12
16
|
@logger = Log4r::Logger.new('Resourceful Base')
|
13
17
|
@logger.add(Log4r::StdoutOutputter.new('console'))
|
14
18
|
end
|
15
19
|
|
20
|
+
def get(path, opts={}, &block)
|
21
|
+
path, opts = check_config(path, opts)
|
22
|
+
format = Resourceful::Resource::Format.get(opts[:format])
|
23
|
+
|
24
|
+
full_resource_path = self.class.resource_path(path, format, opts[:params])
|
25
|
+
resource_summary = summary('get', full_resource_path)
|
26
|
+
cache_key = Resourceful::Resource::Cache.key(@host, 'get', full_resource_path)
|
27
|
+
|
28
|
+
if opts[:force] || (resp = cache.read(cache_key)).nil?
|
29
|
+
log "Resource call: #{resource_summary}"
|
30
|
+
resp = cache.write(cache_key, block.call(full_resource_path))
|
31
|
+
else
|
32
|
+
log "Resource call: [CACHE] #{resource_summary}"
|
33
|
+
end
|
34
|
+
format.build(resp)
|
35
|
+
end
|
36
|
+
|
16
37
|
protected
|
17
38
|
|
39
|
+
def check_config(path, opts) # :nodoc:
|
40
|
+
raise Resourceful::Exceptions::ConfigurationError, "invalid Mechanize agent" unless agent && agent_url && !agent_url.empty?
|
41
|
+
opts ||= {}
|
42
|
+
opts[:force] ||= false
|
43
|
+
if path =~ /^(.+)\.(.+)$/
|
44
|
+
path = $1
|
45
|
+
opts[:format] ||= $2
|
46
|
+
end
|
47
|
+
opts[:format] ||= Resourceful::Resource::Json.to_s
|
48
|
+
opts[:params] ||= {}
|
49
|
+
[path, opts]
|
50
|
+
end
|
51
|
+
|
52
|
+
def self.resource_path(path, format, params) # :nodoc:
|
53
|
+
"#{path}.#{format}#{params.to_http_query_str unless params.empty?}"
|
54
|
+
end
|
55
|
+
|
56
|
+
def summary(verb, full_resource_path) # :nodoc:
|
57
|
+
"#{verb.upcase} #{agent_url}#{full_resource_path}"
|
58
|
+
end
|
59
|
+
|
60
|
+
def agent_url
|
61
|
+
@host
|
62
|
+
end
|
63
|
+
|
18
64
|
def log(msg, level = :info) # :nodoc:
|
19
65
|
if(msg)
|
20
66
|
if @logger && @logger.respond_to?(level)
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'mechanize'
|
2
|
+
require File.join(File.dirname(__FILE__), 'base.rb')
|
3
|
+
|
4
|
+
module Resourceful
|
5
|
+
module Agent
|
6
|
+
class Mechanize < Resourceful::Agent::Base
|
7
|
+
|
8
|
+
def initialize(args={})
|
9
|
+
super(args)
|
10
|
+
self.log = yield if block_given?
|
11
|
+
@mechanize = ::WWW::Mechanize.new
|
12
|
+
end
|
13
|
+
|
14
|
+
def get(path, opts={})
|
15
|
+
super(path, opts) do |path|
|
16
|
+
@mechanize.get("#{@host}#{path}") do |page|
|
17
|
+
if block_given?
|
18
|
+
yield page
|
19
|
+
else
|
20
|
+
page
|
21
|
+
end
|
22
|
+
end.body
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
protected
|
27
|
+
|
28
|
+
def agent
|
29
|
+
@mechanize
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -5,56 +5,24 @@ module Resourceful
|
|
5
5
|
module Agent
|
6
6
|
class RestClient < Resourceful::Agent::Base
|
7
7
|
|
8
|
-
|
9
|
-
ATTRS.each { |a| attr_reader a }
|
10
|
-
|
11
|
-
def initialize(args={})
|
12
|
-
ATTRS.each { |a| instance_variable_set("@#{a.to_s}", args.delete(a)) }
|
8
|
+
def initialize(args={})
|
13
9
|
super(args)
|
14
10
|
self.log = ::RestClient.log = yield if block_given?
|
15
|
-
@
|
11
|
+
@rest_client = ::RestClient::Resource.new(@host, :user => @user, :password => @password)
|
16
12
|
end
|
17
13
|
|
18
14
|
def get(path, opts={})
|
19
|
-
path, opts
|
20
|
-
|
21
|
-
|
22
|
-
full_resource_path = self.class.resource_path(path, format, opts[:params])
|
23
|
-
resource_summary = summary('get', full_resource_path)
|
24
|
-
cache_key = Resourceful::Resource::Cache.key(@host, 'get', full_resource_path)
|
25
|
-
|
26
|
-
if opts[:force] || (resp = cache.read(cache_key)).nil?
|
27
|
-
log "Resource call: #{resource_summary}"
|
28
|
-
resp = cache.write(cache_key, @agent[full_resource_path].get)
|
29
|
-
else
|
30
|
-
log "Resource call: [CACHE] #{resource_summary}"
|
15
|
+
super(path, opts) do |path|
|
16
|
+
@rest_client[path].get
|
31
17
|
end
|
32
|
-
format.build(resp)
|
33
18
|
end
|
34
|
-
|
19
|
+
|
35
20
|
protected
|
36
|
-
|
37
|
-
def check_config(path, opts)
|
38
|
-
raise Resourceful::Exceptions::ConfigurationError, "invalid RestClient agent" unless @agent && @agent.url
|
39
|
-
opts ||= {}
|
40
|
-
opts[:force] ||= false
|
41
|
-
if path =~ /^(.+)\.(.+)$/
|
42
|
-
path = $1
|
43
|
-
opts[:format] ||= $2
|
44
|
-
end
|
45
|
-
opts[:format] ||= Resourceful::Resource::Json.to_s
|
46
|
-
opts[:params] ||= {}
|
47
|
-
[path, opts]
|
48
|
-
end
|
49
21
|
|
50
|
-
def
|
51
|
-
|
22
|
+
def agent
|
23
|
+
@rest_client
|
52
24
|
end
|
53
25
|
|
54
|
-
def summary(verb, full_resource_path) # :nodoc:
|
55
|
-
"#{verb.upcase} #{@agent.url}#{full_resource_path}"
|
56
|
-
end
|
57
|
-
|
58
26
|
end
|
59
27
|
end
|
60
28
|
end
|
data/lib/resourceful/version.rb
CHANGED
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.
|
4
|
+
version: 0.4.3
|
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-
|
12
|
+
date: 2009-08-04 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -76,6 +76,7 @@ files:
|
|
76
76
|
- lib/resourceful
|
77
77
|
- lib/resourceful/agent
|
78
78
|
- lib/resourceful/agent/base.rb
|
79
|
+
- lib/resourceful/agent/mechanize.rb
|
79
80
|
- lib/resourceful/agent/rest_client.rb
|
80
81
|
- lib/resourceful/agent.rb
|
81
82
|
- lib/resourceful/exceptions.rb
|
@@ -91,7 +92,7 @@ files:
|
|
91
92
|
- lib/resourceful/resource.rb
|
92
93
|
- lib/resourceful/version.rb
|
93
94
|
- lib/resourceful.rb
|
94
|
-
has_rdoc:
|
95
|
+
has_rdoc: false
|
95
96
|
homepage: ""
|
96
97
|
licenses:
|
97
98
|
post_install_message:
|
@@ -117,7 +118,7 @@ requirements: []
|
|
117
118
|
rubyforge_project:
|
118
119
|
rubygems_version: 1.3.5
|
119
120
|
signing_key:
|
120
|
-
specification_version:
|
121
|
+
specification_version: 3
|
121
122
|
summary: A ruby gem to abstract web resource handling.
|
122
123
|
test_files: []
|
123
124
|
|