carwow-json_api_client 1.19.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (48) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +20 -0
  3. data/README.md +706 -0
  4. data/Rakefile +32 -0
  5. data/lib/json_api_client.rb +30 -0
  6. data/lib/json_api_client/associations.rb +8 -0
  7. data/lib/json_api_client/associations/base_association.rb +33 -0
  8. data/lib/json_api_client/associations/belongs_to.rb +31 -0
  9. data/lib/json_api_client/associations/has_many.rb +8 -0
  10. data/lib/json_api_client/associations/has_one.rb +16 -0
  11. data/lib/json_api_client/connection.rb +41 -0
  12. data/lib/json_api_client/error_collector.rb +91 -0
  13. data/lib/json_api_client/errors.rb +107 -0
  14. data/lib/json_api_client/formatter.rb +145 -0
  15. data/lib/json_api_client/helpers.rb +9 -0
  16. data/lib/json_api_client/helpers/associatable.rb +88 -0
  17. data/lib/json_api_client/helpers/callbacks.rb +27 -0
  18. data/lib/json_api_client/helpers/dirty.rb +75 -0
  19. data/lib/json_api_client/helpers/dynamic_attributes.rb +78 -0
  20. data/lib/json_api_client/helpers/uri.rb +9 -0
  21. data/lib/json_api_client/implementation.rb +12 -0
  22. data/lib/json_api_client/included_data.rb +58 -0
  23. data/lib/json_api_client/linking.rb +6 -0
  24. data/lib/json_api_client/linking/links.rb +22 -0
  25. data/lib/json_api_client/linking/top_level_links.rb +39 -0
  26. data/lib/json_api_client/meta_data.rb +19 -0
  27. data/lib/json_api_client/middleware.rb +7 -0
  28. data/lib/json_api_client/middleware/json_request.rb +26 -0
  29. data/lib/json_api_client/middleware/parse_json.rb +31 -0
  30. data/lib/json_api_client/middleware/status.rb +67 -0
  31. data/lib/json_api_client/paginating.rb +6 -0
  32. data/lib/json_api_client/paginating/nested_param_paginator.rb +140 -0
  33. data/lib/json_api_client/paginating/paginator.rb +89 -0
  34. data/lib/json_api_client/parsers.rb +5 -0
  35. data/lib/json_api_client/parsers/parser.rb +102 -0
  36. data/lib/json_api_client/query.rb +6 -0
  37. data/lib/json_api_client/query/builder.rb +239 -0
  38. data/lib/json_api_client/query/requestor.rb +73 -0
  39. data/lib/json_api_client/relationships.rb +6 -0
  40. data/lib/json_api_client/relationships/relations.rb +55 -0
  41. data/lib/json_api_client/relationships/top_level_relations.rb +30 -0
  42. data/lib/json_api_client/request_params.rb +57 -0
  43. data/lib/json_api_client/resource.rb +643 -0
  44. data/lib/json_api_client/result_set.rb +25 -0
  45. data/lib/json_api_client/schema.rb +154 -0
  46. data/lib/json_api_client/utils.rb +48 -0
  47. data/lib/json_api_client/version.rb +3 -0
  48. metadata +213 -0
