class MusicTodayApiWrapper::RestClient

Attributes

catalog_number[RW]

Public Class Methods

new() click to toggle source
# File lib/rest_clients/music_today_rest_client.rb, line 10
def initialize
  config = MusicTodayApiWrapper::Configuration.new
  @url = config.url
  @user = config.user
  @api_key = config.api_key
  @catalog_number = config.catalog.to_i
  @department = config.department
  @common_response = MusicTodayApiWrapper::RestClients::CommonResponse.new
end

Public Instance Methods

all_products(per_page = 1000, page_number = nil) click to toggle source
# File lib/rest_clients/music_today_rest_client.rb, line 20
def all_products(per_page = 1000, page_number = nil)
  @common_response.work do
    options = {}
    options[:size] = per_page if per_page
    options[:page] = page_number if page_number
    department = @department ? "/#{@department}" : ''

    url = "#{@url}/catalog/content/#{@catalog_number}#{department}/"
    @common_response.data[:products] = get(url, options)['products']
  end
end
checkout(params) click to toggle source
# File lib/rest_clients/music_today_rest_client.rb, line 47
def checkout(params)
  @common_response.work do
    url = "#{@url}/cart/pricing"
    @common_response.data[:session] =
      post(url, {}, params)['addresses'].first
  end
end
find_product(product_id) click to toggle source
# File lib/rest_clients/music_today_rest_client.rb, line 32
def find_product(product_id)
  @common_response.work do
    url = "#{@url}/catalog/product/#{@catalog_number}/#{product_id}"
    @common_response.data[:product] = get(url)['product']
  end
end
purchase(params) click to toggle source
# File lib/rest_clients/music_today_rest_client.rb, line 55
def purchase(params)
  @common_response.work do
    url = "#{@url}/order/submit"
    response = post(url, {}, params)
    errors = response['errors']
    @common_response.data[:order] =
      response['orders'].first if errors.empty?
    purchase_errors(errors[0]['errorDetails']) if errors.any?
  end
end
shipping_options(params) click to toggle source
# File lib/rest_clients/music_today_rest_client.rb, line 39
def shipping_options(params)
  @common_response.work do
    url = "#{@url}/order/shippingOptionsGet"
    @common_response.data[:shipping_options] =
      post(url, {}, params)['shippingOptions']
  end
end

Private Instance Methods

get(url, options = {}) click to toggle source
# File lib/rest_clients/music_today_rest_client.rb, line 68
def get(url, options = {})
  uri = URI(url)
  hit(uri, options) do
    Net::HTTP.get_response(uri)
  end
end
hit(uri, options = {}) { || ... } click to toggle source
# File lib/rest_clients/music_today_rest_client.rb, line 88
def hit(uri, options = {})
  options.merge!(apiuser: @user, apikey: @api_key)
  uri.query = URI.encode_www_form(options)
  response = yield
  JSON.parse(response.body)
end
post(url, options = {}, body = {}) click to toggle source
# File lib/rest_clients/music_today_rest_client.rb, line 75
def post(url, options = {}, body = {})
  uri = URI(url)
  hit(uri, options) do
    request = Net::HTTP::Post.new(uri)
    request.body = body.to_json
    request.content_type = 'application/json'
    use_ssl = (uri.scheme == 'https')
    Net::HTTP.start(uri.hostname, use_ssl: use_ssl) do |http|
      http.request(request)
    end
  end
end
purchase_errors(error_details) click to toggle source
# File lib/rest_clients/music_today_rest_client.rb, line 95
def purchase_errors(error_details)
  errors = @common_response.errors
  if error_details['billingErrors'].any? ||
     error_details['destinationErrors'].any?
    errors.push(type: :address_error)
  end

  return unless error_details['paymentErrors'].any?
  errors.push(type: :credit_card_error)
end