Class: Gurunavi::Client

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
AreaCodes, Areas, Categories, Photos, Prefs, Rests
Defined in:
lib/gurunavi/client.rb

Constant Summary collapse

DEFAULT_CONNECTION_MIDDLEWARE =
[
  Faraday::Request::Multipart,
  Faraday::Request::UrlEncoded,
  FaradayMiddleware::Mashify,
  FaradayMiddleware::ParseJson
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Categories

#category_large_search, #category_small_search

Methods included from AreaCodes

#g_area_large_search, #g_area_middle_search, #g_area_small_search

Methods included from Prefs

#pref_search

Methods included from Areas

#area_search

Methods included from Photos

#map_photos_from_response, #photo_search

Methods included from Rests

#foreign_rest_search, #rest_search

Constructor Details

#initialize(options = {}) ⇒ Client

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.



32
33
34
35
36
37
38
# 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

Instance Attribute Details

#api_versionObject

Returns the value of attribute api_version.



21
22
23
# File 'lib/gurunavi/client.rb', line 21

def api_version
  @api_version
end

#keyidObject

Returns the value of attribute keyid.



21
22
23
# File 'lib/gurunavi/client.rb', line 21

def keyid
  @keyid
end

Instance Method Details

#api_urlObject

Base URL for api requests.



53
54
55
# File 'lib/gurunavi/client.rb', line 53

def api_url
  "https://api.gnavi.co.jp/"
end

#connectionObject



40
41
42
43
44
45
46
47
48
49
50
# 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) ⇒ Object

Helper method to wrap object into array.

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



84
85
86
87
88
89
90
91
# 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_versionObject

Dedault api version for api url.



63
64
65
# File 'lib/gurunavi/client.rb', line 63

def default_api_version
  "20150630"
end

#default_formatObject

Default parser format for api requests.



58
59
60
# File 'lib/gurunavi/client.rb', line 58

def default_format
  "json"
end

#return_error_or_body(response, response_body) ⇒ Object

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.



70
71
72
73
74
75
76
77
78
79
# 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