Rails api for iOS app? - ios

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.

Related

Using Rails Devise in API mode

So as you may know Rails can work as an API only, this will prevent view generations and some other stuff.
So now I installed the Devise gem but I don't know how to start working with it.
I'm using a ReactJS front end but how do I create a user for example? With the views I figured it out before but not with only json requests.
my comment being said, and vote for close being done, here's my answer to you:
if you don't use rails own views, it's fine, though that means you'll have to build manually all the basic CRUDs for each model element you'll want to expose. Or you can have a backoffice in vanilla rails that will help you manage the basic model elements, and a frontoffice communicating through the API (because you can have both).

What would be the best way to use AngularJS with Ruby on Rails?

I'm about to start a new project and I am unsure if using AngularJS for my front end would be a good idea not. I've read about people saying this isn't the smartest way of doing a project. And even if I did, Is there a way to get AngularJS to interact with the controllers? This question may be redundant but I am actually curious of how to effectively do this without it being a waste of time.
I've never completely done it, but I believe the way to go is to build a Rails api and then have a separate Angular project use said api. The api could also be used to build a mobile app. I think the Angular project would still need to be served from a Node.js server in production, but I don't think that would be a big deal.
This is what I used to learn how to build a Rails api: http://apionrails.icalialabs.com/book/chapter_one
You can do it within an existing project and share the models from it.
There are several different approaches to accomplish that. I tried about 5 different guides out there, the best I found (and I finally sticked to) was https://thinkster.io/angular-rails - This guide should help you build a basic CRUD app with angular connected to rails.
You use Rails as an JSON RESTful API which responds to Ajax-Requests (Get, Post, Put, Delete). Angular will handle the frontend stuff - sending those Ajax requests to the routes/methods defined in your rails controllers. So yes, of course your AngularJS app can interact with your rails controllers.
This also helped me to understand the setup in the beginning: Instead of the Rails View, you will be using AngularJS as your view:
I really love using angular with rails, because setting up the JSON responses (especially with Active Model Serializer Gem) is very easy and quickly done. i deffinitely can recommend it, and I have not encountered any unsolvable problems - so far.
Just go trough this guide I linked and you will see if this setup fits your needs.
The short answer is that your Rails application will have to present some kind of a public API for your AngularJS application to consume. Angular (and it's brethren, like React and Ember) runs client-side, on the browser, and it needs "something" to make AJAX calls against. That "something", i.e. your backend, can be Firebase, Parse, AWS Lambdas, Rails API, etc. Since you already have a Rails application, it probably makes the most sense to add some RESTful API endpoints that use the existing models (and possibly controllers) to consume/produce JSON payloads from/for the client.

RoR - Project that exclusively uses another systems API?

Absolute beginner with ruby and with rails. My first full blown project will interface with an existing system through a REST api.
So it doesn't employ the normal ActiveRecord model and I was hoping for some examples of projects that replace the normal use of models with API calls. The entire CRUD set will require the app to make the corresponding API calls. I'd like to do this the right way, just don't know what that looks like yet.
Thanks! :)
Helpful links for googlers:
http://yetimedia.tumblr.com/post/35233051627/activeresource-is-dead-long-live-activeresource
https://github.com/rails/activeresource
I don't know of any projects that are open source that do this, but I'd recommend looking into Active Resource. It was part of Rails through 3.2, but has been moved to a separate repo for Rails 4. The idea is that it lets you replace database persistance with RESTful APIs, which sounds like exactly what you're trying to do. There is some documentation here which should give you enough info to get off the ground. There's also a pretty old Railscast on it, but I'm sure the concepts haven't really changed that much.
Here's an example from the docs that shows how to setup your "model":
class Person < ActiveResource::Base
self.site = "http://api.people.com:3000"
end
I'd suggest leveraging ActiveModel so your models aren't 'AR' backed, i.e. no persistance via AR. You'll handle CRUD accordingly in your controllers and craft your own 'business logic' that you can tuck into your ActiveModel.

