class Fakesite::Wechat::Base

Public Class Methods

match(external_uri) click to toggle source
# File lib/fakesite/wechat/base.rb, line 56
def self.match(external_uri)
  external_uri.host == Host
end

Public Instance Methods

parameters() click to toggle source
# File lib/fakesite/wechat/base.rb, line 6
def parameters
  now = Time.now.to_i.to_s
  {
    "code" => now,
    "state" => external_params["state"],
    "openid" => openid.to_s,
    "access_token" => "T#{now}",
    "refresh_token" => "R#{now}",
    "scope" => external_params["scope"],
    "nickname" => nickname,
    "sex" => sex,
    "province" => province,
    "city" => city,
    "country" => country,
    "headimgurl" => headimgurl
  }
end
return_parameters() click to toggle source
# File lib/fakesite/wechat/base.rb, line 24
def return_parameters
  openid = params["openid"]

  body = {}
  ["openid", "access_token", "refresh_token", "scope"].each do |key|
    body[key] = params[key]
    params.delete(key)
  end
  body["expires_in"] = 7200

  stub_request(:get, "https://#{ApiHost}/sns/oauth2/access_token")
    .with(:query => hash_including({:code => params["code"]}))
    .to_return(:status => 200, :body => body.to_json)

  body = {}
  ["nickname", "sex", "province", "city", "country", "headimgurl"].each do |key|
    body[key] = params[key]
    params.delete(key)
  end
  body["openid"] = openid

  stub_request(:get, "https://#{ApiHost}/sns/userinfo")
    .with(:query => hash_including({:openid => openid}))
    .to_return(:status => 200, :body => body.to_json)

  return params
end
return_url() click to toggle source
# File lib/fakesite/wechat/base.rb, line 52
def return_url
  external_params["redirect_uri"]
end

Protected Instance Methods

get_value(obj, attr_name) click to toggle source
# File lib/fakesite/wechat/base.rb, line 62
def get_value(obj, attr_name)
  !obj.nil? && obj.respond_to?(attr_name) ? obj.send(attr_name) : nil
end
openid() click to toggle source
# File lib/fakesite/wechat/base.rb, line 66
def openid
  get_value(user, :id)
end