class SncfApi::Response
A reponse represent a request response from API. Note that means only ONE PAGE AT A TIME There's helper methods like each_page
and page(index)
to help you to crawl easily in the result
Constants
- BASE_URL
- KNOWN_HTTP_ERROR_CODES
Attributes
body[R]
content[R]
http_params[R]
pagination[R]
request[R]
response[R]
start_page[R]
Public Class Methods
new(path:, request:, http_params: nil)
click to toggle source
# File lib/sncf_api/response.rb, line 14 def initialize(path:, request:, http_params: nil) @path = path @http_params = http_params @response = Http.basic_auth(:user => request.api_token, :pass => nil).get(BASE_URL + @path, params: @http_params) @body = response.body @request = request @request.send :decrease_quotas if @response.code == 200 @content = '' loop do chunk = @body.readpartial(HTTP::Connection::BUFFER_SIZE) rescue nil break if chunk.nil? @content << chunk end if @response.content_type.mime_type == 'application/json' @content = Oj.load(@content) @pagination = @content['pagination'] @start_page = @pagination ? @pagination['start_page'] : 0 end end if KNOWN_HTTP_ERROR_CODES.include?(@response.code) raise ArgumentError, "Unauthorized (#{@response.code}) #{@response.body}" end end
Public Instance Methods
each_page() { |content| ... }
click to toggle source
Execute given block from the current page until last rel=next
-
block param is the block that will be yield with the page content
-
return self
# File lib/sncf_api/response.rb, line 54 def each_page(&block) yield @content content = @content loop do next_page = content && content['links'] && content['links'].find { |e| e['type'] == 'next' } break unless next_page resp = Http.basic_auth(user: request.api_token, pass: nil).get(next_page['href']) @request.send :decrease_quotas break unless resp.code == 200 break unless resp.content_type.mime_type == 'application/json' content = '' loop do chunk = resp.body.readpartial(HTTP::Connection::BUFFER_SIZE) rescue nil break if chunk.nil? content << chunk end content = Oj.load(content) yield content end self end
links()
click to toggle source
return Hash
# File lib/sncf_api/response.rb, line 41 def links @content['links'] end
page(index=0)
click to toggle source
# File lib/sncf_api/response.rb, line 45 def page(index=0) new_http_params = @http_params.dup new_http_params['start_page'] = index SncfApi::Response.new(path: @path, request: @request, http_params: new_http_params) end