Getting many models in one Rails API endpoint - ruby-on-rails

I'm sifting through a few Rails 5 API tutorials and all the endpoints are as you would expect: /todos for getting all todos, /todos/:id/items for getting a todo item, etc.
I'm building an API where a Site has Tutorials and TutorialItems. I am first wondering if with a Rails API it is possible to create a custom endpoint like /init_tutorials that would get a site's tutorials and tutorial items OR does it instead make sense to have the endpoint /site/:id that would pull Site's tutorials and tutorial items all in one go like so in app/controllers/sites_controller.rb:
# GET /sites/:id
def show
# get #Site.tutorial.tutorial_items
end

Either way is obviously possible. One option is to do have the /site/:id endpoint accept a parameter, something like /sites/1?tutorials=true. This works well for simple APIs but can quickly get out of had for larger APIs with more diverse clients and many potential options.
If you are likely to be doing something more complex you might want to start out using an API style more like Facebook's FQL or Netflix's Falcor API style. There are a few Gems which can help you with either one.

Related

get recommended posts from own profile through api

Using the Medium API, is it possible to get all of the posts I have recommended (or clapped)?
Within my Medium profile, I can see the posts I have clapped (see image below). I wish to retrieve these through the API.
I'm not sure this is possible with the API, but you can do this using the RSS feeds. Here's the recommendations from my own Medium page:
https://medium.com/feed/#jamesjefferyuk/has-recommended
Replace the username with your own.
Based off Medium's API docs, there does not seem to be a forward facing endpoint to get your recommended posts. You could try to scrape your recommendations page, though I don't know if Medium's Terms of Service allow scraping, so if you're going to go that route, read through their terms to make sure you won't be putting your account in danger of getting banned, shutdown, etc.

