Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 114 lines (86 sloc) 2.607 kb
b6ee191 @clayallsopp testing the read me markdown
authored
1 # RemoteModel
2
3
4 JSON API <-> NSObject in one line. Powered by RubyMotion and [BubbleWrap](https://github.com/mattetti/BubbleWrap/).
5
6 ## Installation
7 ------------
8
9 Add the git repos as submodules in ./vendor:
10
11 git submodule add git://github.com/mattetti/BubbleWrap.git ./vendor/BubbleWrap
12 git submodule add git://github.com:clayallsopp/remote_model.git ./vendor/remote_model
13
14 Then add the lib paths to your ./Rakefile:
15
16 ```ruby
17 Motion::Project::App.setup do |app|
18 ...
19 app.files = Dir.glob(File.join(app.project_dir, 'vendor/BubbleWrap/lib/**/*.rb')) + Dir.glob(File.join(app.project_dir, 'vendor/remote_model/lib/**/*.rb')) + app.files
20 ...
21 end
22 ```
23
24 Add an initialization file somewhere, like ./app/initializers/remote_model.rb. This is where we put the API specifications:
25
26 ```ruby
27 module RemoteModule
28 class RemoteModel
29 # The default URL for our requests.
30 # Overrideable per model subclass
31 self.root_url = "http://localhost:5000/"
32
33 # Options attached to every request
34 # Appendable per model subclass
35 self.default_url_options = {
36 :headers => {
37 "x-api-token" => "some_token",
38 "Accept" => "application/json"
39 }
40 }
41 end
42 end
43 ```
44
45 ## Example
46 ------------
47
48 Let's say we have some User, Question, and Answer objects retrievable via our API. Here's what our files can look like:
49
50 #### ./app/models/user
51 ```ruby
52 class User < RemoteModule::RemoteModel
53 attr_accessor :id, :phone, :email
54
55 has_many :questions
56
57 class << self; attr_accessor :current_user; end;
58
59 collection_url "users"
60 member_url "users/:id"
61 end
62 ```
63
64 #### ./app/models/question.rb
65 ```ruby
66 class Question < RemoteModule::RemoteModel
67 attr_accessor :id, :question, :is_active
68
69 belongs_to :user
70 has_many :answers
71
72 collection_url "users/:user_id/questions"
73 member_url "users/:user_id/questions/:id"
74
75 custom_urls :active_url => member_url + "/make_active"
76
77 def user_id
78 user && user.id
79 end
80
81 def make_active(active, &block)
82 post(self.active_url, payload: {active: active}) do |response, json|
83 self.is_active = json[:question][:is_active]
84 if block
85 block.call self
86 end
87 end
88 end
89 end
90 ```
91
92 #### ./app/models/answer.rb
93 ```ruby
94 class Answer < RemoteModule::RemoteModel
95 attr_accessor :id, :answer
96
97 belongs_to :question
98 end
99 ```
100
101 And now we can do fun stuff:
102
103 ```ruby
104 user = User.find(1) do |user|
105 # async
106 Question.find_all(user_id: user.id) do |questions|
107 # async
108 puts questions
109 end
110 end
111
112
113 => [#<Question @answers=[#<Answer>, #<Answer>] @user=#<User>, #<Question @answers=[#<Answer>, #<Answer>] @user=#<User>]
114 ```
Something went wrong with that request. Please try again.