forked from Kandiie/pushbots
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactorize push class to several classes
- Loading branch information
1 parent
44ed25b
commit a5f85c1
Showing
6 changed files
with
106 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,4 +8,5 @@ | |
/spec/reports/ | ||
/tmp/ | ||
.DS_Store | ||
*.gem | ||
*.gem | ||
build.rb |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
module Pushbots | ||
# Push class | ||
class One < Push | ||
attr_accessor :token, :sound, :badge, :payload, :json | ||
|
||
def initialize(platform, message, token, sound, options = {}) | ||
super(platform, message) | ||
self.token = token | ||
self.sound = sound | ||
self.badge = options[:badge] | ||
self.payload = options[:payload] | ||
self.json = options[:json] | ||
end | ||
|
||
def send | ||
request = Request.new(body) | ||
self.response = request.send | ||
self.status = | ||
response.failed? ? STATUS[:failed] : STATUS[:delivered] | ||
end | ||
|
||
def body | ||
data = { | ||
platform: @platform, | ||
token: token, | ||
msg: message, | ||
sound: sound | ||
} | ||
data[:badge] if badge | ||
data[:json] if json | ||
data | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,29 @@ | ||
require 'http' | ||
|
||
module Pushbots | ||
# Push class | ||
# Holds shared data between single and batch push notifications | ||
class Push | ||
attr_accessor :token, :platafform, :message | ||
def initialize(token, platafform, message) | ||
@token = token | ||
@platafform = platafform | ||
@message = message | ||
attr_accessor :platform, :message, :status, :response | ||
PLATFORM_TYPE = { ios: 0, android: 1 }.freeze | ||
PLATFORM_TYPE_R = [:ios, :android].freeze | ||
|
||
STATUS = { created: 'created', delivered: 'delivered', | ||
failed: 'failed' }.freeze | ||
|
||
def initialize(platform, message) | ||
validates_platform(platform) | ||
self.platform = PLATFORM_TYPE[platform] | ||
self.message = message | ||
self.status = STATUS[:created] | ||
end | ||
|
||
def deliver | ||
HTTP.headers(create_header).post(Config.config.pushbots_url, | ||
json: create_body) | ||
def platform | ||
PLATFORM_TYPE_R[@platform.to_i] | ||
end | ||
|
||
private | ||
|
||
def create_header | ||
{ | ||
:'X-PushBots-AppID' => Config.config.application_id, | ||
:'X-PushBots-Secret' => Config.config.application_secret, | ||
:'Content-Type' => 'application/json' | ||
} | ||
end | ||
|
||
def create_body | ||
{ | ||
platform: '0', | ||
token: @token, | ||
msg: @message, | ||
sound: 'sa' | ||
} | ||
def validates_platform(platform) | ||
raise 'platform is not valid' if PLATFORM_TYPE[platform].nil? | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
require 'http' | ||
|
||
module Pushbots | ||
# Request class | ||
class Request | ||
attr_accessor :body | ||
|
||
def initialize(body) | ||
self.body = body | ||
end | ||
|
||
def send | ||
response = HTTP.headers(header).post(Config.config.pushbots_url, | ||
json: body) | ||
Response.new(response) | ||
end | ||
|
||
def header | ||
{ | ||
:'X-PushBots-AppID' => Config.config.application_id, | ||
:'X-PushBots-Secret' => Config.config.application_secret, | ||
:'Content-Type' => 'application/json' | ||
} | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
module Pushbots | ||
# Response class | ||
class Response | ||
attr_accessor :code, :message | ||
|
||
def initialize(http_response) | ||
self.code = http_response.code | ||
if http_response.to_s.empty? | ||
self.message = 'OK' | ||
else | ||
http_response = JSON.parse(http_response) | ||
self.message = "#{http_response['code']}: #{http_response['message']}" | ||
end | ||
end | ||
|
||
def failed? | ||
code != 200 | ||
end | ||
|
||
def to_s | ||
"{code: #{code}, message: #{message}}" | ||
end | ||
end | ||
end |