Class: SilencerShop::Order

Inherits:
Base
  • Object
show all
Includes:
API
Defined in:
lib/silencer_shop/order.rb

Constant Summary collapse

SUBMIT_ATTRS =
{
  permitted: i(
    purchase_order  other_information notes customer_email customer_phone
    customer_name company_name address_line_1 address_line_2 ffl_information city state
    zip shipping_method sales_tax_collected line_items dealer_part_number quantity price
    universal_product_code description
  ).freeze,
  required: i(
    purchase_order ffl_information customer_email customer_phone customer_name
    address_line_1 city state zip line_items dealer_part_number quantity
  ).freeze
}
ENDPOINTS =
{
  submit:  "commerce/order".freeze,
  fetch:   "commerce/order/%s?emailAddress=%s".freeze,
  invoice: "commerce/order/pdf/%s?emailAddress=%s".freeze
}

Constants included from API

API::API_URL, API::FILE_UPLOAD_ATTRS, API::USER_AGENT

Instance Method Summary collapse

Methods included from API

#get_request, #post_file_request, #post_request

Constructor Details

#initialize(client) ⇒ Order

Returns a new instance of Order.



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

def initialize(client)
  @client = client
end

Instance Method Details

#fetch(order_id, customer_email) ⇒ Object



45
46
47
48
49
# File 'lib/silencer_shop/order.rb', line 45

def fetch(order_id, customer_email)
  endpoint = ENDPOINTS[:fetch] % [order_id, customer_email]

  get_request(endpoint, auth_header(@client.access_token))
end

#invoice(order_id, customer_email) ⇒ Object



51
52
53
54
55
# File 'lib/silencer_shop/order.rb', line 51

def invoice(order_id, customer_email)
  endpoint = ENDPOINTS[:invoice] % [order_id, customer_email]

  get_request(endpoint, auth_header(@client.access_token))
end

#submit(order_data) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/silencer_shop/order.rb', line 31

def submit(order_data)
  requires!(order_data, *SUBMIT_ATTRS[:required])

  endpoint = ENDPOINTS[:submit]
  headers = [
    *auth_header(@client.access_token),
    *content_type_header('application/json'),
  ].to_h

  order_data = standardize_body_data(order_data, SUBMIT_ATTRS[:permitted])

  post_request(endpoint, order_data, headers)
end