Jump to content

The ultimate community for Ruby on Rails developers.


Photo

Should I use sessions or cookies when working with API data?


  • Please log in to reply
3 replies to this topic

#1 dutchM

dutchM

    Passenger

  • Members
  • 4 posts

Posted 26 January 2014 - 07:35 PM

I'm trying to build my own rails app on the side while working with different tutorials. I'm trying to create a basic store that works with an API to handle all of the order process. The way this API works is I create/instantiate an order then build up the order as I go along in the shopping cart/checkout process.

 

What I have so far is pulling the catalog of products from the API and displaying it at /store. In the .each loop in the view for the products I have a link_to tag to a create_order method that checks if there is an order_id cookie, if so do nothing currently, if not create a new order and store the order id in a cookie.

 

 def create_order
  
  if cookies[:get_order_id].nil?
  @new_order = HTTParty.post("myURL",
  :headers => { "Content-length" => "0" })
  @order_id = @new_order['OrderID']
  cookies[:get_order_id] = @order_id
  end

 end

My thoughts from here were to also store the product id into a cookie upon button press and use that to traverse through the API products array and pull in the relevant product info (Name, Description, Image, Price, etc) and display it into a partial view as a shopping cart type deal. Is this the best way to do it? I still don't quite understand if I should be using sessions or cookies.. I've read a ton on it but I'm still confused :( I thought about storing the products into a database as well but that seems redundant as I can just access the order through the API and get any product info I need.

 

Any help would be greatly appreciated!

 



#2 dutchM

dutchM

    Passenger

  • Members
  • 4 posts

Posted 27 January 2014 - 02:26 PM

So I got through that, I'm using sessions fine now and can put in new values and retrieve them. Next hurdle I'm dealing with is POST to the API using JSON. I actually got it to work but it seems like there is a better way to do it? Maybe not..

 

per the API I'm working with, to create a new line in an order it is expecting a request body like this:

 

 

Request Body:


{
    "ProductCode":"5x7",
    "Quantity":2,
    "UnitPrice":1.25,
    "Pages":
    [
        {
            "PageNumber":1,
            "Assets":
            [
                {
                    "AssetNumber":1,
                    "Name":"000-R20110419153212.jpg",
                    "HiResImage":"http:\/\/www.google.com\/xyz.jpg",
                    "CropMode":"FILLIN"
                }
            ]
        }
    ]
}

So to test it I pretty much just submitted that information back exactly and I got a Success Response Body:

 

   @new_line = HTTParty.post("myURL",
   :body => {
   :ProductCode => "Gift;65002",
   :Quantity => "2",
   :UnitPrice => "10.99",
   :Pages =>
   [
       {
           :PageNumber => "1",
           :Assets =>
           [
               {
                   :AssetNumber => "1",
                   :Name => "000-R20110419153212.jpg",
                   :HiResImage => "http:\/\/www.google.com\/xyz.jpg",
                   :CropMode => "FILLIN"
               }
           ]
       }
   ]
}.to_json,
    :headers => { 'Content-Type' => 'application/json' })

So my ultimate question is.. I have to construct these huge things like this every time I want to post something?? It seems like there would be an "easy rails way"



#3 james

james

    Guard

  • Moderators
  • 221 posts
  • LocationLeeds, U.K.

Posted 27 January 2014 - 03:28 PM

Rails has various json template handlers available.

I think this issue can be best solved by pointing you to various railscasts but basically, you write a show or index or whatever template just like would an html.erb view and place it in the same relevant view folders as html.erb templates only you call then html.json and how you fill them out depends on the json template handler gem you choose to go with,

 

My favourite is RABL

http://railscasts.co...isodes/322-rabl

 

JBuilder is also very good

http://railscasts.co...es/320-jbuilder


Programming is just about problem solving!


#4 dutchM

dutchM

    Passenger

  • Members
  • 4 posts

Posted 28 January 2014 - 08:49 PM

Ah very nice, thanks!






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users