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

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')

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

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

Instance Attribute Details

#appidObject

Returns the value of attribute appid


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

def appid
  @appid
end

#appkeyObject

Returns the value of attribute appkey


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

def appkey
  @appkey
end

#envObject

Returns the value of attribute env


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

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
 = gateway.get('/v3/user/get_info', {:openid => '11'}, {:raw => true} )
.nickname # => '{"nickname":"foo"}'

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

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


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

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


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

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