knuverse-knufactor 0.0.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.
Files changed (37) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +11 -0
  3. data/.rspec +2 -0
  4. data/.rubocop.yml +14 -0
  5. data/.travis.yml +14 -0
  6. data/CONTRIBUTING.md +16 -0
  7. data/Gemfile +4 -0
  8. data/LICENSE +22 -0
  9. data/README.md +125 -0
  10. data/Rakefile +10 -0
  11. data/bin/console +14 -0
  12. data/bin/setup +8 -0
  13. data/knuverse-knufactor.gemspec +42 -0
  14. data/lib/core_extensions/string/transformations.rb +28 -0
  15. data/lib/knuverse/knufactor.rb +42 -0
  16. data/lib/knuverse/knufactor/api_client.rb +30 -0
  17. data/lib/knuverse/knufactor/api_client_base.rb +112 -0
  18. data/lib/knuverse/knufactor/api_exception.rb +7 -0
  19. data/lib/knuverse/knufactor/exceptions/api_client_not_configured.rb +9 -0
  20. data/lib/knuverse/knufactor/exceptions/immutable_modification.rb +9 -0
  21. data/lib/knuverse/knufactor/exceptions/invalid_arguments.rb +9 -0
  22. data/lib/knuverse/knufactor/exceptions/invalid_options.rb +9 -0
  23. data/lib/knuverse/knufactor/exceptions/invalid_property.rb +9 -0
  24. data/lib/knuverse/knufactor/exceptions/invalid_where_query.rb +9 -0
  25. data/lib/knuverse/knufactor/exceptions/missing_path.rb +9 -0
  26. data/lib/knuverse/knufactor/exceptions/new_instance_with_id.rb +9 -0
  27. data/lib/knuverse/knufactor/helpers/api_client.rb +36 -0
  28. data/lib/knuverse/knufactor/helpers/resource.rb +112 -0
  29. data/lib/knuverse/knufactor/helpers/resource_class.rb +49 -0
  30. data/lib/knuverse/knufactor/resource.rb +207 -0
  31. data/lib/knuverse/knufactor/resource_collection.rb +189 -0
  32. data/lib/knuverse/knufactor/resources/client.rb +44 -0
  33. data/lib/knuverse/knufactor/simple_api_client.rb +20 -0
  34. data/lib/knuverse/knufactor/validations/api_client.rb +42 -0
  35. data/lib/knuverse/knufactor/validations/resource.rb +17 -0
  36. data/lib/knuverse/knufactor/version.rb +10 -0
  37. metadata +220 -0
