ubersicht-ruby-sdk 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: 6c517f3a08f30c20b2f5f3a219547519c797a91b2b8bc167dc838b24607a4d9f
4
- data.tar.gz: dce099aceebd7d36c2dcac19ed007a2f8cf432db81d0ddbe5c5b14a3744327ad
3
+ metadata.gz: 4b8ecf09cd444b1b7a991f05714a5bc9b0c76013a916e9ab3d52671c5f97550f
4
+ data.tar.gz: d767066e3bdbe8d2781de2f47d05e685c467c2dfbd3a898e9646efbd7bb12384
5
5
  SHA512:
6
- metadata.gz: 0f62f52d9d9ff24342861466cd4d2ebdefd193cb7940e051152b77d0a62f68d8b8f9df5658ed53db5bd6bb1d75b235c96d4764c7ebbd391618594e89866a9853
7
- data.tar.gz: 833078838952d0a4fb62f656578f0d7c351a77e8869c2dbc920d7a6695de142926596d9bf0e11658bf8c9d0b7d69747c631d09aa504bcca983268295bd488fee
6
+ metadata.gz: b934e49a27e2edae6b96b3829610d47be0dd496ecb03e0b507d518acfa3cb03f528356b9f57ad12cccf02d4b2aa6cf09a0c50159c3d5678975241984f4d76920
7
+ data.tar.gz: 8c4fece1fd162df31c5ddbbdcb8201b3dc1012c6e0b41fbb0939b165d718fe9f552cfcc05f7eead8df25634e0ee2b8cb25209846cf8a839a8f55119d69665b0d
data/CHANGELOG.md CHANGED
@@ -1,11 +1,23 @@
1
1
  ## Not released
2
+
3
+ ## 0.3.0
4
+
5
+ Breaking changes:
6
+ - [ingestion] replace basic auth with token based authentication
7
+ - [ingestion] changed singnature from keywords to arguments in `ingest` method
8
+ - [ingestion] removed username from client constructor
9
+ - [ingestion] removed password from client constructor
10
+ - [ingestion] removed account_id from client constructor
11
+ - [ingestion] removed HMAC key from client constructor
12
+ - [ingestion] removed provider key from client constructor
13
+
2
14
  ## 0.2.0
3
15
 
4
16
  - [ingestion] added `event.payload.event_id` to prevent events duplication on Ubersicht side
5
17
  - [ingestion] added `event.payload.event_id` to event signature
6
18
  - [ingestion] added `debug` option to ingestion client. If false then request will be ran in a new thread.
7
19
 
8
- Braking changes:
20
+ Breaking changes:
9
21
 
10
22
  - [ingestion] replaced `ingest` signature to keywords
11
23
  - [ingestion] renamed `event.type` with `event.transaction_type`
data/Gemfile.lock CHANGED
@@ -1,8 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- ubersicht-ruby-sdk (0.2.0)
5
- dry-struct (~> 1.0)
4
+ ubersicht-ruby-sdk (0.3.0)
6
5
  faraday (>= 0.9.2, < 2)
7
6
 
8
7
  GEM
@@ -12,33 +11,10 @@ GEM
12
11
  public_suffix (>= 2.0.2, < 5.0)
13
12
  ast (2.4.2)
14
13
  coderay (1.1.3)
15
- concurrent-ruby (1.1.9)
16
14
  crack (0.4.5)
17
15
  rexml
18
16
  diff-lcs (1.4.4)
19
17
  docile (1.4.0)
20
- dry-configurable (0.12.1)
21
- concurrent-ruby (~> 1.0)
22
- dry-core (~> 0.5, >= 0.5.0)
23
- dry-container (0.8.0)
24
- concurrent-ruby (~> 1.0)
25
- dry-configurable (~> 0.1, >= 0.1.3)
26
- dry-core (0.7.1)
27
- concurrent-ruby (~> 1.0)
28
- dry-inflector (0.2.1)
29
- dry-logic (1.2.0)
30
- concurrent-ruby (~> 1.0)
31
- dry-core (~> 0.5, >= 0.5)
32
- dry-struct (1.4.0)
33
- dry-core (~> 0.5, >= 0.5)
34
- dry-types (~> 1.5)
35
- ice_nine (~> 0.11)
36
- dry-types (1.5.1)
37
- concurrent-ruby (~> 1.0)
38
- dry-container (~> 0.3)
39
- dry-core (~> 0.5, >= 0.5)
40
- dry-inflector (~> 0.1, >= 0.1.2)
41
- dry-logic (~> 1.0, >= 1.0.2)
42
18
  faraday (1.7.1)
