Class: MWS::Client

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

Overview

MWS::Client

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Client

Returns a new instance of Client.



28
29
30
31
# File 'lib/mws.rb', line 28

def initialize(args={})
  args.each { |k, v| send("#{k}=", v) }
  @items = []
end

Instance Attribute Details

#access_key_idObject

Amazon Access Key ID



34
35
36
# File 'lib/mws.rb', line 34

def access_key_id
  @access_key_id
end

#from_addressObject

From Address



46
47
48
# File 'lib/mws.rb', line 46

def from_address
  @from_address
end

#fulfillment_center_idObject

Returns the value of attribute fulfillment_center_id.



67
68
69
# File 'lib/mws.rb', line 67

def fulfillment_center_id
  @fulfillment_center_id
end

#inbound_shipmentObject

Inbound Shipment



55
56
57
# File 'lib/mws.rb', line 55

def inbound_shipment
  @inbound_shipment
end

#inbound_shipment_planObject

Inbound Shipment



55
56
57
# File 'lib/mws.rb', line 55

def inbound_shipment_plan
  @inbound_shipment_plan
end

#itemsObject

Items



52
53
54
# File 'lib/mws.rb', line 52

def items
  @items
end

#label_prep_preferenceObject

Label Prep Preference



49
50
51
# File 'lib/mws.rb', line 49

def label_prep_preference
  @label_prep_preference
end

#marketplace_idObject

Amazon Marketplace



43
44
45
# File 'lib/mws.rb', line 43

def marketplace_id
  @marketplace_id
end

#merchant_idObject

Amazon Merchant ID (also Seller ID)



40
41
42
# File 'lib/mws.rb', line 40

def merchant_id
  @merchant_id
end

#s3_access_key_idObject

Returns the value of attribute s3_access_key_id.



63
64
65
# File 'lib/mws.rb', line 63

def s3_access_key_id
  @s3_access_key_id
end

#s3_secret_access_keyObject

Returns the value of attribute s3_secret_access_key.



63
64
65
# File 'lib/mws.rb', line 63

def s3_secret_access_key
  @s3_secret_access_key
end

#secret_access_keyObject

Amazon Secret Access Key



37
38
39
# File 'lib/mws.rb', line 37

def secret_access_key
  @secret_access_key
end

#shipment_idObject

Shipment ID (assigned by amazon)



61
62
63
# File 'lib/mws.rb', line 61

def shipment_id
  @shipment_id
end

#shipment_nameObject

Shipment Name



58
59
60
# File 'lib/mws.rb', line 58

def shipment_name
  @shipment_name
end

#shipment_statusObject

Returns the value of attribute shipment_status.



65
66
67
# File 'lib/mws.rb', line 65

def shipment_status
  @shipment_status
end

Instance Method Details

#create_inbound_shipmentObject

CreateInboundShipment



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/mws.rb', line 117

def create_inbound_shipment
  puts "creating new inbound shipment..."
  
  request = Request.new({
    'Action'                                                    => 'CreateInboundShipment',
    'Marketplace'                                               => @marketplace_id,
    'SellerId'                                                  => @merchant_id,
    'AWSAccessKeyId'                                            => @access_key_id,
    'InboundShipmentHeader.ShipmentName'                        => @shipment_name,
    'InboundShipmentHeader.ShipFromAddress.Name'                => @from_address[:name],
    'InboundShipmentHeader.ShipFromAddress.AddressLine1'        => @from_address[:address_line_1],
    'InboundShipmentHeader.ShipFromAddress.AddressLine2'        => @from_address[:address_line_2],
    'InboundShipmentHeader.ShipFromAddress.City'                => @from_address[:city],
    'InboundShipmentHeader.ShipFromAddress.StateOrProvinceCode' => @from_address[:state_or_province_code],
    'InboundShipmentHeader.ShipFromAddress.PostalCode'          => @from_address[:postal_code],
    'InboundShipmentHeader.ShipFromAddress.CountryCode'         => @from_address[:country_code],
    'InboundShipmentHeader.DestinationFulfillmentCenterId'      => @inbound_shipment_plan["member"]["DestinationFulfillmentCenterId"]
  },@secret_access_key)
  
  puts "created request"

  @inbound_shipment_plan["member"]["Items"]["member"].each_with_index do |item, index|
    request << {
      "InboundShipmentItems.member.#{index+1}.QuantityShipped"  => item["Quantity"],
      "InboundShipmentItems.member.#{index+1}.SellerSKU"        => item["SellerSKU"]
    }
  end
  
  puts "added items"
  response = request.post.to_hash
  ap response
  @shipment_id = request.post["CreateInboundShipmentResponse"]["CreateInboundShipmentResult"]["ShipmentId"]
end

#create_inbound_shipment_planObject

CreateInboundShipmentPlan



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/mws.rb', line 84

