json_api_client 0.0.3 → 0.1.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f5db14498ef10dc6d6aaa4bcb65b0a6c5bdcb300
4
- data.tar.gz: 75c000bfceb5104a78ab2929f0c160254ce129e5
3
+ metadata.gz: 55c6437405ac925859c235fc1c28899da18f54b2
4
+ data.tar.gz: abfb9a165a77775fc473128713443599a75b6821
5
5
  SHA512:
6
- metadata.gz: f5eae53897bbd0f37a524e979b3c81374ba74607ee4f8e3594d6c581230ad4cca41fb9b355d306ca7da6ef222e9af37613c19e9b4856545908f66a28a16dde2c
7
- data.tar.gz: de3bcf1db477b01d2f89bd9744b5c94eeb7bdd1644752335f44ea667e9b6c0c0ccca3e042a54e6a3e540cb4c1ed63db41c1caa5f2d1ab4232c84eff8350be4fa
6
+ metadata.gz: 1d6599831ca8fe60b51f2f1012549ea816aa7cfb04ee372c2a4e0a0a7810c04881e9d83e6da8e51f1383d4fb8e558110df0d0ebea4c9113b0421bd02ac148e89
7
+ data.tar.gz: 9d7cd4619f496614c8f29bf1913afef270a314c567c571fa229d4c6b0982b6a2313a31bdbba10a3a2d21a595f121150d6f4dcd2e00738934eb95738d292635ee
@@ -7,6 +7,7 @@ module JsonApiClient
7
7
  autoload :Attributes, 'json_api_client/attributes'
8
8
  autoload :Connection, 'json_api_client/connection'
9
9
  autoload :Errors, 'json_api_client/errors'
10
+ autoload :Helpers, 'json_api_client/helpers'
10
11
  autoload :Links, 'json_api_client/links'
11
12
  autoload :Middleware, 'json_api_client/middleware'
12
13
  autoload :Parser, 'json_api_client/parser'
@@ -4,60 +4,5 @@ module JsonApiClient
4
4
  autoload :BelongsTo, 'json_api_client/associations/belongs_to'
5
5
  autoload :HasMany, 'json_api_client/associations/has_many'
6
6
  autoload :HasOne, 'json_api_client/associations/has_one'
7
-
8
- extend ActiveSupport::Concern
9
-
10
- included do
11
- class_attribute :associations
12
- self.associations = []
13
-
14
- include BelongsTo
15
- include HasMany
16
- include HasOne
17
-
18
- initialize :load_associations
19
- end
20
-
21
- module ClassMethods
22
- def belongs_to_associations
23
- associations.select{|association| association.is_a?(BelongsTo::Association) }
24
- end
25
-
26
- def prefix_params
27
- belongs_to_associations.map(&:param)
28
- end
29
-
30
- def prefix_path
31
- belongs_to_associations.map(&:to_prefix_path).join("/")
32
- end
33
-
34
- def path(params = nil)
35
- parts = [table_name]
36
- if params
37
- slurp = params.slice(*prefix_params)
38
- prefix_params.each do |param|
39
- params.delete(param)
40
- end
41
- parts.unshift(prefix_path % slurp)
42
- else
43
- parts.unshift(prefix_path)
44
- end
45
- parts.reject!{|part| part == "" }
46
- File.join(*parts)
47
- rescue KeyError
48
- raise ArgumentError, "Not all prefix parameters specified"
49
- end
50
- end
51
-
52
- protected
53
-
54
- def load_associations(params)
55
- associations.each do |association|
56
- if params.has_key?(association.attr_name.to_s)
57
- set_attribute(association.attr_name, association.parse(params[association.attr_name.to_s]))
58
- end
59
- end
60
- end
61
-
62
7
  end
63
8
  end
