Class: ZeroPushWoosh::Client

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

Constant Summary collapse

URL =
'https://zeropush.pushwoosh.com'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(auth_token) ⇒ Client

Returns a new instance of Client.



10
11
12
13
# File 'lib/zero_push_woosh/client.rb', line 10

def initialize(auth_token)
  self.auth_token = auth_token
  self.extend(Compatibility)
end

Instance Attribute Details

#auth_tokenObject

Returns the value of attribute auth_token.



8
9
10
# File 'lib/zero_push_woosh/client.rb', line 8

def auth_token
  @auth_token
end

Instance Method Details

#broadcast(params) ⇒ Object

Sends a notification to all of the devices registered with the ZeroPushWoosh backend

Example response “sent_count”:10

Parameters:

  • params (Hash)


39
40
41
# File 'lib/zero_push_woosh/client.rb', line 39

def broadcast(params)
  http.post('/broadcast', params)
end

#channel(channel_name) ⇒ Object

Returns the list of device tokens for the given channel zeropush.com/documentation/api_reference#channels_show

Example Response:

"channel": "player-1",
"device_tokens": [
  "1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcedf"
]



235
236
237
# File 'lib/zero_push_woosh/client.rb', line 235

def channel(channel_name)
  http.get("/channels/#{channel_name}")
end

#channels(params = {page:1}) ⇒ Object

Returns paginated list of channels zeropush.com/documentation/api_reference#channels_index

Example Response: [

"player-1",
"player-2",
"player-9",
"game-256",
"admins",
"lobby"

]



221
222
223
# File 'lib/zero_push_woosh/client.rb', line 221

def channels(params = {page:1})
  http.get('/channels', params)
end

#delete_channel(channel_name) ⇒ Object

Deletes a channels and unsubscribes all of the devices from it. zeropush.com/documentation/api_reference#channels_destroy

"channel": "player-1",
"device_tokens": [
  "1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcedf"
]



248
249
250
# File 'lib/zero_push_woosh/client.rb', line 248

def delete_channel(channel_name)
  http.delete("/channels/#{channel_name}")
end

#device(token) ⇒ Object

Return detailed information about a device zeropush.com/documentation/api_reference#devices_show

Example response

"token": "1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcedf",
"active": true,
"marked_inactive_at": null,
"badge": 1,
"channels": [
  "testflight",
  "[email protected]"
]



152
153
154
# File 'lib/zero_push_woosh/client.rb', line 152

def device(token)
  http.get("/devices/#{token}")
end

#devices(params = {page:1}) ⇒ Object

Returns a paginated list of devices zeropush.com/documentation/api_reference#devices_index

Example response [

{
  "token": "1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcedf",
  "active": true,
  "marked_inactive_at": null,
  "badge": 1
},
{
  "token": "234567890abcdef1234567890abcdef1234567890abcdef1234567890abcedf0",
  "active": true,
  "marked_inactive_at": null,
  "badge": 2
}

]



134
135
136
# File 'lib/zero_push_woosh/client.rb', line 134

def devices(params = {page:1})
  http.get('/devices', params)
end

#httpObject Also known as: client

Instantiate a new http client configured for making requests to the API



253
254
255
256
257
258
259
260
# File 'lib/zero_push_woosh/client.rb', line 253

def http
  Faraday.new(url: URL) do |c|
    c.token_auth self.auth_token
    c.request    http_config[:request_encoding]
    c.response   :json, :content_type => /\bjson$/ # parse responses to JSON
    c.adapter    http_config[:http_adapter]
  end
end

#inactive_tokens(params = {page:1}) ⇒ Object

Returns a list of tokens that have been marked inactive

Example response [

{
  "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"
}

]



112
113
114
# File 'lib/zero_push_woosh/client.rb', line 112

def inactive_tokens(params = {page:1})
  http.get('/inactive_tokens', params)
end

#notify(params) ⇒ Object

Sends a notification to the list of devices

Example response href=""abc"">sent_count”:10,“inactive_tokens”:[],“unregistered_tokens”:

Parameters:

  • params (Hash)


29
30
31
# File 'lib/zero_push_woosh/client.rb', line 29

def notify(params)
  http.post('/notify', params)
end

#register(device_token, channel = nil) ⇒ Object

Registers a device token with the ZeroPushWoosh backend

Example response “message”:“ok”

Parameters:

  • device_token


71
72
73
74
75
# File 'lib/zero_push_woosh/client.rb', line 71

def register(device_token, channel=nil)
  params = {device_token: device_token}
  params.merge!(channel: channel) unless channel.nil?
  http.post('/register', params)
end

#set_badge(device_token, badge) ⇒ Object

Sets the badge for a particular device

Example response “message”:“ok”

Parameters:

  • device_token
  • badge


95
96
97
# File 'lib/zero_push_woosh/client.rb', line 95

def set_badge(device_token, badge)
  http.post('/set_badge', device_token: device_token, badge: badge)
end

#set_device(token, params) ⇒ Object

Replace the channel subscriptions with a new set of channels. This will remove all previous subscriptions of the device. If you want to append a list of channels, use #update_device. zeropush.com/documentation/api_reference#devices_update_put

Example Request

ZeroPushWoosh.set_device(token, channel_list: ‘player-1, game-256’)

Example Response

"token": "1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcedf",
"active": true,
"marked_inactive_at": null,
"badge": 1,
"channels": [
  "player-1",
  "game-256"
]

Parameters:

  • token

    String token identifying the device

  • channel_list

    String Comma separated list of channels



179
180
181
# File 'lib/zero_push_woosh/client.rb', line 179

def set_device(token, params)
  http.put("/devices/#{token}", params)
end

#subscribe(device_token, channel) ⇒ Object

Subscribes a device to a particular notification channel

Example response “channels”:

Parameters:

  • device_token (String)
  • channel (String)


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

def subscribe(device_token, channel)
  http.post("/subscribe/#{channel}", device_token:device_token)
end

#unregister(device_token) ⇒ Object

Unregisters a device token that has previously been registered with ZeroPushWoosh

Example response “message”:“ok”

Parameters:

  • device_token


84
85
86
# File 'lib/zero_push_woosh/client.rb', line 84

def unregister(device_token)
  http.delete('/unregister', device_token: device_token)
end

#unsubscribe(device_token, channel) ⇒ Object

Unsubscribes a device from a particular notification channel

Example response “channels”:[]

Parameters:

  • device_token (String)
  • channel (String)


61
62
63
# File 'lib/zero_push_woosh/client.rb', line 61

def unsubscribe(device_token, channel)
  http.delete("/subscribe/#{channel}", device_token:device_token)
end

#update_device(token, params) ⇒ Object

Append the channel subscriptions with a set of new channels. If you want to replace the list of channels, use #set_device. zeropush.com/documentation/api_reference#devices_update_patch

Example Request

ZeroPushWoosh.update_device(token, channel_list: ‘player-1, game-256’)

Example Response

"token": "1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcedf",
"active": true,
"marked_inactive_at": null,
"badge": 1,
"channels": [
  "player-1",
  "game-256"
]

Parameters:

  • token

    String token identifying the device

  • channel_list

    String Comma separated list of channels



205
206
207
# File 'lib/zero_push_woosh/client.rb', line 205

def update_device(token, params)
  http.patch("/devices/#{token}", params)
end

#verify_credentialsBoolean

verifies credentials

Returns:

  • (Boolean)


18
19
20
21
# File 'lib/zero_push_woosh/client.rb', line 18

def verify_credentials
  response = http.get('/verify_credentials')
  response.status == 200
end