Class: MusicTodayApiWrapper::Services::CheckoutServices

Inherits:
Object
  • Object
show all
Defined in:
lib/services/checkout_services.rb

Instance Method Summary collapse

Constructor Details

#initializeCheckoutServices

Returns a new instance of CheckoutServices.



11
12
13
14
# File 'lib/services/checkout_services.rb', line 11

def initialize
  @common_response = RestClients::CommonResponse.new
  @rest_client = RestClient.new
end

Instance Method Details

#checkout(address, items) ⇒ Object

rubocop:disable AbcSize



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/services/checkout_services.rb', line 17

def checkout(address, items)
  items_hash = items.map(&:as_hash)
  address_hash =
    address.as_hash.merge(id: 1, items: items_hash)

  @common_response.work do
    params = checkout_params(address_hash)
    response = @rest_client.checkout(params)

    session = response.data[:session]
    response_data = @common_response.data

    response_data[:session] =
      Resources::Checkout::Session.from_hash(session)
    response_data[:items] = session['items'].map do |item|
      Resources::Purchase::Item.from_hash(item)
    end
  end
end

#confirm_and_purchase(order) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/services/checkout_services.rb', line 44

def confirm_and_purchase(order)
  purchase do
    address_collection =
      Resources::Address.from_destinations(order.destinations)
    response = checkout(address_collection.first, order.items)
    order.items = response.data[:items]
    @rest_client.purchase(orders: [order.as_hash])
  end
end

#only_purchase(order) ⇒ Object



37
38
39
40
41
42
# File 'lib/services/checkout_services.rb', line 37

def only_purchase(order)
  purchase do
    orders = [order.as_hash]
    @rest_client.purchase(orders: orders)
  end
end