lib/dashx/client.rb in dashx-0.1.1 vs lib/dashx/client.rb in dashx-0.1.2
- old
+ new
@@ -1,46 +1,94 @@
require 'httparty'
module DashX
class Client
include HTTParty
- base_uri 'https://api.dashx.com/v1'
+ base_uri 'https://api.dashx.com'
+ 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
+ }
+ }
+ '
+
def initialize(config)
@config = config
self.class.base_uri(config.base_uri)
- self.class.headers({
+
+ headers = {
'X-Public-Key' => config.public_key,
- 'X-Private-Key' => config.private_key
- })
- end
+ 'X-Private-Key' => config.private_key,
+ }
- def deliver(parcel)
- symbolize_keys! parcel
+ if !config.target_environment.nil?
+ headers['X-Target-Environment'] = config.target_environment
+ end
- check_presence!(parcel[:to], 'Recipient')
+ if !config.target_installation.nil?
+ headers['X-Target-Installation'] = config.target_installation
+ end
- make_http_request('deliver', parcel)
+ self.class.headers(headers)
+ end
+
+ def deliver(urn, parcel)
+ options = if urn.is_a? String && parcel != nil
+ symbolize_keys! parcel
+ check_presence!(parcel[:to], 'Recipient (:to)')
+
+ contentTypeIdentifier, contentIdentifier = urn.split('/', 1)
+
+ {
+ contentTypeIdentifier: contentTypeIdentifier,
+ contentIdentifier: contentIdentifier,
+ attachments: [],
+ cc: [],
+ bcc: [],
+ }.merge(parcel)
+ else
+ symbolize_keys! urn
+ check_presence!(urn[:from], 'Sender (:from)')
+
+ { attachments: [], cc: [], bcc: [] }.merge(urn)
+ end
+
+ make_graphql_request(CREATE_DELIVERY_REQUEST, options)
end
- def identify(uid, options = {})
+ def identify(uid, options)
symbolize_keys! options
- params = if uid.is_a? String
+ params = if uid.is_a? String && options != nil
{ uid: uid }.merge(options)
else
- { anonymous_uid: SecureRandom.uuid }.merge(options)
+ { anonymousUid: SecureRandom.uuid }.merge(uid)
end
- make_http_request('identify', params)
+ make_graphql_request(IDENTIFY_ACCOUNT_REQUEST, params)
end
def track(event, uid, data = nil)
symbolize_keys! data unless data.nil?
- make_http_request('track', { event: event, uid: uid, data: data })
+ make_graphql_request(TRACK_EVENT_REQUEST, { event: event, uid: uid, data: data })
end
def generate_identity_token(uid)
check_presence!(uid, 'uid')
@@ -53,11 +101,12 @@
Base64.urlsafe_encode64(encrypted_token)
end
private
- def make_http_request(uri, body)
- self.class.send(:post, "/#{uri}", { body: body })
+ def make_graphql_request(request, params)
+ body = { query: request, variables: { input: params } }.to_json
+ request = self.class.post('/graphql', { body: body })
end
def symbolize_keys!(hash)
new_hash = hash.each_with_object({}) do |(k, v), memo|
memo[k.to_sym] = v