class SpreeMatkahuolto::API::ShippingLabel
Constants
- PRODUCTION_ENDPOINT
- TEST_ENDPOINT
Attributes
test_mode[RW]
Public Class Methods
new(login, password, test_mode = false)
click to toggle source
# File lib/spree_matkahuolto/api/shipping_label.rb, line 133 def initialize(login, password, test_mode = false) @login = login @password = password @test_mode = test_mode end
Public Instance Methods
get_labels(shipments)
click to toggle source
# File lib/spree_matkahuolto/api/shipping_label.rb, line 139 def get_labels(shipments) return nil unless shipments #shipments = [shipments] unless shipments.is_a?(Array) if @test_mode endpoint = TEST_ENDPOINT else endpoint = PRODUCTION_ENDPOINT end request = build_request(shipments) response = RestClient.post endpoint, request, :content_type => 'text/xml', :accept => :xml parser = Nori.new parsed_data = parser.parse(response) filename = parsed_data['MHShipmentReply']['PdfName'] path = Rails.root.join("tmp/shipping_labels/#{filename}") dirname = File.dirname(path) FileUtils.mkdir_p(dirname) unless File.directory?(dirname) File.open(path, 'wb') do |f| f.write(Base64.decode64(parsed_data['MHShipmentReply']['ShipmentPdf'])) end return { filename: "#{filename}", path: "#{path}" } end
Private Instance Methods
build_request(shipments)
click to toggle source
# File lib/spree_matkahuolto/api/shipping_label.rb, line 170 def build_request(shipments) request = { 'MHShipmentRequest' => { 'UserId' => @login, 'Password' => @password, 'Version' => '2.0', 'Shipment' => [] } } shipments.each do |shipment| s = { 'ShipmentType' => shipment.shipment_type, 'MessageType' => shipment.message_type, 'Weight' => shipment.weight, 'Packages' => shipment.packages, 'SenderId' => shipment.sender_id, 'SenderReference' => shipment.sender_reference, 'ReceiverName1' => shipment.receiver_name, 'ReceiverAddress' => shipment.receiver_address, 'ReceiverPostal' => shipment.receiver_postal, 'ReceiverCity' => shipment.receiver_city, 'ReceiverEmail' => shipment.receiver_email, 'ReceiverContactName' => shipment.receiver_contact_name, 'ReceiverContactNumber' => shipment.receiver_contact_number, 'DestinationPlaceCode' => shipment.destination_place_code, 'ProductCode' => shipment.product_code } request['MHShipmentRequest']['Shipment'].push s end Gyoku.xml(request, { :key_converter => :none }) end