arke 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f2b484cebed2989532174c0001b0016ac4fe3e31
4
- data.tar.gz: 94cc8cd064543d16f67a99ea951bd57179418bcf
3
+ metadata.gz: 747bbdfb0c8f1c05d707315be454b3e4d40a4560
4
+ data.tar.gz: 5c0907b7b22ac017ff67a99d5797b82df7f416ad
5
5
  SHA512:
6
- metadata.gz: f43786d9a78de90132bf3b8c710da9281df73d726713fbd4e5cd570633c7ef051660464aba64722c521e45a67a61189ab90550a55f339f84cad1fc5a7f283706
7
- data.tar.gz: 4f5e32f17f5e0bc727a0506d155a26f4a871e477a40c79d126ba5614d4d0014140f89d11ec14c6f5aeef37554a5dd6dcaa0377c61adfdd32d4dbac7feda880e7
6
+ metadata.gz: e82f7dc3099d68ec00e9be21c37192f066590a936f8f25886837d7ee8a6b52b191d791b0130a34bf267b160fe9a578a430ffed10ffbcfcb7fcf33d965f33ed2a
7
+ data.tar.gz: f885c70be045a80cc613ff48ce6ace55a4d395f500feba98997f83dc6c06e0be5ee5d494330a6c7ae24f1d963c51012319b73ee41622cccdc4e096349f6c2bbf
data/.ruby-version CHANGED
@@ -1 +1 @@
1
- 2.0.0
1
+ 2.1.0
data/README.md CHANGED
@@ -1,29 +1,77 @@
1
1
  # Arke
2
2
 
3
- TODO: Write a gem description
3
+ Arke is a RESTful API client library designed specifically for utilizing distributed services in a service oriented
4
+ environment.
4
5
 
5
- ## Installation
6
+ ## Compatibility
6
7
 
7
- Add this line to your application's Gemfile:
8
+ Arke is developed against MRI 2.1.0 and tested against MRI 1.9.3, 2.0.0, and 2.1.0.
8
9
 
9
- gem 'arke'
10
+ ## Basic Usage
10
11
 
11
- And then execute:
12
+ Arke's model format is similar to that of Mongoid's:
12
13
 
13
- $ bundle
14
+ ```ruby
15
+ class User
16
+ include Arke::Resource::model
14
17
 
15
- Or install it yourself as:
18
+ host 'http://localhost:3000'
16
19
 
17
- $ gem install arke
20
+ field :email, type: String
18
21
 
19
- ## Usage
22
+ end
23
+ ```
20
24
 
21
- TODO: Write usage instructions here
25
+ ## Advanced Usage
22
26
 
23
- ## Contributing
27
+ ### Fields
28
+
29
+ Fields are required to access data returned from the service resource. If a field is not defined it will not be
30
+ accessible from the model. This allows the fields to be serialized as the developer requires, and allows them to
31
+ be fully integrated into Rails ```form_for```. The field type defaults to ```String```.
32
+
33
+ ### Hosts
34
+
35
+ Arke makes it easy to access multiple different hosts. The ```host``` directive defines the host the request will be
36
+ sent to.
37
+
38
+ ### Endpoints
39
+
40
+ The endpoint is the location on the host for the specified resource. By default the endpoint is the tablelized name
41
+ of the model. So in the case of a class named ```User``` the endpoint would be ```/users```
42
+
43
+ Endpoints can be redefined as necessary by passing the ```endpoint``` directive:
44
+
45
+ ```
46
+ class User
47
+ include Arke::Resource::model
48
+
49
+ host 'http://localhost:3000'
50
+ endpoint 'members'
51
+
52
+ field :email, type: String
53
+
54
+ end
55
+ ```
56
+
57
+ ### Deserializers
58
+
59
+ By default Arke assumes data will be coming back as JSON. You can define a custom deserialization method using the
60
+ ```deserializer``` directive:
61
+
62
+ ```ruby
63
+ class User
64
+ include Arke::Resource::model
65
+
66
+ host 'http://localhost:3000'
67
+
68
+ deserializer { |response| MessagePack.unpack(response.body) }
69
+
70
+ field :email, type: String
71
+
72
+ end
73
+ ```
74
+
75
+ ### URL Templates
76
+ Arke uses Addressable for url templating.
24
77
 