Resource Generators in Sinatra

I have developed few apps in Rails, and I needed to develop an API. I received the advise to build it in Sinatra, so I started looking into it.
It seemed quite nice, but it seems that a lot of things you get automated in Rails does not seem to exist in Sinatra. Specifically, I seem to have to write my resources from scratch. eg. The model itself, the migrations, and the REST routes.
I was wondering if there are any generators for Sinatra like the ones provided by Rails? Or should I simply use Rails if I want these kind of things automated?
Check out the sinatra-rest gem that can be used to handle RESTful routes. Quoted below for convenience:
[sinatra-rest is] a set of templates to introduce RESTful routes into Sinatra. The only thing for you to do is to provide the views. Automatically works nicely for models based on ActiveRecord, DataMapper, or Stone.
For example, if your model’s class is called Person you only need to add this line:
rest Person
Which will add the following RESTful routes to your application. (Note the pluralization of Person to the /people/* routes.)
Verb Route Controller View
GET /people index /people/index.haml
GET /people/new new /people/new.haml
POST /people create → redirect to show
GET /people/1 show /people/show.haml
GET /people/1/edit edit /people/edit.haml
PUT /people/1 update → redirect to show
DELETE /people/1 destroy → redirect to index
I don't personally use Sinatra but a lot of feedback I've gotten from other Rails developers is that they eventually end up switching back to Rails. I'm sure there are good arguments for using Sinatra over Rails, but if you already know Rails, and you don't have speed or application size constraints, I would just stick with that.
Another alternative to Sinatra is the Rails API project which doesn't include any of the view-related part of the Rails framework. I have used that in the past and liked it, but was it better than just using Rails? It's hard to say.
You should check out Padrino if you must have Sinatra.
Padrino is a ruby framework built upon the Sinatra web library.
Sinatra is a DSL for creating simple web applications in Ruby. Padrino
was created to make it fun and easy to code more advanced web
applications while still adhering to the spirit that makes Sinatra
great!
Or as Beerlington mentioned, you could use Rails API if you feel more at home with Rails. We've been using it recently with good success. We created a Simple API, with a mongo backend. Starts up very quickly :)
Or should I simply use Rails if I want these kind of things automated?
If you're that used to Rails that using Ruby is a problem, then maybe. Alternatively, you could try this API generator that uses Sinatra:
https://github.com/mattetti/Weasel-Diesel

Best Way to Handle the UI Layer Within a Ruby Architecture

Are there some tools like CMS or UI-level frameworks that help in the maintenance and development of the UI layer within Ruby architectures?
I am still learning Ruby but as I understand it, it is very coupled with the front-end, correct?
Thanks,
Alex
You can use active_scaffold to get ajaxified crud interface and it can get integrated inside your existing rails app easily.
github.com/activescaffold/active_scaffold/wiki/Getting-Started
You can also use hobo but from I have gathered you need to use it right from the beginning as its generator creates the app.
hobocentral.net/
You can use comatose cms for integrating a basic content management functionality in your existing ror app.
comatose.rubyforge.org/getting-started-01.html
Also if you are looking for something pre-packed with rails then you can use command:
$ rails generate scaffold Post name:string title:string content:text
Command above is specific for rails 3 (in previous version scaffolding was available via ruby script/generate scaffold).
guides.rubyonrails.org/getting_started.html#getting-up-and-running-quickly-with-scaffolding
Hope this helps.
Are there some tools like CMS or UI-level frameworks that help in the maintenance and development of the UI layer within Ruby architectures?
CMS and UI level frameworks are very broad categories that you are talking about. Just to get you started, I will list down some I started with
Nifty-generators
blueprint-css
jquery-rails
I am still learning Ruby but as I understand it, it is very coupled with the front-end, correct?
No, its not very coupled with the front end as you think it is. You can assume Rails(Ruby) as just the back-end that will just send back data and when data has arrived at front-end, its your wish how to present it, format it or whatever you want to do with it.

Resources