class Pigeon::Client

Public Class Methods

new(config) click to toggle source
# File lib/pigeon-ruby/client.rb, line 10
def initialize(config)
  @config = config

  self.class.base_uri(config.base_uri || 'https://api.pigeonapp.io/v1')
  self.class.headers({
    'X-Public-Key' => config.public_key,
    'X-Private-Key' => config.private_key
  })
end

Public Instance Methods

add_contact(customer_id, attrs = {}) click to toggle source
# File lib/pigeon-ruby/client.rb, line 40
def add_contact(customer_id, attrs = {})
  send_request(:post, '/contacts', {
    body: process_contact_attributes(customer_id, attrs)
  })
end
deliver(message_identifier, attrs) click to toggle source
# File lib/pigeon-ruby/client.rb, line 20
def deliver(message_identifier, attrs)
  send_request(:post, '/deliveries', {
    body: process_delivery_attributes(attrs).merge({
      message_identifier: message_identifier
    })
  })
end
generate_token(customer_id) click to toggle source
# File lib/pigeon-ruby/client.rb, line 46
def generate_token(customer_id)
  cipher = OpenSSL::Cipher::AES256.new :CBC
  cipher.encrypt
  cipher.key = OpenSSL::Digest::SHA256.digest @config.private_key
  encrypted = cipher.update(customer_id.to_s) + cipher.final
  Base64.urlsafe_encode64(encrypted)
end
identify(attrs = {}) click to toggle source
# File lib/pigeon-ruby/client.rb, line 34
def identify(attrs = {})
  send_request(:post, '/customers', {
    body: process_identify_attributes(attrs)
  })
end
track(attrs = {}) click to toggle source
# File lib/pigeon-ruby/client.rb, line 28
def track(attrs = {})
  send_request(:post, '/event_logs', {
    body: process_track_attributes(attrs)
  })
end

Private Instance Methods

check_presence!(obj, name = obj) click to toggle source
# File lib/pigeon-ruby/client.rb, line 128
def check_presence!(obj, name = obj)
  raise ArgumentError, "#{name} cannot be blank." if obj.nil? || (obj.is_a?(String) && obj.empty?)
end
generate_anonymous_uid() click to toggle source
# File lib/pigeon-ruby/client.rb, line 116
def generate_anonymous_uid
  SecureRandom.uuid
end
prepare_attachment_content(attachment) click to toggle source
# File lib/pigeon-ruby/client.rb, line 66
def prepare_attachment_content(attachment)
  attachment_file = attachment[:file]

  if File.file?(attachment_file)
    file = File.open(attachment_file)
    attachment[:content] = Base64.strict_encode64(file.read)
    attachment[:name] ||= File.basename(file, '.*')
  else
    attachment[:content] = attachment_file
  end

  attachment.delete(:file)
end
process_contact_attributes(uid, attrs) click to toggle source
# File lib/pigeon-ruby/client.rb, line 105
def process_contact_attributes(uid, attrs)
  symbolize_keys! attrs

  check_presence!(attrs[:name], 'Name')
  check_presence!(attrs[:value], 'Value')
  check_presence!(attrs[:kind], 'Kind')

  attrs[:uid] = uid
  attrs
end
process_delivery_attributes(attrs) click to toggle source
# File lib/pigeon-ruby/client.rb, line 56
def process_delivery_attributes(attrs)
  symbolize_keys! attrs

  check_presence!(attrs[:to], 'Recipient')

  (attrs[:attachments] || []).each { |attachment| prepare_attachment_content(attachment) }

  attrs
end
process_identify_attributes(attrs) click to toggle source
# File lib/pigeon-ruby/client.rb, line 92
def process_identify_attributes(attrs)
  symbolize_keys! attrs

  extras = attrs[:extras] || {}
  raise ArgumentError, 'Extras must be a Hash' if !extras.is_a? Hash

  if !attrs[:uid] && !attrs[:anonymous_uid]
    attrs[:anonymous_uid] = generate_anonymous_uid
  end

  attrs
end
process_track_attributes(attrs) click to toggle source
# File lib/pigeon-ruby/client.rb, line 80
def process_track_attributes(attrs)
  symbolize_keys! attrs

  check_presence!(attrs[:event], 'Event')
  check_presence!(attrs[:customer_uid], 'Customer UID')

  data = attrs[:data] || {}
  raise ArgumentError, 'data must be a Hash' if !data.is_a? Hash

  attrs
end
send_request(method, path, options) click to toggle source
# File lib/pigeon-ruby/client.rb, line 132
def send_request(method, path, options)
  if !!@config.stub
    Net::HTTPResponse.new(method, 200, {})
  else
    self.class.send(method, path, options)
  end
end
symbolize_keys!(hash) click to toggle source
# File lib/pigeon-ruby/client.rb, line 120
def symbolize_keys!(hash)
  new_hash = hash.each_with_object({}) do |(k, v), memo|
    memo[k.to_sym] = v
  end

  hash.replace(new_hash)
end