43
19
  faraday-em_http (~> 1.0)
44
20
  faraday-em_synchrony (~> 1.0)
@@ -59,7 +35,6 @@ GEM
59
35
  faraday-patron (1.0.0)
60
36
  faraday-rack (1.0.0)
61
37
  hashdiff (1.0.1)
62
- ice_nine (0.11.2)
63
38
  method_source (1.0.0)
64
39
  multipart-post (2.1.1)
65
40
  parallel (1.20.1)
data/README.md CHANGED
@@ -6,6 +6,12 @@ This is the officially supported Ruby library for using Ubersicht's APIs.
6
6
 
7
7
  Add this line to your application's Gemfile:
8
8
 
9
+ ```ruby
10
+ gem 'ubersicht-ruby-sdk', '0.2.0', require: 'ubersicht'
11
+ ```
12
+
13
+ The latest `main` branch:
14
+
9
15
  ```ruby
10
16
  gem 'ubersicht-ruby-sdk', git: 'https://github.com/starfish-codes/ubersicht-ruby-sdk.git',
11
17
  branch: 'main', require: 'ubersicht'
@@ -27,39 +33,31 @@ Build client:
27
33
 
28
34
  ```sh
29
35
  client = Ubersicht::Ingestion::Client.new(
30
- hmac_key: '44782DEF547AAA06C910C43932B1EB0C71FC68D9D0C057550C48EC2ACF6BA056',
31
- account_id: '1001',
32
- pass: 'password',
33
- provider: 'DAuth',
36
+ token: '<ubersicht-plugin-token>',
34
37
  url: '<api-base-url>',
35
- user: 'user',
36
38
  debug: true
37
39
  )
38
40
  ```
39
41
 
40
42
  Parameters:
41
43
 
42
- * `hmac_key` - (required) used for signing notification on client side and validation on server side (Ubersicht plugin config)
43
- * `account_id` - (required) account which receives notification events
44
- * `pass` - (required) basic auth password (Ubersicht plugin config)
45
- * `user` - (required) basic auth username (Ubersicht plugin config)
46
- * `provider` - (required) plugin provider in Ubersicht platform, e.g. DAuth
44
+ * `token` - (required) authentication token for Ubersicth plugin.
47
45
  * `url` - (required) Ubersicht API root url. Different for production and testing environment.
48
46
  * `debug` - (optional) default=false. If true then request is performed in the current thread, if false then new thread is created.
49
47
 
50
48
  Send event:
51
49
 
52
50
  ```sh
