Class: BatchPushNotification::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/batch_push_notification/client.rb

Instance Method Summary collapse

Constructor Details

#initializeClient

Returns a new instance of Client.

Raises:

  • (StandardError)


6
7
8
9
# File 'lib/batch_push_notification/client.rb', line 6

def initialize
  # fail if the required params are not set:
  raise(StandardError, 'Configuration is missing') unless BatchPushNotification.endpoint && BatchPushNotification.api_key && BatchPushNotification.rest_api_key && !BatchPushNotification.sandbox.nil?
end

Instance Method Details

#connectionObject



25
26
27
28
29
30
31
32
33
# File 'lib/batch_push_notification/client.rb', line 25

def connection
  return Faraday.new(:url => BatchPushNotification.endpoint) do |faraday|
    faraday.response :logger # log requests to STDOUT
    faraday.headers['Content-Type'] = 'application/json'
    faraday.headers['X-Authorization'] = BatchPushNotification.rest_api_key

    faraday.adapter Faraday.default_adapter # make requests with Net::HTTP
  end
end

#send_asynchronously(notification, &on_complete) ⇒ Object



17
18
19
20
21
22
# File 'lib/batch_push_notification/client.rb', line 17

def send_asynchronously(notification, &on_complete)
  Thread.new do
    response = connection.post send_url, notification.payload
    on_complete.call(JSON.parse(response.body))
  end
end

#send_notification(notification, &on_complete) ⇒ Object



11
12
13
14
15
# File 'lib/batch_push_notification/client.rb', line 11

def send_notification(notification, &on_complete)
  send_asynchronously(notification) do |response|
    on_complete.call(response)
  end
end

#send_urlObject



35
36
37
# File 'lib/batch_push_notification/client.rb', line 35

def send_url
  BatchPushNotification.api_key + '/transactional/send'
end