Skip to content

Commit

Permalink
Refactorize push class to several classes
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosguerreroo committed Mar 5, 2016
1 parent 44ed25b commit a5f85c1
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 26 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@
/spec/reports/
/tmp/
.DS_Store
*.gem
*.gem
build.rb
3 changes: 3 additions & 0 deletions lib/pushbots.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
require 'pushbots/version'
require 'pushbots/config'
require 'pushbots/push'
require 'pushbots/request'
require 'pushbots/response'
require 'pushbots/one'

# Pushbots module
module Pushbots
Expand Down
34 changes: 34 additions & 0 deletions lib/pushbots/one.rb
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
42 changes: 17 additions & 25 deletions lib/pushbots/push.rb
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
26 changes: 26 additions & 0 deletions lib/pushbots/request.rb
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
24 changes: 24 additions & 0 deletions lib/pushbots/response.rb
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

0 comments on commit a5f85c1

Please sign in to comment.