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) {
        success
    }
  }
'
SAVE_CONTACTS_REQUEST =
'mutation SaveContacts($input: SaveContactsInput!) {
    saveContacts(input: $input) {
        contacts {
          id
        }
    }
  }
'
FETCH_ITEM_REQUEST =
'query FetchItem($input: FetchItemInput) {
    fetchItem(input: $input) {
      id
      installationId
      name
      identifier
      description
      createdAt
      updatedAt
      pricings {
        id
        kind
        amount
        originalAmount
        isRecurring
        recurringInterval
        recurringIntervalUnit
        appleProductIdentifier
        googleProductIdentifier
        currencyCode
        createdAt
        updatedAt
      }
    }
  }
'
FETCH_CONTACTS_REQUEST =
'query FetchContacts($input: FetchContactsInput!) {
    fetchContacts(input: $input) {
      contacts {
        id
        accountId
        name
        kind
        value
        unverifiedValue
        verifiedAt
        status
        tag
        createdAt
        updatedAt
      }
    }
  }
'
FETCH_STORED_PREFERENCES =
'query FetchStoredPreferences($input: FetchStoredPreferencesInput) {
    fetchStoredPreferences(input: $input) {
      preferenceData
    }
  }
'
SAVE_STORED_PREFERENCES =
'mutation SaveStoredPreferences($input: SaveStoredPreferencesInput) {
    saveStoredPreferences(input: $input) {
      success
    }
  }
'

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Client

Returns a new instance of Client.



98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/dashx/client.rb', line 98

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,
    'X-Target-Environment' => config.target_environment,
  }

  self.class.headers(headers)
end

Instance Method Details

#deliver(urn, options) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/dashx/client.rb', line 112

def deliver(urn, options)
  templateSubkind, templateIdentifier = 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 = {
    templateSubkind: templateSubkind.upcase,
    templateIdentifier: templateIdentifier
  }.merge(options)

  make_graphql_request(CREATE_DELIVERY_REQUEST, params)
end

#fetch_contacts(uid) ⇒ Object



159
160
161
# File 'lib/dashx/client.rb', line 159

def fetch_contacts(uid)
  make_graphql_request(FETCH_CONTACTS_REQUEST, { uid: uid })
end

#fetch_item(identifier) ⇒ Object



163
164
165
# File 'lib/dashx/client.rb', line 163

def fetch_item(identifier)
  make_graphql_request(FETCH_ITEM_REQUEST, { identifier: identifier })
end

#fetch_stored_preferences(uid) ⇒ Object



167
168
169
# File 'lib/dashx/client.rb', line 167

def fetch_stored_preferences(uid)
  make_graphql_request(FETCH_STORED_PREFERENCES, { accountUid: uid })
end

#identify(uid, options) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
# File 'lib/dashx/client.rb', line 136

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



154
155
156
157
# File 'lib/dashx/client.rb', line 154

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

#save_stored_preferences(uid, preferenceData) ⇒ Object



171
172
173
# File 'lib/dashx/client.rb', line 171

def save_stored_preferences(uid, preferenceData)
  make_graphql_request(SAVE_STORED_PREFERENCES, { accountUid: uid, preferenceData: preferenceData })
end

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



148
149
150
151
152
# File 'lib/dashx/client.rb', line 148

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

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