Class: DashX::Client

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/dashx/client.rb

Constant Summary collapse

CREATE_DELIVERY_REQUEST =
'mutation CreateDelivery($input: CreateDeliveryInput!) {
    createDelivery(input: $input) {
        id
    }
  }
'
IDENTIFY_ACCOUNT_REQUEST =
'mutation IdentifyAccount($input: IdentifyAccountInput!) {
    identifyAccount(input: $input) {
        id
    }
  }
'
TRACK_EVENT_REQUEST =
'mutation TrackEvent($input: TrackEventInput!) {
    trackEvent(input: $input) {
        id
    }
  }
'
SAVE_CONTACTS_REQUEST =
'mutation SaveContacts($input: SaveContactsInput!) {
    saveContacts(input: $input) {
        contacts {
          id
        }
    }
  }
'

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Client

Returns a new instance of Client.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/dashx/client.rb', line 38

def initialize(config)
  @config = config

  self.class.base_uri(config.base_uri)

  headers = {
    'X-Public-Key' => config.public_key,
    'X-Private-Key' => config.private_key,
  }

  if !config.target_environment.nil?
    headers['X-Target-Environment'] = config.target_environment
  end

  if !config.target_installation.nil?
    headers['X-Target-Installation'] = config.target_installation
  end

  self.class.headers(headers)
end

Instance Method Details

#deliver(urn, options) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/dashx/client.rb', line 59

def deliver(urn, options)
  contentTypeIdentifier, contentIdentifier = urn.split(/\//, 2)

  options ||= {}

  symbolize_keys! options

  options[:content] ||= {}

  [:to, :cc, :bcc].each do |kind|
    value = options.delete(kind)

    options[:content][kind] ||= value if value
    options[:content][kind] = wrap_array(options[:content][kind]) if options[:content][kind]
  end

  params = {
    contentTypeIdentifier: contentTypeIdentifier,
    contentIdentifier: contentIdentifier
  }.merge(options)

  make_graphql_request(CREATE_DELIVERY_REQUEST, params)
end

#generate_identity_token(uid, options = {}) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/dashx/client.rb', line 101

def generate_identity_token(uid, options = {})
  check_presence!(uid, 'uid')
  symbolize_keys! options

  kind = options[:kind] || 'regular'
  plain_text = "v1;#{kind};#{uid}"

  cipher = OpenSSL::Cipher::AES.new(256, :GCM).encrypt
  cipher.key = @config.private_key
  nonce = cipher.random_iv
  cipher.iv = nonce
  encrypted = cipher.update(plain_text) + cipher.final
  encrypted_token = "#{nonce}#{encrypted}#{cipher.auth_tag}"
  Base64.urlsafe_encode64(encrypted_token)
end

#identify(uid, options) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
# File 'lib/dashx/client.rb', line 83

def identify(uid, options)
  symbolize_keys! options

  params = if uid.is_a?(String) && options != nil
             { uid: uid }.merge(options)
           else
             { anonymousUid: SecureRandom.uuid }.merge(uid)
           end

  make_graphql_request(IDENTIFY_ACCOUNT_REQUEST, params)
end

#save_contacts(uid, contacts = []) ⇒ Object



117
118
119
120
# File 'lib/dashx/client.rb', line 117

def save_contacts(uid, contacts = [])
  contacts.each(&:symbolize_keys!)
  make_graphql_request(SAVE_CONTACTS_REQUEST, { uid: uid, contacts: contacts })
end

#track(event, uid, data = nil) ⇒ Object



95
96
97
98
99
# File 'lib/dashx/client.rb', line 95

def track(event, uid, data = nil)
  symbolize_keys! data unless data.nil?

  make_graphql_request(TRACK_EVENT_REQUEST, { event: event, uid: uid, data: data })
end