@@ -0,0 +1,189 @@
1
+ module KnuVerse
2
+ module Knufactor
3
+ # The ResourceCollection class
4
+ # Should not allow or use mixed types
5
+ class ResourceCollection
6
+ include Enumerable
7
+ include Comparable
8
+
9
+ # @return [Class] a collection of this {Resource} subclass
10
+ attr_reader :type
11
+
12
+ def initialize(list, options = {})
13
+ # TODO: better options validations
14
+ raise Exceptions::InvalidOptions unless options.is_a?(Hash)
15
+ raise Exceptions::InvalidArguments if list.empty? && options[:type].nil?
16
+ @api_client = options[:api_client] || APIClient.instance
17
+ @list = list
18
+ @type = options[:type] || list.first.class
19
+ end
20
+
21
+ def each(&block)
22
+ @list.each(&block)
23
+ end
24
+
25
+ # Does the collection contain anything?
26
+ # @return [Boolean]
27
+ def empty?
28
+ @list.empty?
29
+ end
30
+
31
+ # Provide the first (or first `n`) entries
32
+ # @param n [Fixnum] How many to provide
33
+ # @return [ResourceCollection,Resource]
34
+ def first(n = nil)
35
+ if n
36
+ self.class.new(@list.first(n), type: @type, api_client: @api_client)
37
+ else
38
+ @list.first
39
+ end
40
+ end
41
+
42
+ # Provide the last (or last `n`) entries
43
+ # @param n [Fixnum] How many to provide
44
+ # @return [ResourceCollection,Resource]
45
+ def last(n = nil)
46
+ if n
47
+ self.class.new(@list.last(n), type: @type, api_client: @api_client)
48
+ else
49
+ @list.last
50
+ end
51
+ end
52
+
53
+ # Merge two collections
54
+ # @param other [ResourceCollection]
55
+ # @return [ResourceCollection]
56
+ def merge(other)
57
+ raise Exceptions::InvalidArguments unless other.is_a?(self.class)
58
+ self + (other - self)
59
+ end
60
+
61
+ # An alias for {#type}
62
+ def model
63
+ type
64
+ end
65
+
66
+ # Hacked together #or() method in the same spirit as #where().
67
+ # This method can be chained for multiple / more specific queries.
68
+ #
69
+ # @param attribute [Symbol] the attribute to query
70
+ # @param value [Object] the value to compare against
71
+ # - allowed options are "'==', '!=', '>', '>=', '<', '<=', and 'match'"
72
+ # @raise [Exceptions::InvalidWhereQuery] if not the right kind of comparison
73
+ # @return [ResourceCollection]
74
+ def or(attribute, value, options = {})
75
+ options[:comparison] ||= value.is_a?(Regexp) ? :match : '=='
76
+ if empty?
77
+ @type.where(attribute, value, comparison: options[:comparison], api_client: @api_client)
78
+ else
79
+ merge first.class.where(
80
+ attribute, value,
81
+ comparison: options[:comparison],
82
+ api_client: @api_client
83
+ )
84
+ end
85
+ end
86
+
87
+ # Pass pagination through to the Array (which passes to will_paginate)
88
+ def paginate(*args)
89
+ @list.paginate(*args)
90
+ end
91
+
92
+ # Returns the number of Resource instances in the collection
93
+ # @return [Fixnum]
94
+ def size
95
+ @list.size
96
+ end
97
+
98
+ # Allow complex sorting like an Array
99
+ # @return [ResourceCollection] sorted collection
100
+ def sort(&block)
101
+ self.class.new(super(&block), type: @type, api_client: @api_client)
102
+ end
103
+
104
+ # Horribly inefficient way to allow querying Resources by their attributes.
105
+ # This method can be chained for multiple / more specific queries.
106
+ #
107
+ # @param attribute [Symbol] the attribute to query
108
+ # @param value [Object] the value to compare against
109
+ # - allowed options are "'==', '!=', '>', '>=', '<', '<=', and 'match'"
110
+ # @raise [Exceptions::InvalidWhereQuery] if not the right kind of comparison
111
+ # @return [ResourceCollection]
112
+ def where(attribute, value, options = {})
113
+ valid_comparisons = [:'==', :'!=', :>, :'>=', :<, :'<=', :match]
114
+ options[:comparison] ||= value.is_a?(Regexp) ? :match : '=='
115
+ unless valid_comparisons.include?(options[:comparison].to_sym)
116
+ raise Exceptions::InvalidWhereQuery
117
+ end
118
+ self.class.new(
119
+ @list.collect do |item|
120
+ if item.send(attribute).nil?
121
+ nil
122
+ elsif item.send(attribute).send(options[:comparison].to_sym, value)
123
+ item
124
+ end
125
+ end.compact,
126
+ type: @type,
127
+ api_client: @api_client
128
+ )
129
+ end
130
+
131
+ alias and where
132
+
133
+ # Return the collection item at the specified index
134
+ # @return [Resource,ResourceCollection] the item at the requested index
135
+ def [](index)
136
+ if index.is_a?(Range)
137
+ self.class.new(@list[index], type: @type, api_client: @api_client)
138
+ else
139
+ @list[index]
140
+ end
141
+ end
142
+
143
+ # Return a collection after subtracting from the original
144
+ # @return [ResourceCollection]
145
+ def -(other)
146
+ if other.respond_to?(:to_a)
147
+ self.class.new(@list - other.to_a, type: @type, api_client: @api_client)
148
+ elsif other.is_a?(Resource)
149
+ self.class.new(@list - Array(other), type: @type, api_client: @api_client)
150
+ else
151
+ raise Exceptions::InvalidArguments
152
+ end
153
+ end
154
+
155
+ # Return a collection after adding to the original
156
+ # Warning: this may cause duplicates or mixed type joins! For safety,
157
+ # use #merge
158
+ # @return [ResourceCollection]
159
+ def +(other)
160
+ if other.is_a?(self.class)
161
+ self.class.new(@list + other.to_a, type: @type, api_client: @api_client)
162
+ elsif other.is_a?(@type)
163
+ self.class.new(@list + [other], type: @type, api_client: @api_client)
164
+ else
165
+ raise Exceptions::InvalidArguments
166
+ end
167
+ end
168
+
169
+ def <<(other)
170
+ raise Exceptions::InvalidArguments, 'Resource Type Mismatch' unless other.class == @type
171
+ @list << other
172
+ end
173
+
174
+ def <=>(other)
175
+ collect(&:id).sort <=> other.collect(&:id).sort
176
+ end
177
+
178
+ # Allow comparison of collection
179
+ # @return [Boolean] do the collections contain the same resource ids?
180
+ def ==(other)
181
+ if other.is_a? self.class
182
+ collect(&:id).sort == other.collect(&:id).sort
183
+ else
184
+ false
185
+ end
186
+ end
187
+ end
188
+ end
189
+ end
@@ -0,0 +1,44 @@
1
+ module KnuVerse
2
+ module Knufactor
3
+ module Resources
4
+ # The Knufactor Client resource
5
+ # rubocop:disable Style/ExtraSpacing
6
+ class Client < Resource
7
+ property :bypass_expiration
8
+ property :bypass_limit
9
+ # @return [Boolean]
10
+ # @!method bypass_require_code?
11
+ property :bypass_require_code, type: :boolean, read_only: true
12
+ property :bypass_require_pin, type: :boolean
13
+ property :bypass_spacing_minutes
14
+ property :client_id, id_property: true
15
+ property :disable_clients_global, type: :boolean
16
+ property :email_verified, type: :boolean, read_only: true
17
+ property :enroll_date, type: :time, read_only: true
18
+ property :enroll_deadline_date, type: :time
19
+ property :enroll_deadline_remaining_minutes
20
+ property :has_password, type: :boolean, read_only: true
21
+ property :has_pin, type: :boolean, read_only: true
22
+ property :has_verified, type: :boolean, read_only: true
23
+ property :help_tip, read_only: true
24
+ property :is_disabled, type: :boolean
25
+ property :is_gauth, type: :boolean, read_only: true
26
+ property :is_tenant_client, type: :boolean, read_only: true
27
+ property :last_verification_date, type: :time, read_only: true
28
+ property :name, read_only: true
29
+ property :notification, read_only: true
30
+ property :password, write_only: true
31
+ property :password_lock, type: :boolean
32
+ property :phone_number_last, read_only: true
33
+ property :pin_rev, read_only: true
34
+ property :role
35
+ property :role_rationale
36
+ property :row_doubling
37
+ property :state, read_only: true
38
+ property :verification_lock, type: :boolean
39
+ property :verification_speed
40
+ property :verification_speed_floor, read_only: true
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,20 @@
1
+ module KnuVerse
2
+ module Knufactor
3
+ # The Simple API Client class
4
+ class SimpleAPIClient < APIClientBase
5
+ def initialize(opts = {})
6
+ # validations
7
+ validate_opts(opts)
8
+
9
+ # Set up the client's state
10
+ @server = opts[:server] || 'https://cloud.knuverse.com'
11
+ @apikey = opts[:apikey]
12
+ @secret = opts[:secret]
13
+ @base_uri = opts[:base_uri] || '/api/v1/'
14
+ @last_auth = nil
15
+ @auth_token = nil
16
+ @configured = true
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,42 @@
1
+ module KnuVerse
2
+ module Knufactor
3
+ module Validations
4
+ # APIClient validation methods
5
+ module APIClient
6
+ def validate_creds(opts, exception)
7
+ raise(exception, 'Must Provide Auth') unless opts.key?(:apikey) && opts.key?(:secret)
8
+ end
9
+
10
+ # Validate the options passed on initialize
11
+ def validate_opts(opts)
12
+ e = Exceptions::InvalidOptions
13
+ raise(e, 'Is not Hash-like') unless opts.respond_to?(:[]) && opts.respond_to?(:key?)
14
+ validate_server(opts[:server])
15
+ if opts.key?(:base_uri)
16
+ begin
17
+ URI.parse(opts[:base_uri])
18
+ rescue URI::InvalidURIError => e
19
+ raise(e, "Invalid base_uri: #{e}")
20
+ end
21
+ end
22
+
23
+ validate_creds(opts, e)
24
+ true
25
+ end
26
+
27
+ # Validate the server name provided
28
+ def validate_server(name)
29
+ return true unless name # if no name is provided, just go with the defaults
30
+ valid = name.is_a? String
31
+ begin
32
+ u = URI.parse(name)
33
+ valid = false unless u.class == URI::HTTP || u.class == URI::HTTPS
34
+ rescue URI::InvalidURIError
35
+ valid = false
36
+ end
37
+ valid || raise(Exceptions::InvalidOptions, 'Invalid server')
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,17 @@
1
+ module KnuVerse
2
+ module Knufactor
3
+ module Validations
4
+ # Resource validation methods
5
+ module Resource
6
+ def validate_mutability
7
+ raise Exceptions::ImmutableModification if immutable? && @tainted # this shouldn't happen
8
+ end
9
+
10
+ # The 'id' field should not be set manually
11
+ def validate_id
12
+ raise Exceptions::NewInstanceWithID if @entity.key?('id') && @tainted
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,10 @@
1
+ module KnuVerse
2
+ module Knufactor
3
+ # The current version of the app
4
+ VERSION = [
5
+ 0, # Major
6
+ 0, # Minor
7
+ 1, # Patch
8
+ ].join('.')
9
+ end
10
+ end
metadata ADDED
@@ -0,0 +1,220 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: knuverse-knufactor
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Jonathan Gnagy
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-11-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: linguistics
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rest-client
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: will_paginate
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.1'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.1'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.12'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.12'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '10.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '10.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '3.1'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '3.1'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rubocop
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '0.35'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '0.35'
111
+ - !ruby/object:Gem::Dependency
112
+ name: yard
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '0.8'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '0.8'
125
+ - !ruby/object:Gem::Dependency
126
+ name: simplecov
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: travis
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - "~>"
144
+ - !ruby/object:Gem::Version
145
+ version: '1.8'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - "~>"
151
+ - !ruby/object:Gem::Version
152
+ version: '1.8'
153
+ description:
154
+ email:
155
+ - jgnagy@knuedge.com
156
+ executables: []
157
+ extensions: []
158
+ extra_rdoc_files: []
159
+ files:
160
+ - ".gitignore"
161
+ - ".rspec"
162
+ - ".rubocop.yml"
163
+ - ".travis.yml"
164
+ - CONTRIBUTING.md
165
+ - Gemfile
166
+ - LICENSE
167
+ - README.md
168
+ - Rakefile
169
+ - bin/console
170
+ - bin/setup
171
+ - knuverse-knufactor.gemspec
172
+ - lib/core_extensions/string/transformations.rb
173
+ - lib/knuverse/knufactor.rb
174
+ - lib/knuverse/knufactor/api_client.rb
175
+ - lib/knuverse/knufactor/api_client_base.rb
176
+ - lib/knuverse/knufactor/api_exception.rb
177
+ - lib/knuverse/knufactor/exceptions/api_client_not_configured.rb
178
+ - lib/knuverse/knufactor/exceptions/immutable_modification.rb
179
+ - lib/knuverse/knufactor/exceptions/invalid_arguments.rb
180
+ - lib/knuverse/knufactor/exceptions/invalid_options.rb
181
+ - lib/knuverse/knufactor/exceptions/invalid_property.rb
182
+ - lib/knuverse/knufactor/exceptions/invalid_where_query.rb
183
+ - lib/knuverse/knufactor/exceptions/missing_path.rb
184
+ - lib/knuverse/knufactor/exceptions/new_instance_with_id.rb
185
+ - lib/knuverse/knufactor/helpers/api_client.rb
186
+ - lib/knuverse/knufactor/helpers/resource.rb
187
+ - lib/knuverse/knufactor/helpers/resource_class.rb
188
+ - lib/knuverse/knufactor/resource.rb
189
+ - lib/knuverse/knufactor/resource_collection.rb
190
+ - lib/knuverse/knufactor/resources/client.rb
191
+ - lib/knuverse/knufactor/simple_api_client.rb
192
+ - lib/knuverse/knufactor/validations/api_client.rb
193
+ - lib/knuverse/knufactor/validations/resource.rb
194
+ - lib/knuverse/knufactor/version.rb
195
+ homepage: https://github.com/KnuVerse/knuverse-knufactor-sdk-ruby
196
+ licenses:
197
+ - MIT
198
+ metadata:
199
+ yard.run: yri
200
+ post_install_message: Thanks for installing the KnuVerse Knufactor Ruby SDK!
201
+ rdoc_options: []
202
+ require_paths:
203
+ - lib
204
+ required_ruby_version: !ruby/object:Gem::Requirement
205
+ requirements:
206
+ - - "~>"
207
+ - !ruby/object:Gem::Version
208
+ version: '2.0'
209
+ required_rubygems_version: !ruby/object:Gem::Requirement
210
+ requirements:
211
+ - - ">="
212
+ - !ruby/object:Gem::Version
213
+ version: '0'
214
+ requirements: []
215
+ rubyforge_project:
216
+ rubygems_version: 2.4.8
217
+ signing_key:
218
+ specification_version: 4
219
+ summary: KnuVerse Knufactor Ruby SDK
220
+ test_files: []