@@ -0,0 +1,11 @@
1
+ module JsonApiClient
2
+ module Helpers
3
+ autoload :Associable, 'json_api_client/helpers/associable'
4
+ autoload :Attributable, 'json_api_client/helpers/attributable'
5
+ autoload :Initializable, 'json_api_client/helpers/initializable'
6
+ autoload :Linkable, 'json_api_client/helpers/linkable'
7
+ autoload :Parsable, 'json_api_client/helpers/parsable'
8
+ autoload :Queryable, 'json_api_client/helpers/queryable'
9
+ autoload :Serializable, 'json_api_client/helpers/serializable'
10
+ end
11
+ end
@@ -0,0 +1,60 @@
1
+ module JsonApiClient
2
+ module Helpers
3
+ module Associable
4
+ extend ActiveSupport::Concern
5
+
6
+ included do
7
+ class_attribute :associations
8
+ self.associations = []
9
+
10
+ include Associations::BelongsTo
11
+ include Associations::HasMany
12
+ include Associations::HasOne
13
+
14
+ initializer :load_associations
15
+ end
16
+
17
+ module ClassMethods
18
+ def belongs_to_associations
19
+ associations.select{|association| association.is_a?(Associations::BelongsTo::Association) }
20
+ end
21
+
22
+ def prefix_params
23
+ belongs_to_associations.map(&:param)
24
+ end
25
+
26
+ def prefix_path
27
+ belongs_to_associations.map(&:to_prefix_path).join("/")
28
+ end
29
+
30
+ def path(params = nil)
31
+ parts = [table_name]
32
+ if params
33
+ slurp = params.slice(*prefix_params)
34
+ prefix_params.each do |param|
35
+ params.delete(param)
36
+ end
37
+ parts.unshift(prefix_path % slurp)
38
+ else
39
+ parts.unshift(prefix_path)
40
+ end
41
+ parts.reject!{|part| part == "" }
42
+ File.join(*parts)
43
+ rescue KeyError
44
+ raise ArgumentError, "Not all prefix parameters specified"
45
+ end
46
+ end
47
+
48
+ protected
49
+
50
+ def load_associations(params)
51
+ associations.each do |association|
52
+ if params.has_key?(association.attr_name.to_s)
53
+ set_attribute(association.attr_name, association.parse(params[association.attr_name.to_s]))
54
+ end
55
+ end
56
+ end
57
+
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,62 @@
1
+ module JsonApiClient
2
+ module Helpers
3
+ module Attributable
4
+ extend ActiveSupport::Concern
5
+
6
+ included do
7
+ attr_reader :attributes
8
+ attr_accessor :errors
9
+ initializer do |obj, params|
10
+ obj.attributes = params
11
+ end
12
+ end
13
+
14
+ def attributes=(attrs = {})
15
+ @attributes ||= {}.with_indifferent_access
16
+ @attributes.merge!(attrs)
17
+ end
18
+
19
+ def update_attributes(attrs = {})
20
+ self.attributes = attrs
21
+ save
22
+ end
23
+
24
+ def method_missing(method, *args, &block)
25
+ if match = method.to_s.match(/^(.*)=$/)
26
+ set_attribute(match[1], args.first)
27
+ elsif has_attribute?(method)
28
+ attributes[method]
29
+ else
30
+ super
31
+ end
32
+ end
33
+
34
+ def persisted?
35
+ attributes.has_key?(primary_key)
36
+ end
37
+
38
+ def query_params
39
+ attributes.except(primary_key)
40
+ end
41
+
42
+ def to_param
43
+ attributes.fetch(primary_key, "").to_s
44
+ end
45
+
46
+ protected
47
+
48
+ def read_attribute(name)
49
+ attributes.fetch(name, nil)
50
+ end
51
+
52
+ def set_attribute(name, value)
53
+ attributes[name] = value
54
+ end
55
+
56
+ def has_attribute?(attr_name)
57
+ attributes.has_key?(attr_name)
58
+ end
59
+
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,28 @@
1
+ module JsonApiClient
2
+ module Helpers
3
+ module Initializable
4
+ extend ActiveSupport::Concern
5
+
6
+ included do
7
+ class_attribute :initializers
8
+ self.initializers = []
9
+ end
10
+
11
+ module ClassMethods
12
+ def initializer(method = nil, &block)
13
+ self.initializers.push(method || block)
14
+ end
15
+ end
16
+
17
+ def initialize(params = {})
18
+ initializers.each do |initializer|
19
+ if initializer.respond_to?(:call)
20
+ initializer.call(self, params)
21
+ else
22
+ self.send(initializer, params)
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,16 @@
1
+ module JsonApiClient
2
+ module Helpers
3
+ module Linkable
4
+ extend ActiveSupport::Concern
5
+
6
+ included do
7
+ attr_accessor :links
8
+
9
+ initializer do |obj, params|
10
+ links = params.delete(:links)
11
+ obj.links = links if links
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,19 @@
1
+ module JsonApiClient
2
+ module Helpers
3
+ module Parsable
4
+ extend ActiveSupport::Concern
5
+
6
+ included do
7
+ class_attribute :parser
8
+ self.parser = Parser
9
+ end
10
+
11
+ module ClassMethods
12
+ def parse(data)
13
+ parser.parse(self, data)
14
+ end
15
+ end
16
+
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,39 @@
1
+ module JsonApiClient
2
+ module Helpers
3
+ module Queryable
4
+ extend ActiveSupport::Concern
5
+
6
+ included do
7
+ class << self
8
+ extend Forwardable
9
+ def_delegators :new_scope, :where, :order, :includes, :all, :paginate, :page, :first
10
+ end
11
+ end
12
+
13
+ module ClassMethods
14
+ def new_scope
15
+ Scope.new(self)
16
+ end
17
+
18
+ def connection
19
+ @connection ||= begin
20
+ super
21
+ rescue
22
+ build_connection
23
+ end
24
+ yield(@connection) if block_given?
25
+ @connection
26
+ end
27
+
28
+ def build_connection
29
+ connection_class.new(site)
30
+ end
31
+
32
+ def connection_class
33
+ Connection
34
+ end
35
+ end
36
+
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,13 @@
1
+ module JsonApiClient
2
+ module Helpers
3
+ module Serializable
4
+ def as_json
5
+ attributes
6
+ end
7
+
8
+ def read_attribute_for_serialization(name)
9
+ read_attribute(name)
10
+ end
11
+ end
12
+ end
13
+ end
@@ -6,19 +6,13 @@ require 'active_support/core_ext/class/attribute'
6
6
 
