Class: Qplus::App

Inherits:
Object
  • Object
show all
Defined in:
lib/qplus.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app_id, app_secret, lang = 2052) ⇒ App

Returns a new instance of App.



17
18
19
20
21
# File 'lib/qplus.rb', line 17

def initialize(app_id, app_secret, lang = 2052)
  @app_id = app_id
  @app_secret = app_secret + "&"
  @lang = lang
end

Instance Attribute Details

#app_idObject

Returns the value of attribute app_id.



15
16
17
# File 'lib/qplus.rb', line 15

def app_id
  @app_id
end

#app_secretObject

Returns the value of attribute app_secret.



15
16
17
# File 'lib/qplus.rb', line 15

def app_secret
  @app_secret
end

#langObject

Returns the value of attribute lang.



15
16
17
# File 'lib/qplus.rb', line 15

def lang
  @lang
end

Instance Method Details

#check_login(open_data) ⇒ Object



39
40
41
# File 'lib/qplus.rb', line 39

def (open_data)
  send_request "app_verify", open_data
end

#check_sig(url) ⇒ Object



43
44
45
46
47
# File 'lib/qplus.rb', line 43

def check_sig(url)
  data = url[(url.index("?") + 1)..(url.index("&sig") - 1)]
  sig = url[url.index("&sig=") + "&sig".length + 1, url.length]
  sig.downcase == get_sig(data)
end

#get_login_params(user_ip) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/qplus.rb', line 49

def (user_ip)
  data = {
    :app_id => @app_id,
    :app_nonce => get_nonce,
    :app_ts => Time.now.utc.to_i,
    :app_userip => user_ip,
    :app_lang => @lang
  }

  data = Hash[data.sort]

  params = URI.encode_www_form data
   = params + "&sig=" + get_sig("app_qqlogin" + params)
end

#get_nonceObject



68
69
70
# File 'lib/qplus.rb', line 68

def get_nonce
  rand 1_000_000_000..9_999_999_999
end

#get_sig(data) ⇒ Object



64
65
66
# File 'lib/qplus.rb', line 64

def get_sig(data)
  OpenSSL::HMAC.hexdigest("sha1", @app_secret, data) 
end

#get_userinfo(open_data) ⇒ Object



35
36
37
# File 'lib/qplus.rb', line 35

def get_userinfo(open_data)
  send_request "app_get_userinfo",  open_data
end

#push(data) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/qplus.rb', line 72

def push(data)
  data.update "APPID" => @app_id,
              "TIME" => Time.now.utc.to_i,
              "NONCE" => get_nonce
  data = Hash[data.sort]
  # don't encode here
  sig = get_sig "app_push&#{ data.collect { |i| "#{i.first}=#{i.last}" }.join("&") }"
  params = URI::encode_www_form data
  push_uri = URI("#{Qplus::PUSH_URL}?#{params}&IDENTIFYSTRING=#{sig}")
  JSON Net::HTTP.get(push_uri)
end

#send_request(action, open_data) ⇒ Object



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

def send_request(action, open_data)
  open_data.update  :app_id => @app_id,
                    :app_ts => Time.now.utc.to_i,
                    :app_nonce => get_nonce
  open_data = Hash[open_data.sort]
  sig = get_sig "#{action}&#{URI.encode_www_form open_data}"
  open_data.update :sig => sig
  uri = URI(Qplus::OPEN_URL + action)
  res = Net::HTTP.post_form(uri, open_data)
  JSON.parse res.body
end