class Gurunavi::Client

Constants

DEFAULT_CONNECTION_MIDDLEWARE

Attributes

api_version[RW]
keyid[RW]

Public Class Methods

new(options={}) click to toggle source

Note: A data of :api_version and :format has been already determined. As long as there is no special reason, you don't have to change its.

# File lib/gurunavi/client.rb, line 32
def initialize(options={})
  @keyid = options[:keyid]
  @connection_middleware = options[:connection_middleware] || DEFAULT_CONNECTION_MIDDLEWARE

  @format = default_format
  @api_version = default_api_version
end

Public Instance Methods

api_url() click to toggle source

Base URL for api requests.

# File lib/gurunavi/client.rb, line 53
def api_url
  "https://api.gnavi.co.jp/"
end
connection() click to toggle source
# File lib/gurunavi/client.rb, line 40
def connection
  params = {}
  params[:keyid] = @keyid if @keyid
  params[:format] = default_format
  @connection ||= Faraday::Connection.new(:url => api_url, :params => params, :headers => default_headers) do |builder|
    @connection_middleware.each do |middleware|
      builder.use *middleware
    end
    builder.adapter Faraday.default_adapter
  end
end
convert_to_array_if_needed(response_body) click to toggle source

Helper method to wrap object into array.

this use case is return a object for Gurunavi API when hit per page is 1.

# File lib/gurunavi/client.rb, line 84
def convert_to_array_if_needed(response_body)
  unless response_body.instance_of?(Hashie::Array)
    array = Hashie::Array.new
    array.push(response_body)
    return array
  end
  response_body
end
default_api_version() click to toggle source

Dedault api version for api url.

# File lib/gurunavi/client.rb, line 63
def default_api_version
  "20150630"
end
default_format() click to toggle source

Default parser format for api requests.

# File lib/gurunavi/client.rb, line 58
def default_format
  "json"
end
return_error_or_body(response, response_body) click to toggle source

Helper method to return errors or desired response data as appropriate.

Added just for convenience to avoid having to traverse farther down the response just to get to returned data.

# File lib/gurunavi/client.rb, line 70
def return_error_or_body(response, response_body)
  error_status = nil
  error_status = response.body["error"] if response.body["error"]
  error_status = response.body["gnavi"]["error"] if response.body["gnavi"] && response.body["gnavi"]["error"]

  if error_status
    raise Gurunavi::APIErrorFactory.call_api_errors(error_status.code, error_status.message)
  end
  response_body
end

Private Instance Methods

default_headers() click to toggle source
# File lib/gurunavi/client.rb, line 95
def default_headers
  headers = {
    :accept =>  'application/json',
    :user_agent => 'Ruby gem'
  }
end