7
7
  module JsonApiClient
8
8
  class Resource
9
- class_attribute :site, :primary_key, :link_style, :default_headers, :initializers, :parser
9
+ class_attribute :site, :primary_key, :link_style, :default_headers
10
10
 
11
11
  self.primary_key = :id
12
12
  self.link_style = :id # or :url
13
13
  self.default_headers = {}
14
- self.initializers = []
15
- self.parser = Parser
16
14
 
17
15
  class << self
18
- # first 'scope' should build a new scope object
19
- extend Forwardable
20
- def_delegators :new_scope, :where, :order, :includes, :all, :paginate, :page, :first
21
-
22
16
  # base URL for this resource
23
17
  def resource
24
18
  File.join(site, path)
@@ -42,53 +36,18 @@ module JsonApiClient
42
36
  result.first
43
37
  end
44
38
 
45
- def connection
46
- @connection ||= begin
47
- super
48
- rescue
49
- build_connection
50
- end
51
- yield(@connection) if block_given?
52
- @connection
53
- end
54
-
55
39
  def run_request(query)
56
40
  parse(connection.execute(query))
57
41
  end
58
-
59
- private
60
-
61
- def new_scope
62
- Scope.new(self)
63
- end
64
-
65
- def parse(data)
66
- parser.parse(self, data)
67
- end
68
-
69
- def build_connection
70
- Connection.new(site)
71
- end
72
-
73
- def initialize(method = nil, &block)
74
- self.initializers.push(method || block)
75
- end
76
42
  end
77
43
 
78
- include Attributes
79
- include Associations
80
- include Links
81
-
82
- attr_accessor :errors
83
- def initialize(params = {})
84
- initializers.each do |initializer|
85
- if initializer.respond_to?(:call)
86
- initializer.call(self, params)
87
- else
88
- self.send(initializer, params)
89
- end
90
- end
91
- end
44
+ include Helpers::Initializable
45
+ include Helpers::Attributable
46
+ include Helpers::Associable
47
+ include Helpers::Parsable
48
+ include Helpers::Queryable
49
+ include Helpers::Serializable
50
+ include Helpers::Linkable
92
51
 
93
52
  def save
94
53
  query = persisted? ?
@@ -1,3 +1,3 @@
1
1
  module JsonApiClient
2
- VERSION = "0.0.3"
2
+ VERSION = "0.1.1"
3
3
  end
metadata CHANGED
@@ -1,69 +1,69 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json_api_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeff Ching
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-10-09 00:00:00.000000000 Z
11
+ date: 2014-01-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
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: faraday
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - ~>
31
+ - - "~>"
32
32
  - !ruby/object:Gem::Version
33
33
  version: 0.8.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.8.0
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: faraday_middleware
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - ~>
45
+ - - "~>"
46
46
  - !ruby/object:Gem::Version
47
47
  version: 0.9.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.9.0
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: webmock
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: :development
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
  description: Build client libraries compliant with specification defined by jsonapi.org
@@ -72,34 +72,40 @@ executables: []
72
72
  extensions: []
73
73
  extra_rdoc_files: []
74
74
  files:
75
+ - LICENSE
76
+ - README.md
77
+ - Rakefile
78
+ - lib/json_api_client.rb
79
+ - lib/json_api_client/associations.rb
75
80
  - lib/json_api_client/associations/base_association.rb
76
81
  - lib/json_api_client/associations/belongs_to.rb
77
82
  - lib/json_api_client/associations/has_many.rb