Working with APIs in RoR [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.
Closed 8 years ago.
Improve this question
This seems naive to ask, but seriously I have not seen a resource which explains how to work with an API. The basics. More specifically the procedure to start-up with an API.
There are loads of things in the docs. But how to get it working with an RoR app. Like what variables to put in the db, in the attr-accessible. Or is it even required.
Then parsing it in models. Managing from the controllers and displaying/calling in the view. Dont get me wrong, I do know about RoR, I have made apps, but with API, seems confusing. Need to know all the basics.
Is there any resource which teaches to work with an API.
For my scenario, I want to work with a travel API. What should be the first procedure(after getting the API key).
How you work with an API is largely defined by how that API exposes itself, and what libraries of code you have to deal with it.
Many APIs are presented as HTTP endpoints that you can use CURL, OpenURI, HTTParty or something like that to consume the resource, or deal with the data sent to you, often as JSON or XML.
Here is a list of some of the more popular Ruby HTTP API client libraries at the Ruby-Toolbox.
Pick one of these and write some code that gets an endpoint of that API, then use that data to do whatever you need. Treat the API somewhat like a database.
Codeschool has a cool course that you can start with and do the first few levels for free here:
Surviving APIs with Rails
First, before choosing to work with an API or not, I highly suggest to try to work with REST API since these kind usually adhere to the same structure (there is even a book on REST API) and then you know what to expect.
Lets take a fictional Movie DB REST API example
so REST API is all about resources. Every movie can be created read, updating or deleted (CRUD).
so you know that you have these commands in your disposal - if you want to get a movie in your app you would use the read command that the api supplies. It usually looks like GET /movie/13 (HTTP GET request)
so in your app you would have to insert that line whereever you want it (perhaps in some panel where the user search for a movie and you access the movie DB to get the details of it
so you would have a
def movie_panel
#some stuff
#movie = MovieDB::get(movie_name)
end
so now you have the movie.. now what? parsing it is indeed annoying. luckly most API have gems you can use so they did the parsing for you. Here I used the fake gem MovieDB but if there is no gem you need to query the api yourself, which in this case I advise you to build your own gem, or atleast have a module with some classes and method to make life easier for you. something like
module MovieDB
class Movie
attr_accesor :name, :year, etc...
def initialize(movie_params)
#initizalize the movie attributes from a given movie params
#name = movie_params["name"]
etc..
end
end
end
so in this case you would have your own API wrapper to use. So once you are given the movie params from the external API you can do MovieDB::Movie.new(movie_params) and that will create a movie object that has method (that you need to write) for the functionality that you need in your code.
Inside that module you can handle everything about that API and abstract all that from the core of your app.
so to sum up -
REST API - easy, predictable, managable
non/rest api - make sure there is proper documentation
use what the api gives you as a resource, and ask yourself questions - what do I want from the API? what will I do with the resources (such as a movie, or a facebook page, or a tweet)? Does the API docs say its capable of doing what you want to do with the resource?
as documentation goes, you need to read it to understand what to do. No all docs are clear and not everything is clear on the first reading because you don't know where to start. So start at the beginning - why do you want to API? What does the API promise to do? You need to authenticate? Is there a tutorial? Has someone already made a gem for the API?
take some famous API's and not-so famous API as an example.
Facebook Graph API is very well documented. You know exactly what you get from each command and what to expect. You don't need to write it yourself, since Koala gem have you covered. On the other hand of the API rainbow, lies a Pinterest API, which you might wonder what's it for.. No one knows, because its barely useable, you need to get access to it, and even then its very limited. So if you want to plung in to the Pinterest API, make sure you understand what it gives you and what it doesn't (hint - it doesn't). That's true for every API where it has good docs or bad docs.
Good luck and might I suggest to read http://shop.oreilly.com/product/0636920028468.do and maybe this as well - https://github.com/interagent/http-api-design
Update with the actual API
ok so the api http://www.wego.com/api/flights/docs you wanted is documented quite well
the first thing it says is to get the API rate check
GET http://www.wego.com/api/flights/usage.html?format=json&apiKey=testAPIKey
so right here you can begin to structure your code. You need to save the apiKey somewhere and have a url prefilled with http://www.wego.com/api/flights/usage.html and the parameters are the format and the apiKey
you can create a helper method that does that.
def check_rate(format, apiKey)
...
end
inside you will need to send a request and get the response and parse it something like
uri = URI.parse(#url)
https = Net::HTTP.new(uri.host,uri.port)
req = Net::HTTP::Get.new(uri.path)
req.body = ["format=" + #format, "apiKey=" + #apikey]
res = https.request(req)
and that takes you forward one step. Take the result and parse it. Best in this case is to create a class for this kind of response and add methods such as rate_ok? and such. Basically a Class for each response and instantiate a class per response. The constructor of the class would parse the response and expose whatever attributes you need

Displaying tweets from multiple users (similar to Embedded Timelines) without twitter-side user lists

I am new to Twitter and need some tips.
I need to display tweet feed from multiple users on some webpage.
The first thing I stumbled upon is Embedded Timelines. It allows to display tweets from list of users but the gotcha is that those lists should be maintained on Twitter-side (i.e. I cannot specify #qwe and #asd only on my side and get timeline without adding those users into list on Twitter-side).
The thing is that list of users that should be included into timeline is dynamic and managing those lists through Twitter API will probably be painful. Not to mention that my website will probably generate tons of those lists and I feel that I will violate some api quotas sooner or later.
So, my question is - am I stuck with using Embedded Timelines that refer some user list on Twitter-side and managing those lists through, say Twitter REST api, or there is a simplier way to do what I want?
It's pretty simple to display tweets for multiple users.
Links to start with
This post explains some of the search queries you can make
This post is a simple library to make requests to the twitter API that 'just works'
Your Query
Okay, so you want multiple users. The endpoint you're looking at using is the search/tweets one: https://api.twitter.com/1.1/search/tweets.json.
The query string uses :from and you can interpolate multiple froms with AND/OR.
An example query for the GET request:
?q=from:user1+OR+from:user2
Read more about the search API queries here.
Your "over-the-quote" issue
This is something you're going to need to figure out yourself - depending on the number of requests you expect to make, and the twitter imposed limits, maybe some sort of caching or saving information when you hit your limit, and only pull back from the cache whilst you're hitting your limit..

Finding Twitter users with follower count = MAX_FOLLOWER

I have been looking at finding Twitter users with followers more than MAX_FOLLOWER (any number set up by programmer).
I visited the twitter API and found I can search via q="keyword" but could not find any way where I can have a search in API using followers_count >= MAX_FOLLOWER
Tried looking some methods at https://dev.twitter.com/tags/finding-users as well but they were not really helpful as I could not find exactly what I am looking for.
Even for oAuth stuff, I tried creating a URL after creating a signature but still not able to consume that.
From the signature base string if I copy the url and decode it for addreess bar and send the request it says page does not exist. So looks like I am not even able to consume the services properly.
My end result will be I want a list of users who have followers_count more than MAX_FOLLOWER and then I can do a new query on those users.
I have been trying this stuff for over 4-5 hours and totally clueless where I am doing wrong or how to proceed. Read a lot of twitter api docs but nothing I could find was useful in my case.

Ruby on Rails 3 search external website source based on top google result

I'm having a hard time finding out where to start with this one. I pull information from an external website and put some of the content on my page. I think I need two things done. 1. A google search that takes the url of the top search given a name of my current object. 2. A way to examine the source of the result and output the information of a tag with a specific class.
To better explain this, I'll create a hypothetical situation: Say I have a website that lists mattresses and gives reviews. Say I want to add other websites reviews and in this website there's a tag like 3.5/5. Then I want to display this review along with a link to the external page. Is there a way to search the site like "site:http://mattressreviewsite/ #matress.name", pull that top url, and then search the source for the string "class='rating'" and display this in my view?
Thanks for any help or guidance. I'm using Rails 3.
You need an HTTP client (httparty, net/http-default) for that and do some parsing to get the required results.
Go study the url patterns of google (as far as I remember it was google.com?q=search_string) and use the http client for requests (get/post). Parse the result (there are many HTML parser gems available too) to get what you need and for any subsequent HTTP requests. And don't forget the 'I am feeling lucky' feature of google which returns only one result.
All the best!

Resources