Class: ChartMogul::Client

Inherits:
Object
  • Object
show all
Includes:
ImportApi
Defined in:
lib/chart_mogul/client.rb

Overview

Public: Primary class for interacting with the ChartMogul API.

Defined Under Namespace

Classes: UnauthorizedError, ValidationError

Constant Summary collapse

API_ROOT_URL =
"https://api.chartmogul.com"

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ImportApi

#create_data_source, #delete_data_source!, #import_customer, #import_invoice, #import_invoices, #import_plan, #list_customers, #list_customers_each, #list_data_sources, #list_invoices, #list_invoices_each, #list_plans, #list_plans_each

Methods included from Assertive

included

Constructor Details

#initialize(options = {}) ⇒ Client

Public: Initialize a new ChartMogul::Client.

options - A Hash of options used to initialize the client (default: {}):

:account_token - The Account Token assigned to your account
                 (default: ENV["CHART_MOGUL_ACCOUNT_TOKEN"]).
:secret_key    - The Secret key assigned to your account
                 (default: ENV["CHART_MOGUL_SECRET_KEY"]).


21
22
23
24
# File 'lib/chart_mogul/client.rb', line 21

def initialize(options={})
  @account_token = options.fetch(:account_token, ENV["CHART_MOGUL_ACCOUNT_TOKEN"])
  @secret_key    = options.fetch(:secret_key, ENV["CHART_MOGUL_SECRET_KEY"])
end

Instance Attribute Details

#account_tokenObject (readonly)

Returns the value of attribute account_token.



11
12
13
# File 'lib/chart_mogul/client.rb', line 11

def 
  @account_token
end

#secret_keyObject (readonly)

Returns the value of attribute secret_key.



12
13
14
# File 'lib/chart_mogul/client.rb', line 12

def secret_key
  @secret_key
end

Instance Method Details

#connectionObject



26
27
28
29
30
31
# File 'lib/chart_mogul/client.rb', line 26

def connection
  @connection ||= Faraday.new(API_ROOT_URL) do |builder|
                    builder.basic_auth(, secret_key)
                    builder.adapter Faraday.default_adapter
                  end
end

#credentials?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/chart_mogul/client.rb', line 33

def credentials?
   && secret_key
end

#format_datetime(value) ⇒ Object



67
68
69
# File 'lib/chart_mogul/client.rb', line 67

def format_datetime(value)
  value.strftime("%Y-%m-%d %H:%M:%S")
end

#paged_get(path, params, data_field) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/chart_mogul/client.rb', line 42

def paged_get(path, params, data_field)
  begin
    params[:page_number] = 1 unless params[:page_number]
    response = connection.get(path, params)
    result = preprocess_response(response)
    yield result[data_field]
    params[:page_number] = result[:current_page]
  end while params[:page_number] < result[:total_pages]
end

#ping?Boolean

Returns:

  • (Boolean)


37
38
39
40
# File 'lib/chart_mogul/client.rb', line 37

def ping?
  response = connection.get("/v1/ping")
  preprocess_response(response)[:data] == 'pong!'
end

#preprocess_response(response) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/chart_mogul/client.rb', line 52

def preprocess_response(response)
  case response.status
  when 200..299
    JSON.parse(response.body, symbolize_names: true)
  when 401
    raise UnauthorizedError.new
  when 422
    result = JSON.parse(response.body, symbolize_names: true)
    raise ValidationError.new(result)
  else
    puts response.inspect
    raise StandardError.new("#{response.status} #{response.body.slice(0,50)}")
  end
end