springcm-sdk 0.4.0 → 0.5.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: 4ede0e8cbe39a1ba56490a8747d394136877c4ba297f42bc618d665a37822b5b
4
- data.tar.gz: 2c6c5bf9194b9f63f937f491cd3522d8175db3d8aabd5cf799b8a09aba7689e6
3
+ metadata.gz: d6147bd5e61e56858a0c7bd91d8aab95a718b580e32e42f1fe7de34a2599742a
4
+ data.tar.gz: 84260d0c7050d3675765f7d481da6ca261e98ad900c272731de26daa8d1792a5
5
5
  SHA512:
6
- metadata.gz: 9aee61e667d073e51bc57f7e8aa00c67e6f47067cc106b11e1caee6306836e390035df29d63714f7f10c8d6351bbca8eaabca34ef95a65a4cc71eecbd60f9874
7
- data.tar.gz: 40aeda6cd074142c6546a3451fd29c495dc1f1a73bebce8dab2d2382f730e268cc5690de41d9c89b7b5203d149a5915a7997802e871b7bc559c3e70558271512
6
+ metadata.gz: 3ed35a3f5b082bf08b7e4162f8a6491f8a27d8f4d6ffb9a88de4116dd2b1c078cb564f161d683c557c0eda46b9251dfb23f7ffee6aec592d64ca804cde7af254
7
+ data.tar.gz: cf064b9ea38ac974f63f87d67e548aab6e58fed69d545bff2e84da68e30be373b9c1924954c16ee6a5ad582cd58eb1a6930e56a0f4b37f2b4657b31fa4b9c3ea
data/CHANGELOG.md CHANGED
@@ -4,6 +4,27 @@ All notable changes to springcm-sdk will be documented in this file.
4
4
 
5
5
  ## [Unreleased]
6
6
 
7
+ ## [0.5.0] - 2020-01-03
8
+ ### Added
9
+ * #patch and #put for Resources.
10
+ * #set_attribute for Attribute mixin.
11
+ * Safeguard against permanent Document deletions. DeleteRefusedError will be
12
+ be raised on documents that are in the trash folder unless #delete! is used.
13
+
14
+ ### Changed
15
+ * Moved attribute-related tests to a separate test file. May need to re-add
16
+ to document/folder specs just for live testing.
17
+ * AttributeGroupBuilder JSON now includes IsSystem property.
18
+ * Fix property default values on DocumentBuilder
19
+ * Raise error on invalid attribute groups or fields.
20
+
21
+ ### Bugfixes
22
+ * Fix issue with Resource#resource_params not being applied properly in
23
+ request methods.
24
+ * Fix JSON returned from builders when retrieving data in lists, e.g.
25
+ `/folders/<uid>/documents` does not include attribute groups. Tests have
26
+ been modified to call #reload where full data is needed.
27
+
7
28
  ## [0.4.0] - 2019-12-27
8
29
  ### Added
9
30
  * Faraday middleware for authorization expiration and rate limit handling.
@@ -89,7 +110,7 @@ All notable changes to springcm-sdk will be documented in this file.
89
110
  ### Added
90
111
  * Initial release to reserve gem name
91
112
 
92
- [Unreleased]: https://github.com/paulholden2/springcm-sdk/compare/0.4.0...HEAD
113
+ [Unreleased]: https://github.com/paulholden2/springcm-sdk/compare/0.5.0...HEAD
93
114
  [0.1.0]: https://github.com/paulholden2/springcm-sdk/releases/tag/0.1.0
94
115
  [0.1.1]: https://github.com/paulholden2/springcm-sdk/releases/tag/0.1.1
95
116
  [0.1.2]: https://github.com/paulholden2/springcm-sdk/releases/tag/0.1.2
@@ -102,3 +123,4 @@ All notable changes to springcm-sdk will be documented in this file.
102
123
  [0.3.5]: https://github.com/paulholden2/springcm-sdk/releases/tag/0.3.5
103
124
  [0.3.6]: https://github.com/paulholden2/springcm-sdk/releases/tag/0.3.6
104
125
  [0.4.0]: https://github.com/paulholden2/springcm-sdk/releases/tag/0.4.0
126
+ [0.5.0]: https://github.com/paulholden2/springcm-sdk/releases/tag/0.5.0
@@ -6,7 +6,7 @@ module Springcm
6
6
  class AttributeGroup < Resource
7
7
  # Retrieve an attribute set by name.
