Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 115 lines (89 sloc) 2.558 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 ...
c673fbc @clayallsopp clean up the readme
authored
19 app.files = Dir.glob(File.join(app.project_dir, 'vendor/BubbleWrap/lib/**/*.rb'))
20 + Dir.glob(File.join(app.project_dir, 'vendor/remote_model/lib/**/*.rb'))
21 + app.files
b6ee191 @clayallsopp testing the read me markdown
authored
22 ...
23 end
24 ```
25
26 Add an initialization file somewhere, like ./app/initializers/remote_model.rb. This is where we put the API specifications:
27
28 ```ruby
29 module RemoteModule
30 class RemoteModel
31 # The default URL for our requests.
32 # Overrideable per model subclass
33 self.root_url = "http://localhost:5000/"
34
35 # Options attached to every request
36 # Appendable per model subclass
37 self.default_url_options = {
38 :headers => {
39 "x-api-token" => "some_token",
40 "Accept" => "application/json"
41 }
42 }
43 end
44 end
45 ```
46
47 ## Example
48 ------------
49
50 Let's say we have some User, Question, and Answer objects retrievable via our API. Here's what our files can look like:
51
52 #### ./app/models/user
53 ```ruby
54 class User < RemoteModule::RemoteModel
c673fbc @clayallsopp clean up the readme
authored
55 attr_accessor :id
b6ee191 @clayallsopp testing the read me markdown
authored
56
57 has_many :questions
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
c673fbc @clayallsopp clean up the readme
authored
81 def make_active(active)
b6ee191 @clayallsopp testing the read me markdown
authored
82 post(self.active_url, payload: {active: active}) do |response, json|
83 self.is_active = json[:question][:is_active]
c673fbc @clayallsopp clean up the readme
authored
84 if block_given?
85 yield self
b6ee191 @clayallsopp testing the read me markdown
authored
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
c673fbc @clayallsopp clean up the readme
authored
112 # Later...
113 => [#<Question @answers=[#<Answer>, #<Answer>] @user=#<User>,
114 #<Question @answers=[#<Answer>, #<Answer>] @user=#<User>]
b6ee191 @clayallsopp testing the read me markdown
authored
115 ```
Something went wrong with that request. Please try again.