Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 134 lines (101 sloc) 3.407 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 ## Example
7
f331547 @clayallsopp more concise readme
authored
8 Let's say we have some User and Question objects retrievable via our API. We can do fun stuff like:
9
10 ```ruby
11 user = User.find(1) do |user|
12 # async
13 Question.find_all(user_id: user.id) do |questions|
14 # async
15 puts questions
16 end
17 end
18
19 # Later...
6d41a3d @clayallsopp updated readme
authored
20 => [#<Question @user=#<User>,
21 #<Question @user=#<User>]
f331547 @clayallsopp more concise readme
authored
22 ```
23
24 Here's what our files look like:
b6ee191 @clayallsopp testing the read me markdown
authored
25
26 #### ./app/models/user
27 ```ruby
28 class User < RemoteModule::RemoteModel
c673fbc @clayallsopp clean up the readme
authored
29 attr_accessor :id
b6ee191 @clayallsopp testing the read me markdown
authored
30
31 has_many :questions
32
33 collection_url "users"
34 member_url "users/:id"
35 end
36 ```
37
38 #### ./app/models/question.rb
39 ```ruby
40 class Question < RemoteModule::RemoteModel
41 attr_accessor :id, :question, :is_active
42
43 belongs_to :user
44
45 collection_url "users/:user_id/questions"
46 member_url "users/:user_id/questions/:id"
47
48 custom_urls :active_url => member_url + "/make_active"
49
50 def user_id
51 user && user.id
52 end
53
c673fbc @clayallsopp clean up the readme
authored
54 def make_active(active)
b6ee191 @clayallsopp testing the read me markdown
authored
55 post(self.active_url, payload: {active: active}) do |response, json|
56 self.is_active = json[:question][:is_active]
c673fbc @clayallsopp clean up the readme
authored
57 if block_given?
58 yield self
b6ee191 @clayallsopp testing the read me markdown
authored
59 end
60 end
61 end
62 end
6d41a3d @clayallsopp updated readme
authored
63 ```
64
52767b0 @clayallsopp more changes
authored
65 ## Installation
66
67 Add the git repos as submodules in ./vendor:
68
69 ```shell
70 git submodule add git://github.com/mattetti/BubbleWrap.git ./vendor/BubbleWrap
71 git submodule add git://github.com:clayallsopp/remote_model.git ./vendor/remote_model
72 ```
73
74 Then add the lib paths to your ./Rakefile:
75
76 ```ruby
77 Motion::Project::App.setup do |app|
78 ...
79 app.files = Dir.glob(File.join(app.project_dir, 'vendor/BubbleWrap/lib/**/*.rb'))
80 + Dir.glob(File.join(app.project_dir, 'vendor/remote_model/lib/**/*.rb'))
81 + app.files
82 ...
83 end
84 ```
85
86 Add an initialization file somewhere, like ./app/initializers/remote_model.rb. This is where we put the API specifications:
87
88 ```ruby
89 module RemoteModule
90 class RemoteModel
91 # The default URL for our requests.
92 # Overrideable per model subclass
93 self.root_url = "http://localhost:5000/"
94
95 # Options attached to every request
96 # Appendable per model subclass
97 self.default_url_options = {
98 :headers => {
99 "x-api-token" => "some_token",
100 "Accept" => "application/json"
101 }
102 }
103 end
104 end
105 ```
106
6d41a3d @clayallsopp updated readme
authored
107 ## How?
108
109 RemoteModel is designed for JSON apis which return structures with "nice" properties.
110
111 When you make a request with a RemoteModel (self.get/put/post/delete), the result is always parsed as JSON. The ActiveRecord-esque methods take this JSON and create objects out of it. It's clever and creates the proper associations (belongs_to/has_one/has_many) within the objects, as defined in the models.
112
113 #### FormatableString
114
115 The AR methods also use the member/collection defined URLs to make requests. These URLs are a string which you can use :symbols to input dynamic values. These strings can be formatted using a hash and/or taking an object (it will look to see if the object responds to these symbols and call the method if applicable):
116
117 ```ruby
118 >> s = RemoteModule::FormatableString.new("url/:param")
119 => "url/:param"
120 >> s.format({param: 6})
121 => "url/6"
52767b0 @clayallsopp more changes
authored
122 >> obj = SomeObjectThatRespondsToParam.new
123 => ...
124 >> obj.param = 10
125 >> s.format({}, obj)
126 => "url/100"
6d41a3d @clayallsopp updated readme
authored
127 ```
128
129 RemoteModels can define custom urls and call those as methods (see question.rb above).
130
131 ## Todo
132
133 - More tests
134 - CoreData integration
Something went wrong with that request. Please try again.