Restaurant Menu Items - Locu API in Rails - ruby-on-rails

I need help using Locu API. I want to get list of restaurants and their menu items, but it seems like I don't have enough permission. For example, I cannot query data with custom offset/limit, I only get first 25 or 100 items. I get an error message that looks something like this: "Your API key does not have enough privilege,.."
I wonder if I should contact and pay in order to access and query using offset/limits, or is there another way to get all of the data without setting offset/limit values?
Also, I am new to accessing API data, I am not sure how to maintain them. I need to store them in local database, but avoid API rate limits. I should probably update existing database by accessing recently updated data. What should I do in order to do this? Use delayed_job in Rails?
How do I save data to local database? If I create Model instance and does not call .save, it will be stored in my local database?
Thanks for your help.

Related

Make a copy / clone of an active object in ruby, and make changes to it, without messing with the database

I am new to ruby. I've coded in many languages, and normally get things quickly if there is a good reference and things are explained logically. I am going out of my mind. I've looked at every possible question related to this on stackoverflow, as well as on other websites. Everybody says use .clone or .dub or freeze and even something like Marshal.load(Marshal.dump(arr)) but none of those work.
I just want a copy of the original object, that I can modify at runtime, without it making any changes to the database.
The rails project loads a bunch of products into an object, which is then injected into a dropdown. Let's say it's called #products. The client wants me to remove certain entries from being displayed in the dropdown, but they must NOT be removed from the database.
In php for instance, you would just load the db object into a variable, and delete what you do not want by id for instance, and then loop through the resulting object / array and that creates the drop down. This makes NO alteration to the database.
I realized very quickly, Ruby does not work like that, and it deletes things direct from the database, even if I use .clone or .dup.
Let's say I use tempproducts = #products, and I do something like tempproducts.delete(11) , I don't want the system to go and delete #products(11) as well.
This is an elementary function of programming, why can't I figure out to do something as simple as this?
Thanks kindly to anybody that can help me out with this, or even post a link to the correct answer!
Checkout Array#select method to filter collection of records
e.g Let's say your Product model has one column shipping_category and you want to show only product with local and zonal shipping categories then it will look like
#product.select { |p| p.shipping_category == 'local' || shipping_category == 'zonal' }
Ideally you should use https://guides.rubyonrails.org/active_record_querying.html to filter data based on some condition at DB level use where method of Active Record.

Using Ruby on Rails, what is an efficient method of ordering separate users post_id's sequentially?

My domain structure is similar to this:
www.domain.com/:user_id/post/:post_id
i.e. www.domain.com/user1/post/1
I would like it to work where each user's post increments up the user's last :post_id, disregarding the actual ID of the database row, as multiple users posts will be stored there, like so:
www.domain.com/user1/post/1
www.domain.com/user1/post/2
www.domain.com/user2/post/1
www.domain.com/user2/post/2
I understand I will need a "secondary_id" column in the Post database and before committing a post to the database I will need to query the last post of the user to obtain that secondary_id to increment but I'm unsure where this logic would best reside in my app structure and what can I do to simplify/automate the process?
Also, how can I avoid race conditions on the secondary_id if this where implemented in a team environment where users could be submitting posts in between when another user has queried for the last secondary_id to increment and the second user would error out since the other user got to that secondary_id first?

Rails model to pull data from REST api and save to database

I'm making an app that needs to make an API request every 15 mins, put that data into a database, and put then load the data into highcharts objects.
How do I generate a model that will have these values that will have corresponding table columns:
id, calls_offered, calls_handled, timestamp
Additionally how do I access that data to dynamically update the charts I have created?
Bonus question: will that automatically pass over to Heroku when I deploy to it?

saving object to database

I have a ruby on rails 4 app and I'm using omniauth-facebook gem for authentication. I have
#omniauth = request.env["omniauth.auth"]
Now I'd like to save a user's education history (#omniauth[:extra][:raw_info][:education]) in the education column of Auth table in the database. #omniauth[:extra][:raw_info][:education] is an Array object which contains OmniAuth::AuthHash objects (i.e. #omniauth[:extra][:raw_info][:education][0].class returns OmniAuth::AuthHash).
Rails api says that "Active Record can serialize any object in text columns using YAML. To do so, you must specify this with a call to the class method serialize. This makes it possible to store arrays, hashes, and other non-mappable objects without doing any additional work." So I did the following and it seems to be working:
auth.education = #omniauth[:extra][:raw_info][:education]
auth.save
Is there any downside to this? Is there a better way (like using json, etc)? One downside I can think is that saving data this way makes it difficult for search and query which I probably won't be needing.
Thanks a lot.

Scaffolding user ID resetting

in the application i am currently creating in ruby on rails. I am trying to do some tests in rails console where i have to destroy data in the database and the database is connected to a server. I am importing an XML and parsing it and putting it into a database with scaffolding.
Now what i need: Basically what i am attempting to do is to destroy the data and replace it with a new one every week..but the problem i am getting, the userid is gone up to 700+ and there are only 50 records :S cause it doesnt reset...
To delete all records i am currently using "whatever.destroy_all" does the trick
Any help?
Btw i am using SQLITE
The ID column created in the table usually is set as unique and to increment by 1 for each new record, which is why each time you destroy and add new data the ID keeps getting higher.
The fact that the ID # is getting larger and larger is not an issue at all.
If you really want to start back at zero, I would think you could drop the table and recreate it, but that seems like overkill for a trivial issue.
Regarding the connection to the other scaffold, how are you connecting the two and what do they both represent?
Ideally the data population for testing should be done through fixtures (or easy tools such as factorygirl etc..)
The main advantage of having a fix data set is you can run your tests in any environment. But as per your requirement you can do something like this,
When you populate the date through the active records pass the id parameter as well
Ex: User.new(:id => 1, :name => "sameera").create
By this way you can have constant id's But make sure you increment the id accordingly.

Resources