Skip to content

Commit

Permalink
Generalize 'Link' header parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
ivailop committed Feb 18, 2021
1 parent 7d57eda commit 8a90af2
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 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 @@ -48,8 +48,12 @@ 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
links = ( @headers["Link"] || "" ).split(',').map do |link|
href, params = link.match(/<(.*?)>(.*)/).captures
name = nil
params.split(';').find do |param|
param.match(/rel="?(\w+)/) { |m| name = m[1] }
end

[name.to_sym, Relation.from_link(@agent, name, :href => href)]
end
Expand Down

0 comments on commit 8a90af2

Please sign in to comment.