gifnoc 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,71 @@
1
+ module Gifnoc
2
+ class Model
3
+
4
+ extend TypedAttrAccessor
5
+
6
+ def self.attributes_hash
7
+ return const_get(:ATTRIBUTES)
8
+ end
9
+
10
+ def self.attribute_names
11
+ return self.attributes_hash.keys
12
+ end
13
+
14
+ def self.api_attribute_names
15
+ return self.attribute_names & const_get(:API_ATTRIBUTE_LIST)
16
+ end
17
+
18
+ def self.api_attributes_hash
19
+ return self.attributes_hash.select { |k, _| self.api_attribute_names.include?(k) }
20
+ end
21
+
22
+ def initialize(**args)
23
+ raise NotImplementedError if self.class == Model
24
+ self.class.validate_attributes(args)
25
+ self.class.attribute_names.each { |arg| self.send("#{arg}=", args[arg]) }
26
+ end
27
+
28
+ def to_api_hash
29
+ return self.class.api_attributes_hash.inject({}) do |hash, (name, type)|
30
+ value = self.send(name)
31
+ value = value&.to_s if type == TypedAttrAccessor::TYPE_DATE_TIME
32
+ hash[name.to_s] = value
33
+ next(hash)
34
+ end
35
+ end
36
+
37
+ def self.from_api_hash(api_hash)
38
+ validate_api_attributes(api_hash)
39
+ args = api_hash.inject({}) do |hash, (key, value)|
40
+ case api_attributes_hash[key.to_sym]
41
+ when TypedAttrAccessor::TYPE_DATE_TIME
42
+ hash[key.to_sym] = value.nil? ? value : DateTime.iso8601(value)
43
+ else
44
+ hash[key.to_sym] = value
45
+ end
46
+ hash
47
+ end
48
+ return new(**args)
49
+ end
50
+
51
+ private
52
+
53
+ def self.validate_api_attributes(args)
54
+ return validate_attributes_internal(args, self.api_attribute_names)
55
+ end
56
+
57
+ def self.validate_attributes(args)
58
+ return validate_attributes_internal(args, self.attribute_names)
59
+ end
60
+
61
+ # method should be considered private, but it's called from both an instance method and a class method
62
+ def self.validate_attributes_internal(args, allowed_arguments)
63
+ current_arguments = args.keys.map(&:to_sym)
64
+ allowed_arguments = allowed_arguments.map(&:to_sym)
65
+ if (current_arguments | allowed_arguments).size > allowed_arguments.size
66
+ raise ArgumentError, "incorrect arguments: #{self.name} has the following attributes: #{allowed_arguments}"
67
+ end
68
+ end
69
+
70
+ end
71
+ end
@@ -0,0 +1,20 @@
1
+ module Gifnoc
2
+ class Namespace < Model
3
+
4
+ ATTRIBUTES = {
5
+ uuid: TypedAttrAccessor::TYPE_STRING,
6
+ name: TypedAttrAccessor::TYPE_STRING,
7
+ delete_protection: TypedAttrAccessor::TYPE_BOOLEAN,
8
+ record_count: TypedAttrAccessor::TYPE_INTEGER,
9
+ created_at: TypedAttrAccessor::TYPE_DATE_TIME,
10
+ updated_at: TypedAttrAccessor::TYPE_DATE_TIME,
11
+ }.deep_freeze
12
+
13
+ API_ATTRIBUTE_LIST = [:uuid, :name, :delete_protection, :record_count, :created_at, :updated_at].deep_freeze
14
+
15
+ attributes_hash.each do |name, type_alias|
16
+ typed_attr_accessor(name, type_alias)
17
+ end
18
+
19
+ end
20
+ end
@@ -0,0 +1,53 @@
1
+ require "base64"
2
+
3
+ module Gifnoc
4
+ class Record < Model
5
+
6
+ ATTRIBUTES = {
7
+ uuid: TypedAttrAccessor::TYPE_STRING,
8
+ key: TypedAttrAccessor::TYPE_STRING,
9
+ raw_value: TypedAttrAccessor::TYPE_BASE64,
10
+ metadata: TypedAttrAccessor::TYPE_HASH,
11
+ ttl: TypedAttrAccessor::TYPE_INTEGER,
12
+ created_at: TypedAttrAccessor::TYPE_DATE_TIME,
13
+ updated_at: TypedAttrAccessor::TYPE_DATE_TIME,
14
+ last_access: TypedAttrAccessor::TYPE_DATE_TIME,
15
+ value: TypedAttrAccessor::TYPE_STRING,
16
+ }.deep_freeze
17
+
18
+ API_ATTRIBUTE_LIST = [:uuid, :key, :raw_value, :metadata, :ttl, :created_at, :updated_at, :last_access].deep_freeze
19
+
20
+ attributes_hash.each do |name, type_alias|
21
+ typed_attr_accessor(name, type_alias)
22
+ end
23
+
24
+ # keep raw_value and value in sync, allow calling super for typed_attr_accessor
25
+ module SetValueSyncer
26
+ def raw_value=(new_raw_value)
27
+ super(new_raw_value)
28
+ new_value = Base64.strict_decode64(new_raw_value) unless new_raw_value.nil?
29
+ instance_variable_set("@value", new_value)
30
+ return new_raw_value
31
+ end
32
+
33
+ def value=(new_value)
34
+ super(new_value)
35
+ new_raw_value = Base64.strict_encode64(new_value) unless new_value.nil?
36
+ instance_variable_set("@raw_value", new_raw_value)
37
+ return new_value
38
+ end
39
+
40
+ def initialize(**args)
41
+ if args.has_key?(:raw_value) && args.has_key?(:value)
42
+ raise ArgumentError, "only one of the :raw_value and :value attributes should be provided"
43
+ end
44
+ super(**args)
45
+ self.raw_value = args[:raw_value] if args.has_key?(:raw_value)
46
+ self.value = args[:value] if args.has_key?(:value)
47
+ end
48
+ end
49
+ private_constant :SetValueSyncer
50
+ prepend SetValueSyncer
51
+
52
+ end
53
+ end
@@ -0,0 +1,20 @@
1
+ module Gifnoc
2
+ class Token < Model
3
+
4
+ ATTRIBUTES = {
5
+ uuid: TypedAttrAccessor::TYPE_STRING,
6
+ description: TypedAttrAccessor::TYPE_STRING,
7
+ secret: TypedAttrAccessor::TYPE_STRING,
8
+ expiry: TypedAttrAccessor::TYPE_DATE_TIME,
9
+ created_at: TypedAttrAccessor::TYPE_DATE_TIME,
10
+ updated_at: TypedAttrAccessor::TYPE_DATE_TIME,
11
+ }.deep_freeze
12
+
13
+ API_ATTRIBUTE_LIST = [:uuid, :description, :secret, :expiry, :created_at, :updated_at].deep_freeze
14
+
15
+ attributes_hash.each do |name, type_alias|
16
+ typed_attr_accessor(name, type_alias)
17
+ end
18
+
19
+ end
20
+ end
@@ -0,0 +1,62 @@
1
+ require "base64"
2
+
3
+ module Gifnoc
4
+ module TypedAttrAccessor
5
+
6
+ TYPE_STRING = :string
7
+ TYPE_DATE_TIME = :date_time
8
+ TYPE_BOOLEAN = :boolean
9
+ TYPE_INTEGER = :integer
10
+ TYPE_HASH = :hash
11
+ TYPE_BASE64 = :base64
12
+
13
+ def typed_attr_accessor(name, type_alias)
14
+ define_method(name) do
15
+ instance_variable_get("@#{name}")
16
+ end
17
+
18
+ define_method("#{name}=") do |value|
19
+ TypedAttrAccessor.type_checks(type_alias, value)
20
+ TypedAttrAccessor.type_specific_checks(type_alias, value)
21
+ instance_variable_set("@#{name}", value)
22
+ end
23
+ end
24
+
25
+ def self.allowed_types(type_alias)
26
+ allowed_types = []
27
+ allowed_types += [NilClass]
28
+ allowed_types += type_alias_to_type_array(type_alias)
29
+ return allowed_types
30
+ end
31
+
32
+ def self.type_alias_to_type_array(type_alias)
33
+ case type_alias
34
+ when TYPE_STRING
35
+ return [String]
36
+ when TYPE_BASE64
37
+ return [String]
38
+ when TYPE_DATE_TIME
39
+ return [DateTime]
40
+ when TYPE_BOOLEAN
41
+ return [TrueClass, FalseClass]
42
+ when TYPE_INTEGER
43
+ return [Integer]
44
+ when TYPE_HASH
45
+ return [Hash]
46
+ end
47
+ end
48
+
49
+ def self.type_checks(type_alias, value)
50
+ allowed_types = self.allowed_types(type_alias)
51
+ raise ArgumentError.new("Invalid Type (allowed #{ allowed_types.inspect })") unless allowed_types.any? { |type| value.is_a?(type) }
52
+ end
53
+
54
+ def self.type_specific_checks(type_alias, value)
55
+ case type_alias
56
+ when TYPE_BASE64
57
+ Base64.strict_decode64(value) if value # raises ArgumentError (invalid base64) if value is not a Base64 string
58
+ end
59
+ end
60
+
61
+ end
62
+ end
@@ -0,0 +1,3 @@
1
+ module Gifnoc
2
+ VERSION = '0.0.1'
3
+ end
metadata ADDED
@@ -0,0 +1,213 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gifnoc
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Cloud 66
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-11-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry-byebug
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.6'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.6'
69
+ - !ruby/object:Gem::Dependency
70
+ name: webmock
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.4'
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: 3.4.2
79
+ type: :development
80
+ prerelease: false
81
+ version_requirements: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - "~>"
84
+ - !ruby/object:Gem::Version
85
+ version: '3.4'
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: 3.4.2
89
+ - !ruby/object:Gem::Dependency
90
+ name: vcr
91
+ requirement: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - "~>"
94
+ - !ruby/object:Gem::Version
95
+ version: '4.0'
96
+ type: :development
97
+ prerelease: false
98
+ version_requirements: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - "~>"
101
+ - !ruby/object:Gem::Version
102
+ version: '4.0'
103
+ - !ruby/object:Gem::Dependency
104
+ name: simplecov
105
+ requirement: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - "~>"
108
+ - !ruby/object:Gem::Version
109
+ version: 0.16.1
110
+ type: :development
111
+ prerelease: false
112
+ version_requirements: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - "~>"
115
+ - !ruby/object:Gem::Version
116
+ version: 0.16.1
117
+ - !ruby/object:Gem::Dependency
118
+ name: httparty
119
+ requirement: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - "~>"
122
+ - !ruby/object:Gem::Version
123
+ version: 0.16.2
124
+ type: :runtime
125
+ prerelease: false
126
+ version_requirements: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - "~>"
129
+ - !ruby/object:Gem::Version
130
+ version: 0.16.2
131
+ - !ruby/object:Gem::Dependency
132
+ name: ice_nine
133
+ requirement: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - "~>"
136
+ - !ruby/object:Gem::Version
137
+ version: 0.11.2
138
+ type: :runtime
139
+ prerelease: false
140
+ version_requirements: !ruby/object:Gem::Requirement
141
+ requirements:
142
+ - - "~>"
143
+ - !ruby/object:Gem::Version
144
+ version: 0.11.2
145
+ description:
146
+ email:
147
+ - hello@cloud66.com
148
+ executables:
149
+ - console
150
+ - setup
151
+ extensions: []
152
+ extra_rdoc_files: []
153
+ files:
154
+ - ".editorconfig"
155
+ - ".gitignore"
156
+ - ".ruby-gemset"
157
+ - ".ruby-version"
158
+ - CHANGELOG
159
+ - Gemfile
160
+ - Gemfile.lock
161
+ - LICENSE
162
+ - README.md
163
+ - Rakefile
164
+ - bin/console
165
+ - bin/setup
166
+ - gifnoc.gemspec
167
+ - lib/gifnoc.rb
168
+ - lib/gifnoc/apis.rb
169
+ - lib/gifnoc/apis/account_api_methods.rb
170
+ - lib/gifnoc/apis/api.rb
171
+ - lib/gifnoc/apis/namespace_api_methods.rb
172
+ - lib/gifnoc/apis/record_api_methods.rb
173
+ - lib/gifnoc/apis/token_api_methods.rb
174
+ - lib/gifnoc/clients.rb
175
+ - lib/gifnoc/clients/account_client_methods.rb
176
+ - lib/gifnoc/clients/client.rb
177
+ - lib/gifnoc/clients/namespace_client_methods.rb
178
+ - lib/gifnoc/clients/record_client_methods.rb
179
+ - lib/gifnoc/clients/token_client_methods.rb
180
+ - lib/gifnoc/exceptions.rb
181
+ - lib/gifnoc/models.rb
182
+ - lib/gifnoc/models/account.rb
183
+ - lib/gifnoc/models/model.rb
184
+ - lib/gifnoc/models/namespace.rb
185
+ - lib/gifnoc/models/record.rb
186
+ - lib/gifnoc/models/token.rb
187
+ - lib/gifnoc/models/typed_attr_accessor.rb
188
+ - lib/gifnoc/version.rb
189
+ homepage: https://github.com/cloud66-oss/gifnoc-ruby
190
+ licenses:
191
+ - Apache-2.0
192
+ metadata: {}
193
+ post_install_message:
194
+ rdoc_options: []
195
+ require_paths:
196
+ - lib
197
+ required_ruby_version: !ruby/object:Gem::Requirement
198
+ requirements:
199
+ - - ">="
200
+ - !ruby/object:Gem::Version
201
+ version: '0'
202
+ required_rubygems_version: !ruby/object:Gem::Requirement
203
+ requirements:
204
+ - - ">="
205
+ - !ruby/object:Gem::Version
206
+ version: '0'
207
+ requirements: []
208
+ rubyforge_project:
209
+ rubygems_version: 2.7.7
210
+ signing_key:
211
+ specification_version: 4
212
+ summary: A Ruby Gifnoc client implementation
213
+ test_files: []