Class: ZeroPush::Client
- Inherits:
-
Object
- Object
- ZeroPush::Client
- Defined in:
- lib/zero_push/client.rb
Constant Summary collapse
- URL =
'https://api.zeropush.com'.freeze
Instance Attribute Summary collapse
-
#auth_token ⇒ Object
Returns the value of attribute auth_token.
Instance Method Summary collapse
-
#broadcast(params) ⇒ Object
Sends a notification to all of the devices registered with the ZeroPush backend.
-
#client ⇒ Object
the HTTP client configured for API requests.
-
#inactive_tokens ⇒ Object
Returns a list of tokens that have been marked inactive.
-
#initialize(auth_token) ⇒ Client
constructor
A new instance of Client.
-
#notify(params) ⇒ Object
Sends a notification to the list of devices.
-
#register(device_token, channel = nil) ⇒ Object
Registers a device token with the ZeroPush backend.
-
#set_badge(device_token, badge) ⇒ Object
Sets the badge for a particular device.
-
#subscribe(device_token, channel) ⇒ Object
Subscribes a device to a particular notification channel.
-
#unregister(device_token) ⇒ Object
Unregisters a device token that has previously been registered with ZeroPush.
-
#unsubscribe(device_token, channel) ⇒ Object
Unsubscribes a device from a particular notification channel.
-
#verify_credentials ⇒ Boolean
verifies credentials.
Constructor Details
#initialize(auth_token) ⇒ Client
Returns a new instance of Client.
9 10 11 |
# File 'lib/zero_push/client.rb', line 9 def initialize(auth_token) self.auth_token = auth_token end |
Instance Attribute Details
#auth_token ⇒ Object
Returns the value of attribute auth_token.
7 8 9 |
# File 'lib/zero_push/client.rb', line 7 def auth_token @auth_token end |
Instance Method Details
#broadcast(params) ⇒ Object
Sends a notification to all of the devices registered with the ZeroPush backend
Ex. “sent_count”:10
37 38 39 |
# File 'lib/zero_push/client.rb', line 37 def broadcast(params) client.post('/broadcast', params) end |
#client ⇒ Object
the HTTP client configured for API requests
116 117 118 119 120 121 122 123 |
# File 'lib/zero_push/client.rb', line 116 def client Faraday.new(url: URL) do |c| c.token_auth self.auth_token c.request :url_encoded # form-encode POST params c.response :json, :content_type => /\bjson$/ # parse responses to JSON c.adapter Faraday.default_adapter # Net::HTTP end end |
#inactive_tokens ⇒ Object
Returns a list of tokens that have been marked inactive
Ex. [
{
"device_token":"238b8cb09011850cb4bd544dfe0c8f5eeab73d7eeaae9bdca59076db4ae49947",
"marked_inactive_at":"2013-07-17T01:27:53-04:00"
},
{
"device_token":"8c97be6643eea2143322005bc4c44a1aee5e549bce5e2bb2116114f45484ddaf",
"marked_inactive_at":"2013-07-17T01:27:50-04:00"
}
]
110 111 112 |
# File 'lib/zero_push/client.rb', line 110 def inactive_tokens client.get('/inactive_tokens') end |
#notify(params) ⇒ Object
Sends a notification to the list of devices
Ex. href=""abc"">sent_count“:10,”inactive_tokens“:[],”unregistered_tokens“:
27 28 29 |
# File 'lib/zero_push/client.rb', line 27 def notify(params) client.post('/notify', params) end |
#register(device_token, channel = nil) ⇒ Object
Registers a device token with the ZeroPush backend
Ex. “message”:“ok”
69 70 71 72 73 |
# File 'lib/zero_push/client.rb', line 69 def register(device_token, channel=nil) params = {device_token: device_token} params.merge!(channel: channel) unless channel.nil? client.post('/register', params) end |
#set_badge(device_token, badge) ⇒ Object
Sets the badge for a particular device
Ex. “message”:“ok”
93 94 95 |
# File 'lib/zero_push/client.rb', line 93 def set_badge(device_token, badge) client.post('/set_badge', device_token: device_token, badge: badge) end |
#subscribe(device_token, channel) ⇒ Object
Subscribes a device to a particular notification channel
Ex. “channels“:
48 49 50 |
# File 'lib/zero_push/client.rb', line 48 def subscribe(device_token, channel) client.post("/subscribe/#{channel}", device_token:device_token) end |
#unregister(device_token) ⇒ Object
Unregisters a device token that has previously been registered with ZeroPush
Ex. “message”:“ok”
82 83 84 |
# File 'lib/zero_push/client.rb', line 82 def unregister(device_token) client.delete('/unregister', device_token: device_token) end |
#unsubscribe(device_token, channel) ⇒ Object
Unsubscribes a device from a particular notification channel
Ex. “channels”:[]
59 60 61 |
# File 'lib/zero_push/client.rb', line 59 def unsubscribe(device_token, channel) client.delete("/subscribe/#{channel}", device_token:device_token) end |
#verify_credentials ⇒ Boolean
verifies credentials
16 17 18 19 |
# File 'lib/zero_push/client.rb', line 16 def verify_credentials response = client.get('/verify_credentials') response.status == 200 end |