class Persistiq::Client

Attributes

api_key[R]
logger[R]

Public Class Methods

new(api_key, logger = true) click to toggle source
# File lib/persistiq/client.rb, line 14
def initialize(api_key, logger = true)
  @api_key = api_key
  @logger  = logger
end

Public Instance Methods

delete(path, options = {}) click to toggle source
# File lib/persistiq/client.rb, line 34
def delete(path, options = {})
  connection.delete(path, options).body
end
get(path, options={}) click to toggle source
# File lib/persistiq/client.rb, line 19
def get(path, options={})
  connection.get(path, options).body
end
paginate(path) click to toggle source
# File lib/persistiq/client.rb, line 38
def paginate(path)
  results = []

  begin
    res = get(path)
    unless res.data.empty?
      results.push res.data
      page = res.next_page
      page.slice!('https://api.persistiq.com/v1/')
      path = page
    end
  end while res.has_more
  json = {has_more: false, data: results.flatten}
  Hashie::Mash.new json
end
post(path, req_body) click to toggle source
# File lib/persistiq/client.rb, line 23
def post(path, req_body)
  connection.post do |req|
    req.url(path)
    req.body = req_body
  end.body
end
put(path, options={}) click to toggle source
# File lib/persistiq/client.rb, line 30
def put(path, options={})
  connection.put(path, options).body
end

Private Instance Methods

connection() click to toggle source
# File lib/persistiq/client.rb, line 56
def connection
  Faraday.new(url: "https://api.persistiq.com/v1", headers: { 'accept' => 'application/json', 'x-api-key' => api_key }) do |conn|
    conn.request    :json
    conn.response   :logger if logger
    conn.use        FaradayMiddleware::Mashify
    conn.response   :json
    conn.adapter    Faraday.default_adapter
  end
end