Class: MpWeixin::Interface::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/mp_weixin/interface/base.rb

Overview

The Base class of API

Direct Known Subclasses

Group, Menu, Message, Promotion, User

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Base

Returns a new instance of Base.



6
7
8
# File 'lib/mp_weixin/interface/base.rb', line 6

def initialize(client)
  @client = client
end

Instance Method Details

#default_request_paramsObject



10
11
12
13
14
15
# File 'lib/mp_weixin/interface/base.rb', line 10

def default_request_params
  access_token = @client.token.token
  #request_params = {"oauth_consumer_key" => self.id, "access_token" => access_token, "openid" => params["openid"], "oauth_version" => "2.a", "scope" => "all"}
  # {"appid" => @client.id, "access_token" => access_token}
  {"access_token" => access_token}
end

#get(path, opts = {}, &block) ⇒ Object



33
34
35
# File 'lib/mp_weixin/interface/base.rb', line 33

def get(path, opts={}, &block)
  request(:get, path, opts, &block)
end

#post(path, opts = {}, &block) ⇒ Object



37
38
39
# File 'lib/mp_weixin/interface/base.rb', line 37

def post(path, opts={}, &block)
  request(:post, path, opts, &block)
end

#request(verb, path, opts = {}, &block) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/mp_weixin/interface/base.rb', line 17

def request(verb, path, opts={}, &block)
  unless @client.is_authorized?
    raise "I can't find a valid access_token. Forgot to get it or expired?"
  end

  opts[:params] ||= {}
  #opts[:params].merge!(default_request_params)
  opts = ActiveSupport::HashWithIndifferentAccess.new(opts)

  response = @client.token.request(verb, path, opts, &block)
  if response.error
    raise Error.new(response)
  end
  response
end