Module: Justimmo::API::Query

Included in:
RealtyQuery
Defined in:
lib/justimmo/api/v1/query.rb

Overview

Generic query interface for the API.

Defined Under Namespace

Classes: AuthenticationFailed, RetrievalFailed

Instance Method Summary collapse

Instance Method Details

#logObject


80
81
82
# File 'lib/justimmo/api/v1/query.rb', line 80

def log
  Justimmo::Logger
end

#mapperModule

The mapper that this query is associated with.


33
34
35
36
# File 'lib/justimmo/api/v1/query.rb', line 33

def mapper
  map = module_name.underscore.split('_').first.capitalize
  "Justimmo::API::#{map}Mapper".constantize
end

#module_nameString

The module name without the leading path


27
28
29
# File 'lib/justimmo/api/v1/query.rb', line 27

def module_name
  name.split('::').last
end

#parse_value(value) ⇒ Object

Convert an internal value to an API value.


72
73
74
75
76
77
78
# File 'lib/justimmo/api/v1/query.rb', line 72

def parse_value(value)
  case value
  when true then 1
  when false then 0
  else value
  end
end

#raw_request(url, params = {}) ⇒ String

Executes the actual GET request.

Raises:


54
55
56
57
58
59
60
# File 'lib/justimmo/api/v1/query.rb', line 54

def raw_request(url, params = {})
  with_error_handler do
    response = RestClient.get(url, params)
    log.debug(response.request.url)
    response.body
  end
end

#request(path, params = {}) ⇒ Object

Sends a request to the API.

See Also:


40
41
42
43
44
45
46
47
# File 'lib/justimmo/api/v1/query.rb', line 40

def request(path, params = {})
  options = {
    params: params,
    Authorization: "Basic #{Justimmo::Config.credentials}"
  }

  raw_request(url(path), options)
end

#url(path) ⇒ String

Build a request url.


65
66
67
# File 'lib/justimmo/api/v1/query.rb', line 65

def url(path)
  "#{Justimmo::Config.url}/#{path}"
end

#with_error_handlerObject


84
85
86
87
88
89
90
91
92
93
# File 'lib/justimmo/api/v1/query.rb', line 84

def with_error_handler
  yield
rescue RestClient::Unauthorized
  raise AuthenticationFailed
rescue RestClient::BadRequest, RestClient::NotFound => err
  log.error(err.response.request.url)
  raise RetrievalFailed, err.response.body
rescue RestClient::InternalServerError
  raise RetrievalFailed, 'Internal server error'
end