Rails for mobile API - ruby-on-rails

I want to create a service with many posts, which have many attributes.
Users enter data (age, gender, category, etc.) and get some posts.
Which gem use for search by categories?
Tell me please the best way to create API for mobile phones.

There are many options for implementing search, depending on what you need. Rails has easy integration with elasticsearch which provides full text search capabilities. For, search by categories, you can even use plain ActiveRecord queries to get this done to some extent. Again, depending on your need.
You can use active_model_serializers or jbuilder for building JSON API using Rails and then your mobile clients can consume these APIs.
There are lots of resources about how to build a JSON API in Rails using AMS or jbuilder. You just need to google it.

Related

Rails api for iOS app?

I'm developing a iOS app and want it to talk to a rails server.
I was wondering the normal approach for making API's for iOS consumption is? My original plan was to develop the site in rails with the functionality and the way I want it to work and then take that site and create an API for it. This seems overkill considering I don't want a web version of the app.
I'm interested to hear if anyone has had any experience with approaching this or how they would go about it.
I know I could develop the API standalone but am unsure how to develop the site functionality within a standalone API without views e.g using the rails-api gem.
Sorry if this questions is not explained well as I'm still relatively new to rails.
Thanks.
Assumption: rails 4+, you want the API to be JSON based.
If you use scaffold to create your model objects the you've got the API pretty much written for you. I mean it will create the views for you, and it's up to you to do any changes you want to the controller.rb (probably not) and to the view (action.json.jbuilder).
http://guides.rubyonrails.org/v3.2.13/getting_started.html
(go to) 5 Getting Up and Running Quickly with Scaffolding
A common change you'll make will be formatting a datetime property from your Rails model to be a certain format (lets go with unix timestamp), so you put those changes in your action jbuilder file, i.e
app/views/person/show.json.jbuilder
json.extract! #person, :first_name, :last_name, :id
json.date #person.date_of_birth.to_i
So now when you browse to
/person/23.json
you'll get
{
"first_name":"Rails",
"last_name":"is great",
"id": 23,
"date_of_birth": 1395101106
}
In summary, use
rails generate scaffold model_name property:string other_property:int
for your model objects
Use ActiveModelSerializers with the JSONAPI adapter, and jsonapi-ios on the iPhone. That way you have a well thought out JSON format out of the box.
For authentication I'd recommend Devise with devise_token_auth, and for roles the cancancan gem.

Rails 4 search options. What options are available to combine a full HTML site search with an ActiveRecord search?

I'm looking for a solution that combines the power of Google Site Search with Elasticsearch for a Rails 4 app preferably as a Rails gem.
As far as I can tell the most popular search solutions, ThinkingSphinx, ElasticSearch with the tire gem, Sunspot Solr and lastly PostreSQL searching functionality, all seem to only handle database searching and do not have page/html template searching functionality.
If I have this wrong then please correct me and I will happily pick one of the above.
The site contains mainly static HTML so the Google Site Search api is the obvious solution but there are some ActiveRecord results that should also be included in the results of a search.
If there really isn't a simple combined solution then I would appreciate any pointers as to how to achieve the merging of an elasticsearch (my preffered AR search solution) with a google site search where the results happen to include the same pages so page results are not duplicated.
To rephrase your question, you want to combine results of Google Site Search with custom search results provided by Sphinx, Solr or Elasticsearch?
First, you cannot really customize the content of Google Site Search results easily. You can customize the design, and could employ some JavaScript tricks to "merge" its results with another data source, but I'd say the approach is not something maintainable and, more importantly, usable.
Notice, that you can display the search results from Elasticsearch with Tire in the same way as ActiveRecord instances, all the usual Rails helpers such as url_for etc. work. The easiest way to evaluate the integration is to generate the example application with the Rails template.
If you want to combine the results from ActiveRecord data and the results from any static pages you might have on your site/application, it wouldn't be hard to write a simple crawler which would retrieve, parse and index the content of static pages and store it as ActiveModel-compatible documents in Elasticsearch.