def create_inbound_shipment_plan
  request = Request.new({
    'Action'                              => 'CreateInboundShipmentPlan',
    'Marketplace'                         => @marketplace_id,
    'SellerId'                            => @merchant_id,
    'AWSAccessKeyId'                      => @access_key_id,
    'ShipFromAddress.Name'                => @from_address[:name],
    'ShipFromAddress.AddressLine1'        => @from_address[:address_line_1],
    'ShipFromAddress.AddressLine2'        => @from_address[:address_line_2],
    'ShipFromAddress.City'                => @from_address[:city],
    'ShipFromAddress.StateOrProvinceCode' => @from_address[:state_or_province_code],
    'ShipFromAddress.PostalCode'          => @from_address[:postal_code],
    'ShipFromAddress.CountryCode'         => @from_address[:country_code],
    'LabelPrepPreference'                 => @label_prep_preference
  },@secret_access_key)
  
  @items.flatten!.each_with_index do |item, index|
    request << {
      "InboundShipmentPlanRequestItems.member.#{index+1}.Condition" => item[:condition],
      "InboundShipmentPlanRequestItems.member.#{index+1}.SellerSKU" => item[:seller_sku],
      "InboundShipmentPlanRequestItems.member.#{index+1}.ASIN"      => item[:asin],
      "InboundShipmentPlanRequestItems.member.#{index+1}.Quantity"  => item[:quantity]
    }
  end
  
  @inbound_shipment_plan = request.post["CreateInboundShipmentPlanResponse"]["CreateInboundShipmentPlanResult"]["InboundShipmentPlans"]
  @fulfillment_center_id = @inbound_shipment_plan["member"]["DestinationFulfillmentCenterId"]
  @inbound_shipment_plan
end

#fba_labelObject



171
172
173
174
175
176
177
178
179
180
181
# File 'lib/mws.rb', line 171

def fba_label
  FBA::ShippingLabel.new({
    :shipment_id => @shipment_id,
    :ship_from_address => format_from_address,
    :ship_to_address => format_to_address,
    :access_key_id => @s3_access_key_id,
    :secret_access_key => @s3_secret_access_key,
    :template_url => "http://bsgnpo.s3.amazonaws.com/template.fba.label.pdf",
    :bucket => 'bsgnpo'
  })
end

#fba_packing_slipObject



183
184
185
186
187
188
189
190
191
192
193
# File 'lib/mws.rb', line 183

def fba_packing_slip
  MWS::PackingSlip.new({
    :shipment_id => @shipment_id,
    :ship_from_address => format_from_address,
    :ship_to_address => format_to_address,
    :access_key_id => @s3_access_key_id,
    :secret_access_key => @s3_secret_access_key,
    :template_url => "http://bsgnpo.s3.amazonaws.com/template.fba.packing.slip.pdf",
    :bucket => 'bsgnpo'
  })
end

#format_from_addressObject



151
152
153
154
155
156
157
158
# File 'lib/mws.rb', line 151

def format_from_address
  [
    @from_address[:name],
    [@from_address[:address_line_1], @from_address[:address_line_2]].join,
    "#{@from_address[:city]}, #{@from_address[:state_or_province_code]} #{@from_address[:postal_code]}",
    TZInfo::Country.get(@from_address[:country_code]).name
  ].join("\n")
end

#format_to_addressObject



160
161
162
163
164
165
166
167
168
169
# File 'lib/mws.rb', line 160

def format_to_address
  ship_to_address = @inbound_shipment_plan["member"]["ShipToAddress"]

  [
    ship_to_address["Name"],
    [ship_to_address["AddressLine1"], (ship_to_address["AddressLine2"] || [])].join,
    "#{ship_to_address["City"]}, #{ship_to_address["StateOrProvinceCode"]} #{ship_to_address["PostalCode"]}",
    TZInfo::Country.get(ship_to_address["CountryCode"]).name
  ].join("\n")
end

#get_service_statusObject

GetServiceStatus



72
73
74
75
76
77
78
79
# File 'lib/mws.rb', line 72

def get_service_status
  Request.new({
    'Action' => 'GetServiceStatus',
    'Marketplace' => @marketplace_id,
    'SellerId' => @merchant_id,
    'AWSAccessKeyId' => @access_key_id
  },@secret_access_key).post
end

#set_shipment_status(status) ⇒ Object



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/mws.rb', line 195

def set_shipment_status(status)
  request = Request.new({
    'Action'                                                    => 'CreateInboundShipment',
    'Marketplace'                                               => @marketplace_id,
    'SellerId'                                                  => @merchant_id,
    'AWSAccessKeyId'                                            => @access_key_id,
    'InboundShipmentHeader.ShipmentName'                        => @shipment_name,
    'InboundShipmentHeader.ShipmentStatus'                      => status,
    'InboundShipmentHeader.ShipFromAddress.Name'                => @from_address[:name],
    'InboundShipmentHeader.ShipFromAddress.AddressLine1'        => @from_address[:address_line_1],
    'InboundShipmentHeader.ShipFromAddress.AddressLine2'        => @from_address[:address_line_2],
    'InboundShipmentHeader.ShipFromAddress.City'                => @from_address[:city],
    'InboundShipmentHeader.ShipFromAddress.StateOrProvinceCode' => @from_address[:state_or_province_code],
    'InboundShipmentHeader.ShipFromAddress.PostalCode'          => @from_address[:postal_code],
    'InboundShipmentHeader.ShipFromAddress.CountryCode'         => @from_address[:country_code],
    'InboundShipmentHeader.DestinationFulfillmentCenterId'      => @fulfillment_center_id
  },@secret_access_key)
  
  response = request.post.to_hash
  shipment_id = response["CreateInboundShipmentResponse"]["CreateInboundShipmentResult"]["ShipmentId"]

  if shipment_id
    @shipment_status = status
  end
  shipment_id
end