kelredd-resourceful 0.7.20 → 0.7.21
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +53 -11
- data/Rakefile +0 -1
- data/lib/resourceful/agent/base.rb +4 -4
- data/lib/resourceful/model/base.rb +3 -1
- data/lib/resourceful/resource/cache.rb +5 -4
- data/lib/resourceful/version.rb +1 -1
- metadata +2 -12
data/README.rdoc
CHANGED
@@ -2,27 +2,31 @@
|
|
2
2
|
|
3
3
|
== Description
|
4
4
|
|
5
|
-
A ruby gem to abstract web resource handling.
|
6
|
-
|
7
|
-
|
5
|
+
A ruby gem to abstract web resource handling.
|
6
|
+
|
7
|
+
The key to resourceful is being able to interact with web resources using custom defined objects instead of the raw data. Unlike ActiveResource, Resourceful needs no rigid restful API backing its models. All you need is data in either JSON or XML (or HTML), and be able to creatively define models from that data.
|
8
8
|
|
9
9
|
The supported formats are:
|
10
10
|
* JSON (objects returned as Ruby Hash)
|
11
11
|
* XML (objects returned as Nokogiri Xml object)
|
12
|
+
|
13
|
+
Resourceful uses agents to fetch and push data to and from web resources. Supported agents are:
|
14
|
+
* rest_client: great for interacting with Restful resources
|
15
|
+
* mechanize: great for interacting with raw resources, or secured resources (and for screen scraping raw html)
|
12
16
|
|
13
17
|
== Installation
|
14
18
|
|
15
|
-
sudo gem install kelredd-resourceful --source http://
|
19
|
+
sudo gem install kelredd-resourceful --source http://gemcutter.org
|
16
20
|
|
17
21
|
== Dependencies
|
18
22
|
|
19
23
|
* nokogiri (xml resource format handling)
|
20
24
|
* json (json resource format handling)
|
21
|
-
* rest_client (
|
25
|
+
* rest_client (restful web resource access agent)
|
26
|
+
* mechanize (more raw web resource access agent; access secured resources)
|
22
27
|
* log4r (for resource interaction logging)
|
23
|
-
* kelredd-useful (some nice ruby helpers)
|
24
28
|
|
25
|
-
== Usage
|
29
|
+
== Basic Usage
|
26
30
|
|
27
31
|
# Single host resource handling
|
28
32
|
require 'resourceful'
|
@@ -31,11 +35,49 @@ The supported formats are:
|
|
31
35
|
Resourceful::Resource.get '/widgets', :format => 'json' # specify explicit format
|
32
36
|
Resourceful::Resource.get '/widgets.json' # or, imply format from resource request
|
33
37
|
|
34
|
-
|
38
|
+
== Defining Models
|
39
|
+
|
40
|
+
So it's all about the models, right. So you have this data resource on the web: a JSON API, an XML feed, an old crappy HTML page, a secured page with personal data you want to access. You start noticing attributes and relationships in this data - it's time to define some models. Enter resourceful. Resourceful gives you fancy pants ways to define and test some models, without bothering with all the kruft of accessing those resources via the web. Check it:
|
41
|
+
|
42
|
+
# TODO: show examples of
|
43
|
+
* base access stuff
|
44
|
+
* defining attributes
|
45
|
+
* defining relationships
|
46
|
+
* testing the model
|
47
|
+
|
48
|
+
== Cacheing
|
49
|
+
|
50
|
+
Resourceful provides resource caching by default, both at the model level and at the agent level.
|
51
|
+
|
52
|
+
=== Model Caching
|
53
|
+
|
54
|
+
Model caching refers to using the instance of the model to store attribute and association values. Resourceful, by default, caches all attribute values and associations in the model instance. Both attribute values and association values can be "reloaded" by passing true as a single parameter to the attribute or association method. Please note:
|
55
|
+
* For attribute values, "reloading" simply means parsing the value from the existing raw agent data. The resource is not fetched via the web for attribute reloading.
|
56
|
+
* For associations, "reloading" means calling out for the resource and fetching it from the web. This bypasses any agent caching and forces the resource data to be downloaded.
|
57
|
+
|
58
|
+
=== Agent Caching
|
59
|
+
|
60
|
+
Agent caching refers to using a built in cache to store web resource responses for future access. Resourceful, by default, caches all web requests made via the agents. Resource responses are cached in memory, based on a unique key, and expire, by default, after 60 seconds. Custom expiration periods can be specified. This improves performance when repetitively accessing the same resource, while ensuring the resource does not become stale. Caching can be bypassed on any request by forcing data download using the :force => true option in agent or model data access methods.
|
61
|
+
* Note: all cached requests will be prepended with "[CACHE]" in resource agent logs
|
62
|
+
|
63
|
+
== Logging
|
64
|
+
|
65
|
+
Resourceful provides resource agent logging for you by default. Log information is written to the console for confirmation of resource access. In addition, you can specify a file to log to by implementing a block that returns the path of a file to log to, such as:
|
66
|
+
|
67
|
+
@mechanize ||= Resourceful::Agent::Mechanize.new(:host => @host, :agent_alias => @agent_alias, :verbose => @verbose) do
|
68
|
+
@log_file ? File.expand_path(@log_file) : nil
|
69
|
+
end
|
70
|
+
|
71
|
+
== Examples
|
72
|
+
|
73
|
+
# TODO: add in examples:
|
74
|
+
* Restful JSON / XML resources
|
75
|
+
* Resourceful external associations
|
76
|
+
* Using associations with non resourceful ruby class (such as an ActiveRecord model)
|
77
|
+
* Screen scraping example (using mechanize to access a secured resource)
|
35
78
|
|
36
|
-
== Testing
|
37
|
-
A suite of cucumber features are available for you to run as
|
38
|
-
for additional documentation and usage scenarios. To run the features:
|
79
|
+
== Testing the Resourceful gem
|
80
|
+
A suite of cucumber features are available for you to run as acceptance tests. You should look to the features for additional documentation and usage scenarios. To run the features:
|
39
81
|
|
40
82
|
# make sure you have these gems
|
41
83
|
$ sudo gem install cucumber
|
data/Rakefile
CHANGED
@@ -5,14 +5,14 @@ module Resourceful
|
|
5
5
|
module Agent
|
6
6
|
class Base
|
7
7
|
|
8
|
-
attr_reader :logger, :cache
|
8
|
+
attr_reader :logger, :cache, :expiration, :log_prefix
|
9
9
|
|
10
|
-
ATTRS = [:host, :user, :password]
|
10
|
+
ATTRS = [:host, :user, :password, :expiration, :log_prefix]
|
11
11
|
ATTRS.each { |a| attr_reader a }
|
12
12
|
|
13
13
|
def initialize(args={})
|
14
14
|
ATTRS.each { |a| instance_variable_set("@#{a.to_s}", args.delete(a)) }
|
15
|
-
@cache = Resourceful::Resource::Cache.new
|
15
|
+
@cache = Resourceful::Resource::Cache.new @expiration
|
16
16
|
@logger = Log4r::Logger.new('[Resourceful]')
|
17
17
|
@logger.add(Log4r::StdoutOutputter.new('console'))
|
18
18
|
end
|
@@ -61,7 +61,7 @@ module Resourceful
|
|
61
61
|
end
|
62
62
|
|
63
63
|
def log(msg, level = :info) # :nodoc:
|
64
|
-
@logger.send(level.to_s, msg) if msg
|
64
|
+
@logger.send(level.to_s, "#{"[#{@log_prefix.to_s}]" if @log_prefix} #{msg}") if msg
|
65
65
|
end
|
66
66
|
|
67
67
|
def log=(file)
|
@@ -105,7 +105,9 @@ module Resourceful
|
|
105
105
|
fetch_attribute(clean_name, config, content_method)
|
106
106
|
end
|
107
107
|
# method to read the attribute value (intended to be overridden when necessary)
|
108
|
-
define_method(name) do
|
108
|
+
define_method(name) do |*args|
|
109
|
+
reload = args.first || false
|
110
|
+
instance_variable_set("@#{clean_name}", nil) if reload
|
109
111
|
fetch_attribute(clean_name, config, content_method)
|
110
112
|
end
|
111
113
|
# method to write the attribute value
|
@@ -2,15 +2,16 @@ module Resourceful
|
|
2
2
|
module Resource
|
3
3
|
class Cache
|
4
4
|
|
5
|
-
attr_reader :store
|
5
|
+
attr_reader :store, :expiration
|
6
6
|
|
7
|
-
EXPIRY_SECS =
|
7
|
+
EXPIRY_SECS = 60
|
8
8
|
|
9
9
|
def self.key(host, verb, resource)
|
10
10
|
"#{host}_#{verb}_#{resource}"
|
11
11
|
end
|
12
12
|
|
13
|
-
def initialize
|
13
|
+
def initialize(expiration=EXPIRY_SECS)
|
14
|
+
@expiration = (expiration && expiration.kind_of?(::Fixnum)) ? expiration : EXPIRY_SECS
|
14
15
|
@store = {}
|
15
16
|
end
|
16
17
|
|
@@ -28,7 +29,7 @@ module Resourceful
|
|
28
29
|
end
|
29
30
|
|
30
31
|
def write(key, value)
|
31
|
-
@store[key] = {:value => value, :expires => Time.now.to_i +
|
32
|
+
@store[key] = {:value => value, :expires => Time.now.to_i + @expiration}
|
32
33
|
value
|
33
34
|
end
|
34
35
|
|
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.7.
|
4
|
+
version: 0.7.21
|
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-10-
|
12
|
+
date: 2009-10-03 00:00:00 -05:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -62,16 +62,6 @@ dependencies:
|
|
62
62
|
- !ruby/object:Gem::Version
|
63
63
|
version: "0"
|
64
64
|
version:
|
65
|
-
- !ruby/object:Gem::Dependency
|
66
|
-
name: kelredd-useful
|
67
|
-
type: :runtime
|
68
|
-
version_requirement:
|
69
|
-
version_requirements: !ruby/object:Gem::Requirement
|
70
|
-
requirements:
|
71
|
-
- - ">="
|
72
|
-
- !ruby/object:Gem::Version
|
73
|
-
version: "0"
|
74
|
-
version:
|
75
65
|
description:
|
76
66
|
email: kelly@kelredd.com
|
77
67
|
executables: []
|