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

#cancel_subscription(customer_code) ⇒ Object

Returns the customer:

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


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

def cancel_subscription(customer_code)
  response = post("/customers/cancel/productCode/#{@product_code}/code/#{customer_code}")
  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

#delete_all_customersObject



105
106
107
# File 'lib/cheddargetter.rb', line 105

def delete_all_customers
  post("/customers/delete-all/confirm/1/productCode/#{@product_code}")
end

#delete_customer(customer_code) ⇒ Object



109
110
111
# File 'lib/cheddargetter.rb', line 109

def delete_customer(customer_code)
  post("/customers/delete/productCode/#{@product_code}/code/#{customer_code}")
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

#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

#update_subscription(customer_code, attributes) ⇒ Object

Pass an attributes hash for the updated subscription:

:planCode     => "INDY",          # optional
:ccFirstName  => "Justin",        # required unless plan is free or already has credit card
:ccLastName   => "Blake",         # required unless plan is free or already has credit card
:ccNumber     => "numbers only",  # required unless plan is free or already has credit card
:ccExpiration => "MM-YYYY",       # required unless plan is free or already has credit card
:ccZip        => "5 digits only"  # required unless plan is free or already has credit card

Returns the customer:

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


100
101
102
103
# File 'lib/cheddargetter.rb', line 100

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