sawyer 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/lib/sawyer/agent.rb CHANGED
@@ -42,7 +42,7 @@ module Sawyer
42
42
  end
43
43
 
44
44
  options ||= {}
45
- url = URITemplate.new(url).expand(options[:uri] || {})
45
+ url = expand_url(url, options[:uri])
46
46
  res = @conn.send method, url do |req|
47
47
  req.body = encode_body(data) if data
48
48
  if params = options[:query]
@@ -74,6 +74,12 @@ module Sawyer
74
74
  Yajl.load str, :symbolize_keys => true
75
75
  end
76
76
 
77
+ def expand_url(url, options = nil)
78
+ tpl = url.respond_to?(:expand) ? url : URITemplate.new(url.to_s)
79
+ expand = tpl.method(:expand)
80
+ options ? expand.call(options) : expand.call
81
+ end
82
+
77
83
  def inspect
78
84
  %(<#{self.class} #{@endpoint}>)
79
85
  end
@@ -46,7 +46,7 @@ module Sawyer
46
46
 
47
47
  attr_reader :agent,
48
48
  :name,
49
- :href,
49
+ :href_template,
50
50
  :method,
51
51
  :available_methods
52
52
 
@@ -93,8 +93,8 @@ module Sawyer
93
93
  # method - The Symbol HTTP method. Default: :get
94
94
  def initialize(agent, name, href, method = nil)
95
95
  @agent = agent
96
- @name = name.to_sym
97
- @href = href.to_s
96
+ @name = name.to_sym
97
+ @href_template = URITemplate.new(href.to_s)
98
98
 
99
99
  methods = nil
100
100
 
@@ -224,6 +224,11 @@ module Sawyer
224
224
  call data, opt
225
225
  end
226
226
 
227
+ def href(options = nil)
228
+ method = @href_template.method(:expand)
229
+ options ? method.call(options) : method.call
230
+ end
231
+
227
232
  # Public: Makes an API request with the curent Relation.
228
233
  #
229
234
  # data - The Optional Hash or Resource body to be sent. :get or :head
@@ -242,11 +247,11 @@ module Sawyer
242
247
  raise ArgumentError, "method #{m.inspect} is not available: #{@available_methods.to_a.inspect}"
243
248
  end
244
249
 
245
- @agent.call m || @method, @href, data, options
250
+ @agent.call m || @method, @href_template, data, options
246
251
  end
247
252
 
248
253
  def inspect
249
- %(#<#{self.class}: #{@name}: #{@method} #{@href}>)
254
+ %(#<#{self.class}: #{@name}: #{@method} #{@href_template}>)
250
255
  end
251
256
  end
252
257
  end
data/lib/sawyer.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  module Sawyer
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
 
4
4
  class Error < StandardError; end
5
5
  end
data/sawyer.gemspec CHANGED
@@ -13,7 +13,7 @@ Gem::Specification.new do |s|
13
13
  ## If your rubyforge_project name is different, then edit it and comment out
14
14
  ## the sub! line in the Rakefile
15
15
  s.name = 'sawyer'
16
- s.version = '0.0.1'
16
+ s.version = '0.0.2'
17
17
  s.date = '2012-09-25'
18
18
  s.rubyforge_project = 'sawyer'
19
19
 
@@ -25,6 +25,7 @@ module Sawyer
25
25
  assert_equal '/comments', rel.href
26
26
  assert_equal :get, rel.method
27
27
  assert_equal [:get, :post], rel.available_methods.to_a
28
+ assert_kind_of URITemplate, rel.href_template
28
29
  end
29
30
 
30
31
  def test_builds_rels_from_hash_index
@@ -41,6 +42,7 @@ module Sawyer
41
42
  assert_equal '/users/1', rel.href
42
43
  assert_equal :get, rel.method
43
44
  assert_equal [:get], rel.available_methods.to_a
45
+ assert_kind_of URITemplate, rel.href_template
44
46
  end
45
47
 
46
48
  def test_builds_rels_from_nil
@@ -101,6 +103,9 @@ module Sawyer
101
103
 
102
104
  rel = Sawyer::Relation.new agent, :repo, "{/user,repo}{?a,b}"
103
105
 
106
+ assert_equal '', rel.href
107
+ assert_equal '/octocat', rel.href(:user => :octocat)
108
+
104
109
  assert_equal 404, rel.get.status
105
110
  assert_equal 200, rel.get(:uri => {'user' => 'octocat', 'repo' => 'hello', 'a' => 1, 'b' => 2}).status
106
111
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sawyer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: