Class: CheddarGetter

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/cheddargetter.rb

Defined Under Namespace

Classes: Error

Instance Method Summary collapse

Constructor Details

#initialize(username, password, product_code) ⇒ CheddarGetter

Returns a new instance of CheddarGetter.



9
10
11
12
# File 'lib/cheddargetter.rb', line 9

def initialize(username, password, product_code)
  @product_code = product_code
  self.class.basic_auth(username, password)
end

Instance Method Details

#add_charge(customer_code, item_code, attributes) ⇒ Object

Allows for a custom tracked item to be added



102
103
104
105
# File 'lib/cheddargetter.rb', line 102

def add_charge(customer_code, item_code, attributes)
  response = post("/customers/add-charge/productCode/#{@product_code}/code/#{customer_code}/itemCode/#{item_code}", :body => attributes)
  normalize(response, 'customers', 'customer')
end

#add_item_quantity(customer_code, item_code, attributes = nil) ⇒ Object

Adding support for tracked items Quantity is only required when the increment quantity is > 1



83
84
85
86
# File 'lib/cheddargetter.rb', line 83

def add_item_quantity(customer_code, item_code, attributes=nil)
  response = post("/customers/add-item-quantity/productCode/#{@product_code}/code/#{customer_code}/itemCode/#{item_code}", :body => attributes)
  normalize(response, 'customers', 'customer')
end

#create_customer(attributes) ⇒ Object

Pass an attributes hash for the new customer:

:code       => 'CUSTOMER-1',            # required
:firstName  => 'Justin',                # required
:lastName   => 'Blake',                 # required
:email      => '[email protected]', # required
:company    => 'ADS',                   # optional
:subscription => {
  :planCode     => "INDY",          # required
  :ccFirstName  => "Justin",        # required unless plan is free
  :ccLastName   => "Blake",         # required unless plan is free
  :ccNumber     => "numbers only",  # required unless plan is free
  :ccExpiration => "MM-YYYY",       # required unless plan is free
  :ccZip        => "5 digits only"  # required unless plan is free
}

Returns the customer:

{"firstName" => "Justin", "lastName" => "Blake", etc...}


66
67
68
69
# File 'lib/cheddargetter.rb', line 66

def create_customer(attributes)
  response = post("/customers/new/productCode/#{@product_code}", :body => attributes)
  normalize(response, 'customers', 'customer')
end

#customer(customer_code) ⇒ Object



42
43
44
45
# File 'lib/cheddargetter.rb', line 42

def customer(customer_code)
  response = get("/customers/get/productCode/#{@product_code}/code/#{customer_code}")
  normalize(response, 'customers', 'customer')
end

#customersObject



31
32
33
34
35
36
37
38
39
40
# File 'lib/cheddargetter.rb', line 31

def customers
  response = get("/customers/get/productCode/#{@product_code}")
  normalize_collection(response, 'customers', 'customer')
rescue Error => e # HACK! the api is inconsitent about returning empty nodes vs. sending errors
  if e.message =~ /no customers found/i
    return []
  else
    raise
  end
end

#plan(plan_code) ⇒ Object

Returns the requested plan as a hash:

{"name" => "Little", "code" => "LITTLE", "recurringChargeAmount" => "1.00", etc...}


26
27
28
29
# File 'lib/cheddargetter.rb', line 26

def plan(plan_code)
  response = get("/plans/get/productCode/#{@product_code}/code/#{plan_code}")
  normalize(response, 'plans', 'plan')
end

#plansObject

Returns an array of all plans:

[{"name" => "Little", "code" => "LITTLE", "recurringChargeAmount" => "1.00", etc...},
 {"name" => "Big",    "code" => "BIG",    "recurringChargeAmount" => "100.00", etc..}]


18
19
20
21
# File 'lib/cheddargetter.rb', line 18

def plans
  response = get("/plans/get/productCode/#{@product_code}")
  normalize_collection(response, 'plans', 'plan')
end

#remove_item_quantity(customer_code, item_code, attributes = nil) ⇒ Object



88
89
90
91
# File 'lib/cheddargetter.rb', line 88

def remove_item_quantity(customer_code, item_code, attributes=nil)
  response = post("/customers/remove-item-quantity/productCode/#{@product_code}/code/#{customer_code}/itemCode/#{item_code}", :body => attributes)
  normalize(response, 'customers', 'customer')
end

#set_item_quantity(customer_code, item_code, attributes) ⇒ Object

Allows tracked item quantity to be set to any number (even 0!)



95
96
97
98
# File 'lib/cheddargetter.rb', line 95

def set_item_quantity(customer_code, item_code, attributes)
  response = post("/customers/set-item-quantity/productCode/#{@product_code}/code/#{customer_code}/itemCode/#{item_code}", :body => attributes)
  normalize(response, 'customers', 'customer')
end

#update_customer(customer_code, attributes) ⇒ Object

Attributes are the same as #create_customer Only included attributes will be udpated. Credit Card information is only required if the plan is not free and no credit card information is already saved.



75
76
77
78
# File 'lib/cheddargetter.rb', line 75

def update_customer(customer_code, attributes)
  response = post("/customers/edit/productCode/#{@product_code}/code/#{customer_code}", :body => attributes)
  normalize(response, 'customers', 'customer')
end