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.



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

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.



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

def access_ticket
  @access_ticket
end

#access_tokenObject (readonly)

Returns the value of attribute access_token.



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

def access_token
  @access_token
end

#clientObject (readonly)

Returns the value of attribute client.



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

def client
  @client
end

#got_ticket_atObject (readonly)

Returns the value of attribute got_ticket_at.



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

def got_ticket_at
  @got_ticket_at
end

#jsapi_ticket_fileObject (readonly)

Returns the value of attribute jsapi_ticket_file.



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

def jsapi_ticket_file
  @jsapi_ticket_file
end

#ticket_life_in_secondsObject (readonly)

Returns the value of attribute ticket_life_in_seconds.



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

def ticket_life_in_seconds
  @ticket_life_in_seconds
end

Instance Method Details

#oauth2_stateObject



28
29
30
31
# File 'lib/wechat/ticket/jsapi_base.rb', line 28

def oauth2_state
  ticket
  @oauth2_state
end

#read_ticketObject



73
74
75
# File 'lib/wechat/ticket/jsapi_base.rb', line 73

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
}


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

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



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

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



77
78
79
# File 'lib/wechat/ticket/jsapi_base.rb', line 77

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