@@ -0,0 +1,25 @@
1
+ require 'forwardable'
2
+
3
+ module JsonApiClient
4
+ class ResultSet < Array
5
+ extend Forwardable
6
+
7
+ attr_accessor :errors,
8
+ :record_class,
9
+ :meta,
10
+ :pages,
11
+ :uri,
12
+ :links,
13
+ :implementation,
14
+ :relationships,
15
+ :included
16
+
17
+ # pagination methods are handled by the paginator
18
+ def_delegators :pages, :total_pages, :total_entries, :total_count, :offset, :per_page, :current_page, :limit_value, :next_page, :previous_page, :out_of_bounds?
19
+
20
+ def has_errors?
21
+ errors.present?
22
+ end
23
+
24
+ end
25
+ end
@@ -0,0 +1,154 @@
1
+ require 'bigdecimal'
2
+ module JsonApiClient
3
+ class Schema
4
+ module Types
5
+
6
+ class Integer
7
+ def self.cast(value, _)
8
+ value.to_i
9
+ end
10
+ end
11
+
12
+ class String
13
+ def self.cast(value, _)
14
+ value.to_s
15
+ end
16
+ end
17
+
18
+ class Float
19
+ def self.cast(value, _)
20
+ value.to_f
21
+ end
22
+ end
23
+
24
+ class Time
25
+ def self.cast(value, _)
26
+ value.is_a?(::Time) ? value : ::Time.parse(value)
27
+ end
28
+ end
29
+
30
+ class Decimal
31
+ def self.cast(value, _)
32
+ BigDecimal(value)
33
+ end
34
+ end
35
+
36
+ class Boolean
37
+ def self.cast(value, default)
38
+ case value
39
+ when "false", "0", 0, false
40
+ false
41
+ when "true", "1", 1, true
42
+ true
43
+ else
44
+ # if it's unknown, use the default value
45
+ default
46
+ end
47
+ end
48
+ end
49
+
50
+ end
51
+
52
+ class TypeFactory
53
+ @@types = {}
54
+ # Register a new type key or keys with appropriate classes
55
+ #
56
+ # eg:
57
+ #
58
+ # require 'money'
59
+ #
60
+ # class MyMoneyCaster
61
+ # def self.cast(value, default)
62
+ # begin
63
+ # Money.new(value, "USD")
64
+ # rescue ArgumentError
65
+ # default
66
+ # end
67
+ # end
68
+ # end
69
+ #
70
+ # JsonApiClient::Schema::TypeFactory.register money: MyMoneyCaster
71
+ #
72
+ # You can setup several at once:
73
+ #
74
+ # JsonApiClient::Schema::TypeFactory.register money: MyMoneyCaster,
75
+ # date: MyJsonDateTypeCaster
76
+ #
77
+ #
78
+ #
79
+ #
80
+ def self.register(type_hash)
81
+ @@types.merge!(type_hash)
82
+ end
83
+
84
+ def self.type_for(type)
85
+ @@types[type]
86
+ end
87
+
88
+ self.register int: Types::Integer,
89
+ integer: Types::Integer,
90
+ string: Types::String,
91
+ float: Types::Float,
92
+ time: Types::Time,
93
+ decimal: Types::Decimal,
94
+ boolean: Types::Boolean
95
+
96
+ end
97
+
98
+ Property = Struct.new(:name, :type, :default) do
99
+ def cast(value)
100
+ return nil if value.nil?
101
+ return value if type.nil?
102
+ type_caster = TypeFactory.type_for(type)
103
+ return value if type_caster.nil?
104
+ type_caster.cast(value, default)
105
+ end
106
+ end
107
+
108
+ def initialize
109
+ @properties = {}
110
+ end
111
+
112
+ # Add a property to the schema
113
+ #
114
+ # @param name [Symbol] the name of the property
115
+ # @param options [Hash] property options
116
+ # @option options [Symbol] :type The property type
117
+ # @option options [Symbol] :default The default value for the property
118
+ # @return [void]
119
+ def add(name, options)
120
+ @properties[name.to_sym] = Property.new(name.to_sym, options[:type], options[:default])
121
+ end
122
+
123
+ # How many properties are defined
124
+ #
125
+ # @return [Fixnum] the number of defined properties
126
+ def size
127
+ @properties.size
128
+ end
129
+
130
+ alias_method :length, :size
131
+
132
+ def each_property(&block)
133
+ @properties.values.each(&block)
134
+ end
135
+
136
+ alias_method :each, :each_property
137
+
138
+ # Look up a property by name
139
+ #
140
+ # @param property_name [String] the name of the property
141
+ # @return [Property, nil] the property definition for property_name or nil
142
+ def find(property_name)
143
+ @properties[property_name.to_sym]
144
+ end
145
+
146
+ alias_method :[], :find
147
+
148
+ class << self
149
+ def register(type_hash)
150
+ TypeFactory.register(type_hash)
151
+ end
152
+ end
153
+ end
154
+ end
@@ -0,0 +1,48 @@
1
+ module JsonApiClient
2
+ module Utils
3
+
4
+ def self.compute_type(klass, type_name)
5
+ return klass.custom_type_to_class.fetch(type_name).constantize if klass.custom_type_to_class.key?(type_name)
6
+ # If the type is prefixed with a scope operator then we assume that
7
+ # the type_name is an absolute reference.
8
+ return type_name.constantize if type_name.match(/^::/)
9
+
10
+ # Build a list of candidates to search for
11
+ candidates = []
12
+ klass.name.scan(/::|$/) { candidates.unshift "#{$`}::#{type_name}" }
13
+ candidates << type_name
14
+
15
+ candidates.each do |candidate|
16
+ begin
17
+ constant = candidate.constantize
18
+ return constant if candidate == constant.to_s
19
+ rescue NameError => e
20
+ # We don't want to swallow NoMethodError < NameError errors
21
+ raise e unless e.instance_of?(NameError)
22
+ end
23
+ end
24
+
25
+ raise NameError, "uninitialized constant #{candidates.first}"
26
+ end
27
+
28
+ def self.parse_includes(klass, *tables)
29
+ tables.map do |table|
30
+ case table
31
+ when Hash
32
+ table.map do |k, v|
33
+ parse_includes(klass, *v).map do |sub|
34
+ "#{k}.#{sub}"
35
+ end
36
+ end
37
+ when Array
38
+ table.map do |v|
39
+ parse_includes(klass, *v)
40
+ end
41
+ else
42
+ klass.key_formatter.format(table)
43
+ end
44
+ end.flatten
45
+ end
46
+
47
+ end
48
+ end
@@ -0,0 +1,3 @@
1
+ module JsonApiClient
2
+ VERSION = "1.19.0"
3
+ end
metadata ADDED
@@ -0,0 +1,213 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: carwow-json_api_client
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.19.0
5
+ platform: ruby
6
+ authors:
7
+ - Jeff Ching
8
+ - carwow
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2021-05-06 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activesupport
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ">="
19
+ - !ruby/object:Gem::Version
20
+ version: 3.2.0
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ version: 3.2.0
28
+ - !ruby/object:Gem::Dependency
29
+ name: faraday
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: 0.15.2
35
+ - - "<"
36
+ - !ruby/object:Gem::Version
37
+ version: 1.2.0
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: 0.15.2
45
+ - - "<"
46
+ - !ruby/object:Gem::Version
47
+ version: 1.2.0
48
+ - !ruby/object:Gem::Dependency
49
+ name: faraday_middleware
50
+ requirement: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: 0.9.0
55
+ - - "<"
56
+ - !ruby/object:Gem::Version
57
+ version: 1.2.0
58
+ type: :runtime
59
+ prerelease: false
60
+ version_requirements: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: 0.9.0
65
+ - - "<"
66
+ - !ruby/object:Gem::Version
67
+ version: 1.2.0
68
+ - !ruby/object:Gem::Dependency
69
+ name: addressable
70
+ requirement: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: '2.2'
75
+ type: :runtime
76
+ prerelease: false
77
+ version_requirements: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: '2.2'
82
+ - !ruby/object:Gem::Dependency
83
+ name: activemodel
84
+ requirement: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: 3.2.0
89
+ type: :runtime
90
+ prerelease: false
91
+ version_requirements: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: 3.2.0
96
+ - !ruby/object:Gem::Dependency
97
+ name: rack
98
+ requirement: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0.2'
103
+ type: :runtime
104
+ prerelease: false
105
+ version_requirements: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0.2'
110
+ - !ruby/object:Gem::Dependency
111
+ name: webmock
112
+ requirement: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - "~>"
115
+ - !ruby/object:Gem::Version
116
+ version: 3.5.1
117
+ type: :development
118
+ prerelease: false
119
+ version_requirements: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - "~>"
122
+ - !ruby/object:Gem::Version
123
+ version: 3.5.1
124
+ - !ruby/object:Gem::Dependency
125
+ name: mocha
126
+ requirement: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - ">="
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
131
+ type: :development
132
+ prerelease: false
133
+ version_requirements: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - ">="
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ description: Build client libraries compliant with specification defined by jsonapi.org
139
+ email: developers@carwow.co.uk
140
+ executables: []
141
+ extensions: []
142
+ extra_rdoc_files: []
143
+ files:
144
+ - LICENSE
145
+ - README.md
146
+ - Rakefile
147
+ - lib/json_api_client.rb
148
+ - lib/json_api_client/associations.rb
149
+ - lib/json_api_client/associations/base_association.rb
150
+ - lib/json_api_client/associations/belongs_to.rb
151
+ - lib/json_api_client/associations/has_many.rb
152
+ - lib/json_api_client/associations/has_one.rb
153
+ - lib/json_api_client/connection.rb
154
+ - lib/json_api_client/error_collector.rb
155
+ - lib/json_api_client/errors.rb
156
+ - lib/json_api_client/formatter.rb
157
+ - lib/json_api_client/helpers.rb
158
+ - lib/json_api_client/helpers/associatable.rb
159
+ - lib/json_api_client/helpers/callbacks.rb
160
+ - lib/json_api_client/helpers/dirty.rb
161
+ - lib/json_api_client/helpers/dynamic_attributes.rb
162
+ - lib/json_api_client/helpers/uri.rb
163
+ - lib/json_api_client/implementation.rb
164
+ - lib/json_api_client/included_data.rb
165
+ - lib/json_api_client/linking.rb
166
+ - lib/json_api_client/linking/links.rb
167
+ - lib/json_api_client/linking/top_level_links.rb
168
+ - lib/json_api_client/meta_data.rb
169
+ - lib/json_api_client/middleware.rb
170
+ - lib/json_api_client/middleware/json_request.rb
171
+ - lib/json_api_client/middleware/parse_json.rb
172
+ - lib/json_api_client/middleware/status.rb
173
+ - lib/json_api_client/paginating.rb
174
+ - lib/json_api_client/paginating/nested_param_paginator.rb
175
+ - lib/json_api_client/paginating/paginator.rb
176
+ - lib/json_api_client/parsers.rb
177
+ - lib/json_api_client/parsers/parser.rb
178
+ - lib/json_api_client/query.rb
179
+ - lib/json_api_client/query/builder.rb
180
+ - lib/json_api_client/query/requestor.rb
181
+ - lib/json_api_client/relationships.rb
182
+ - lib/json_api_client/relationships/relations.rb
183
+ - lib/json_api_client/relationships/top_level_relations.rb
184
+ - lib/json_api_client/request_params.rb
185
+ - lib/json_api_client/resource.rb
186
+ - lib/json_api_client/result_set.rb
187
+ - lib/json_api_client/schema.rb
188
+ - lib/json_api_client/utils.rb
189
+ - lib/json_api_client/version.rb
190
+ homepage: http://github.com/carwow/json_api_client
191
+ licenses:
192
+ - MIT
193
+ metadata: {}
194
+ post_install_message:
195
+ rdoc_options: []
196
+ require_paths:
197
+ - lib
198
+ required_ruby_version: !ruby/object:Gem::Requirement
199
+ requirements:
200
+ - - ">="
201
+ - !ruby/object:Gem::Version
202
+ version: '0'
203
+ required_rubygems_version: !ruby/object:Gem::Requirement
204
+ requirements:
205
+ - - ">="
206
+ - !ruby/object:Gem::Version
207
+ version: '0'
208
+ requirements: []
209
+ rubygems_version: 3.1.4
210
+ signing_key:
211
+ specification_version: 4
212
+ summary: Build client libraries compliant with specification defined by jsonapi.org
213
+ test_files: []