Searching WordPress blog from Rails app

I have a site-wide custom written search controller for my Rails 3 app and would like to include results from the site's WordPress blog. What is the best way for me to perform a keyword search on posts from within my Rails app?
If you share database then just use SQL query on it. This solution gives you speed of direct db query but you’ll need to construct that query properly in order to get all relevant data.
If you don't have access to the WP database from your Rails app then the best way will be to use curl, httparty, RestClient or any other file retrieval library.
To do that, create Wordpress page with custom template which will output search results in a format which is best for you to parse in Rails app (json, xml, csv, urlencoded, whatever).
Then request that WP page from your Ruby app using curl/RestClient/httparty…
This solution gives you the power of WP template tags and functions to get the results.
Also instead of creating custom template from scratch you can just simply copy and tweak search.php from core template to provide the results in a format required by your Rails app.
With this solution you are lacking the speed of direct access to db because all search result will have to be transferred through http pipe and you have to process the data twice (encode to the proper format in WP and decode in Rails app).
Interesting problem. I think I would approach it like this:
Use RSS as the text transport from the blog to your rails app. This allows the flexibility to add more blogs in the future, change your blog engine, database host, etc. It also protects you from Wordpress code updates. Given the security history of Wordpress, I like to host them in a protected sandbox anyway. RSS is the native language for blogs, so it seems a natural fit for this kind of content integration.
Use the feedzirra gem to import RSS entries into a rails model.
Use Elasticsearch and tire for fuzzy text searching across both your rails app and your blog entries. See this Railscast for details.
Option 1. is to use search engine for both sites, like elasticsearch, solr etc. So you populate the index from rails and wordpress.
Option 2. You write script, that reads periodically your wordpress RSS and saves data in your rails app.
At the end you should avoid to search from different sources, you should gather the data into one place and then search.
You don't have to stuck with wordpress. You can use Google search APIs. Web search api has been deprecated but still working. Its replacement is Custom Search API. You may need to pay if you query over the limit.
Alternatively you can leverage other search engine APIs like Bing Search API.
I'd suggest using the Wordpress JSON API and plugging that into your search using solr or something similar. You can index as posts are created and then call the articles via the sam JSON interface.
Use Tire and wp-elasticsearch with ElasticSearch.

News / Tag Aggregation Packages?

In an attempt to avoid writing my own aggregator for an existent Ruby on Rails app and WordPress blog, I'd like to be able to aggregate various chatter going on in various mediums at one central page based on particular keywords, etc.
This aggregator can live either on the blog or within the Rails app, it doesn't matter.
For example, let's say I want to continually poll Google News, Twitter, public Facebook, and perhaps others, for "chocolate chip cookies." Is there a package that already facilitates this or am I in a "roll it yourself" situation?
One thing to consider is using HTTParty to deal with the APIs those services expose...

How to build a Search functionality to search multiple models like GitHub?

I am wondering on how to implement a search functionality like Github.
Just one search box on the top header right and when searched for a keyword, displays the results for Repository, Code and User.
Is there any tutorial or example to implement this on Rails 3?
Odds are really good they're doing separate searches across the tables for the same value, then combining the results afterwards.
Use Rails to create a small form containing a text field. When it's submitted take the value of the field and do a query using that as the search term.
If you're not sure how to do queries using ActiveRecord, see "Active Record Query Interface" for a nice overview.
You will have to do several queries, one per model, and put the results together on the same view.
If your question is "how do I do full text searches on several activerecord models in a DRY way" then there are basically two paths:
The common solution, but a bit complex, is using a dedicated daemon on your machine, like Sphinx. Sphinx is a service in (like Apache or MySQL) that indexes your content and allows you to do searches. You can use the Thinking Sphinx gem to communicate with it easily from rails. An alternative to Sphinx is Solr (there's also a gem for it called Sunspot)
If you are using Postgresql, there's a simpler alternative that doesn't require external services running on your server. Postgresql has with some full-text search capabilities built-in. There's a gem called texticle that helps using these services from rails. You can have that working very quickly.

Resources