Ad

How To Map API Params To Model

- 1 answer

I'm new to Ruby and Ruby on Rails and I'm struggling hard trying to get my API to work. I'm trying to create an API that accepts POST requests from Shopify (creating orders in my application). I know the structure of the messages that are coming in (I'm using an example message in my curl command) and they do not match the columns of my model, therefore I need to map them. I find it very hard to find any documentation on how to do this.

Every time I try to create an order using the -curl command below I get this error:

    ArgumentError (wrong number of arguments (15 for 0..2)):
  app/controllers/api/v1/orders_controller.rb:16:in `create'

My curl command:

curl -i -H "Accept: application/json" -H "Content-type: application/json" -X POST -d '{
       "id":2429839748,
       "email":"[email protected]",
       "closed_at":null,
       "created_at":"2016-01-28T07:55:01-07:00",
       "updated_at":"2016-01-28T07:55:02-07:00",
       "number":97539,
       "note":null,
       "token":"b03b758395d043e9a883bd5bededed8d",
       "gateway":"paypal",
       "test":false,
       "total_price":"126.00",
       "subtotal_price":"126.00",
       "total_weight":7189,
       "total_tax":"0.00",
       "taxes_included":false,
       "currency":"USD",
       "financial_status":"paid",
       "confirmed":true,
       "total_discounts":"0.00",
       "total_line_items_price":"126.00",
       "cart_token":"bb3560e80e353582d93e4e6aaa006757",
       "buyer_accepts_marketing":true,
       "name":"#98539",
       "referring_site":"https:\/\/www.earthled.com\/collections\/dimmable-led-light-bulbs\/products\/thinklux-led-par38-17-5-watts-85-watt-equal-dimmable-outdoor-wet-rated-tkupar38?variant=3554253636",
       "landing_site":"\/cart",
       "cancelled_at":null,
       "cancel_reason":null,
       "total_price_usd":"126.00",
       "checkout_token":"898a8d893fe2f10647c8ccd8941515d1",
       "reference":null,
       "user_id":null,
       "location_id":null,
       "source_identifier":null,
       "source_url":null,
       "processed_at":"2016-01-28T07:55:01-07:00",
       "device_id":null,
       "browser_ip":"98.31.59.11",
       "landing_site_ref":null,
       "order_number":98539,
       "tags":"",
       "contact_email":"[email protected]",
       "line_items":[
          {
             "id":4412721796,
             "variant_id":3554253636,
             "title":"Thinklux LED PAR38 - 17.5 Watts - 85 Watt Equal - Dimmable - Outdoor\/Wet Rated",
             "quantity":8,
             "price":"15.75",
             "grams":900,
             "sku":"TKUPAR38-17.5W-830-25D",
             "variant_title":"3000K \/ 1 Piece",
             "vendor":"Thinklux",
             "fulfillment_service":"manual-wg",
             "product_id":905965252,
             "requires_shipping":true,
             "taxable":true,
             "gift_card":false,
             "name":"Thinklux LED PAR38 - 17.5 Watts - 85 Watt Equal - Dimmable - Outdoor\/Wet Rated - 3000K \/ 1 Piece",
             "variant_inventory_management":"shopify",
             "properties":[

             ],
             "product_exists":true,
             "fulfillable_quantity":0,
             "total_discount":"0.00",
             "fulfillment_status":"fulfilled",
             "tax_lines":[

             ],
             "origin_location":{
                "id":559988100,
                "country_code":"US",
                "province_code":"CO",
                "name":"EarthLED.com",
                "address1":"601 16th Street",
                "address2":"Suite C-279",
                "city":"Golden",
                "zip":"80401"
             },
             "destination_location":{
                "id":913894148,
                "country_code":"US",
                "province_code":"OH",
                "name":"Susan Laremore",
                "address1":"4283 County Rd 175",
                "address2":"",
                "city":"Clyde",
                "zip":"43410"
             }
          }
       ],
       "shipping_lines":[
          {
             "id":2175393924,
             "title":"FREE Ground Shipping",
             "price":"0.00",
             "code":"FREE Ground Shipping",
             "source":"shopify",
             "phone":null,
             "tax_lines":[

             ]
          }
       ],
       "shipping_address":{
          "first_name":"Susan",
          "address1":"4283 County Rd 175",
          "phone":"4195478501",
          "city":"Clyde",
          "zip":"43410",
          "province":"Ohio",
          "country":"United States",
          "last_name":"Laremore",
          "address2":"",
          "company":"Class Act Dance Studio",
          "latitude":41.283498,
          "longitude":-82.966808,
          "name":"Susan Laremore",
          "country_code":"US",
          "province_code":"OH"
       },
       "fulfillments":[
          {
             "id":2130025412,
             "order_id":2429839748,
             "status":"success",
             "created_at":"2016-01-28T07:55:02-07:00",
             "service":"manual-wg",
             "updated_at":"2016-01-28T07:55:02-07:00",
             "tracking_company":null,
             "tracking_number":null,
             "tracking_numbers":[

             ],
             "tracking_url":null,
             "tracking_urls":[

             ],
             "receipt":{

             },
             "line_items":[
                {
                   "id":4412721796,
                   "variant_id":3554253636,
                   "title":"Thinklux LED PAR38 - 17.5 Watts - 85 Watt Equal - Dimmable - Outdoor\/Wet Rated",
                   "quantity":8,
                   "price":"15.75",
                   "grams":900,
                   "sku":"TKUPAR38-17.5W-830-25D",
                   "variant_title":"3000K \/ 1 Piece",
                   "vendor":"Thinklux",
                   "fulfillment_service":"manual-wg",
                   "product_id":905965252,
                   "requires_shipping":true,
                   "taxable":true,
                   "gift_card":false,
                   "name":"Thinklux LED PAR38 - 17.5 Watts - 85 Watt Equal - Dimmable - Outdoor\/Wet Rated - 3000K \/ 1 Piece",
                   "variant_inventory_management":"shopify",
                   "properties":[

                   ],
                   "product_exists":true,
                   "fulfillable_quantity":0,
                   "total_discount":"0.00",
                   "fulfillment_status":"fulfilled",
                   "tax_lines":[

                   ],
                   "origin_location":{
                      "id":559988100,
                      "country_code":"US",
                      "province_code":"CO",
                      "name":"EarthLED.com",
                      "address1":"601 16th Street",
                      "address2":"Suite C-279",
                      "city":"Golden",
                      "zip":"80401"
                   },
                   "destination_location":{
                      "id":913894148,
                      "country_code":"US",
                      "province_code":"OH",
                      "name":"Susan Laremore",
                      "address1":"4283 County Rd 175",
                      "address2":"",
                      "city":"Clyde",
                      "zip":"43410"
                   }
                }
             ]
          }
       ],
       "customer":{
          "id":2556660676,
          "email":"[email protected]",
          "accepts_marketing":true,
          "created_at":"2016-01-28T07:51:34-07:00",
          "updated_at":"2016-01-28T07:55:01-07:00",
          "first_name":"Susan",
          "last_name":"Laremore",
          "orders_count":1,
          "state":"disabled",
          "total_spent":"126.00",
          "last_order_id":2429839748,
          "note":null,
          "verified_email":true,
          "multipass_identifier":null,
          "tax_exempt":false,
          "tags":"",
          "last_order_name":"#98539",
          "default_address":{
             "id":2697648836,
             "first_name":"Susan",
             "last_name":"Laremore",
             "company":"Class Act Dance Studio",
             "address1":"4283 County Rd 175",
             "address2":"",
             "city":"Clyde",
             "province":"Ohio",
             "country":"United States",
             "zip":"43410",
             "phone":"4195478501",
             "name":"Susan Laremore",
             "province_code":"OH",
             "country_code":"US",
             "country_name":"United States",
             "default":true
          }
       }
    }' http://localhost:3000/api/v1/orders

And my create function in api/v1/orders_controller.rb

    def create
        order = Order.new(
            id = params[:id],
            shopify_order_id = params[:id],
            shopify_created_at = params[:created_at],
            shopify_order_name = params[:name],
            ship_to_name = params[:shipping_address][:name],
            ship_to_address_1 = params[:shipping_address][:address1],
            ship_to_address_2 = params[:shipping_address][:address2],
            ship_to_company = params[:shipping_address][:company],
            ship_to_city = params[:shipping_address][:city],
            ship_to_state = params[:shipping_address][:state],
            ship_to_country = params[:shipping_address][:country],
            ship_to_zipcode = params[:shipping_address][:zip],
            ship_to_phonenumber = params[:shipping_address][:phone],
            ship_to_email = params[:shipping_address][:email],
            store_id = 1
        )
        if order.save?
            head 200
        else
            head 422
        end
    end

Can anyone tell me what I'm doing wrong?

Ad

Answer

You shouldn't use = but :

Like this:

 order = Order.new(
        shopify_order_id: params[:id],
        ... etc
    )

Note that I also omitted the id attribute, it will set this automatically when you are saving the record.

Also change

if order.save?
  head 200
else
  head 422
end

To:

if order.save
  head 200
else
  head 422
end
Ad
source: stackoverflow.com
Ad