Class: Wechat::Ticket::JsapiBase

Inherits:
Object
  • Object
show all
Defined in:
lib/wechat/ticket/jsapi_base.rb,
lib/generators/wechat/templates/config/initializers/wechat_redis_store.rb

Direct Known Subclasses

CorpJsapiTicket, PublicJsapiTicket

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, access_token, jsapi_ticket_file) ⇒ JsapiBase

Returns a new instance of JsapiBase.



9
10
11
12
13
14
# File 'lib/wechat/ticket/jsapi_base.rb', line 9

def initialize(client, access_token, jsapi_ticket_file)
  @client = client
  @access_token = access_token
  @jsapi_ticket_file = jsapi_ticket_file
  @random_generator = Random.new
end

Instance Attribute Details

#access_ticketObject (readonly)

Returns the value of attribute access_ticket.



7
8
9
# File 'lib/wechat/ticket/jsapi_base.rb', line 7

def access_ticket
  @access_ticket
end

#access_tokenObject (readonly)

Returns the value of attribute access_token.



7
8
9
# File 'lib/wechat/ticket/jsapi_base.rb', line 7

def access_token
  @access_token
end

#clientObject (readonly)

Returns the value of attribute client.



7
8
9
# File 'lib/wechat/ticket/jsapi_base.rb', line 7

def client
  @client
end

#got_ticket_atObject (readonly)

Returns the value of attribute got_ticket_at.



7
8
9
# File 'lib/wechat/ticket/jsapi_base.rb', line 7

def got_ticket_at
  @got_ticket_at
end

#jsapi_ticket_fileObject (readonly)

Returns the value of attribute jsapi_ticket_file.



7
8
9
# File 'lib/wechat/ticket/jsapi_base.rb', line 7

def jsapi_ticket_file
  @jsapi_ticket_file
end

#ticket_life_in_secondsObject (readonly)

Returns the value of attribute ticket_life_in_seconds.



7
8
9
# File 'lib/wechat/ticket/jsapi_base.rb', line 7

def ticket_life_in_seconds
  @ticket_life_in_seconds
end

Instance Method Details

#oauth2_stateObject



26
27
28
29
# File 'lib/wechat/ticket/jsapi_base.rb', line 26

def oauth2_state
  ticket
  @oauth2_state
end

#read_ticketObject



71
72
73
# File 'lib/wechat/ticket/jsapi_base.rb', line 71

def read_ticket
  JSON.parse(File.read(jsapi_ticket_file))
end

#signature(url) ⇒ Object

Obtain the wechat jssdk config signature parameter and return below hash

params = {
  noncestr: noncestr,
  timestamp: timestamp,
  jsapi_ticket: ticket,
  url: url,
  signature: signature
}


39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/wechat/ticket/jsapi_base.rb', line 39

def signature(url)
  params = {
    noncestr: SecureRandom.base64(16),
    timestamp: Time.now.to_i,
    jsapi_ticket: ticket,
    url: url
  }
  pairs = params.keys.sort.map do |key|
    "#{key}=#{params[key]}"
  end
  result = Digest::SHA1.hexdigest pairs.join('&')
  params.merge(signature: result)
end

#ticket(tries = 2) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/wechat/ticket/jsapi_base.rb', line 16

def ticket(tries = 2)
  # Possible two worker running, one worker refresh ticket, other unaware, so must read every time
  read_ticket_from_store
  refresh if remain_life_seconds < @random_generator.rand(30..3 * 60)
  access_ticket
rescue AccessTokenExpiredError
  access_token.refresh
  retry unless (tries -= 1).zero?
end

#write_ticket(ticket_hash) ⇒ Object



75
76
77
# File 'lib/wechat/ticket/jsapi_base.rb', line 75

def write_ticket(ticket_hash)
  File.write(jsapi_ticket_file, ticket_hash.to_json)
end