Module: JPush::Helper::Argument

Included in:
ArgumentHelper
Defined in:
lib/jpush/helper/argument.rb

Constant Summary collapse

MOBILE_RE =
/^(1[34578])(\d{9})$/
MAX_ALIAS_BYTESIZE =
40
MAX_TAG_BYTESIZE =
40

Instance Method Summary collapse

Instance Method Details

#check_alias(alis) ⇒ Object



22
23
24
25
26
# File 'lib/jpush/helper/argument.rb', line 22

def check_alias(alis)
  return '' == alis
  ensure_word_valid('alias', alis)
  ensure_not_over_bytesize('alias', alis, MAX_ALIAS_BYTESIZE)
end

#check_mobile(mobile) ⇒ Object



18
19
20
# File 'lib/jpush/helper/argument.rb', line 18

def check_mobile(mobile)
  raise Utils::Exceptions::InvalidArgumentError.new([], "invalid mobile") unless MOBILE_RE === mobile
end

#check_platform(platform) ⇒ Object



33
34
35
36
# File 'lib/jpush/helper/argument.rb', line 33

def check_platform(platform)
  valid_platform = JPush::Config.settings[:valid_platform]
  raise Utils::Exceptions::InvalidElementError.new('platform', platform, valid_platform) unless valid_platform.include?(platform)
end

#check_registration_id(registration_id) ⇒ Object



14
15
16
# File 'lib/jpush/helper/argument.rb', line 14

def check_registration_id(registration_id)
  ensure_argument_not_blank('registration id': registration_id)
end

#check_tag(tag) ⇒ Object



28
29
30
31
# File 'lib/jpush/helper/argument.rb', line 28

def check_tag(tag)
  ensure_word_valid('tag', tag)
  ensure_not_over_bytesize('tag', tag, MAX_TAG_BYTESIZE)
end

#ensure_argument_not_blank(args) ⇒ Object



38
39
40
41
# File 'lib/jpush/helper/argument.rb', line 38

def ensure_argument_not_blank(args)
  invalid_args = args.select{|key, value| value.blank?}
  raise Utils::Exceptions::InvalidArgumentError.new(invalid_args.keys) unless invalid_args.empty?
end

#ensure_argument_required(args) ⇒ Object



43
44
45
46
# File 'lib/jpush/helper/argument.rb', line 43

def ensure_argument_required(args)
  missing_args = args.select{|key, value| value.nil?}
  raise Utils::Exceptions::MissingArgumentError.new(missing_args.keys) unless missing_args.empty?
end

#ensure_not_over_bytesize(name, value, max_bytesize) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/jpush/helper/argument.rb', line 48

def ensure_not_over_bytesize(name, value, max_bytesize)
  bytesize =
    if value.respond_to? :bytesize
      value.bytesize
    else
      size = value.to_json.bytesize if value.is_a? Hash
      size = value.inject(0){|r, e| r += e.bytesize} if value.is_a? Array
      size
    end
  raise Utils::Exceptions::OverLimitError.new(name, max_bytesize, 'bytes') if bytesize > max_bytesize
end

#ensure_not_over_size(name, value, max_size) ⇒ Object



60
61
62
63
64
65
66
67
68
69
# File 'lib/jpush/helper/argument.rb', line 60

def ensure_not_over_size(name, value, max_size)
  size = value.size
  unit =
    if value.is_a? String
      'characters'
    else
      'items'
    end
  raise Utils::Exceptions::OverLimitError.new(name, max_size, unit) if size > max_size
end

#ensure_word_valid(name, word) ⇒ Object

有效的 tag alias 组成: 字母(区分大小写), 数字, 下划线, 汉字, 特殊字符(@!#$&*+=.|)



72
73
74
# File 'lib/jpush/helper/argument.rb', line 72

def ensure_word_valid(name, word)
  raise Utils::Exceptions::InvalidWordError.new(name, word) unless word.valid_word?
end