path-to 0.2.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt CHANGED
@@ -1,31 +1,30 @@
1
+ == 0.3.0 2009-05-08
2
+
3
+ * Add http_options to Application, to be passed through to the HTTP client with requests
4
+ * Add an example of path-to/described_routes against Delicious
5
+
1
6
  == 0.2.1 2009-04-18
2
7
 
3
- * Minor enhancements
4
- * Use new Addressable API
5
- * support app[params]
6
- * more tests!
8
+ * Use new Addressable API
9
+ * support app[params]
10
+ * more tests!
7
11
 
8
12
  == 0.2.0 2009-04-18
9
13
 
10
- * Major enhancement
11
- * Support for nested described_routes, friendlier initialisation
14
+ * Support for nested described_routes, friendlier initialisation
12
15
 
13
16
  == 0.1.0 2009-04-18
14
17
 
15
- * Major enhancement
16
- * Support for described_routes
18
+ * Support for described_routes
17
19
 
18
20
  == 0.0.3 2009-04-18
19
21
 
20
- * Minor enhancement:
21
- * Update README with installation and contact info
22
+ * Update README with installation and contact info
22
23
 
23
24
  == 0.0.2 2009-04-18
24
25
 
25
- * Minor bugfix:
26
- * lib/path-to/http_client.rb missing from manifest
26
+ * lib/path-to/http_client.rb missing from manifest
27
27
 
28
28
  == 0.0.1 2009-04-04
29
29
 
30
- * 1 major enhancement:
31
- * Initial release
30
+ * Initial release
data/Manifest.txt CHANGED
@@ -4,6 +4,8 @@ Manifest.txt
4
4
  PostInstall.txt
5
5
  README.rdoc
6
6
  Rakefile
7
+ examples/delicious.rb
8
+ examples/delicious.yaml
7
9
  lib/path-to.rb
8
10
  lib/path-to/application.rb
9
11
  lib/path-to/described_routes.rb
data/README.rdoc CHANGED
@@ -1,8 +1,12 @@
1
1
  = path-to README
2
2
 
3
+ Model web apps easily and access them via nice app-specific Ruby APIs.
4
+
5
+ Note to reader: You are invited to comment on the roadmap at http://positiveincline.com/?p=213
6
+
3
7
  == Description
4
8
 
5
- path-to allows web applications to be modelled via URI templates and then accessed through an application-specific Ruby API. It is designed to be extended easily to support discovery mechanisms; support for XRD may be added at some future date.
9
+ path-to allows web applications to be modelled via URI templates and then accessed through an application-specific Ruby API. It is designed to be extended easily to support discovery mechanisms; included is an implementation based on the resource templates of described_routes.
6
10
 
7
11
  == Synopsis
8
12
 
@@ -17,6 +21,8 @@ Create a client application configured from a server that supports described_rou
17
21
  app.users["user_id" => "dojo"].articles.recent #=> http://example.com/users/dojo/articles/recent
18
22
  app.users["user_id" => "dojo"].articles.recent.get #=> "<html>...</html>"
19
23
 
24
+ See examples/delicious.rb for an example based on a partial YAML-based description of the Delicious API held locally.
25
+
20
26
  === Local configuration
21
27
 
22
28
  require "path-to"
data/Rakefile CHANGED
@@ -2,21 +2,14 @@
2
2
  $:.push File.dirname(__FILE__) + '/lib'
3
3
  require 'path-to'
4
4
 
5
- # undefined method `empty?' for nil:NilClass
6
- # /Library/Ruby/Site/1.8/rubygems/specification.rb:886:in `validate'
7
- class NilClass
8
- def empty?
9
- true
10
- end
11
- end
12
-
13
5
  # Generate all the Rake tasks
14
6
  # Run 'rake -T' to see list of generated tasks (from gem root directory)
15
7
  $hoe = Hoe.new('path-to', PathTo::VERSION) do |p|
16
8
  p.developer('Mike Burrows (asplake)', 'mjb@asplake.co.uk')
17
9
  p.changes = p.paragraphs_of("History.txt", 0..1).join("\n\n")
18
10
  p.post_install_message = 'PostInstall.txt' # TODO remove if post-install message not required