78
83
  - lib/json_api_client/associations/has_one.rb
79
- - lib/json_api_client/associations.rb
80
- - lib/json_api_client/attributes.rb
81
84
  - lib/json_api_client/connection.rb
82
85
  - lib/json_api_client/errors.rb
83
- - lib/json_api_client/links.rb
86
+ - lib/json_api_client/helpers.rb
87
+ - lib/json_api_client/helpers/associable.rb
88
+ - lib/json_api_client/helpers/attributable.rb
89
+ - lib/json_api_client/helpers/initializable.rb
90
+ - lib/json_api_client/helpers/linkable.rb
91
+ - lib/json_api_client/helpers/parsable.rb
92
+ - lib/json_api_client/helpers/queryable.rb
93
+ - lib/json_api_client/helpers/serializable.rb
94
+ - lib/json_api_client/middleware.rb
84
95
  - lib/json_api_client/middleware/json_request.rb
85
96
  - lib/json_api_client/middleware/status.rb
86
- - lib/json_api_client/middleware.rb
87
97
  - lib/json_api_client/parser.rb
98
+ - lib/json_api_client/query.rb
88
99
  - lib/json_api_client/query/base.rb
89
100
  - lib/json_api_client/query/create.rb
90
101
  - lib/json_api_client/query/destroy.rb
91
102
  - lib/json_api_client/query/find.rb
92
103
  - lib/json_api_client/query/update.rb
93
- - lib/json_api_client/query.rb
94
104
  - lib/json_api_client/resource.rb
95
105
  - lib/json_api_client/result_set.rb
96
106
  - lib/json_api_client/scope.rb
97
107
  - lib/json_api_client/utils.rb
98
108
  - lib/json_api_client/version.rb
99
- - lib/json_api_client.rb
100
- - LICENSE
101
- - Rakefile
102
- - README.md
103
109
  homepage: http://github.com/chingor13/json_api_client
104
110
  licenses:
105
111
  - MIT
@@ -110,17 +116,17 @@ require_paths:
110
116
  - lib
111
117
  required_ruby_version: !ruby/object:Gem::Requirement
112
118
  requirements:
113
- - - '>='
119
+ - - ">="
114
120
  - !ruby/object:Gem::Version
115
121
  version: '0'
116
122
  required_rubygems_version: !ruby/object:Gem::Requirement
117
123
  requirements:
118
- - - '>='
124
+ - - ">="
119
125
  - !ruby/object:Gem::Version
120
126
  version: '0'
121
127
  requirements: []
122
128
  rubyforge_project:
123
- rubygems_version: 2.0.3
129
+ rubygems_version: 2.2.0
124
130
  signing_key:
125
131
  specification_version: 4
126
132
  summary: Build client libraries compliant with specification defined by jsonapi.org
@@ -1,59 +0,0 @@
1
- module JsonApiClient
2
- module Attributes
3
- extend ActiveSupport::Concern
4
-
5
- included do
6
- attr_reader :attributes
7
- initialize do |obj, params|
8
- obj.attributes = params
9
- end
10
- end
11
-
12
- def attributes=(attrs = {})
13
- @attributes ||= {}.with_indifferent_access
14
- @attributes.merge!(attrs)
15
- end
16
-
17
- def update_attributes(attrs = {})
18
- self.attributes = attrs
19
- save
20
- end
21
-
22
- def method_missing(method, *args, &block)
23
- if match = method.to_s.match(/^(.*)=$/)
24
- set_attribute(match[1], args.first)
25
- elsif has_attribute?(method)
26
- attributes[method]
27
- else
28
- nil
29
- end
30
- end
31
-
32
- def persisted?
33
- attributes.has_key?(primary_key)
34
- end
35
-
36
- def query_params
37
- attributes.except(primary_key)
38
- end
39
-
40
- def to_param
41
- attributes.fetch(primary_key, "").to_s
42
- end
43
-
44
- protected
45
-
46
- def read_attribute(name)
47
- attributes.fetch(name, nil)
48
- end
49
-
50
- def set_attribute(name, value)
51
- attributes[name] = value
52
- end
53
-
54
- def has_attribute?(attr_name)
55
- attributes.has_key?(attr_name)
56
- end
57
-
58
- end
59
- end
@@ -1,14 +0,0 @@
1
- module JsonApiClient
2
- module Links
3
- extend ActiveSupport::Concern
4
-
5
- included do
6
- attr_accessor :links
7
-
8
- initialize do |obj, params|
9
- links = params.delete(:links)
10
- obj.links = links if links
11
- end
12
- end
13
- end
14
- end