Class: BatchPushNotification::Client
- Inherits:
-
Object
- Object
- BatchPushNotification::Client
- Defined in:
- lib/batch_push_notification/client.rb
Instance Attribute Summary collapse
-
#api_key ⇒ Object
Returns the value of attribute api_key.
-
#endpoint ⇒ Object
Returns the value of attribute endpoint.
-
#rest_api_key ⇒ Object
Returns the value of attribute rest_api_key.
-
#sandbox ⇒ Object
Returns the value of attribute sandbox.
Instance Method Summary collapse
- #connection ⇒ Object
-
#initialize(options) ⇒ Client
constructor
A new instance of Client.
- #send_asynchronously(notification, &on_complete) ⇒ Object
- #send_notification(notification, &on_complete) ⇒ Object
- #send_url ⇒ Object
Constructor Details
#initialize(options) ⇒ Client
Returns a new instance of Client.
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() self.api_key = BatchPushNotification.api_key self.endpoint = BatchPushNotification.endpoint self.rest_api_key = BatchPushNotification.rest_api_key self.sandbox = BatchPushNotification.sandbox # overwrite settings: .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_key ⇒ Object
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 |
#endpoint ⇒ Object
Returns the value of attribute endpoint.
6 7 8 |
# File 'lib/batch_push_notification/client.rb', line 6 def endpoint @endpoint end |
#rest_api_key ⇒ Object
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 |
#sandbox ⇒ Object
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
#connection ⇒ Object
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_url ⇒ Object
50 51 52 |
# File 'lib/batch_push_notification/client.rb', line 50 def send_url self.api_key + '/transactional/send' end |