sawyer 0.0.7 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -3,6 +3,8 @@
3
3
  Sawyer is an experimental secret user agent built on top of
4
4
  [Faraday][faraday].
5
5
 
6
+ [faraday]: https://github.com/lostisland/faraday
7
+
6
8
  ![](http://techno-weenie.net/sawyer/images/faraday.jpeg)
7
9
 
8
10
  Think of Faraday as the nerdy scientist behind an HTTP API. Sure, he
@@ -1,5 +1,5 @@
1
1
  module Sawyer
2
- VERSION = "0.0.7"
2
+ VERSION = "0.0.8"
3
3
 
4
4
  class Error < StandardError; end
5
5
  end
@@ -12,5 +12,6 @@ require 'set'
12
12
  response
13
13
  serializer
14
14
  agent
15
- hal_rels_parser
15
+ link_parsers/hal
16
+ link_parsers/simple
16
17
  ).each { |f| require File.expand_path("../sawyer/#{f}", __FILE__) }
@@ -38,7 +38,7 @@ module Sawyer
38
38
  @endpoint = endpoint
39
39
  @conn = (options && options[:faraday]) || Faraday.new
40
40
  @serializer = (options && options[:serializer]) || self.class.serializer
41
- @links_parser = (options && options[:links_parser]) || HalLinksParser.new
41
+ @links_parser = (options && options[:links_parser]) || Sawyer::LinkParsers::Hal.new
42
42
  @allow_undefined_methods = (options && options[:allow_undefined_methods])
43
43
  @conn.url_prefix = @endpoint
44
44
  yield @conn if block_given?
@@ -0,0 +1,15 @@
1
+ module Sawyer
2
+ module LinkParsers
3
+
4
+ class Hal
5
+
6
+ def parse(data)
7
+ links = data.delete(:_links)
8
+
9
+ return data, links
10
+ end
11
+
12
+ end
13
+
14
+ end
15
+ end
@@ -0,0 +1,29 @@
1
+ module Sawyer
2
+ module LinkParsers
3
+
4
+ class Simple
5
+
6
+ LINK_REGEX = /_?url$/
7
+
8
+
9
+ # Public: Parses simple *_url style links on resources
10
+ #
11
+ # data - Hash of resource data
12
+ #
13
+ # Returns a Hash of data with separate links Hash
14
+ def parse(data)
15
+
16
+ links = {}
17
+ inline_links = data.keys.select {|k| k.to_s[LINK_REGEX] }
18
+ inline_links.each do |key|
19
+ rel_name = key.to_s == 'url' ? 'self' : key.to_s.gsub(LINK_REGEX, '')
20
+ links[rel_name.to_sym] = data.delete(key)
21
+ end
22
+
23
+ return data, links
24
+ end
25
+
26
+ end
27
+
28
+ end
29
+ end
@@ -16,6 +16,7 @@ module Sawyer
16
16
  @headers = res.headers
17
17
  @env = res.env
18
18
  @data = process_data(@agent.decode_body(res.body))
19
+ @rels = process_rels
19
20
  end
20
21
 
21
22
  # Turns parsed contents from an API response into a Resource or
@@ -33,6 +34,19 @@ module Sawyer
33
34
  end
34
35
  end
35
36
 
37
+ # Finds link relations from 'Link' response header
38
+ #
39
+ # Returns an array of Relations
40
+ def process_rels
41
+ links = ( @headers["Link"] || "" ).split(', ').map do |link|
42
+ href, name = link.match(/<(.*?)>; rel="(\w+)"/).captures
43
+
44
+ [name.to_sym, Relation.from_link(@agent, name, :href => href)]
45
+ end
46
+
47
+ Hash[*links.flatten]
48
+ end
49
+
36
50
  def timing
37
51
  @timing ||= @env[:sawyer_ended] - @env[:sawyer_started]
38
52
  end
@@ -118,5 +118,25 @@ module Sawyer
118
118
  res[:b] = 2
119
119
  assert_equal 2, res.b
120
120
  end
121
+
122
+ def test_simple_rel_parsing
123
+ @agent.links_parser = Sawyer::LinkParsers::Simple.new
124
+ res = Resource.new @agent,
125
+ :url => '/',
126
+ :user => {
127
+ :id => 1,
128
+ :url => '/users/1',
129
+ :followers_url => '/users/1/followers'
130
+ }
131
+
132
+ assert_equal '/', res.rels[:self].href
133
+ assert_kind_of Resource, res.user
134
+ assert !res.fields.include?(:url)
135
+ assert_equal 1, res.user.id
136
+ assert_equal '/users/1', res.user.rels[:self].href
137
+ assert !res.user.fields.include?(:url)
138
+ assert_equal '/users/1/followers', res.user.rels[:followers].href
139
+ assert !res.user.fields.include?(:followers_url)
140
+ end
121
141
  end
122
142
  end
@@ -9,7 +9,10 @@ module Sawyer
9
9
  conn.builder.handlers.delete(Faraday::Adapter::NetHttp)
10
10
  conn.adapter :test, @stubs do |stub|
11
11
  stub.get '/' do
12
- [200, {'Content-Type' => 'application/json'}, Sawyer::Agent.encode(
12
+ [200, {
13
+ 'Content-Type' => 'application/json',
14
+ 'Link' => '</starred?page=2>; rel="next", </starred?page=19>; rel="last"'
15
+ }, Sawyer::Agent.encode(
13
16
  :a => 1,
14
17
  :_links => {
15
18
  :self => {:href => '/a', :method => 'POST'}
@@ -42,6 +45,10 @@ module Sawyer
42
45
  end
43
46
 
44
47
  def test_gets_rels
48
+ assert_equal '/starred?page=2', @res.rels[:next].href
49
+ assert_equal :get, @res.rels[:next].method
50
+ assert_equal '/starred?page=19', @res.rels[:last].href
51
+ assert_equal :get, @res.rels[:next].method
45
52
  assert_equal '/a', @res.data.rels[:self].href
46
53
  assert_equal :post, @res.data.rels[:self].method
47
54
  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.7
4
+ version: 0.0.8
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-01-05 00:00:00.000000000 Z
12
+ date: 2013-04-15 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: faraday
@@ -55,7 +55,8 @@ files:
55
55
  - Rakefile
56
56
  - sawyer.gemspec
57
57
  - lib/sawyer/agent.rb
58
- - lib/sawyer/hal_rels_parser.rb
58
+ - lib/sawyer/link_parsers/hal.rb
59
+ - lib/sawyer/link_parsers/simple.rb
59
60
  - lib/sawyer/relation.rb
60
61
  - lib/sawyer/resource.rb
61
62
  - lib/sawyer/response.rb
@@ -1,13 +0,0 @@
1
- module Sawyer
2
-
3
- class HalLinksParser
4
-
5
- def parse(data)
6
- links = data.delete(:_links)
7
-
8
- return data, links
9
- end
10
-
11
- end
12
-
13
- end