Module: Polr::Api

Defined in:
lib/polr/api.rb

Class Method Summary collapse

Class Method Details

.api_keyObject



16
17
18
# File 'lib/polr/api.rb', line 16

def self.api_key
  Polr.configuration.api_key
end

.api_url(path = nil) ⇒ Object



12
13
14
# File 'lib/polr/api.rb', line 12

def self.api_url(path = nil)
  %(#{Polr.configuration.api_url}/api/#{API_VERSION}/action/#{path})
end

.processObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/polr/api.rb', line 20

def self.process
  JSON.parse(yield).with_indifferent_access
rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH => e # API unreachable
  raise Polr::Error, 'Polr API is unreachable'
rescue RestClient::Exception => e # HTTP status error
  result = (begin
              JSON.parse(e.response)
            rescue StandardError
              {}
            end)
  raise Polr::Error, result['error'] || 'undefined error'
rescue JSON::ParserError => e # JSON error
  raise Polr::Error, e.message
end

.request(path, params) ⇒ Object



35
36
37
# File 'lib/polr/api.rb', line 35

def self.request(path, params)
  RestClient.get(Api.api_url(path.to_s), params: params.merge(key: Api.api_key, response_type: :json), content_type: :json, accept: :json, timeout: 10)
end