class Yunpiansms::Service

Attributes

apikey[RW]
connection_adapter[RW]
debug_flg[RW]

Public Class Methods

config() { |service = new;| ... } click to toggle source
# File lib/yunpiansms.rb, line 16
def self.config
  yield service = new; service
end
new(apikey: "", connection_adapter: :net_http, debug_flg: false) click to toggle source
# File lib/yunpiansms.rb, line 10
def initialize(apikey: "", connection_adapter: :net_http, debug_flg: false)
  @apikey = apikey
  @connection_adapter = connection_adapter
  @debug_flg = debug_flg
end

Public Instance Methods

get_record(options={}) click to toggle source
# File lib/yunpiansms.rb, line 28
def get_record(options={})
  http.post(Yunpiansms::SmsResources::GET_URL,format_content(options))
end
send_to(content, mobiles = nil) click to toggle source
# File lib/yunpiansms.rb, line 20
def send_to(content, mobiles = nil)
  http.post(Yunpiansms::SmsResources::SEND_URL,params_data(content, mobiles))
end
tpl_send_to(content, mobiles = nil, tpl_id = nil, tpl_value=nil) click to toggle source
# File lib/yunpiansms.rb, line 24
def tpl_send_to(content, mobiles = nil, tpl_id = nil, tpl_value=nil)
  http.post(Yunpiansms::SmsResources::TPL_SEND_URL,params_data_tpl(content, mobiles, tpl_id,tpl_value))
end

Private Instance Methods

format_content(content = {},mobiles) click to toggle source
# File lib/yunpiansms.rb, line 60
def format_content(content = {},mobiles)
  params = {}
  content.map do |k,v|
    params["#{k}".to_sym] = v
  end
  params[:apikey] = params.delete(:apikey) || @apikey
  params[:mobile] = mobile_format(mobiles || params[:mobile])
  params
end
format_tpl_value(tpl_value) click to toggle source
# File lib/yunpiansms.rb, line 70
def format_tpl_value(tpl_value)
  if tpl_value.is_a?(Hash)
    tpl_value = parse_tpl_value(tpl_value)
  else
    tpl_value
  end
end
http() click to toggle source
# File lib/yunpiansms.rb, line 47
def http 
  host = Yunpiansms::SmsResources::SEND_DOMAIN
  Yunpiansms::HttpBase.new(host, @connection_adapter)
end
mobile_format(mobiles = nil) click to toggle source
# File lib/yunpiansms.rb, line 52
def mobile_format(mobiles = nil)
  if mobiles.is_a?(Array)
    mobiles = Array(mobiles).join(',')
  else
    mobiles = mobiles
  end
end
params_data(content, mobiles) click to toggle source
# File lib/yunpiansms.rb, line 34
def params_data(content, mobiles)
  content          = format_content(content,mobiles)
  content[:text]   = "#{format_tpl_value(content[:text])}"
  content
end
params_data_tpl(content, mobiles = nil, tpl_id = nil, tpl_value=nil) click to toggle source
# File lib/yunpiansms.rb, line 40
def params_data_tpl(content, mobiles = nil, tpl_id = nil, tpl_value=nil)
  content               = format_content(content,mobiles)
  content[:tpl_id]      = tpl_id || content.delete(:tpl_id)
  content[:tpl_value]   = "#{format_tpl_value(content[:tpl_value])}"
  content
end
parse_tpl_value(tpl_value) click to toggle source
# File lib/yunpiansms.rb, line 78
def parse_tpl_value tpl_value
  tpl_value.map { |k, v| "##{k}#=#{v}" }.join('&')
end