8
8
  def set(name)
9
- res = @data["Attributes"].select { |attr|
9
+ res = attributes_config.select { |attr|
10
10
  attr["Attributes"].is_a?(Array) && attr["Name"] == name
11
11
  }
12
12
  return nil if !res.any?
@@ -38,7 +38,7 @@ module Springcm
38
38
  end
39
39
 
40
40
  def attributes
41
- @data["Attributes"].map { |attr|
41
+ attributes_config.map { |attr|
42
42
  if attr["Attributes"].is_a?(Array)
43
43
  attr["Attributes"]
44
44
  else
@@ -48,9 +48,16 @@ module Springcm
48
48
  end
49
49
 
50
50
  def sets
51
- @data["Attributes"].select { |attr|
51
+ attributes_config.select { |attr|
52
52
  attr["Attributes"].is_a?(Array)
53
53
  }
54
54
  end
55
+
56
+ protected
57
+
58
+ def attributes_config
59
+ @data = reload.raw if !@data.key?("Attributes")
60
+ @data["Attributes"]
61
+ end
55
62
  end
56
63
  end
@@ -11,7 +11,7 @@ module Springcm
11
11
 
12
12
  def self.resource_params
13
13
  {
14
- "expand" => "attributegroups"
14
+ "expand" => "attributegroups,path"
15
15
  }
16
16
  end
17
17
 
@@ -30,5 +30,20 @@ module Springcm
30
30
  nil
31
31
  end
32
32
  end
33
+
34
+ def delete!
35
+ unsafe_delete
36
+ end
37
+
38
+ alias_method :unsafe_delete, :delete
39
+ private :unsafe_delete
40
+
41
+ def delete
42
+ reload
43
+ if path.start_with?("/#{@client.account.name}/Trash")
44
+ raise Springcm::DeleteRefusedError.new(path)
45
+ end
46
+ unsafe_delete
47
+ end
33
48
  end
34
49
  end
@@ -11,6 +11,25 @@ module Springcm
11
11
  end
12
12
  end
13
13
 
14
+ def self.serialize_field(field_config, value)
15
+ type = field_config.type
16
+ repeating = field_config.repeating_attribute?
17
+ serialized = {
18
+ "AttributeType" => type,
19
+ "RepeatingAttribute" => repeating
20
+ }
21
+ serialized_value = nil
22
+ if type == "Date"
23
+ # Raise if value is not a DateTime
24
+ serialized_value = value.strftime("%m/%d/%Y")
25
+ else
26
+ serialized_value = value.to_s
27
+ end
28
+ return nil if serialized_value.nil?
29
+ serialized["Value"] = serialized_value
30
+ serialized
31
+ end
32
+
14
33
  # Deserialize a SpringCM attribute value
15
34
  def self.deserialize_field(field)
16
35
  type = field["AttributeType"]
@@ -4,16 +4,46 @@ module Springcm
4
4
  module Mixins
5
5
  # Mixin for objects that have attributes.
6
6
  module Attributes
7
+ def attribute_groups=(value)
8
+ @data["AttributeGroups"] = value
9
+ end
10
+
11
+ def set_attribute(group:, field:, index: nil, value:)
12
+ group_config = @client.account.all_attribute_groups.select { |g|
13
+ g.name == group
14
+ }.first
15
+ # Non-existent group
16
+ raise Springcm::NoAttributeGroupError.new(group) if group_config.nil?
17
+ field_config = group_config.field(field)
18
+ field_set_config = group_config.set_for_field(field)
19
+ # Non-existent field
20
+ raise Springcm::NoAttributeFieldError.new(group, field) if field_config.nil?
21
+ groups = attribute_groups || {}
22
+ group_data = groups.fetch(group, {})
23
+ # Repeating set
24
+ if !field_set_config.nil? && field_set_config["RepeatingAttribute"]
25
+ set_name = field_set_config["Name"]
26
+ set_data = group_data.fetch(set_name, {})
27
+ set_data["Items"].insert(index || -1, Springcm::Helpers.serialize_field(field_config, value))
28
+ group_data[set_name] = set_data
29
+ else
30
+ group_data[field] = Springcm::Helpers.serialize_field(field_config, value)
31
+ end
32
+ groups[group] = group_data
33
+ attribute_groups = groups
34
+ value
35
+ end
36
+
7
37
  def get_attribute(group:, field:)
8
38
  group_config = @client.account.all_attribute_groups.select { |g|
9
39
  g.name == group
10
40
  }.first
11
41
  # Non-existent group
12
- return nil if group_config.nil?
42
+ raise Springcm::NoAttributeGroupError.new(group) if group_config.nil?
13
43
  field_config = group_config.field(field)
14
44
  field_set_config = group_config.set_for_field(field)
15
45
  # Non-existent field
16
- return nil if field_config.nil?
46
+ raise Springcm::NoAttributeFieldError.new(group, field) if field_config.nil?
17
47
  groups = attribute_groups
18
48
  # No attribute groups applied
19
49
  return nil if groups.nil?
@@ -30,6 +30,38 @@ module Springcm
30
30
  end
31
31
  end
32
32
 
33
+ # Send a PATCH request for this resource.
34
+ def patch
35
+ conn = @client.authorized_connection(url: @client.object_api_url)
36
+ res = conn.patch do |req|
37
+ req.headers['Content-Type'] = "application/json"
38
+ req.url resource_uri
39
+ req.body = raw.to_json
40
+ end
41
+ if res.success?
42
+ data = JSON.parse(res.body)
43
+ self.class.new(data, @client)
44
+ else
45
+ nil
46
+ end
47
+ end
48
+
49
+ # Send a PUT request for this resource.
50
+ def put
51
+ conn = @client.authorized_connection(url: @client.object_api_url)
52
+ res = conn.put do |req|
53
+ req.headers['Content-Type'] = "application/json"
54
+ req.url resource_uri
55
+ req.body = raw.to_json
56
+ end
57
+ if res.success?
58
+ data = JSON.parse(res.body)
59
+ self.class.new(data, @client)
60
+ else
61
+ nil
62
+ end
63
+ end
64
+
33
65
  # Send a DELETE request for this resource.
34
66
  def delete
35
67
  conn = @client.authorized_connection(url: @client.object_api_url)
@@ -52,10 +84,14 @@ module Springcm
52
84
 
53
85
  # Some resources have query parameters that must be passed when
54
86
  # retrieving it, e.g. expand=attributegroups when retrieving a document.
55
- def resource_params
87
+ def self.resource_params
56
88
  {}
57
89
  end
58
90
 
91
+ def resource_params
92
+ self.class.resource_params
93
+ end
94
+
59
95
  # Pluralized resource name, e.g. documents or folders. Used to construct
60
96
  # request URLs.
61
97
  def resource_name
@@ -1,3 +1,3 @@
1
1
  module Springcm
2
- VERSION = "0.4.0"
2
+ VERSION = "0.5.0"
3
3
  end
data/lib/springcm-sdk.rb CHANGED
@@ -15,6 +15,18 @@ module Springcm
15
15
  class ConnectionInfoError < Error
16
16
  end
17
17
 
18
+ class NoAttributeGroupError < Error
19
+ def initialize(group)
20
+ super("No such attribute group: #{group}")
21
+ end
22
+ end
23
+
24
+ class NoAttributeFieldError < Error
25
+ def initialize(group, field)
26
+ super("No such attribute field: #{group}.#{field}")
27
+ end
28
+ end
29
+
18
30
  class InvalidClientIdOrSecretError < Error
19
31
  def initialize
20
32
  super("Invalid Client Id or Client Secret")
@@ -32,4 +44,10 @@ module Springcm
32
44
  super("Rate limit exceeded")
33
45
  end
34
46
  end
47
+
48
+ class DeleteRefusedError < Error
49
+ def initialize(path)
50
+ super("Refused to delete #{path}, use #delete! instead.")
51
+ end
52
+ end
35
53
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: springcm-sdk
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Paul Holden
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-12-27 00:00:00.000000000 Z
11
+ date: 2020-01-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -187,7 +187,7 @@ metadata:
187
187
  allowed_push_host: https://rubygems.org
188
188
  homepage_uri: https://github.com/paulholden2/springcm-sdk
189
189
  source_code_uri: https://github.com/paulholden2/springcm-sdk
190
- documentation_uri: https://rubydoc.info/github/paulholden2/springcm-sdk/0.4.0
190
+ documentation_uri: https://rubydoc.info/github/paulholden2/springcm-sdk/0.5.0
191
191
  changelog_uri: https://github.com/paulholden2/springcm-sdk/blob/master/CHANGELOG.md
192
192
  post_install_message:
193
193
  rdoc_options: []