Class: OpenQq::Gateway

Inherits:
Object
  • Object
show all
Extended by:
Signature
Defined in:
lib/open_qq/gateway.rb

Direct Known Subclasses

Request

Constant Summary collapse

OPEN_HTTP_TRANSLATE_ERROR =
2001

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Signature

each_pair_escape, make_callback_source, make_source, parsed_params, signature, url_escape, verify_sig

Constructor Details

#initialize(appid, appkey, env) ⇒ Gateway

Returns a new instance of Gateway.

Examples:

OpenQq::Gateway.new('1111', '2222', 'http://119.147.19.43')
OpenQq::Gateway.new('1111', '2222', 'https://openapi.tencentyun.com')

Parameters:

  • appid (String)
  • appkey (String)
  • env (String)

    调用的环境地址



26
27
28
29
30
# File 'lib/open_qq/gateway.rb', line 26

def initialize(appid, appkey, env)
  @appid   = appid
  @appkey  = appkey
  self.env = env
end

Instance Attribute Details

#appidObject

Returns the value of attribute appid.



17
18
19
# File 'lib/open_qq/gateway.rb', line 17

def appid
  @appid
end

#appkeyObject

Returns the value of attribute appkey.



17
18
19
# File 'lib/open_qq/gateway.rb', line 17

def appkey
  @appkey
end

#envObject

Returns the value of attribute env.



17
18
19
# File 'lib/open_qq/gateway.rb', line 17

def env
  @env
end

Instance Method Details

#get(url, params = {}, options = {}) ⇒ String, Object

Returns unformatted string result or parsed OpenStruct instance.

Examples:

gateway   = OpenQq::Gateway.new('1111', '2222', 'http://119.147.19.43')
 = gateway.get('/v3/user/get_info', {:openid => '11'} )
.nickname # => foo

available option

:raw => true will raw the output
user_info = gateway.get('/v3/user/get_info', {:openid => '11'}, {:raw => true} )
user_info.nickname # => '{"nickname":"foo"}'

Parameters:

  • url (String)

    which api you want to call

  • params (Hash) (defaults to: {})

    extra params attach to the url

  • options (options) (defaults to: {})

Returns:

  • (String, Object)

    unformatted string result or parsed OpenStruct instance



59
60
61
62
63
# File 'lib/open_qq/gateway.rb', line 59

def get(url, params = {}, options = {})
  parsed_params = Gateway.parsed_params( wrap(:get, url, params) )
  get_request = Net::HTTP::Get.new("#{url}?#{parsed_params}")
  self.call( get_request, options.merge(:format => params[:format]) )
end

#post(url, params = {}, options = {}) ⇒ String, Object

Returns unformatted string result or parsed OpenStruct instance.

Parameters:

  • url (String)

    which api you want to call

  • params (Hash) (defaults to: {})

    extra params attach to the url

  • options (options) (defaults to: {})

Returns:

  • (String, Object)

    unformatted string result or parsed OpenStruct instance



68
69
70
71
72
73
# File 'lib/open_qq/gateway.rb', line 68

def post(url, params = {}, options = {})
  post_request = Net::HTTP::Post.new(url)
  post_request.body = Gateway.parsed_params( wrap(:post, url, params) )
  post_request.content_type = 'application/x-www-form-urlencoded'
  self.call( post_request, options.merge(:format => params[:format]) )
end

#wrap(http_method, url, params) ⇒ Object

wrap ‘http_method`, `url`, `params` together



76
77
78
79
80
# File 'lib/open_qq/gateway.rb', line 76

def wrap(http_method, url, params)
  params = params.merge(:appid => @appid)
  params[:sig] = Gateway.signature( "#{@appkey}&", Gateway.make_source(http_method.to_s.upcase, url, params) )
  params
end