I would like to use the Google Books API as the model for one of the controllers in my Rails app. Basically -I would still like this to act as a scaffold (though I won't be posting to / modifying it)
But I would still like to have a Book object, and be able to do a Book.find([google_book_id]).
What is the best way to go about setting something like this up? Should I generate a scaffold?
Since the Google Books API seems to be a typical REST service, I would start by looking at ActiveResource. It's a way of implementing ActiveRecord-ish models where the data is stored in an external service instead of your database. If you set up a Book model using ActiveResource, you would indeed be able to call Book.find(google_book_id) and have it return a Book object with the properties returned from the API.
Since I've never used ActiveResource myself I can't offer you much advice on the details, but it looks like you would need a way to either use OAuth or add an API key parameter to each request.
Related
I have a WordPress site and a Rails site hosted on separate servers. I need to pull information from some custom post types on the WordPress site and display them on the Rails app. At a minimum, I would like to display all the post titles (hyper-linked) – there's no writing to WordPress needed.
I'm not well-versed in Ruby on Rails or HAML – I'm basically just a WordPress/PHP developer – so I'm at a loss as to how to begin. I'm thinking the best way is to pull data from WordPress using the WP REST API (http://v2.wp-api.org) plugin, but I don't know how to go about rendering data from an API in RoR/HAML. If you could point me in the right direction, that would be awesome!
Yep exposing an API on the WordPress side would be a good option, you're on the right lines.
In this situation you could do is start by creating a controller, route and view (hopefully some tests too) then within the controller you can request the data from the WordPress API. For this you will need a HTTP client, Net::HTTP is part of the Ruby stdlib or alternatively something like faraday which offers a little more flexibility. From here you can pass the response data into an object that you can use in your HAML views as required. You don't want to be assigning the JSON to an instance variable in your controller and accessing it as a Hash in the view, create a class that parses the response and provides some methods to allow you to access the data using method calls.
This would get you going in terms of a basic implementation but once you get the basics working it may be worth doing a little refactoring. For example what you would have now is a small wrapper around the API and this could be a logic extraction from your controller. The advantage being that the controller code becomes easier to understand and also should you wish to use the wrapper somewhere else within your application that becomes much simpler.
Edit: You could also use a REST ORM like her
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.
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
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
I'm working with some old code and using ActiveResource for a very basic Twitter integration. I'd like to touch the app code as little as possible and just bring OAuth in while still using ActiveResource.
Unfortunately I'm finding no easy way to do this. I did run into the oauth-active-resource gem, but it's not exactly documented and it appears to be designed for creating full-on API wrapper libraries. As you can imagine, I'd like to avoid creating a whole Twitter ActiveResource API wrapper for this one legacy change.
Any success stories out there? In my instance, it might be quicker to just leave ActiveResource rather than get this working. I'm happy to be proven wrong!
I had the same problem and built this simple gem to handle it:
https://github.com/albertopq/oauth-activeresource
You just need to use the oauth gem to retrieve the access token, and assign the final http object to your model. It will use that object instead the ActiveResource::Connection one.
Hope it helps.
I just did this with omniauth and it was surprisingly easy. This screencast and a subsequent one walks you through it. All I needed was an initializer, a controller with login/logout actions and some attributes on my user model to store the Twitter id and screen name.