Parsing JSON data in Rails Controller - ruby-on-rails

Obviously, this question is a bit Rails beginner question, however it is vital for me.
I'm trying to get multiple variables (more than a single id, parameters like username,versionNo), nothing specific. What I'm trying to understand is, I'm writing the backend of an mobile application, when the client sends me JSON variables with post, in where I should try to serialize it, in order to use it for operations such as database querying. I'm assuming that it will be on controller, but still just want to be sure. Also within pre-defined 7 methods I've assumed that index method would be the most suitable function to do operations like database querying and etc.Since there are multiple parameters, I didn't think show operation would be feasible. But again there are tons of Rails experts in here.

According to CRUD, POST request should be equal to create method in your controller.
Read more here: http://guides.rubyonrails.org/routing.html#crud-verbs-and-actions

Related

Restrictions on user-submitted code

I have a Ruby on Rails application, and one of its functions is to present JSON data to the user in table form. Before this step, I intend to add a way for users to tweak the JSON data by means of uploading their own Ruby code files that handle this.
This has its dangers. I definitely don't want any form of access (reading or writing) to the databases, nor do I want it to be able to call anything in another file. How can I limit the file in this way?
Essentially all I need is for the main code to call the function in the user-submitted file with the JSON as the parameter, and returning JSON back. All logic during this manipulation of the JSON must happen in and only in the user file.
I've looked around for ways to do this with no luck. I've seen this question:
Restricting access to user submitted code in Rails
The issue here is that I'd prefer an approach that doesn't require a gem. Also sandboxing seems rather complicated for the approach I want, which is a blanket restriction, and not specific things.
I intend to raise the $SAFE level to 4 before calling the user-supplied code/method. That doesn't seem to prevent calling other methods in the application though.

Best method for simple GET based Ruby on Rails app

Im trying to understand the best method for creating a basic Rails app that is only interfaced with via GET values in the url. Im attempting to understand how to go about starting to write logic for this as well as generally where i would want to start coding. Would this main interface code be considered a helper? or should i migrate the code to the model?
An example of how i'd like to use this app is something like the following.
http://www.railsapp.com/?order_number=123&print_label=true
With this example url i hope to grab the order_number get some information from it, determine if print_label is true/false and output something to the user.
Im not looking for someone to write it all for me, i'm simply needing some guidance on the best structure on writing a rails app that is only interfaced with via a URL as well as the easiest method for gather GET values.
Between the 3 comments on the first post i am going to looking into Restful routing and by that gather my params to interact with them.

using rails and an api

I´m mainly a coder but for a project I've got to do some more with RoR. I've been working with RoR for some projects but mainly on the front-end side. I understand it a basic level, but for this project I have to dive in the deep.
What I want to create is an app where a user can search for movies and add them to their account. I want to use theMovieDb api for this, but I can't find a screencast or tutorial that shows the beginning of how to connect your app with someone else's api. So I was hoping the StackOverflow people could point me in the right direction.
This is kind of a big question, but it seems you have two parts here. First is how to make a request from inside your application. The second is how to interact with an API.
I've set up somethings similar where I had to make API requests from inside a controller using Net::HTTP. The answer to this question helped a lot. From inside the controller, I processed the request response as needed, i.e. putting it into a relational database, or displaying it to the user.
How make a HTTP request using Ruby on Rails?
The second part about interacting with the database. The movie database API is described at
http://docs.themoviedb.apiary.io/reference
You need to reference this API documenation. The first thing, for instance, = is they require you to get an API key for instance. You will also need to be aware that they limit the rate at which you can make requests. Without knowing more specifics its hard to give more detail about how to construct the queries.
Hope this helps.

Correct rails place for no-db data fetching code

I'm looking for the "rails" design pattern for code that fetches data from other websites.
I have a rails controller in my app that fetches data not from the database, but from external API's or scraped from the web.
Where's the "rails" place to put this code.
For quick implementation, I just stuck it in a model, but the model doesn't interact with the database - or support standard model functionality - so that feels wrong, but my understanding of rails and ruby isn't yet solid enough to know where it should go.
The way the code works roughly is
controller calls model.fetchData args
the model uses HTTParty or similar to make the call
processes data
passes it back to the controller
Any advice?
Broadly-speaking I think there are two possible ways to do this:
Create a plain ruby class to contain the methods for making requests to the API(s) and processing responses from it(them). You can include the HTTParty module in this class with include HTTParty. The usual place to put this code is in lib/ (make sure that wherever you put it, the path is in autoload_paths).
If you're doing anything really complex, or the API itself is complex, you might want to consider creating a separate gem to handle interaction with the API(s). The term for this type of gem is an "API wrapper" -- if you look around, you'll see there are lots of them out there for popular services (Twitter, LinkedIn, Flickr, etc.)
Notice I haven't mentioned activerecord. If you're not going to be saving anything to the DB, I don't see any need to even create any activerecord models. You can get by with just controllers and views, and then (if needed) pick and choose components from activemodel (validations, internationalization, etc.) to make your ruby API wrapper class feel more like a Rails model. For example, one thing that I've done in an app I'm working on is to apply validations to query strings before actually making requests to an external API, which is a bit like running validations on database queries before querying a DB. See this article by Yehuda Katz for more on how to make plain ruby objects feel like activerecord models.
Hope that helps. I answered another question very similar to this one just yesterday, you might want to have a look at that answer as well: Using rails to consume web services/apis

Rails query string parameter format when providing an API with filters

I'm opening up a few REST API calls to others to use. Some of them include search filters.
Let's say I have a grocery endpoint When I currently make calls I might use:
/grocery_items/index.json?types[]=fruit&types[]=deli
Leaving me with params[:types] as a nice collection.
However to make things easier for the folks consuming my API I want to be able to offer something like this:
/grocery_items/index.json?types=fruit,deli
Seems trivial to just split my params into a collection but I'm wondering if there are pitfalls since this seems to be against the grain of how rails expects collections to arrive as params.
I don't see anything wrong with doing a quick params[:types].split(',') to make calling your API easier to use. It's pretty common to do tricks with the query string, and this is a really tame change.

Resources