munson 0.1.0 → 0.2.0

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: 77ac2773bff30e5728d4a9e4233c8d4249e549de
4
- data.tar.gz: 401d3dd17993b1b6fe4bc37ad9389304bbcb8afb
3
+ metadata.gz: 58f61fc4c3adf530a74a67fe73b0c1907e097a3a
4
+ data.tar.gz: c0dfb862271eee803921d1a9772514de40f8d669
5
5
  SHA512:
6
- metadata.gz: 1b1ae060b21129c271dd32475f84305b8481577b9c0aa7831352aa3cdfae3918103cf79ed02dd4a1040f63916286cac7a111c7cd9914e5187d57caf9ebcd8ccd
7
- data.tar.gz: 3d0b4b589d45b5df66f9203e3fed3a3745c5addf09e63d4db172a13d0020ba815162d68891f30132635e9e84e3ddff2b899078e485fabf4cc37d11f2cc92fe11
6
+ metadata.gz: 59c16ff0e3445ebdc0f9adc4b422537714e0767a0a780533f2b682a08ce1ce2de0cf3af0cb88672c2b2d7fa96dc7a781a42692665f9e832033480f90fbbcf0ec
7
+ data.tar.gz: fae8f69311ed2cbec3efc7397c8c2c8c01165b0c493b77e4f4c80805c07aee9b0fb798cbbf8dee8b2d06af3dcf70dd803bd4f74323081460ead7e294f9b4ea4c
data/README.md CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  [![Code Climate](https://codeclimate.com/github/coryodaniel/munson/badges/gpa.svg)](https://codeclimate.com/github/coryodaniel/munson)
4
4
  [![Test Coverage](https://codeclimate.com/github/coryodaniel/munson/badges/coverage.svg)](https://codeclimate.com/github/coryodaniel/munson/coverage)
5
+ ![Build Status](https://travis-ci.org/coryodaniel/munson.svg?branch=master)
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
- path: 'products'
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
- path: 'products'
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
- path: 'products'
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
- Changing the type name:
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 = "things"
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 [#to_s] type JSON Spec type
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 [#to_s] type JSON Spec type
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
@@ -31,7 +31,7 @@ module Munson
31
31
 
32
32
  def paginator=(pager)
33
33
  if pager.is_a?(Symbol)
34
- @paginator = "Munson::Paginator::#{pager.to_s.classify}Paginator".constantize
34
+ @paginator = Munson.lookup_paginator(pager)
35
35
  else
36
36
  @paginator = pager
37
37
  end
@@ -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
- }.compact
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
- }.compact
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)
@@ -26,11 +26,11 @@ module Munson
26
26
 
27
27
  # @return [String] query as a query string
28
28
  def to_query_string
29
- to_params.to_query
29
+ Faraday::Utils.build_nested_query(to_params)
30
30
  end
31
31
 
32
32
  def to_s
33
- to_query
33
+ to_query_string
34
34
  end
35
35
 
36
36
  def to_params
@@ -1,19 +1,21 @@
1
1
  module Munson
2
2
  module Resource
3
- extend ActiveSupport::Concern
3
+ def self.included(base)
4
+ base.extend ClassMethods
5
+ end
4
6
 
5
- included do
6
- def self.munson
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)
@@ -1,3 +1,3 @@
1
1
  module Munson
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
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.1.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-02-25 00:00:00.000000000 Z
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