dashx 0.1.0 → 0.2.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: 34de41f0c8c2f7c4dea7e207a48872226ea7ee16f0064f4e71b2e7c1f9f602b9
4
- data.tar.gz: c94f80a573ca480c1ebeb3801809ba1f9a134b86eed8f44c37cdd15ad8b5c227
3
+ metadata.gz: 72a2f7bbabfb1c2a5197ef1de7919a269819a945a29844006734e33cf2a35419
4
+ data.tar.gz: 19ed1f73093d99fa4f5940124891c2555e3557a4a64621f33644b8b23b342529
5
5
  SHA512:
6
- metadata.gz: '0785de22a60d1f49cc10082c0311c7b8300cf29a28553ab56f7ab9e897ca05541f5dacf09cb7dd7e55d5b4a36e95c3727ac7928df9419cd0de72fb3c576d7a9d'
7
- data.tar.gz: 5337c7c04b554a5862f5a7fd7212540924dfde5144d62b0f9809f4d57c661d66fca7866d2a169e7916ed48e0812e62115e31419364816743f68b7850bd5110a0
6
+ metadata.gz: ef4af976040d640d984efa4511337b312cc77af5c1a7aa122efc049e5541bb8d36d522e8089224c631c8eb27f1cccc7020961ba4cbd7b96884cacc53dc7c4cd8
7
+ data.tar.gz: f25b91cdacc3a6b8f5444a47e3d38d4b6fb3e131f8bfc188b28132c0b9d90f1a166e328afb43928d30c13b5414b8c4bad67c045d2f35aa2a5f1620bc9a3a6c04
data/lib/dashx/client.rb CHANGED
@@ -3,60 +3,115 @@ require 'httparty'
3
3
  module DashX
4
4
  class Client
5
5
  include HTTParty
6
- base_uri 'https://api.dashx.com/v1'
6
+ base_uri 'https://api.dashx.com'
7
+
8
+ CREATE_DELIVERY_REQUEST = 'mutation CreateDelivery($input: CreateDeliveryInput!) {
9
+ createDelivery(input: $input) {
10
+ id
11
+ }
12
+ }
13
+ '
14
+
15
+ IDENTIFY_ACCOUNT_REQUEST = 'mutation IdentifyAccount($input: IdentifyAccountInput!) {
16
+ identifyAccount(input: $input) {
17
+ id
18
+ }
19
+ }
20
+ '
21
+
22
+ TRACK_EVENT_REQUEST = 'mutation TrackEvent($input: TrackEventInput!) {
23
+ trackEvent(input: $input) {
24
+ id
25
+ }
26
+ }
27
+ '
7
28
 
8
29
  def initialize(config)
9
30
  @config = config
10
31
 
11
32
  self.class.base_uri(config.base_uri)
12
- self.class.headers({
33
+
34
+ headers = {
13
35
  'X-Public-Key' => config.public_key,
14
- 'X-Private-Key' => config.private_key
15
- })
36
+ 'X-Private-Key' => config.private_key,
37
+ }
38
+
39
+ if !config.target_environment.nil?
40
+ headers['X-Target-Environment'] = config.target_environment
41
+ end
42
+
43
+ if !config.target_installation.nil?
44
+ headers['X-Target-Installation'] = config.target_installation
45
+ end
46
+
47
+ self.class.headers(headers)
16
48
  end
17
49
 
