class Closeio::Client

Attributes

api_key[R]
ca_file[R]
errors[R]
logger[R]
utc_offset[R]

Public Class Methods

new(api_key, logger = true, ca_file = nil, errors = false, utc_offset: 0) click to toggle source
# File lib/closeio/client.rb, line 36
def initialize(api_key, logger = true, ca_file = nil, errors = false, utc_offset: 0)
  @api_key = api_key
  @logger  = logger
  @ca_file = ca_file
  @errors  = errors
  @utc_offset = utc_offset
end

Public Instance Methods

delete(path, options = {}) click to toggle source
# File lib/closeio/client.rb, line 59
def delete(path, options = {})
  connection.delete(path, options).body
end
get(path, options = {}) click to toggle source
# File lib/closeio/client.rb, line 44
def get(path, options = {})
  connection.get(path, options).body
end
paginate(path, options = {}) click to toggle source
# File lib/closeio/client.rb, line 63
def paginate(path, options = {})
  results = []
  skip    = 0

  begin
    res = get(path, options.merge!(_skip: skip))
    unless res['data'].nil? || res['data'].empty?
      results.push res['data']
      skip += res['data'].count
    end
  end while res['has_more']
  { has_more: false, total_results: res['total_results'], data: results.flatten }
end
post(path, req_body) click to toggle source
# File lib/closeio/client.rb, line 48
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/closeio/client.rb, line 55
def put(path, options = {})
  connection.put(path, options).body
end

Private Instance Methods

assemble_list_query(query, options) click to toggle source
# File lib/closeio/client.rb, line 79
def assemble_list_query(query, options)
  options[:query] = if query.respond_to? :map
                      query.map { |k, v| "#{k}:\"#{v}\"" }.join(' ')
                    else
                      query
                    end

  options
end
connection() click to toggle source
# File lib/closeio/client.rb, line 89
def connection
  Faraday.new(
    url: 'https://api.close.com/api/v1',
    headers: {
      accept: 'application/json',
      'User-Agent' => "closeio-ruby-gem/v#{Closeio::VERSION}",
      'X-TZ-Offset' => utc_offset.to_s
    },
    ssl: { ca_file: ca_file }
  ) do |conn|
    conn.request  :authorization, :basic, api_key, ''
    conn.request  :json
    conn.response :logger if logger
    conn.response :json
    conn.use      FaradayMiddleware::CloseioErrorHandler if errors
    conn.adapter  Faraday.default_adapter
  end
end