Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Simple parser for *_url style links

  • Loading branch information...
commit 451e3390cf10d48e8ef80581e43b9f2f0b63a695 1 parent 2ef9b36
@pengwynn pengwynn authored
View
1  lib/sawyer.rb
@@ -13,4 +13,5 @@ class Error < StandardError; end
serializer
agent
link_parsers/hal
+ link_parsers/simple
).each { |f| require File.expand_path("../sawyer/#{f}", __FILE__) }
View
13 lib/sawyer/hal_rels_parser.rb
@@ -1,13 +0,0 @@
-module Sawyer
-
- class HalLinksParser
-
- def parse(data)
- links = data.delete(:_links)
-
- return data, links
- end
-
- end
-
-end
View
29 lib/sawyer/link_parsers/simple.rb
@@ -0,0 +1,29 @@
+module Sawyer
+ module LinkParsers
+
+ class Simple
+
+ LINK_REGEX = /_?url$/
+
+
+ # Public: Parses simple *_url style links on resources
+ #
+ # data - Hash of resource data
+ #
+ # Returns a Hash of data with separate links Hash
+ def parse(data)
+
+ links = {}
+ inline_links = data.keys.select {|k| k.to_s[LINK_REGEX] }
+ inline_links.each do |key|
+ rel_name = key.to_s == 'url' ? 'self' : key.to_s.gsub(LINK_REGEX, '')
+ links[rel_name.to_sym] = data[key]
+ end
+
+ return data, links
+ end
+
+ end
+
+ end
+end
View
17 test/resource_test.rb
@@ -118,5 +118,22 @@ def test_handle_hash_notation_with_string_key
res[:b] = 2
assert_equal 2, res.b
end
+
+ def test_simple_rel_parsing
+ @agent.links_parser = Sawyer::LinkParsers::Simple.new
+ res = Resource.new @agent,
+ :url => '/',
+ :user => {
+ :id => 1,
+ :url => '/users/1',
+ :followers_url => '/users/1/followers'
+ }
+
+ assert_equal '/', res.rels[:self].href
+ assert_kind_of Resource, res.user
+ assert_equal 1, res.user.id
+ assert_equal '/users/1', res.user.rels[:self].href
+ assert_equal '/users/1/followers', res.user.rels[:followers].href
+ end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.