53
- event = {
54
- # required
55
- transaction_type: 'DeviceBinding'
56
- event_code: 'REQUESTED',
57
- # optional
51
+ # required
52
+ transaction_type = 'DeviceBinding'
53
+ event_code = 'REQUESTED'
54
+
55
+ payload = {
58
56
  event_date: 2021-10-10 10:10:10,
59
57
  event_group_id: 'eb2bc8bb-f584-4801-b98c-361a0c2d38f8',
60
- event_id: 'ed62d0c1-f2a5-41b7-ab58-24c033eec508',,
58
+ event_id: 'ed62d0c1-f2a5-41b7-ab58-24c033eec508',
61
59
  }
62
- client.ingest(**event)
60
+ client.ingest(transaction_type, event_code, payload)
63
61
  ```
64
62
 
65
63
  Event attributes:
@@ -2,54 +2,43 @@ module Ubersicht
2
2
  module Ingestion
3
3
  class Client
4
4
  def self.default(*args)
5
+ require 'logger'
6
+
5
7
  new(*args) do |faraday|
6
8
  faraday.request :url_encoded
7
9
  # faraday.response :raise_error
8
- faraday.response :logger, Logger.new($stdout),
10
+ faraday.response :logger, ::Logger.new($stdout),
9
11
  headers: { request: false, response: false },
10
12
  bodies: { request: true, response: true }
11
13
  faraday.adapter Faraday.default_adapter # make requests with Net::HTTP
12
14
  end
13
15
  end
14
16
 
15
- def initialize(options = {}, &block) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength, Metrics/CyclomaticComplexity
16
- raise ArgumentError, 'Account id cannot be blank' if empty?(account_id = options[:account_id])
17
- raise ArgumentError, 'Hmac key cannot be blank' if empty?(hmac_key = options[:hmac_key])
18
- raise ArgumentError, 'Password cannot be blank' if empty?(pass = options[:pass])
17
+ def initialize(options = {}, &block)
18
+ raise ArgumentError, 'Token cannot be blank' if empty?(token = options[:token])
19
19
  raise ArgumentError, 'Url cannot be blank' if empty?(url = options[:url])
20
- raise ArgumentError, 'Username cannot be blank' if empty?(user = options[:user])
21
- unless ::Ubersicht::Ingestion::PROVIDERS.include?(provider = options[:provider])
22
- raise ArgumentError, "Not supported provider '#{provider}'"
23
- end
24
20
 
25
- @account_id = account_id
26
21
  @debug = options[:debug] || false
27
- @hmac_key = hmac_key
28
- @provider = provider
29
- @conn = setup_conn(url, user, pass, &block)
22
+ @conn = setup_conn(url, token, &block)
30
23
  end
31
24
 
32
- def ingest(transaction_type:, event_code:, event_date: Time.now, **payload)
33
- event = {
34
- event_code: event_code,
35
- event_date: event_date.iso8601(3),
36
- payload: payload,
37
- transaction_type: transaction_type
25
+ def ingest(transaction_type, event_code, payload = {})
26
+ raise ArgumentError, 'Transaction type cannot be blank' if empty?(transaction_type)
27
+ raise ArgumentError, 'Event code cannot be blank' if empty?(event_code)
28
+
29
+ body = {
30
+ event: {
31
+ event_code: event_code,
32
+ transaction_type: transaction_type,
33
+ payload: payload
34
+ }
38
35
  }
39
- ingest_events([event])
36
+ process { handle_response(@conn.post('api/v1/events', body.to_json)) }
40
37
  end
41
38
 
42
39
  private
43
40
 
44
- attr_reader :account_id, :debug, :hmac_key, :provider
45
-
46
- def ingest_events(events)
47
- body = {
48
- events: events.map { |event| ::Ubersicht::Ingestion::BuildIngestionEvent.call(event, hmac_key, provider) }
49
- }
50
- url = "accounts/#{account_id}/plugins/#{provider.downcase}/notification_webhooks"
51
- process { handle_response(@conn.post(url, body.to_json)) }
52
- end
41
+ attr_reader :debug
53
42
 
54
43
  def process(&block)
55
44
  return yield if debug
@@ -71,14 +60,14 @@ module Ubersicht
71
60
  value.to_s.empty?
72
61
  end
73
62
 
74
- def setup_conn(url, user, pass)
63
+ def setup_conn(url, token)
75
64
  headers = {
76
65
  'Content-Type' => 'application/json'
77
66
  }
78
- Faraday.new(url: url, headers: headers) do |faraday|
79
- faraday.request(:basic_auth, user, pass)
67
+ Faraday.new(url: url, headers: headers) do |conn|
68
+ conn.request :token_auth, token
80
69
 
81
- yield(faraday) if block_given?
70
+ yield(conn) if block_given?
82
71
  end
83
72
  end
84
73
  end
@@ -1,3 +1,3 @@
1
1
  module Ubersicht
2
- VERSION = '0.2.0'.freeze
2
+ VERSION = '0.3.0'.freeze
3
3
  end
data/lib/ubersicht.rb CHANGED
@@ -1,11 +1,7 @@
1
- require 'dry-struct'
2
1
  require 'faraday'
3
2
 
4
3
  require 'ubersicht/version'
5
- require 'ubersicht/ingestion'
6
- require 'ubersicht/ingestion/build_ingestion_event'
7
4
  require 'ubersicht/ingestion/client'
8
- require 'ubersicht/ingestion/event'
9
5
  require 'ubersicht/ingestion/hmac_validator'
10
6
 
11
7
  module Ubersicht
@@ -27,7 +27,6 @@ Gem::Specification.new do |spec|
27
27
  end
28
28
  spec.require_paths = ['lib']
29
29
 
30
- spec.add_dependency 'dry-struct', '~> 1.0'
31
30
  spec.add_dependency 'faraday', ['>= 0.9.2', '< 2']
32
31
 
33
32
  spec.add_development_dependency 'bundler', '~> 2.0'
metadata CHANGED
@@ -1,29 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ubersicht-ruby-sdk
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Starfish Team
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-09-16 00:00:00.000000000 Z
11
+ date: 2021-10-08 00:00:00.000000000 Z
12
12
  dependencies:
13
- - !ruby/object:Gem::Dependency
14
- name: dry-struct
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - "~>"
18
- - !ruby/object:Gem::Version
19
- version: '1.0'
20
- type: :runtime
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - "~>"
25
- - !ruby/object:Gem::Version
26
- version: '1.0'
27
13
  - !ruby/object:Gem::Dependency
28
14
  name: faraday
29
15
  requirement: !ruby/object:Gem::Requirement
@@ -208,10 +194,7 @@ files:
208
194
  - bin/setup
209
195
  - docker-compose.yml
210
196
  - lib/ubersicht.rb
211
- - lib/ubersicht/ingestion.rb
212
- - lib/ubersicht/ingestion/build_ingestion_event.rb
213
197
  - lib/ubersicht/ingestion/client.rb
214
- - lib/ubersicht/ingestion/event.rb
215
198
  - lib/ubersicht/ingestion/hmac_validator.rb
216
199
  - lib/ubersicht/version.rb
217
200
  - lib/ubersicht_ruby_sdk.rb
@@ -1,33 +0,0 @@
1
- module Ubersicht
2
- module Ingestion
3
- class BuildIngestionEvent
4
- def self.call(*args, &block)
5
- new.call(*args, &block)
6
- end
7
-
8
- def initialize
9
- @validate_hmac = ::Ubersicht::Ingestion::HmacValidator.with_ubersicht
10
- @event_class = ::Ubersicht::Ingestion::Event
11
- end
12
-
13
- def call(event, hmac_key, provider)
14
- validate_transaction_type(event[:transaction_type], provider)
15
-
16
- attrs = @event_class.new(event).to_h
17
- attrs[:payload][:hmac_signature] = @validate_hmac.calculate_notification_hmac(attrs, hmac_key)
18
- attrs[:provider] = provider
19
- attrs
20
- rescue ::Dry::Struct::Error => e
21
- raise ::Ubersicht::ValidationError, e.message
22
- end
23
-
24
- private
25
-
26
- def validate_transaction_type(transaction_type, provider)
27
- return if (types = ::Ubersicht::Ingestion::PROVIDER_TO_TYPES_MAP[provider]).include?(transaction_type)
28
-
29
- raise Ubersicht::Error, "Unknown type '#{transaction_type}'. #{provider} supports only #{types.join(', ')}." \
30
- end
31
- end
32
- end
33
- end
@@ -1,17 +0,0 @@
1
- module Ubersicht
2
- module Ingestion
3
- module Types
4
- include ::Dry.Types()
5
- end
6
-
7
- class Event < ::Dry::Struct
8
- attribute :event_code, Types::String
9
- attribute :event_date, Types::String
10
- attribute :transaction_type, Types::String # DeviceBinding
11
- attribute :payload, Types::Hash.optional.default({}.freeze)
12
- # payload.event_date
13
- # payload.event_group_id
14
- # payload.event_id
15
- end
16
- end
17
- end
@@ -1,19 +0,0 @@
1
- module Ubersicht
2
- module Ingestion
3
- ACCEPTED_RESPONSE = '[accepted]'.freeze
4
- ACCEPTED_STATUS = 200
5
- REJECTED_RESPONSE = '[rejected]'.freeze
6
- REJECTED_STATUS = 400
7
-
8
- PROVIDERS = [
9
- DAUTH_PROVIDER = 'DAuth'.freeze
10
- ].freeze
11
-
12
- PROVIDER_TO_TYPES_MAP = {
13
- DAUTH_PROVIDER => %w[
14
- Authentication
15
- DeviceBinding
16
- ]
17
- }.freeze
18
- end
19
- end