Class: BatchPushNotification::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Client

Returns a new instance of Client.

Raises:

  • (StandardError)


8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/batch_push_notification/client.rb', line 8

def initialize(options)
  self.api_key = BatchPushNotification.api_key
  self.endpoint = BatchPushNotification.endpoint
  self.rest_api_key = BatchPushNotification.rest_api_key
  self.sandbox = BatchPushNotification.sandbox

  # overwrite settings:
  options.each do |key, value|
    instance_variable_set("@#{key}", value)
  end

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

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



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

def api_key
  @api_key
end

#endpointObject

Returns the value of attribute endpoint.



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

def endpoint
  @endpoint
end

#rest_api_keyObject

Returns the value of attribute rest_api_key.



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

def rest_api_key
  @rest_api_key
end

#sandboxObject

Returns the value of attribute sandbox.



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

def sandbox
  @sandbox
end

Instance Method Details

#connectionObject



40
41
42
43
44
45
46
47
48
# File 'lib/batch_push_notification/client.rb', line 40

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

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

#send_asynchronously(notification, &on_complete) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/batch_push_notification/client.rb', line 30

def send_asynchronously(notification, &on_complete)
  Thread.new do
    BatchPushNotification.logger.debug("Started new thread")
    response = connection.post send_url, notification.payload(self.sandbox)
    BatchPushNotification.logger.debug("Post successful, handling body next")
    on_complete.call(JSON.parse(response.body))
  end
end

#send_notification(notification, &on_complete) ⇒ Object



23
24
25
26
27
28
# File 'lib/batch_push_notification/client.rb', line 23

def send_notification(notification, &on_complete)
  BatchPushNotification.logger.debug("Sending Notification")
  send_asynchronously(notification) do |response|
    on_complete.call(response)
  end
end

#send_urlObject



50
51
52
# File 'lib/batch_push_notification/client.rb', line 50

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