munson 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +75 -5
- data/lib/munson.rb +15 -9
- data/lib/munson/agent.rb +1 -1
- data/lib/munson/middleware/encode_json_api.rb +2 -0
- data/lib/munson/paginator/offset_paginator.rb +2 -1
- data/lib/munson/paginator/paged_paginator.rb +3 -1
- data/lib/munson/query_builder.rb +2 -2
- data/lib/munson/resource.rb +10 -8
- data/lib/munson/version.rb +1 -1
- data/munson.gemspec +0 -2
- metadata +2 -30
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 58f61fc4c3adf530a74a67fe73b0c1907e097a3a
|
4
|
+
data.tar.gz: c0dfb862271eee803921d1a9772514de40f8d669
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 59c16ff0e3445ebdc0f9adc4b422537714e0767a0a780533f2b682a08ce1ce2de0cf3af0cb88672c2b2d7fa96dc7a781a42692665f9e832033480f90fbbcf0ec
|
7
|
+
data.tar.gz: fae8f69311ed2cbec3efc7397c8c2c8c01165b0c493b77e4f4c80805c07aee9b0fb798cbbf8dee8b2d06af3dcf70dd803bd4f74323081460ead7e294f9b4ea4c
|
data/README.md
CHANGED
@@ -2,6 +2,7 @@
|
|
2
2
|
|
3
3
|
[](https://codeclimate.com/github/coryodaniel/munson)
|
4
4
|
[](https://codeclimate.com/github/coryodaniel/munson/coverage)
|
5
|
+

|
5
6
|
|
6
7
|
A JSON API Spec client for Ruby
|
7
8
|
|
@@ -58,7 +59,7 @@ class Product
|
|
58
59
|
@munson = Munson::Agent.new(
|
59
60
|
connection: Munson.default_connection, # || Munson::Connection.new(...)
|
60
61
|
paginator: :offset,
|
61
|
-
|
62
|
+
type: 'products'
|
62
63
|
)
|
63
64
|
end
|
64
65
|
end
|
@@ -160,7 +161,7 @@ class Product
|
|
160
161
|
return @munson if @munson
|
161
162
|
@munson = Munson::Agent.new(
|
162
163
|
paginator: :offset,
|
163
|
-
|
164
|
+
type: 'products'
|
164
165
|
)
|
165
166
|
end
|
166
167
|
end
|
@@ -179,7 +180,7 @@ class Product
|
|
179
180
|
return @munson if @munson
|
180
181
|
@munson = Munson::Agent.new(
|
181
182
|
paginator: :paged,
|
182
|
-
|
183
|
+
type: 'products'
|
183
184
|
)
|
184
185
|
end
|
185
186
|
end
|
@@ -224,17 +225,34 @@ when using the bare Munson::Agent, Munson::Resource will pass the JSON Spec attr
|
|
224
225
|
```ruby
|
225
226
|
class Product
|
226
227
|
include Munson::Resource
|
228
|
+
register_munson_type :products
|
227
229
|
end
|
228
230
|
|
229
231
|
# Munson method is there, should you be looking for it.
|
230
232
|
Product.munson #=> Munson::Agent
|
231
233
|
```
|
232
234
|
|
233
|
-
|
235
|
+
**Setting the type**
|
236
|
+
|
237
|
+
This will cause Munson to return a hash instead of a class instance (Product).
|
238
|
+
|
234
239
|
```ruby
|
235
240
|
class Product
|
236
241
|
include Munson::Resource
|
237
|
-
munson.type =
|
242
|
+
munson.type = :products
|
243
|
+
end
|
244
|
+
```
|
245
|
+
|
246
|
+
There are two ways to set the JSON API type when using a Munson::Resource
|
247
|
+
|
248
|
+
**Registering the type**
|
249
|
+
|
250
|
+
This will cause munson to return your model's datatype. Munson will register this in its type map dictionary and use the class to initialize a model
|
251
|
+
|
252
|
+
```ruby
|
253
|
+
class Product
|
254
|
+
include Munson::Resource
|
255
|
+
register_munson_type :products
|
238
256
|
end
|
239
257
|
```
|
240
258
|
|
@@ -354,3 +372,55 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
354
372
|
## Contributing
|
355
373
|
|
356
374
|
Bug reports and pull requests are welcome on GitHub at https://github.com/coryodaniel/munson.
|
375
|
+
|
376
|
+
### Munson::Model
|
377
|
+
WIP see [usage](#usage)
|
378
|
+
|
379
|
+
Finding related resources could set instance methods that mix/override behavior of the class level agent...
|
380
|
+
|
381
|
+
/articles/1
|
382
|
+
Article.find(1) uses Article.munson
|
383
|
+
|
384
|
+
/articles/1/author
|
385
|
+
article = Article.find(1) uses Article.munson
|
386
|
+
article.author uses article.munson as a new connection that sets the base uri to /articles/1
|
387
|
+
|
388
|
+
## Usage
|
389
|
+
```ruby
|
390
|
+
address = Address.new
|
391
|
+
address.state = "FL"
|
392
|
+
address.save
|
393
|
+
address.state = "CA"
|
394
|
+
address.save
|
395
|
+
# Mind mutex adding method accessors on... dangerous, see her, consider storing them in an @attributes hash...
|
396
|
+
user = User.find(1)
|
397
|
+
address = user.addresses.build
|
398
|
+
address.save #posts on the relation
|
399
|
+
|
400
|
+
address = Address.new({...})
|
401
|
+
address = Address.new
|
402
|
+
address.assign_attributes
|
403
|
+
address.update_attributes
|
404
|
+
address.dirty?
|
405
|
+
|
406
|
+
Address.update(1, {}) #update without loading
|
407
|
+
Address.destroy(1) #Destroy without loading
|
408
|
+
|
409
|
+
address = Address.find(300)
|
410
|
+
address.destroy
|
411
|
+
|
412
|
+
Address.find(10)
|
413
|
+
Address.filter(zip_code: 90210).find(10)
|
414
|
+
Address.filter(zip_code: 90210).all
|
415
|
+
Address.filter(zip_code: 90210).fetch
|
416
|
+
Address.includes(:user, 'user.purchases').filter(active: true).all
|
417
|
+
Address.includes(:user, 'user.purchases').find(10)
|
418
|
+
Address.sort(city: :asc)
|
419
|
+
Address.sort(city: :desc)
|
420
|
+
Address.fields(:street1, :street2, {user: :name})
|
421
|
+
Address.fields(:street1, :street2).fields(user: :name)
|
422
|
+
addresses = Address.fields(:street1, :street2).fields(user: :name)
|
423
|
+
addresses.first.shipped_products.filter(min_total: 300.00)
|
424
|
+
|
425
|
+
Custom collection/member methods?
|
426
|
+
```
|
data/lib/munson.rb
CHANGED
@@ -1,9 +1,5 @@
|
|
1
1
|
require 'json'
|
2
|
-
|
3
|
-
require 'active_support/concern'
|
4
|
-
require "active_support/inflector"
|
5
|
-
require "active_support/core_ext/hash"
|
6
|
-
|
2
|
+
require 'cgi'
|
7
3
|
require 'faraday'
|
8
4
|
require 'faraday_middleware'
|
9
5
|
|
@@ -22,6 +18,8 @@ require 'munson/resource'
|
|
22
18
|
|
23
19
|
module Munson
|
24
20
|
@registered_types = {}
|
21
|
+
@registered_paginators = {}
|
22
|
+
|
25
23
|
class << self
|
26
24
|
# Configure the default connection.
|
27
25
|
#
|
@@ -48,18 +46,26 @@ module Munson
|
|
48
46
|
# @example Mapping a type
|
49
47
|
# Munson.register_type("addresses", Address)
|
50
48
|
#
|
51
|
-
# @param [#
|
49
|
+
# @param [#to_sym] type JSON Spec type
|
52
50
|
# @param [Class] klass to map to
|
53
51
|
def register_type(type, klass)
|
54
|
-
@registered_types[type] = klass
|
52
|
+
@registered_types[type.to_sym] = klass
|
53
|
+
end
|
54
|
+
|
55
|
+
def register_paginator(name, klass)
|
56
|
+
@registered_paginators[name.to_sym] = klass
|
57
|
+
end
|
58
|
+
|
59
|
+
def lookup_paginator(name)
|
60
|
+
@registered_paginators[name.to_sym]
|
55
61
|
end
|
56
62
|
|
57
63
|
# Lookup a class by JSON Spec type name
|
58
64
|
#
|
59
|
-
# @param [#
|
65
|
+
# @param [#to_sym] type JSON Spec type
|
60
66
|
# @return [Class] domain model
|
61
67
|
def lookup_type(type)
|
62
|
-
@registered_types[type]
|
68
|
+
@registered_types[type.to_sym]
|
63
69
|
end
|
64
70
|
|
65
71
|
# @private
|
data/lib/munson/agent.rb
CHANGED
@@ -4,8 +4,10 @@ module Munson
|
|
4
4
|
CONTENT_TYPE = 'Content-Type'.freeze
|
5
5
|
ACCEPT = 'Accept'.freeze
|
6
6
|
MIME_TYPE = 'application/vnd.api+json'.freeze
|
7
|
+
USER_AGENT = 'User-Agent'
|
7
8
|
|
8
9
|
def call(env)
|
10
|
+
env[:request_headers][USER_AGENT] = "Munson v#{Munson::VERSION}"
|
9
11
|
env[:request_headers][ACCEPT] ||= MIME_TYPE
|
10
12
|
match_content_type(env) do |data|
|
11
13
|
env[:body] = encode data
|
@@ -16,7 +16,7 @@ module Munson
|
|
16
16
|
page: {
|
17
17
|
limit: @limit || @default_limit || 10,
|
18
18
|
offset: @offset
|
19
|
-
}.
|
19
|
+
}.select { |_, value| !value.nil? }
|
20
20
|
}
|
21
21
|
end
|
22
22
|
|
@@ -42,3 +42,4 @@ module Munson
|
|
42
42
|
end
|
43
43
|
end
|
44
44
|
end
|
45
|
+
Munson.register_paginator(:offset, Munson::Paginator::OffsetPaginator)
|
@@ -16,7 +16,7 @@ module Munson
|
|
16
16
|
page: {
|
17
17
|
size: @size || @default_size || 10,
|
18
18
|
number: @number
|
19
|
-
}.
|
19
|
+
}.select { |_, value| !value.nil? }
|
20
20
|
}
|
21
21
|
end
|
22
22
|
|
@@ -43,3 +43,5 @@ module Munson
|
|
43
43
|
end
|
44
44
|
end
|
45
45
|
end
|
46
|
+
|
47
|
+
Munson.register_paginator(:paged, Munson::Paginator::PagedPaginator)
|
data/lib/munson/query_builder.rb
CHANGED
data/lib/munson/resource.rb
CHANGED
@@ -1,19 +1,21 @@
|
|
1
1
|
module Munson
|
2
2
|
module Resource
|
3
|
-
|
3
|
+
def self.included(base)
|
4
|
+
base.extend ClassMethods
|
5
|
+
end
|
4
6
|
|
5
|
-
|
6
|
-
def
|
7
|
+
module ClassMethods
|
8
|
+
def munson
|
7
9
|
return @munson if @munson
|
8
10
|
@munson = Munson::Agent.new
|
9
11
|
@munson
|
10
12
|
end
|
13
|
+
|
14
|
+
def register_munson_type(name)
|
15
|
+
Munson.register_type(name, self)
|
16
|
+
self.munson.type = name
|
17
|
+
end
|
11
18
|
|
12
|
-
self.munson.type = name.demodulize.tableize
|
13
|
-
Munson.register_type(self.munson.type, self)
|
14
|
-
end
|
15
|
-
|
16
|
-
class_methods do
|
17
19
|
[:includes, :sort, :filter, :fields, :fetch, :find, :page].each do |method|
|
18
20
|
class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
19
21
|
def #{method}(*args)
|
data/lib/munson/version.rb
CHANGED
data/munson.gemspec
CHANGED
@@ -19,7 +19,6 @@ Gem::Specification.new do |spec|
|
|
19
19
|
|
20
20
|
spec.add_dependency "faraday"
|
21
21
|
spec.add_dependency "faraday_middleware"
|
22
|
-
spec.add_dependency "activesupport"
|
23
22
|
|
24
23
|
spec.add_development_dependency "bundler", "~> 1.10"
|
25
24
|
spec.add_development_dependency "rake", "~> 10.0"
|
@@ -29,6 +28,5 @@ Gem::Specification.new do |spec|
|
|
29
28
|
spec.add_development_dependency "pry"
|
30
29
|
spec.add_development_dependency "pry-byebug"
|
31
30
|
spec.add_development_dependency 'yard'
|
32
|
-
spec.add_development_dependency 'yard-activesupport-concern'
|
33
31
|
spec.add_development_dependency 'webmock'
|
34
32
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: munson
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Cory O'Daniel
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-07-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: faraday
|
@@ -38,20 +38,6 @@ dependencies:
|
|
38
38
|
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
|
-
- !ruby/object:Gem::Dependency
|
42
|
-
name: activesupport
|
43
|
-
requirement: !ruby/object:Gem::Requirement
|
44
|
-
requirements:
|
45
|
-
- - ">="
|
46
|
-
- !ruby/object:Gem::Version
|
47
|
-
version: '0'
|
48
|
-
type: :runtime
|
49
|
-
prerelease: false
|
50
|
-
version_requirements: !ruby/object:Gem::Requirement
|
51
|
-
requirements:
|
52
|
-
- - ">="
|
53
|
-
- !ruby/object:Gem::Version
|
54
|
-
version: '0'
|
55
41
|
- !ruby/object:Gem::Dependency
|
56
42
|
name: bundler
|
57
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -164,20 +150,6 @@ dependencies:
|
|
164
150
|
- - ">="
|
165
151
|
- !ruby/object:Gem::Version
|
166
152
|
version: '0'
|
167
|
-
- !ruby/object:Gem::Dependency
|
168
|
-
name: yard-activesupport-concern
|
169
|
-
requirement: !ruby/object:Gem::Requirement
|
170
|
-
requirements:
|
171
|
-
- - ">="
|
172
|
-
- !ruby/object:Gem::Version
|
173
|
-
version: '0'
|
174
|
-
type: :development
|
175
|
-
prerelease: false
|
176
|
-
version_requirements: !ruby/object:Gem::Requirement
|
177
|
-
requirements:
|
178
|
-
- - ">="
|
179
|
-
- !ruby/object:Gem::Version
|
180
|
-
version: '0'
|
181
153
|
- !ruby/object:Gem::Dependency
|
182
154
|
name: webmock
|
183
155
|
requirement: !ruby/object:Gem::Requirement
|