19
- p.rubyforge_name = p.name # TODO this is default value
11
+ p.rubyforge_name = p.name
12
+ p.url = 'http://positiveincline.com/?p=213'
20
13
  p.extra_deps = [
21
14
  ['httparty','>= 0.4.2'],
22
15
  ['addressable','>= 2.0.2'],
@@ -0,0 +1,43 @@
1
+ # Adapted from jnunemaker/httparty/examples/delicious.rb to demonstrate path-to's metadata-driven REST client API capability
2
+ # For more information see http://positiveincline.com/?tag=path-to
3
+
4
+ require 'path-to/described_routes'
5
+ require 'pp'
6
+
7
+ # read $HOME/.delicious (YAML format):
8
+ # username: <username>
9
+ # password: <password>
10
+ config = YAML::load(File.read(File.join(ENV['HOME'], '.delicious')))
11
+
12
+ # Create a basic delicious API object driven by the metadata in delicious.yaml.
13
+ # This defines these resources (of which the last two are equivalent):
14
+ #
15
+ # delicious.posts
16
+ # delicious.posts.recent
17
+ # delicious.recent_posts
18
+ #
19
+ # delicious.posts may take (as delicious.posts(params) or delicious.post[params])
20
+ #
21
+ # tag (optional). Filter by this tag.
22
+ # dt (optional). Filter by this date (CCYY-MM-DDThh:mm:ssZ).
23
+ # url (optional). Filter by this url.
24
+ #
25
+ # delicious.recent_posts may take
26
+ #
27
+ # tag (optional). Filter by this tag.
28
+ # count (optional). Number of items to retrieve (Default:15, Maximum:100)
29
+ #
30
+ # Same for delicious.posts.recent, or it can inherit the tag parameter from posts, as in
31
+ #
32
+ # pp delicious.posts['tag' => 'ruby'].recent
33
+ #
34
+ delicious = PathTo::DescribedRoutes::Application.new(
35
+ :yaml => File.read(File.join(File.dirname(__FILE__), 'delicious.yaml')),
36
+ :http_options => {
37
+ :basic_auth => {
38
+ :username => config['username'],
39
+ :password => config['password']}})
40
+
41
+ pp delicious.posts['tag' => 'ruby'].get
42
+ pp delicious.posts['tag' => 'ruby'].recent['count' => '5'].get
43
+ delicious.recent_posts.get['posts']['post'].each { |post| puts post['href'] }
@@ -0,0 +1,18 @@
1
+ ---
2
+ - name: posts
3
+ options:
4
+ - GET
5
+ uri_template: https://api.del.icio.us/v1/posts/get?{-join|&|tag,dt,url}
6
+ optional_parameters:
7
+ - tag
8
+ - dt
9
+ - url
10
+ resource_templates:
11
+ - name: recent_posts
12
+ options:
13
+ - GET
14
+ uri_template: https://api.del.icio.us/v1/posts/recent?{-join|&|tag,count}
15
+ rel: recent
16
+ optional_parameters:
17
+ - tag
18
+ - count
@@ -35,10 +35,14 @@ module PathTo
35
35
  # An HTTParty or similar
36
36
  attr_reader :http_client
37
37
 
38
+ # Hash of options to be included in HTTP method calls
39
+ attr_reader :http_options
40
+
38
41
  #
39
42
  # Initializes an Application. Parameters:
40
43
  #
41
44
  # [templates] Initial value for the templates attribute, defaults to {}
45
+ # [http_options] Initial value for the http_options attribute, defaults to nil
42
46
  # [default_type] Initial value for the default_type attribute, defaults to Path
43
47
  # [http_client] An object through which http calls are invoked. See HTTPClient and Path#http_client.
44
48
  #
@@ -61,9 +65,9 @@ module PathTo
61
65
  # end
62
66
  # end
63
67
  #
64
- def initialize(templates = {}, default_type = Path, http_client = HTTPClient)
68
+ def initialize(templates = {}, http_options = nil, default_type = Path, http_client = HTTPClient)
65
69
  super() # with default args
66
- @templates, @default_type, @http_client = templates, default_type, http_client
70
+ @templates, @http_options, @default_type, @http_client = templates, http_options, default_type, http_client
67
71
  yield self if block_given?
68
72
  end
69
73
 
@@ -87,6 +87,9 @@ module PathTo
87
87
  # Base URI of the application
88
88
  attr_reader :base
89
89
 
90
+ # Hash of options to be included in HTTP method calls
91
+ attr_reader :http_options
92
+
90
93
  def initialize(options)
91
94
  super(options[:parent], options[:service], options[:params])
92
95
 
@@ -94,6 +97,7 @@ module PathTo
94
97
  @base.sub(/\/$/, '') if base
95
98
  @default_type = options[:default_type] || TemplatedPath
96
99
  @http_client = options[:http_client] || HTTPClient
100
+ @http_options = options[:http_options]
97
101
 
98
102
  @resource_templates = options[:resource_templates]
99
103
  unless @resource_templates
@@ -111,6 +115,7 @@ module PathTo
111
115
  @default_type ||= parent.default_type
112
116
  @http_client ||= parent.http_client
113
117
  @resource_templates ||= parent.resource_templates
118
+ @http_options ||= parent.http_options
114
119
  end
115
120
  end
116
121
 
data/lib/path-to/path.rb CHANGED
@@ -45,30 +45,45 @@ module PathTo
45
45
  # GET request on this object's URI
46
46
  #
47
47
  def get(*args)
48
- http_client.get(uri, *args)
48
+ http_client.get(uri, *merge_http_options(args))
49
49
  end
50
50
 
51
51
  #
52
52
  # PUT request on this object's URI
53
53
  #
54
54
  def put(*args)
55
- http_client.put(uri, *args)
55
+ http_client.put(uri, *merge_http_options(args))
56
56
  end
57
57
 
58
58
  #
59
59
  # POST request on this object's URI
60
60
  #
61
61
  def post(*args)
62
- http_client.post(uri, *args)
62
+ http_client.post(uri, *merge_http_options(args))
63
63
  end
64
64
 
65
65
  #
66
66
  # DELETE request on this object's URI
67
67
  #
68
68
  def delete(*args)
69
- http_client.delete(uri, *args)
69
+ http_client.delete(uri, *merge_http_options(args))
70
70
  end
71
71
 
72
+ #
73
+ # Include application.http_options (if exists) in args, either appending it or reverse-merging with the last element of args if it's a Hash
74
+ #
75
+ def merge_http_options(args)
76
+ if (http_options = application.http_options)
77
+ if args[-1].kind_of?(Hash)
78
+ args[-1] = http_options.merge(args[-1])
79
+ else
80
+ args << http_options
81
+ end
82
+ end
83
+ args
84
+ end
85
+
86
+
72
87
  def inspect #:nodoc:
73
88
  "#{uri} #<#{self.class.name}:#{"0x%x" % object_id} service=#{service.inspect}, params=#{params.inspect}>"
74
89
  end
data/lib/path-to.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  module PathTo
2
- VERSION = "0.2.1"
2
+ VERSION = "0.3.0"
3
3
  end
4
4
 
5
5
  $:.push File.dirname(__FILE__)
@@ -50,5 +50,18 @@ module PathTo
50
50
  users.send(method, request_options)
51
51
  end
52
52
  end
53
+
54
+ def test_http_methods_with_app_options
55
+ request_options = {:b => "b", :c => "c"}
56
+ app_options = {:a => "a", :b => "WILL BE OVERRIDDEN"}
57
+ expected_options = {:a => "a", :b => "b", :c => "c"}
58
+
59
+ app_with_options = Application.new(app.templates, app_options)
60
+
61
+ [:get, :put, :post, :delete].each do |method|
62
+ app_with_options.http_client.expects(method).with("http://example.com/users/", expected_options)
63
+ app_with_options.users.send(method, request_options)
64
+ end
65
+ end
53
66
  end
54
67
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: path-to
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mike Burrows (asplake)
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-04-30 00:00:00 +01:00
12
+ date: 2009-05-08 00:00:00 +01:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -40,7 +40,7 @@ dependencies:
40
40
  requirements:
41
41
  - - ">="
42
42
  - !ruby/object:Gem::Version
43
- version: 1.3.0
43
+ version: 1.4.1
44
44
  version:
45
45
  - !ruby/object:Gem::Dependency
46
46
  name: hoe
@@ -52,7 +52,7 @@ dependencies:
52
52
  - !ruby/object:Gem::Version
53
53
  version: 1.8.0
54
54
  version:
55
- description: path-to allows web applications to be modelled via URI templates and then accessed through an application-specific Ruby API. It is designed to be extended easily to support discovery mechanisms; support for XRD may be added at some future date.
55
+ description: path-to allows web applications to be modelled via URI templates and then accessed through an application-specific Ruby API. It is designed to be extended easily to support discovery mechanisms; included is an implementation based on the resource templates of described_routes.
56
56
  email:
57
57
  - mjb@asplake.co.uk
58
58
  executables: []
@@ -71,6 +71,8 @@ files:
71
71
  - PostInstall.txt
72
72
  - README.rdoc
73
73
  - Rakefile
74
+ - examples/delicious.rb
75
+ - examples/delicious.yaml
74
76
  - lib/path-to.rb
75
77
  - lib/path-to/application.rb
76
78
  - lib/path-to/described_routes.rb
@@ -87,7 +89,7 @@ files:
87
89
  - test/path-to/test_with_params.rb
88
90
  - test/test_helper.rb
89
91
  has_rdoc: true
90
- homepage:
92
+ homepage: http://positiveincline.com/?p=213
91
93
  licenses: []
92
94
 
93
95
  post_install_message: PostInstall.txt
@@ -111,7 +113,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
111
113
  requirements: []
112
114
 
113
115
  rubyforge_project: path-to
114
- rubygems_version: 1.3.2
116
+ rubygems_version: 1.3.3
115
117
  signing_key:
116
118
  specification_version: 3
117
119
  summary: path-to allows web applications to be modelled via URI templates and then accessed through an application-specific Ruby API