Class: Semlink::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/ext/nitlink.rb

Instance Method Summary collapse

Instance Method Details

#parse(response) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/ext/nitlink.rb', line 14

def parse(response)
  link_collection = Nitlink::LinkCollection.new

  # FIXME: url.query could be nil/""

  if (pagination = response['pagination'])
    pagination = JSON.parse(pagination)

    if (next_page = pagination['next_page'])
      next_url = response.env['url'].dup
      next_url.query = [next_url.query, "page=#{next_page}"].join('&')
      link_collection << Nitlink::Link.new(next_url, 'next', nil, nil)
    end

    if (prev_page = pagination['previous_page'])
      prev_url = response.env['url'].dup
      prev_url.query = [prev_url.query, "page=#{prev_page}"].join('&')
      link_collection << Nitlink::Link.new(prev_url, 'previous', nil, nil)
    end

  end

  link_collection
end