25
- 1. Fork it ( http://github.com/<my-github-username>/arke/fork )
26
- 2. Create your feature branch (`git checkout -b my-new-feature`)
27
- 3. Commit your changes (`git commit -am 'Add some feature'`)
28
- 4. Push to the branch (`git push origin my-new-feature`)
29
- 5. Create new Pull Request
@@ -18,10 +18,10 @@ module Arke
18
18
  private
19
19
 
20
20
  def klass
21
- "#{@parent.class.name.split('::')[0..-2].join('::')}::#{name.to_s.classify}"
21
+ "#{@parent.class.name.split('::')[0..-2].join('::')}::#{@options[:class_name] || name.to_s.classify}"
22
22
  end
23
23
 
24
24
  end
25
25
  end
26
26
  end
27
- end
27
+ end
@@ -8,7 +8,7 @@ module Arke
8
8
 
9
9
  def all(params={})
10
10
  target.all(params.merge({
11
- template: "/#{@parent.class.table_name}/#{@parent.id}/{endpoint}"
11
+ template: "/#{@parent.class.table_name}/#{@parent.send(options[:foreign_key] || :id)}/{endpoint}"
12
12
  }))
13
13
  end
14
14
 
@@ -35,4 +35,4 @@ module Arke
35
35
 
36
36
  end
37
37
  end
38
- end
38
+ end
@@ -3,8 +3,12 @@ module Arke
3
3
  module Resource
4
4
  module Errors
5
5
 
6
+ class ConnectionRefused < StandardError; end
7
+
6
8
  class UnexpectedResponse < StandardError; end
7
9
 
10
+ class MethodNotAllowed < StandardError; end
11
+
8
12
  class NotFound < StandardError
9
13
 
10
14
  def initialize
@@ -17,11 +21,13 @@ module Arke
17
21
  class Exception < StandardError
18
22
 
19
23
  def initialize(resource)
20
- message "An error on the specificed resource: Status Code #{resource.code}"
24
+ self.message = "An error on the specificed resource: Status Code #{resource.code}"
21
25
  end
22
26
 
23
27
  end
24
28
 
29
+ class UnprocessableEntity < StandardError; end
30
+
25
31
  end
26
32
  end
27
33
  end
@@ -18,9 +18,10 @@ module Arke
18
18
  instance_eval do
19
19
  define_method(name) do
20
20
  value = @_attributes[name]
21
+ return value if klass == Boolean
21
22
  value ? klass.new(@_attributes[name]) : klass.new
22
23
  end
23
- end
24
+ end
24
25
  end
25
26
 
26
27
  def define_setter(name, klass)
@@ -37,4 +38,4 @@ module Arke
37
38
 
38
39
  end
39
40
  end
40
- end
41
+ end
@@ -25,8 +25,10 @@ module Arke
25
25
  deserializer { |body| JSON.parse(body) }
26
26
 
27
27
  handle([200, 201]) { |response| JSON.parse(response.body) }
28
-
28
+
29
29
  handle(404) { raise Errors::NotFound }
30
+ handle(405) { raise Errors::MethodNotAllowed }
31
+ handle(422) { raise Errors::UnprocessableEntity }
30
32
 
31
33
  field :id, type: String
32
34
 
@@ -53,7 +55,10 @@ module Arke
53
55
 
54
56
  def has_many(name, options={})
55
57
  self.associations ||= {}
56
- self.associations[name] = Associations::HasManyAssociation
58
+ self.associations[name] = {
59
+ association: Associations::HasManyAssociation,
60
+ options: options
61
+ }
57
62
  end
58
63
 
59
64
  def after_initialize(&block)
@@ -179,7 +184,14 @@ module Arke
179
184
  end
180
185
 
181
186
  def persisted?
182
- !self.id.nil?
187
+ !self.id.empty?
188
+ end
189
+
190
+ def save
191
+ unless self.persisted?
192
+ return make_request(:post, @_attributes) && true
193
+ end
194
+ false
183
195
  end
184
196
 
185
197
 
@@ -190,10 +202,25 @@ module Arke
190
202
 
191
203
  private
192
204
 
205
+ def make_request(method, body={}, params={})
206
+ begin
207
+ response = self.class.send(method, self.class.url(params.empty? ? body : params), {body: body})
208
+ rescue Errno::ECONNREFUSED
209
+ raise Errors::ConnectionRefused.new(host)
210
+ end
211
+ handle_response(response)
212
+ end
213
+
214
+ def handle_response(response)
215
+ self.class.response_handlers[response.code.to_i].call(response, self)
216
+ end
217
+
218
+
193
219
  def method_missing(meth, *args)
194
- return self.class.associations[meth.to_sym].new(self, meth.to_sym) if self.class.associations && self.class.associations[meth.to_sym]
220
+ return self.class.associations[meth.to_sym][:association].
221
+ new(self, meth.to_sym, associations[meth.to_sym][:options]) if self.class.associations && self.class.associations[meth.to_sym]
195
222
  super
196
223
  end
197
224
  end
198
225
  end
199
- end
226
+ end
data/lib/arke/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Arke
2
- VERSION = "0.0.1"
2
+ VERSION = '0.0.2'
3
3
  end
metadata CHANGED
@@ -1,139 +1,139 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: arke
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jarod Reid
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-03-04 00:00:00.000000000 Z
11
+ date: 2014-05-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httparty
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - '>='
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: '0'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - '>='
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: activemodel
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - '>='
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
33
  version: '0'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - '>='
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: addressable
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - '>='
45
+ - - ">="
46
46
  - !ruby/object:Gem::Version
47
47
  version: '0'
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - '>='
52
+ - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: activesupport
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - '>='
59
+ - - ">="
60
60
  - !ruby/object:Gem::Version
61
61
  version: '0'
62
62
  type: :runtime
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - '>='
66
+ - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: bundler
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
- - - ~>
73
+ - - "~>"
74
74
  - !ruby/object:Gem::Version
75
75
  version: '1.5'
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
- - - ~>
80
+ - - "~>"
81
81
  - !ruby/object:Gem::Version
82
82
  version: '1.5'
83
83
  - !ruby/object:Gem::Dependency
84
84
  name: rake
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
- - - '>='
87
+ - - ">="
88
88
  - !ruby/object:Gem::Version
89
89
  version: '0'
90
90
  type: :development
91
91
  prerelease: false
92
92
  version_requirements: !ruby/object:Gem::Requirement
93
93
  requirements:
94
- - - '>='
94
+ - - ">="
95
95
  - !ruby/object:Gem::Version
96
96
  version: '0'
97
97
  - !ruby/object:Gem::Dependency
98
98
  name: pry-debugger
99
99
  requirement: !ruby/object:Gem::Requirement
100
100
  requirements:
101
- - - '>='
101
+ - - ">="
102
102
  - !ruby/object:Gem::Version
103
103
  version: '0'
104
104
  type: :development
105
105
  prerelease: false
106
106
  version_requirements: !ruby/object:Gem::Requirement
107
107
  requirements:
108
- - - '>='
108
+ - - ">="
109
109
  - !ruby/object:Gem::Version
110
110
  version: '0'
111
111
  - !ruby/object:Gem::Dependency
112
112
  name: yard
113
113
  requirement: !ruby/object:Gem::Requirement
114
114
  requirements:
115
- - - '>='
115
+ - - ">="
116
116
  - !ruby/object:Gem::Version
117
117
  version: '0'
118
118
  type: :development
119
119
  prerelease: false
120
120
  version_requirements: !ruby/object:Gem::Requirement
121
121
  requirements:
122
- - - '>='
122
+ - - ">="
123
123
  - !ruby/object:Gem::Version
124
124
  version: '0'
125
125
  - !ruby/object:Gem::Dependency
126
126
  name: rspec
127
127
  requirement: !ruby/object:Gem::Requirement
128
128
  requirements:
129
- - - '>='
129
+ - - ">="
130
130
  - !ruby/object:Gem::Version
131
131
  version: '0'
132
132
  type: :development
133
133
  prerelease: false
134
134
  version_requirements: !ruby/object:Gem::Requirement
135
135
  requirements:
136
- - - '>='
136
+ - - ">="
137
137
  - !ruby/object:Gem::Version
138
138
  version: '0'
139
139
  description: Arke is a service oriented api client, providing similar functionality
@@ -144,8 +144,8 @@ executables: []
144
144
  extensions: []
145
145
  extra_rdoc_files: []
146
146
  files:
147
- - .gitignore
148
- - .ruby-version
147
+ - ".gitignore"
148
+ - ".ruby-version"
149
149
  - Gemfile
150
150
  - LICENSE.txt
151
151
  - README.md
@@ -174,12 +174,12 @@ require_paths:
174
174
  - lib
175
175
  required_ruby_version: !ruby/object:Gem::Requirement
176
176
  requirements:
177
- - - '>='
177
+ - - ">="
178
178
  - !ruby/object:Gem::Version
179
179
  version: '0'
180
180
  required_rubygems_version: !ruby/object:Gem::Requirement
181
181
  requirements:
182
- - - '>='
182
+ - - ">="
183
183
  - !ruby/object:Gem::Version
184
184
  version: '0'
185
185
  requirements: []