Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle relation types with dashes and URLs #65

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/sawyer/response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def initialize(agent, res, options = {})

def data
@data ||= begin
return(body) unless (headers[:content_type] =~ /json|msgpack/)
return(body) unless (headers[:content_type] =~ /json|msgpack/)
process_data(agent.decode_body(body))
end
end
Expand All @@ -49,7 +49,7 @@ def process_data(data)
# Returns an array of Relations
def process_rels
links = ( @headers["Link"] || "" ).split(', ').map do |link|
href, name = link.match(/<(.*?)>; rel="(\w+)"/).captures
href, name = link.match(/<(.*?)>; rel="(.+?)"/).captures

[name.to_sym, Relation.from_link(@agent, name, :href => href)]
end
Expand Down
19 changes: 18 additions & 1 deletion test/response_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,17 @@ def setup
emails = %w([email protected] [email protected])
[200, {'Content-Type' => 'application/json'}, Sawyer::Agent.encode(emails)]
end

stub.get '/link_relations' do
[
200,
{
'Content-Type' => 'application/json',
'Link' => '</link_relations?page=2>; rel="next", </starred?page=19>; rel="last", </blog_post>; rel="with-dashes"; type="type="text/html", </>; rel="http://example.net/foo"'
},
{}
]
end
end
end

Expand Down Expand Up @@ -81,6 +92,12 @@ def test_handles_arrays_of_strings
res = @agent.call(:get, '/emails')
assert_equal '[email protected]', res.data.first
end

def test_link_relations
res = @agent.call(:get, '/link_relations')

assert_equal "/", res.rels[:"http://example.net/foo"].href
assert_equal "/blog_post", res.rels[:"with-dashes"].href
end
end
end