Class: Unleashed::CustomerResource

Inherits:
BaseResource show all
Defined in:
lib/unleashed/resources/customer_resource.rb

Overview

Resource for the Customers API The Customers resource allows Customers to be listed, viewed, created, and updated. An individual Customer’s details can be viewed, or updated by appending its identifier (a GUID formatted as XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX) to the URI

Instance Method Summary collapse

Methods inherited from BaseResource

#initialize, #method_missing, #respond_to?

Constructor Details

This class inherits a constructor from Unleashed::BaseResource

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Unleashed::BaseResource

Instance Method Details

#all(options = { Page: 1, PageSize: 200 }) ⇒ Array<Unleashed::Customer>

List all customers /Customers - also returns the first 200 customers because page number 1 is the default.

Parameters:

  • options (Hash) (defaults to: { Page: 1, PageSize: 200 })

    Optional options.

  • PageSize: (Hash)

    a customizable set of options

  • Page: (Hash)

    a customizable set of options

  • CustomerType: (Hash)

    a customizable set of options

  • SellPriceTier: (Hash)

    a customizable set of options

  • Currency: (Hash)

    a customizable set of options

  • Customer: (Hash)

    a customizable set of options

  • CustomerCode: (Hash)

    a customizable set of options

  • CustomerName: (Hash)

    a customizable set of options

  • ContactEmail: (Hash)

    a customizable set of options

  • ModifiedSince: (Hash)

    a customizable set of options

  • IncludeObsolete: (Hash)

    a customizable set of options

  • XeroContactId: (Hash)

    a customizable set of options

  • OrderBy: (Hash)

    a customizable set of options

  • Sort: (Hash)

    a customizable set of options

Returns:



33
34
35
36
37
38
39
40
41
42
# File 'lib/unleashed/resources/customer_resource.rb', line 33

def all(options = { Page: 1, PageSize: 200 })
  endpoint = 'Customers'
  params = options.dup

  # Handle Page option
  endpoint << "/#{params[:Page]}" if params[:Page].present?
  response = JSON.parse(@client.get(endpoint, params).body)
  customers = response.key?('Items') ? response['Items'] : []
  customers.map { |attributes| Unleashed::Customer.new(@client, attributes) }
end

#create_or_update(attributes) ⇒ Unleashed::Customer

Create a new customer

attributes = { Guid: “59f21e05-07fe-4d9d-b460-a09db4c3caa9”, Addresses: [ { AddressType: “Shipping”,

AddressName: "Example Address", StreetAddress: "Example Address",
StreetAddress2: "Example Address 2", Suburb: "Example Suburb", City: "Auckland",
Region: "Auckland", Country: "New Zealand", PostalCode: "1061", IsDefault: false,
DeliveryInstruction: "Have a good day" } ], TaxCode: "", TaxRate: nil,
CustomerCode: "UnleashedCustomer", CustomerName: "Unleashed Customer", GSTVATNumber: nil,
BankName: nil, BankBranch: nil, BankAccount: nil, Website: nil, PhoneNumber: nil, FaxNumber: nil,
MobileNumber: nil, DDINumber: nil, TollFreeNumber: nil, Email: nil, EmailCC: nil,
Currency: { CurrencyCode: "NZD", Description: "New Zealand, Dollars",
  Guid: "6cb5d67a-1c96-4fa8-bf59-b23c2d69f22a" }, Notes: nil, Taxable: true, SalesPerson: nil,
  DiscountRate: 0, PrintPackingSlipInsteadOfInvoice: false, PrintInvoice: false,
  StopCredit: false, Obsolete: false, XeroSalesAccount: nil, XeroCostOfGoodsAccount: nil,
  SellPriceTier: "", SellPriceTierReference: nil, CustomerType: "Cash",
  PaymentTerm: "20th Month following", ContactFirstName: nil, ContactLastName: nil }

Parameters:

  • attributes (Hash)

    Customer’s attributes.

Returns:



89
90
91
92
93
94
95
# File 'lib/unleashed/resources/customer_resource.rb', line 89

def create_or_update(attributes)
  id = attributes[:Guid].present? ? attributes[:Guid] : ''
  endpoint = 'Customers'
  endpoint << "/#{id}" if id.present?
  response = JSON.parse(@client.post(endpoint, attributes).body)
  Unleashed::Customer.new(@client, response)
end

#find(guid) ⇒ Unleashed::Customer

Get a single customer /Customers/E6E8163F-6911-40e9-B740-90E5A0A3A996 - returns details of a particular customer.

Parameters:

  • guid (String)

    customer ID.

Returns:



50
51
52
53
54
# File 'lib/unleashed/resources/customer_resource.rb', line 50

def find(guid)
  endpoint = "Customers/#{guid}"
  response = JSON.parse(@client.get(endpoint).body)
  Unleashed::Customer.new(@client, response)
end

#firstUnleashed::Customer

Get a first customer in all

Returns:



59
60
61
# File 'lib/unleashed/resources/customer_resource.rb', line 59

def first
  all.first
end

#lastUnleashed::Customer

Get a last customer in all

Returns:



66
67
68
# File 'lib/unleashed/resources/customer_resource.rb', line 66

def last
  all.last
end

#modelObject



9
10
11
# File 'lib/unleashed/resources/customer_resource.rb', line 9

def model
  Unleashed::Customer
end