Module: Ubox

Extended by:
Ubox
Included in:
Ubox
Defined in:
lib/ubox.rb,
lib/ubox/version.rb,
lib/ubox/configuration.rb

Defined Under Namespace

Classes: Configuration

Constant Summary collapse

VERSION =
"0.2.2"

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#configObject



13
14
15
# File 'lib/ubox.rb', line 13

def config
  @config ||= Configuration.new
end

Instance Method Details

#check_product_in_vm(attributes) ⇒ Object

查询售货机货道是否售卖某个商品



97
98
99
# File 'lib/ubox.rb', line 97

def check_product_in_vm(attributes)
  post_request('/checkProductInVm', attributes)
end

#check_vm_status(attributes) ⇒ Object

检查售货机是否联网



102
103
104
# File 'lib/ubox.rb', line 102

def check_vm_status(attributes)
  post_request('/checkVmstatus', attributes)
end

#configure {|config| ... } ⇒ Object

Yields:



17
18
19
# File 'lib/ubox.rb', line 17

def configure
  yield(config)
end

#deliver_trade(attributes) ⇒ Object

出货接口



92
93
94
# File 'lib/ubox.rb', line 92

def deliver_trade(attributes)
  post_request('/deliverTrade', attributes)
end

#notify_order(attributes) ⇒ Object

扫码下单请求 notify_order(tran_id:xxxx,retain_period:300,app_tran_id:xxxx,app_uid:uid)



60
61
62
# File 'lib/ubox.rb', line 60

def notify_order(attributes)
  post_request('/notifyOrder', attributes)
end

#notify_payment(attributes) ⇒ Object

m买取货吗(支付结果通知) notify_payment(tran_id:xxx,pay_time: Time.now.to_i,app_current_time: Time.now.to_i,deliver_now:true)



66
67
68
# File 'lib/ubox.rb', line 66

def notify_payment(attributes)
  post_request('/notifyPayment', attributes)
end

#order_code(attributes) ⇒ Object

根据售货机和商品买码接口



87
88
89
# File 'lib/ubox.rb', line 87

def order_code(attributes)
  post_request('/orderCode', attributes)
end

#post_request(api_path, attributes) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/ubox.rb', line 35

def post_request(api_path, attributes)
  params = {
      app_id: self.config.app_id,
  }.merge(attributes)
  params = params.merge({sign: self.sign(params)})

  puts "\n\n#{api_path}\n"
  puts params

  url = "#{self.config.api_url}/#{api_path}"
  uri = URI(url)
  res = Net::HTTP.post_form(uri, params)

  body = res.body
  puts body
  JSON.parse(body)
end

#product_detail(attributes) ⇒ Object

商品详情



54
55
56
# File 'lib/ubox.rb', line 54

def product_detail(attributes)
  post_request('/getProductDetail', attributes)
end

#product_info(attributes) ⇒ Object

根据商品id和售货机编号获取商品详情



107
108
109
# File 'lib/ubox.rb', line 107

def product_info(attributes)
  post_request('/productInfo', attributes)
end

#product_list(attributes) ⇒ Object

根据售货机查询商品列表接口



82
83
84
# File 'lib/ubox.rb', line 82

def product_list(attributes)
  post_request('/productList', attributes)
end

#search_vm_list(attributes) ⇒ Object

搜索售货机接口



77
78
79
# File 'lib/ubox.rb', line 77

def search_vm_list(attributes)
  post_request('/searchVmList', attributes)
end

#sign(params) ⇒ Object

获取签名



22
23
24
25
26
27
28
# File 'lib/ubox.rb', line 22

def sign(params)
  str = params.keys.reject { |k| k.to_sym==:sign }.sort_by { |k| k }.inject('') do |s, key|
    s += "#{key}=#{params[key]}"
    s
  end
  Digest::SHA1.hexdigest("#{str}_#{self.config.app_key}")
end

#to_params(params) ⇒ Object



30
31
32
# File 'lib/ubox.rb', line 30

def to_params(params)
  params.inject([]) { |a, (k, v)| a<<"#{k}=#{v}"; a }.join("&")
end

#trade_order_status(attributes) ⇒ Object

出货结果询问 trade_order_status(tran_id:xxx)



72
73
74
# File 'lib/ubox.rb', line 72

def trade_order_status(attributes)
  post_request('/getTradeOrderStatus', attributes)
end