Commit 3ec8a9df by Victor Wang

Merge pull request #14 from wjp2013/develop

Develop
parents 14c36c15 66a6f5b9
...@@ -30,6 +30,7 @@ end ...@@ -30,6 +30,7 @@ end
require "igetui/version" require "igetui/version"
require 'protobuf/GtReq.pb' require 'protobuf/GtReq.pb'
require "igetui/template" require "igetui/template"
require "igetui/validate"
require "igetui/message" require "igetui/message"
require "igetui/pusher" require "igetui/pusher"
require "igetui/client" require "igetui/client"
...@@ -14,7 +14,7 @@ module IGeTui ...@@ -14,7 +14,7 @@ module IGeTui
data = { data = {
'action' => 'pushMessageToSingleAction', 'action' => 'pushMessageToSingleAction',
'appkey' => app_key, 'appkey' => app_key,
'clientData' => base64Str(template), 'clientData' => template.get_client_data(self),
'transmissionContent' => template.transmission_content, 'transmissionContent' => template.transmission_content,
'isOffline' => message.is_offline, 'isOffline' => message.is_offline,
'offlineExpireTime' => message.offline_expire_time, 'offlineExpireTime' => message.offline_expire_time,
...@@ -50,7 +50,7 @@ module IGeTui ...@@ -50,7 +50,7 @@ module IGeTui
data = { data = {
'action' => 'pushMessageToAppAction', 'action' => 'pushMessageToAppAction',
'appkey' => app_key, 'appkey' => app_key,
'clientData' => base64Str(template), 'clientData' => template.get_client_data(self),
'transmissionContent' => template.transmission_content, 'transmissionContent' => template.transmission_content,
'isOffline' => message.is_offline, 'isOffline' => message.is_offline,
'offlineExpireTime' => message.offline_expire_time, 'offlineExpireTime' => message.offline_expire_time,
...@@ -91,7 +91,7 @@ module IGeTui ...@@ -91,7 +91,7 @@ module IGeTui
data = { data = {
'action' => 'getContentIdAction', 'action' => 'getContentIdAction',
'appkey' => app_key, 'appkey' => app_key,
'clientData' => base64Str(template), 'clientData' => template.get_client_data(self),
'transmissionContent' => template.transmission_content, 'transmissionContent' => template.transmission_content,
'isOffline' => message.is_offline, 'isOffline' => message.is_offline,
'offlineExpireTime' => message.offline_expire_time, 'offlineExpireTime' => message.offline_expire_time,
...@@ -113,11 +113,6 @@ module IGeTui ...@@ -113,11 +113,6 @@ module IGeTui
private private
def base64Str(template)
string = template.get_transparent(self).serialize_to_string
Base64.strict_encode64 string
end
def connect def connect
time_stamp = Time.now.to_i time_stamp = Time.now.to_i
sign = Digest::MD5.hexdigest(app_key + time_stamp.to_s + master_secret) sign = Digest::MD5.hexdigest(app_key + time_stamp.to_s + master_secret)
......
module IGeTui module IGeTui
class BaseTemplate class BaseTemplate
attr_accessor :transmission_type, :transmission_content attr_accessor :transmission_type, :transmission_content, :push_info
def initialize def initialize
@transmission_type = 0 @transmission_type = 0
@transmission_content = '' @transmission_content = ''
@push_info = GtReq::PushInfo.new
end end
def get_transparent(pusher) def get_transparent(pusher)
...@@ -29,39 +28,41 @@ module IGeTui ...@@ -29,39 +28,41 @@ module IGeTui
raise NotImplementedError, 'Must be implemented by subtypes.' raise NotImplementedError, 'Must be implemented by subtypes.'
end end
def get_client_data(pusher)
string = self.get_transparent(pusher).serialize_to_string
Base64.strict_encode64 string
end
def get_push_info def get_push_info
@push_info.actionKey = '' @push_info || init_push_info
@push_info.badge = ''
@push_info.message = ''
@push_info.sound = ''
@push_info
end end
# Need TEST: # NOTE:
# iOS Pusher need the top three fields of 'push_info' are required. # iOS Pusher need the top four fields of 'push_info' are required.
# the others can be blank string. # options can be includes [:payload, :loc_key, :loc_args, :launch_image]
def set_push_info(action_loc_key, badge, message, sound, payload, loc_key, loc_args, launch_image) # http://docs.igetui.com/pages/viewpage.action?pageId=590588
def set_push_info(action_loc_key, badge, message, sound, options = {})
init_push_info
@push_info.actionLocKey = action_loc_key @push_info.actionLocKey = action_loc_key
@push_info.badge = badge @push_info.badge = badge.to_s
@push_info.message = message @push_info.message = message
@push_info.sound = sound if sound @push_info.sound = sound
@push_info.payload = payload if payload @push_info.payload = options[:payload]
@push_info.locKey = loc_key if loc_key @push_info.locKey = options[:loc_key]
@push_info.locArgs = loc_args if loc_args @push_info.locArgs = options[:loc_args]
@push_info.launchImage = launch_image if launch_image @push_info.launchImage = options[:launch_image]
# validate method need refactoring.
# Validate.new.validate(args)
end
args = { private
loc_key: loc_key,
loc_args: locArgs,
message: message,
action_loc_key: action_loc_key,
launch_image: launch_image,
badge: badge,
sound: sound,
payload: payload
}
Validate.new.validate(args) def init_push_info
@push_info = GtReq::PushInfo.new
@push_info.message = ''
@push_info.actionKey = ''
@push_info.sound = ''
@push_info.badge = ''
@push_info @push_info
end end
......
require 'json'
module IGeTui module IGeTui
class Validate class Validate
def validate(args = {}) def validate(args = {})
payload_map = get_payload(args) # payload_map = get_payload(args)
json = JSON.generate payload_map # json = JSON.generate payload_map
if (json.length > 256) # if (json.length > 256)
raise ArgumentError.new("PushInfo length over limit: #{json.length}. Allowed: 256.") # raise ArgumentError.new("PushInfo length over limit: #{json.length}. Allowed: 256.")
# end
is_validate = validate_payload(args)
unless is_validate
payload_len = validate_payload_length(args)
raise ArgumentError.new("PushInfo length over limit: #{payload_len.length}. Allowed: 256.")
end
end
def validate_payload(args)
length = validate_payload_length(args)
length <= 256
end
def validate_payload_length(args)
json = process_payload(args)
json.length
end
def process_payload(args)
is_valid = false
pb = Payload.new
if !args[:loc_key].nil? && args[:loc_key].length > 0
pb.alert_loc_key = args[:loc_key]
if !args[:loc_args].nil? && args[:loc_args].length > 0
pb.alert_loc_args = args[:loc_args].split(",")
end
is_valid = true
end
if !args[:message].nil? && args[:message].length > 0
pb.alert_body = args[:message]
is_valid = true
end
if !args[:action_loc_key].nil? && args[:action_loc_key].length > 0
pb.alert_action_loc_key = args[:action_loc_key]
end
if !args[:launch_image].nil? && args[:launch_image].length > 0
pb.alert_launch_image = args[:launch_image]
end end
badge_num = args[:badge].to_i
if badge_num >= 0
pb.badge = badge_num
is_valid = true
end
if !args[:sound].nil? && args[:sound].length > 0
pb.sound = args[:sound]
end
if !args[:payload].nil? && args[:payload].length > 0
pb.add_param("payload", payload)
end
unless is_valid
puts "one of the params(locKey,message,badge) must not be null"
end
json = pb.to_s
if json.nil?
puts "payload json is null"
end
json
# do something
end end
def get_payload(args = {}) def get_payload(args = {})
...@@ -26,8 +95,8 @@ module IGeTui ...@@ -26,8 +95,8 @@ module IGeTui
if validate_length(args, :loc_args) if validate_length(args, :loc_args)
alertMap["loc-args"] = args[:loc_args].split(", ") alertMap["loc-args"] = args[:loc_args].split(", ")
end end
elsif validate_length(nil, message) elsif validate_length(nil, args[:message])
alertMap["body"] = message alertMap["body"] = args[:message]
end end
apnsMap["alert"] = alertMap apnsMap["alert"] = alertMap
...@@ -35,11 +104,11 @@ module IGeTui ...@@ -35,11 +104,11 @@ module IGeTui
apnsMap["action-loc-key"] = args[:action_loc_key] apnsMap["action-loc-key"] = args[:action_loc_key]
end end
apnsMap["badge"] = badge apnsMap["badge"] = args[:badge]
h = Hash.new h = Hash.new
h["aps"] = apnsMap h["aps"] = apnsMap
h["payload"] = payload if validate_length(nil, payload) h["payload"] = args[:payload] if validate_length(nil, args[:payload])
return h return h
end end
......
module IGeTui module IGeTui
VERSION = "1.1.0" VERSION = "1.2.0"
end end
...@@ -81,12 +81,14 @@ class PusherTest < MiniTest::Unit::TestCase ...@@ -81,12 +81,14 @@ class PusherTest < MiniTest::Unit::TestCase
template = IGeTui::LinkTemplate.new template = IGeTui::LinkTemplate.new
set_template_base_info(template) set_template_base_info(template)
template.url = "http://www.baidu.com" template.url = "http://www.baidu.com"
template.set_push_info("open", 4, "message", "")
template template
end end
def notification_template def notification_template
template = IGeTui::NotificationTemplate.new template = IGeTui::NotificationTemplate.new
set_template_base_info(template) set_template_base_info(template)
template.set_push_info("open", 4, "message", "")
template template
end end
...@@ -94,14 +96,17 @@ class PusherTest < MiniTest::Unit::TestCase ...@@ -94,14 +96,17 @@ class PusherTest < MiniTest::Unit::TestCase
template = IGeTui::TransmissionTemplate.new template = IGeTui::TransmissionTemplate.new
# Notice: content should be string. # Notice: content should be string.
content = { content = {
action: "notification", "action" => "notification",
title: "标题aaa", "title" => "标题aaa",
content: "内容", "content" => "内容",
type: "article", "type" => "Article",
id: "4274" "id" => "1234"
} }
content = content.to_s.gsub(":", "").gsub("=>", ":") content = content.to_s.gsub(":", "").gsub("=>", ":")
template.transmission_content = content template.transmission_content = content
puts template.transmission_content
template.set_push_info("test", 1, "test1", "")
template template
end end
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment