class Primotexto::Client

Constants

API_HOST
API_METHODS_MAP
ERROR_CODES

Attributes

key[R]

Public Class Methods

id_to_param(id) click to toggle source
# File lib/primotexto/client.rb, line 18
def id_to_param(id)
  parts = id.split('-')
  name = ActiveSupport::Inflector.singularize(parts.first)
  parts[0] = name
  parts.join('_').to_sym
end
ids_to_params(ids) click to toggle source
# File lib/primotexto/client.rb, line 14
def ids_to_params(ids)
  ids.map { |id| id_to_param(id) }
end
new(options = {}) click to toggle source
# File lib/primotexto/client.rb, line 9
def initialize(options = {})
  @key = options.fetch(:key) { ENV.fetch('PRIMOTEXTO_API_KEY') }
end

Private Class Methods

no_params_behaviour(verb, uri) click to toggle source
# File lib/primotexto/client.rb, line 27
def no_params_behaviour(verb, uri)
  -> { send(verb, uri) }
end
params_behaviour(verb, uri) click to toggle source
# File lib/primotexto/client.rb, line 31
def params_behaviour(verb, uri)
  -> (params) { send(verb, uri, params) }
end
params_with_ids_behaviour(verb, method, ids) click to toggle source
# File lib/primotexto/client.rb, line 35
def params_with_ids_behaviour(verb, method, ids)
  lambda do |params|
    # Validating uri params presence
    Client.ids_to_params(ids).each do |pid|
      fail Error, "The '#{pid}' params is mandatory for this method" unless params.include?(pid)
    end

    method_with_ids = method.dup

    # Replacing values
    ids.each do |pid|
      param_name = pid.split('-').first
      param_value = params[Client.id_to_param(pid)]
      method_with_ids.map! do |v|
        (v == pid) ? "#{param_name}/#{param_value}" : v
      end
    end

    uri = '/'
    uri += method_with_ids.join('/')
    send(verb, uri, params)
  end
end

Private Instance Methods

delete(path, params) click to toggle source
# File lib/primotexto/client/http.rb, line 28
def delete(path, params)
  uri = full_url_with(path)
  uri.query = query_string(params)
  delete_request = Net::HTTP::Delete.new(uri.request_uri)
  delete_request['X-Primotexto-ApiKey'] = @key
  delete_request['Content-Type'] = 'application/json'
  parse query(uri.host, delete_request)
end
escape(component) click to toggle source
# File lib/primotexto/client/http.rb, line 67
def escape(component)
  CGI.escape(component.to_s)
end
full_url_with(path) click to toggle source
# File lib/primotexto/client/http.rb, line 43
def full_url_with(path)
  URI.join("https://#{API_HOST}", "/v2#{path}")
end
get(path, params = {}) click to toggle source
# File lib/primotexto/client/http.rb, line 10
def get(path, params = {})
  uri = full_url_with(path)
  uri.query = query_string(params)
  get_request = Net::HTTP::Get.new(uri.request_uri)
  get_request['X-Primotexto-ApiKey'] = @key
  get_request['Content-Type'] = 'application/json'
  parse query(uri.host, get_request)
end
parse(http_response) click to toggle source
# File lib/primotexto/client/http.rb, line 47
def parse(http_response)
  case http_response
  when Net::HTTPSuccess
    if http_response['Content-Type'].try(:split, ';').try(:first) == 'application/json'
      JSON.parse!(http_response.body, symbolize_names: true)
    else
      http_response.body
    end
  when Net::HTTPUnauthorized
    fail AuthenticationError
  else
    error_code = JSON.parse(http_response.body)['code']
    fail Error, "#{ERROR_CODES[error_code]} (code: #{error_code})"
  end
end
post(path, params) click to toggle source
# File lib/primotexto/client/http.rb, line 19
def post(path, params)
  uri = full_url_with(path)
  post_request = Net::HTTP::Post.new(uri.request_uri)
  post_request['X-Primotexto-ApiKey'] = @key
  post_request['Content-Type'] = 'application/json'
  post_request.body = params.to_json
  parse query(uri.host, post_request)
end
query(host, http_request) click to toggle source
# File lib/primotexto/client/http.rb, line 37
def query(host, http_request)
  http = Net::HTTP.new(host, Net::HTTP.https_default_port)
  http.use_ssl = true
  http.request(http_request)
end
query_string(params) click to toggle source
# File lib/primotexto/client/http.rb, line 63
def query_string(params)
  params.flat_map { |k, vs| Array(vs).map { |v| "#{escape(k)}=#{escape(v)}" } }.join('&')
end