Commit 3ec8a9df by Victor Wang

Merge pull request #14 from wjp2013/develop

Develop
parents 14c36c15 66a6f5b9
......@@ -30,6 +30,7 @@ end
require "igetui/version"
require 'protobuf/GtReq.pb'
require "igetui/template"
require "igetui/validate"
require "igetui/message"
require "igetui/pusher"
require "igetui/client"
......@@ -14,7 +14,7 @@ module IGeTui
data = {
'action' => 'pushMessageToSingleAction',
'appkey' => app_key,
'clientData' => base64Str(template),
'clientData' => template.get_client_data(self),
'transmissionContent' => template.transmission_content,
'isOffline' => message.is_offline,
'offlineExpireTime' => message.offline_expire_time,
......@@ -50,7 +50,7 @@ module IGeTui
data = {
'action' => 'pushMessageToAppAction',
'appkey' => app_key,
'clientData' => base64Str(template),
'clientData' => template.get_client_data(self),
'transmissionContent' => template.transmission_content,
'isOffline' => message.is_offline,
'offlineExpireTime' => message.offline_expire_time,
......@@ -91,7 +91,7 @@ module IGeTui
data = {
'action' => 'getContentIdAction',
'appkey' => app_key,
'clientData' => base64Str(template),
'clientData' => template.get_client_data(self),
'transmissionContent' => template.transmission_content,
'isOffline' => message.is_offline,
'offlineExpireTime' => message.offline_expire_time,
......@@ -113,11 +113,6 @@ module IGeTui
private
def base64Str(template)
string = template.get_transparent(self).serialize_to_string
Base64.strict_encode64 string
end
def connect
time_stamp = Time.now.to_i
sign = Digest::MD5.hexdigest(app_key + time_stamp.to_s + master_secret)
......
module IGeTui
class BaseTemplate
attr_accessor :transmission_type, :transmission_content
attr_accessor :transmission_type, :transmission_content, :push_info
def initialize
@transmission_type = 0
@transmission_content = ''
@push_info = GtReq::PushInfo.new
end
def get_transparent(pusher)
......@@ -29,39 +28,41 @@ module IGeTui
raise NotImplementedError, 'Must be implemented by subtypes.'
end
def get_client_data(pusher)
string = self.get_transparent(pusher).serialize_to_string
Base64.strict_encode64 string
end
def get_push_info
@push_info.actionKey = ''
@push_info.badge = ''
@push_info.message = ''
@push_info.sound = ''
@push_info
@push_info || init_push_info
end
# Need TEST:
# iOS Pusher need the top three fields of 'push_info' are required.
# the others can be blank string.
def set_push_info(action_loc_key, badge, message, sound, payload, loc_key, loc_args, launch_image)
# NOTE:
# iOS Pusher need the top four fields of 'push_info' are required.
# options can be includes [: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.badge = badge
@push_info.badge = badge.to_s
@push_info.message = message
@push_info.sound = sound if sound
@push_info.payload = payload if payload
@push_info.locKey = loc_key if loc_key
@push_info.locArgs = loc_args if loc_args
@push_info.launchImage = launch_image if launch_image
@push_info.sound = sound
@push_info.payload = options[:payload]
@push_info.locKey = options[:loc_key]
@push_info.locArgs = options[:loc_args]
@push_info.launchImage = options[:launch_image]
# validate method need refactoring.
# Validate.new.validate(args)
end
args = {
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
}
private
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
end
......
require 'json'
module IGeTui
class Validate
def validate(args = {})
payload_map = get_payload(args)
# payload_map = get_payload(args)
json = JSON.generate payload_map
if (json.length > 256)
raise ArgumentError.new("PushInfo length over limit: #{json.length}. Allowed: 256.")
# json = JSON.generate payload_map
# if (json.length > 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
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
def get_payload(args = {})
......@@ -26,8 +95,8 @@ module IGeTui
if validate_length(args, :loc_args)
alertMap["loc-args"] = args[:loc_args].split(", ")
end
elsif validate_length(nil, message)
alertMap["body"] = message
elsif validate_length(nil, args[:message])
alertMap["body"] = args[:message]
end
apnsMap["alert"] = alertMap
......@@ -35,11 +104,11 @@ module IGeTui
apnsMap["action-loc-key"] = args[:action_loc_key]
end
apnsMap["badge"] = badge
apnsMap["badge"] = args[:badge]
h = Hash.new
h["aps"] = apnsMap
h["payload"] = payload if validate_length(nil, payload)
h["payload"] = args[:payload] if validate_length(nil, args[:payload])
return h
end
......
module IGeTui
VERSION = "1.1.0"
VERSION = "1.2.0"
end
......@@ -81,12 +81,14 @@ class PusherTest < MiniTest::Unit::TestCase
template = IGeTui::LinkTemplate.new
set_template_base_info(template)
template.url = "http://www.baidu.com"
template.set_push_info("open", 4, "message", "")
template
end
def notification_template
template = IGeTui::NotificationTemplate.new
set_template_base_info(template)
template.set_push_info("open", 4, "message", "")
template
end
......@@ -94,14 +96,17 @@ class PusherTest < MiniTest::Unit::TestCase
template = IGeTui::TransmissionTemplate.new
# Notice: content should be string.
content = {
action: "notification",
title: "标题aaa",
content: "内容",
type: "article",
id: "4274"
"action" => "notification",
"title" => "标题aaa",
"content" => "内容",
"type" => "Article",
"id" => "1234"
}
content = content.to_s.gsub(":", "").gsub("=>", ":")
template.transmission_content = content
puts template.transmission_content
template.set_push_info("test", 1, "test1", "")
template
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