fulfil_api 0.2.0 → 0.3.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
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e0460289a73e01bd52c8b06a997b9fc62396113cd981dc9a09e87ca900f5c9a6
|
4
|
+
data.tar.gz: abc95e2905c9a95482dbbba457e2e088a3fd9f02043f89846f2bf60a9bad7b62
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 80682546344bb1ead394205e822881b499188a8d924984acf62c564f5b37527926039b7d6b6bc771a6cf18217396ffbdc7e1483bda04f6a4b2d4fe801c358ea9
|
7
|
+
data.tar.gz: a7b246c3a4a3addebcfa72ba38bb0e1fe5ccb689e92b3f076beb5686b258c18300ed56481f2aaba8cf367bc98a946a0677e8ecc5cb0884e04092e80c23e188ba
|
@@ -22,11 +22,31 @@ module FulfilApi
|
|
22
22
|
# @param name [String, Symbol] The attribute name
|
23
23
|
# @param value [Any] The attribute value
|
24
24
|
# @return [Hash] The resource attributes
|
25
|
-
def assign_attribute(name, value)
|
25
|
+
def assign_attribute(name, value) # rubocop:disable Metrics/MethodLength
|
26
26
|
attribute = build_attribute(name, value)
|
27
27
|
attribute.deep_stringify_keys!
|
28
28
|
|
29
|
-
|
29
|
+
# NOTE: Fulfil will assign the ID of a nested resource to its own namespace.
|
30
|
+
# This leads to conflicts when we're trying to parse the returned fields
|
31
|
+
# from the API.
|
32
|
+
#
|
33
|
+
# To address this problem, we're manually handling these cases. We're dealing
|
34
|
+
# with a nested relation when one of the values is an integer and the other
|
35
|
+
# is an hash.
|
36
|
+
#
|
37
|
+
# @example a nested relation
|
38
|
+
#
|
39
|
+
# $ resource.assign_attributes({ "warehouse.name" => "Toronto", "warehouse" => 10 })
|
40
|
+
# => <FulfilApi::Resource @attributes={"warehouse" => { "id" => 10, "name" => "Toronto" }} />
|
41
|
+
@attributes = @attributes.deep_merge(attribute) do |_key, current_value, other_value|
|
42
|
+
if current_value.is_a?(Integer) && other_value.is_a?(Hash)
|
43
|
+
{ "id" => current_value }.deep_merge(other_value)
|
44
|
+
elsif current_value.is_a?(Hash) && other_value.is_a?(Integer)
|
45
|
+
current_value.deep_merge({ "id" => other_value })
|
46
|
+
else
|
47
|
+
other_value
|
48
|
+
end
|
49
|
+
end
|
30
50
|
end
|
31
51
|
|
32
52
|
private
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module FulfilApi
|
4
|
+
class Resource
|
5
|
+
module Comparable
|
6
|
+
def ==(other)
|
7
|
+
other.is_a?(FulfilApi::Resource) &&
|
8
|
+
other.hash == hash
|
9
|
+
end
|
10
|
+
|
11
|
+
def eql?(other)
|
12
|
+
self == other
|
13
|
+
end
|
14
|
+
|
15
|
+
def hash
|
16
|
+
@attributes.hash
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module FulfilApi
|
4
|
+
class Resource
|
5
|
+
module Serializable
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
|
8
|
+
class_methods do
|
9
|
+
# Turns a JSON string into a {FulfilApi::Resource}.
|
10
|
+
#
|
11
|
+
# @note it's required to include the name of the model as part of the JSON
|
12
|
+
# string too. Otherwise, you will encounter a naming error when attempting
|
13
|
+
# to turn the JSON into a {FulfilApi::Resource}
|
14
|
+
#
|
15
|
+
# @param json [String] The JSONified data
|
16
|
+
# @param root_included [true, false] When using Rails, one can include
|
17
|
+
# @return [FulfilApi::Resource]
|
18
|
+
def from_json(json, root_included: false)
|
19
|
+
attributes = JSON.parse(json)
|
20
|
+
attributes = attributes.values.first if root_included
|
21
|
+
|
22
|
+
new(attributes)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
# Overwrites the default {#as_json} method because {ActiveModel} will nest
|
27
|
+
# the attributes when the model is transformed to JSON.
|
28
|
+
#
|
29
|
+
# @param options [Hash, nil] An optional list of options
|
30
|
+
# @return [Hash] A set of attributes available to be JSONified.
|
31
|
+
def as_json(options = nil)
|
32
|
+
# NOTE: We're including the model name by default. Otherwise, we can't use
|
33
|
+
# the {.from_json} method to parse it when reading from JSON.
|
34
|
+
hash = to_h.merge("model_name" => @model_name)
|
35
|
+
|
36
|
+
case options
|
37
|
+
in { root: }
|
38
|
+
{ root => hash }
|
39
|
+
else
|
40
|
+
hash
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
# Turns the {Resource} into a JSON object.
|
45
|
+
#
|
46
|
+
# @param options [Hash, nil] An optional list of options
|
47
|
+
# @return [String] The JSONified resource data
|
48
|
+
def to_json(options = nil)
|
49
|
+
as_json(options).to_json
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
data/lib/fulfil_api/resource.rb
CHANGED
@@ -5,7 +5,9 @@ module FulfilApi
|
|
5
5
|
# endpoints of Fulfil.
|
6
6
|
class Resource
|
7
7
|
include AttributeAssignable
|
8
|
+
include Comparable
|
8
9
|
include Persistable
|
10
|
+
include Serializable
|
9
11
|
|
10
12
|
# The model name is required to be able to build the API endpoint to
|
11
13
|
# perform the search/read/count HTTP requests.
|
data/lib/fulfil_api/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fulfil_api
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Stefan Vermaas
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-12-
|
11
|
+
date: 2024-12-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -95,8 +95,10 @@ files:
|
|
95
95
|
- lib/fulfil_api/resource.rb
|
96
96
|
- lib/fulfil_api/resource/attribute_assignable.rb
|
97
97
|
- lib/fulfil_api/resource/attribute_type.rb
|
98
|
+
- lib/fulfil_api/resource/comparable.rb
|
98
99
|
- lib/fulfil_api/resource/errors.rb
|
99
100
|
- lib/fulfil_api/resource/persistable.rb
|
101
|
+
- lib/fulfil_api/resource/serializable.rb
|
100
102
|
- lib/fulfil_api/test_helper.rb
|
101
103
|
- lib/fulfil_api/version.rb
|
102
104
|
- sig/fulfil_api.rbs
|