class Sheepla::API

Constants

BASE_URL

Attributes

api_key[RW]
http[RW]
uri[RW]

Public Class Methods

new(api_key) click to toggle source
# File lib/sheepla.rb, line 14
def initialize(api_key)
  @api_key = api_key
end

Public Instance Methods

add_shipment_to_order(external_order_id) click to toggle source
# File lib/sheepla.rb, line 27
def add_shipment_to_order(external_order_id)
  connection('addShipmentToOrder')

  body = body_wrapper('addShipmentToOrderRequest') do |xml|
    xml.orders do
      xml.order do
        xml.externalOrderId external_order_id
      end
    end
  end

  request_method(body)
end
create_order(params) click to toggle source
# File lib/sheepla.rb, line 18
def create_order(params)
  order = params.deep_transform_keys{ |key| key.to_s.camelize(:lower) }

  # raise ApiError.new("Order parameters don't contain all obligatory keys") unless validate_order(order)

  connection('createOrder')
  request_method(build_order(order))
end

Protected Instance Methods

add_delivery_address(xml, delivery_address) click to toggle source
# File lib/sheepla.rb, line 102
def add_delivery_address(xml, delivery_address)
  xml.deliveryAddress do
    delivery_address.each do |delivery_key, delivery_value|
      xml.send(delivery_key, delivery_value)
    end
  end
end
add_delivery_options(xml, delivery_options) click to toggle source
# File lib/sheepla.rb, line 122
def add_delivery_options(xml, delivery_options)
  xml.deliveryOptions do
    xml.cod delivery_options.delete('cod')
    xml.insurance delivery_options.delete('insurance')
    delivery_options.each do |delivery_partner, delivery_partner_data|
      xml.send(delivery_partner) do
        delivery_partner_data.each do |k,v|
          xml.send(k, v)
        end
      end
    end
  end 
end
add_order_items(xml, order_items) click to toggle source
# File lib/sheepla.rb, line 110
def add_order_items(xml, order_items)
  xml.orderItems do
    order_items.each do |order_item|
      xml.orderItem do
        order_item.each do |k, v|
          xml.send(k, v)
        end
      end
    end
  end
end
add_other_order_data(xml, order_data) click to toggle source
# File lib/sheepla.rb, line 136
def add_other_order_data(xml, order_data)
  order_data.each do |k,v|
    xml.send(k, v)
  end        
end
authentication(xml) click to toggle source
# File lib/sheepla.rb, line 42
def authentication(xml)
  xml.authentication do
    xml.apiKey @api_key
  end
end
body_wrapper(method) { |xml| ... } click to toggle source
# File lib/sheepla.rb, line 75
def body_wrapper(method)
  Nokogiri::XML::Builder.new(encoding: 'utf-8') do |xml|
    xml.send(method, xmlns: "http://www.sheepla.pl/webapi/1_0") do
      authentication(xml)
      yield xml
    end
  end
end
build_order(order) click to toggle source
# File lib/sheepla.rb, line 84
def build_order(order)
  address = order.delete('deliveryAddress')
  items = order.delete('orderItems') 
  options = order.delete('deliveryOptions')

  body_wrapper('createOrderRequest') do |xml|
    xml.orders do
      xml.order do
        add_other_order_data(xml, order)

        add_delivery_address(xml, address)
        add_order_items(xml, items) if items
        add_delivery_options(xml, options) if options
      end
    end
  end
end
connection(method) click to toggle source
# File lib/sheepla.rb, line 48
def connection(method)
  @uri = URI.parse(BASE_URL + method)
  @http = Net::HTTP.new(uri.host, uri.port)
  @http.use_ssl = true
end
request_method(params) click to toggle source
# File lib/sheepla.rb, line 55
def request_method(params)
  body = params.to_xml
  request = Net::HTTP::Post.new(@uri.request_uri)
  request['content-type'] = 'text/xml'
  request['content-length'] = body.size
  request.body = body
  response = @http.request(request)

  raise ApiError.new("401 Unauthorized") if response.code.to_i == 401
  raise ApiError.new("400 Bad Request") if response.code.to_i == 400
  raise ApiError.new("500 Internal Sever Error") if response.code.to_i == 500
  raise ApiError.new("501 Not implemented") if response.code.to_i == 501

  unless response.code.to_i == 200
    raise ApiError.new("Response status code: #{response.code}")
  end

  Hash.from_xml(response.body)
end