18
- def deliver(parcel)
19
- symbolize_keys! parcel
50
+ def deliver(urn, options)
51
+ contentTypeIdentifier, contentIdentifier = urn.split(/\//, 2)
52
+
53
+ options ||= {}
54
+
55
+ symbolize_keys! options
20
56
 
21
- check_presence!(parcel[:to], 'Recipient')
57
+ options[:content] ||= {}
22
58
 
23
- make_http_request('deliver', parcel)
59
+ [:to, :cc, :bcc].each do |kind|
60
+ value = options.delete(kind)
61
+
62
+ options[:content][kind] ||= value if value
63
+ options[:content][kind] = wrap_array(options[:content][kind]) if options[:content][kind]
64
+ end
65
+
66
+ params = {
67
+ contentTypeIdentifier: contentTypeIdentifier,
68
+ contentIdentifier: contentIdentifier
69
+ }.merge(options)
70
+
71
+ make_graphql_request(CREATE_DELIVERY_REQUEST, params)
24
72
  end
25
73
 
26
- def identify(uid, options = {})
74
+ def identify(uid, options)
27
75
  symbolize_keys! options
28
76
 
29
- params = if uid.is_a? String
77
+ params = if uid.is_a?(String) && options != nil
30
78
  { uid: uid }.merge(options)
31
79
  else
32
- { anonymous_uid: SecureRandom.uuid }.merge(options)
80
+ { anonymousUid: SecureRandom.uuid }.merge(uid)
33
81
  end
34
82
 
35
- make_http_request('identify', params)
83
+ make_graphql_request(IDENTIFY_ACCOUNT_REQUEST, params)
36
84
  end
37
85
 
38
86
  def track(event, uid, data = nil)
39
87
  symbolize_keys! data unless data.nil?
40
88
 
41
- make_http_request('track', { event: event, uid: uid, data: data })
89
+ make_graphql_request(TRACK_EVENT_REQUEST, { event: event, uid: uid, data: data })
42
90
  end
43
91
 
44
- def generate_identity_token(uid)
92
+ def generate_identity_token(uid, options = {})
45
93
  check_presence!(uid, 'uid')
94
+ symbolize_keys! options
95
+
96
+ kind = options[:kind] || 'regular'
97
+ plain_text = "v1;#{kind};#{uid}"
46
98
 
47
99
  cipher = OpenSSL::Cipher::AES.new(256, :GCM).encrypt
48
100
  cipher.key = @config.private_key
49
101
  nonce = cipher.random_iv
50
102
  cipher.iv = nonce
51
- encrypted = cipher.update(uid) + cipher.final
103
+ encrypted = cipher.update(plain_text) + cipher.final
52
104
  encrypted_token = "#{nonce}#{encrypted}#{cipher.auth_tag}"
53
105
  Base64.urlsafe_encode64(encrypted_token)
54
106
  end
55
107
 
56
108
  private
57
109
 
58
- def make_http_request(uri, body)
59
- self.class.send(:post, "/#{uri}", { body: body })
110
+ def make_graphql_request(request, params)
111
+ body = { query: request, variables: { input: params } }.to_json
112
+ response = self.class.post('/graphql', { body: body })
113
+ raise "Request Failed: #{response}" if !response.success? || response.parsed_response.nil? || !response.parsed_response['errors'].nil?
114
+ response
60
115
  end
61
116
 
62
117
  def symbolize_keys!(hash)
@@ -67,6 +122,10 @@ module DashX
67
122
  hash.replace(new_hash)
68
123
  end
69
124
 
125
+ def wrap_array(obj)
126
+ obj.is_a?(Array) ? obj : [obj]
127
+ end
128
+
70
129
  def check_presence!(obj, name = obj)
71
130
  raise ArgumentError, "#{name} cannot be blank." if obj.nil? || (obj.is_a?(String) && obj.empty?)
72
131
  end
data/lib/dashx/config.rb CHANGED
@@ -3,5 +3,7 @@ module DashX
3
3
  attr_accessor :base_uri
4
4
  attr_accessor :public_key
5
5
  attr_accessor :private_key
6
+ attr_accessor :target_installation
7
+ attr_accessor :target_environment
6
8
  end
7
9
  end
data/lib/dashx/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module DashX
2
- VERSION = '0.1.0'.freeze
2
+ VERSION = '0.2.0'.freeze
3
3
  end
data/lib/dashx.rb CHANGED
@@ -11,7 +11,11 @@ module DashX
11
11
  @clients[client_name] = DashX::Client.new(config)
12
12
  end
13
13
 
14
- def self.identify(uid = {}, options = {})
14
+ def self.deliver(urn, parcel)
15
+ @clients[:default].deliver(urn, parcel)
16
+ end
17
+
18
+ def self.identify(uid, options)
15
19
  @clients[:default].identify(uid, options)
16
20
  end
17
21
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dashx
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - DashX
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-09-26 00:00:00.000000000 Z
11
+ date: 2022-01-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httparty
@@ -24,8 +24,8 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0.16'
27
- description:
28
- email:
27
+ description:
28
+ email:
29
29
  executables: []
30
30
  extensions: []
31
31
  extra_rdoc_files: []
@@ -51,7 +51,7 @@ licenses:
51
51
  metadata:
52
52
  homepage_uri: https://github.com/dashxhq/dashx-ruby#readme
53
53
  source_code_uri: https://github.com/dashxhq/dashx-ruby
54
- post_install_message:
54
+ post_install_message:
55
55
  rdoc_options: []
56
56
  require_paths:
57
57
  - lib
@@ -66,8 +66,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
66
66
  - !ruby/object:Gem::Version
67
67
  version: '0'
68
68
  requirements: []
69
- rubygems_version: 3.1.2
70
- signing_key:
69
+ rubygems_version: 3.0.8
70
+ signing_key:
71
71
  specification_version: 4
72
72
  summary: DashX SDK for Ruby.
73
73
  test_files: []