Reputation: 851
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.
Upvotes: 0
Views: 403
Reputation: 6840
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.
Upvotes: 1
Reputation: 307
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
Upvotes: 1