Class: GunBroker::Order

Inherits:
Object
  • Object
show all
Defined in:
lib/gun_broker/order.rb,
lib/gun_broker/order/constants.rb

Overview

Represents a GunBroker order (listing).

Defined Under Namespace

Modules: Constants

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ Order

Returns a new instance of Order.

Parameters:

  • attrs (Hash) (defaults to: {})

    The JSON attributes from the API response.



27
28
29
# File 'lib/gun_broker/order.rb', line 27

def initialize(attrs = {})
  @attrs = attrs
end

Instance Attribute Details

#attrsHash (readonly)

TODO: Refactor this, #attributes, and #[] into a module.

Returns:

  • (Hash)

    Attributes parsed from the JSON response.



7
8
9
# File 'lib/gun_broker/order.rb', line 7

def attrs
  @attrs
end

Class Method Details

.find(order_id, params = {}, headers = {}) ⇒ Order

Returns An Order instance or nil if no Order with order_id exists.

Parameters:

  • order_id (Integer, String)

    The ID of the Order to find.

Returns:

  • (Order)

    An Order instance or nil if no Order with order_id exists.



11
12
13
14
15
# File 'lib/gun_broker/order.rb', line 11

def self.find(order_id, params = {}, headers = {})
  find!(order_id, params, headers)
rescue GunBroker::Error::NotFound
  nil
end

.find!(order_id, params = {}, headers = {}) ⇒ Order

Same as find but raises GunBroker::Error::NotFound if no Order is found.

Parameters:

  • order_id (Integer, String)

    The ID of the Order to find.

Returns:

  • (Order)

    An Order instance or nil if no Order with order_id exists.

Raises:



21
22
23
24
# File 'lib/gun_broker/order.rb', line 21

def self.find!(order_id, params = {}, headers = {})
  response = GunBroker::API.get("/Orders/#{order_id}", params, headers)
  new(response.body)
end

Instance Method Details

#[](key) ⇒ Object

Returns The value of the given key or nil.

Parameters:

  • key (String)

    An Order attribute name (from the JSON response).

Returns:

  • The value of the given key or nil.



106
107
108
# File 'lib/gun_broker/order.rb', line 106

def [](key)
  @attrs[key]
end

#attributesHash

Returns Attributes parsed from the JSON response.

Returns:

  • (Hash)

    Attributes parsed from the JSON response.



32
33
34
# File 'lib/gun_broker/order.rb', line 32

def attributes
  @attrs
end

#bill_toHash

Returns Billing info for this Order.

Returns:

  • (Hash)

    Billing info for this Order.



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/gun_broker/order.rb', line 52

def bill_to
  {
    name:         @attrs['billToName'],
    address_1:    @attrs['billToAddress1'],
    address_2:    @attrs['billToAddress2'],
    city:         @attrs['billToCity'],
    state:        @attrs['billToState'],
    zip:          @attrs['billToPostalCode'],
    email:        @attrs['billToEmail'],
    phone:        @attrs['billToPhone']
  }
end

#ffl_numberString

Returns FFL Number (if applicable) for this Order.

Returns:

  • (String)

    FFL Number (if applicable) for this Order.



42
43
44
# File 'lib/gun_broker/order.rb', line 42

def ffl_number
  @attrs['fflNumber']
end

#idInteger

Returns The Order ID.

Returns:

  • (Integer)

    The Order ID.



37
38
39
# File 'lib/gun_broker/order.rb', line 37

def id
  @attrs['orderID']
end

#item_idsArray

Returns Item IDs of associated items for this Order.

Returns:

  • (Array)

    Item IDs of associated items for this Order.



47
48
49
# File 'lib/gun_broker/order.rb', line 47

def item_ids
  (@attrs['items'] || @attrs['orderItemsCollection']).collect { |item| item['itemID'] }
end

#order_totalFloat

Returns Total sales amount for this Order.

Returns:

  • (Float)

    Total sales amount for this Order.



90
91
92
# File 'lib/gun_broker/order.rb', line 90

def order_total
  @attrs['orderTotal'] || @attrs['totalPrice']
end

#payment_methodsString

Returns Payment methods used for this Order.

Returns:

  • (String)

    Payment methods used for this Order.



95
96
97
# File 'lib/gun_broker/order.rb', line 95

def payment_methods
  @attrs['paymentMethod'].values
end

#sales_tax_totalFloat

Returns Total sales tax for this Order.

Returns:

  • (Float)

    Total sales tax for this Order.



85
86
87
# File 'lib/gun_broker/order.rb', line 85

def sales_tax_total
  @attrs['salesTaxTotal']
end

#ship_toHash

Returns Shipping info for this Order.

Returns:

  • (Hash)

    Shipping info for this Order.



66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/gun_broker/order.rb', line 66

def ship_to
  {
    name:         @attrs['shipToName'],
    address_1:    @attrs['shipToAddress1'],
    address_2:    @attrs['shipToAddress2'],
    city:         @attrs['shipToCity'],
    state:        @attrs['shipToState'],
    zip:          @attrs['shipToPostalCode'],
    email:        @attrs['shipToEmail'],
    phone:        @attrs['shipToPhone']
  }
end

#shipping_totalFloat

Returns Total shipping amount for this Order.

Returns:

  • (Float)

    Total shipping amount for this Order.



80
81
82
# File 'lib/gun_broker/order.rb', line 80

def shipping_total
  @attrs['shipCost']
end

#status_keyInteger

Returns Status key for this Order.

Returns:

  • (Integer)

    Status key for this Order.



100
101
102
# File 'lib/gun_broker/order.rb', line 100

def status_key
  @attrs['status'].keys.first.to_i
end