Class: Haoyaoshi::Client

Inherits:
Object
  • Object
show all
Includes:
Api::Drug, Api::EInvoice, Api::Image, Api::Logistics, Api::Order, Api::OrderCenter, Api::Stock, MonitorMixin
Defined in:
lib/haoyaoshi/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Api::OrderCenter

#get_syn_stock_info

Methods included from Api::Stock

#get_branch_stocks, #get_stock

Methods included from Api::EInvoice

#get_e_invoice_url

Methods included from Api::Image

#img_url

Methods included from Api::Logistics

#get_company, #get_deliveries, #get_deliveries_list

Methods included from Api::Order

#delete_batch_order, #delete_order, #get_order_rx, #post_order, #post_order_prescription_img, #post_order_prescription_url, #post_order_rx

Methods included from Api::Drug

#get_drug_image, #get_drug_list, #get_drug_price

Constructor Details

#initialize(client_id, client_secret, channel, parnterkey, grant_type = "client_credentials", scope = "order", options = {}) ⇒ Client

options: redis_key, custom_access_token



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/haoyaoshi/client.rb', line 23

def initialize(client_id, client_secret, channel , parnterkey, grant_type = "client_credentials" , scope = "order", options={})
  @client_id = client_id
  @client_secret = client_secret
  @expired_at = Time.now.to_i
  @channel = channel
  @parnterkey = parnterkey
  @grant_type =  grant_type if grant_type.present?
  @scope = scope
  @redis_key = security_redis_key(options[:redis_key] || "haoyaoshi_#{client_id}")
  @custom_access_token = options[:custom_access_token]
  @custom_token_type = options[:custom_token_type]
  super() # Monitor#initialize
end

Instance Attribute Details

#access_tokenObject

Returns the value of attribute access_token.



20
21
22
# File 'lib/haoyaoshi/client.rb', line 20

def access_token
  @access_token
end

#channelObject

Time.now + expires_in



19
20
21
# File 'lib/haoyaoshi/client.rb', line 19

def channel
  @channel
end

#client_idObject

Time.now + expires_in



19
20
21
# File 'lib/haoyaoshi/client.rb', line 19

def client_id
  @client_id
end

#client_secretObject

Time.now + expires_in



19
20
21
# File 'lib/haoyaoshi/client.rb', line 19

def client_secret
  @client_secret
end

#custom_access_tokenObject

Returns the value of attribute custom_access_token.



20
21
22
# File 'lib/haoyaoshi/client.rb', line 20

def custom_access_token
  @custom_access_token
end

#expired_atObject

Time.now + expires_in



19
20
21
# File 'lib/haoyaoshi/client.rb', line 19

def expired_at
  @expired_at
end

#grant_typeObject

Time.now + expires_in



19
20
21
# File 'lib/haoyaoshi/client.rb', line 19

def grant_type
  @grant_type
end

#parnterkeyObject

Time.now + expires_in



19
20
21
# File 'lib/haoyaoshi/client.rb', line 19

def parnterkey
  @parnterkey
end

#redis_keyObject

Returns the value of attribute redis_key.



20
21
22
# File 'lib/haoyaoshi/client.rb', line 20

def redis_key
  @redis_key
end

#scopeObject

Time.now + expires_in



19
20
21
# File 'lib/haoyaoshi/client.rb', line 19

def scope
  @scope
end

#token_typeObject

Returns the value of attribute token_type.



20
21
22
# File 'lib/haoyaoshi/client.rb', line 20

def token_type
  @token_type
end

#token_type_tokenObject

Returns the value of attribute token_type_token.



20
21
22
# File 'lib/haoyaoshi/client.rb', line 20

def token_type_token
  @token_type_token
end

Instance Method Details

#get_access_tokenObject

return token



38
39
40
41
# File 'lib/haoyaoshi/client.rb', line 38

def get_access_token
  return custom_access_token if !custom_access_token.nil?
  synchronize{ token_store.access_token }
end

#get_token_typeObject

return token



44
45
46
47
# File 'lib/haoyaoshi/client.rb', line 44

def get_token_type
  return custom_token_type if !custom_token_type.nil?
  synchronize{ token_store.token_type }
end

#http_delete(url, delete_body = {}, url_params = {}, endpoint = "plain") ⇒ Object



70
71
72
73
74
# File 'lib/haoyaoshi/client.rb', line 70

def http_delete(url, delete_body={}, url_params={}, endpoint="plain")
  url_params = access_token_param.merge(url_params)
  url = "#{url}?access_token=#{get_access_token}"
  Haoyaoshi.http_delete_without_token(url, delete_body, url_params, endpoint)
end

#http_drug_get(url, url_params = {}, path = nil, endpoint = "plain") ⇒ Object

暴露出:http_get,http_post两个方法,方便第三方开发者扩展未开发的微信API。



77
78
79
80
81
82
83
# File 'lib/haoyaoshi/client.rb', line 77

def http_drug_get(url, url_params = {}, path = nil, endpoint="plain")
  url_params = {channel: @channel}.merge(url_params)
  tmp_option = url_params.deep_dup
  url_params[:sign] = sign(tmp_option,path)
  Rails.logger.info("#{url_params[:sign]}")
  Haoyaoshi.http_get_without_token(url, url_params, endpoint)
end

#http_get(url, url_params = {}, endpoint = "plain") ⇒ Object

暴露出:http_get,http_post两个方法,方便第三方开发者扩展未开发的微信API。



60
61
62
63
# File 'lib/haoyaoshi/client.rb', line 60

def http_get(url, url_params={}, endpoint="plain")
  url_params = url_params.merge(access_token_param)
  Haoyaoshi.http_get_without_token(url, url_params, endpoint)
end

#http_post(url, post_body = {}, url_params = {}, endpoint = "plain") ⇒ Object



65
66
67
68
# File 'lib/haoyaoshi/client.rb', line 65

def http_post(url, post_body={}, url_params={}, endpoint="plain")
  url_params = access_token_param.merge(url_params)
  Haoyaoshi.http_post_without_token(url, post_body, url_params, endpoint)
end

#is_valid?Boolean

检查appid和app_secret是否有效。

Returns:

  • (Boolean)


50
51
52
53
# File 'lib/haoyaoshi/client.rb', line 50

def is_valid?
  return true if !custom_access_token.nil?
  token_store.valid?
end

#token_storeObject



55
56
57
# File 'lib/haoyaoshi/client.rb', line 55

def token_store
  Token::Store.init_with(self)
end