Class: Wechat::Request

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(http_verb, path, params = {}) ⇒ Request

Returns a new instance of Request.



6
7
8
9
10
# File 'lib/wechat/request.rb', line 6

def initialize(http_verb, path, params = {})
  self.path = path
  self.http_verb = http_verb
  self.params = params
end

Instance Attribute Details

#http_verbObject

Returns the value of attribute http_verb.



4
5
6
# File 'lib/wechat/request.rb', line 4

def http_verb
  @http_verb
end

#paramsObject

Returns the value of attribute params.



4
5
6
# File 'lib/wechat/request.rb', line 4

def params
  @params
end

#pathObject

Returns the value of attribute path.



4
5
6
# File 'lib/wechat/request.rb', line 4

def path
  @path
end

Instance Method Details

#access_tokenObject



25
26
27
# File 'lib/wechat/request.rb', line 25

def access_token
  Wechat::AccessToken.new.token
end

#append_access_tokenObject



38
39
40
41
42
43
44
# File 'lib/wechat/request.rb', line 38

def append_access_token
  if @path.include?'?'
    return "&access_token=#{access_token}"
  else
    return "?&access_token=#{access_token}"
  end
end

#send_requestObject



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/wechat/request.rb', line 12

def send_request
  case http_verb.to_sym
  when :post
    RestClient.post(wechat_uri, params) do |response, request, result, &block|
      Wechat::Response.new(response).result
    end
  else 
    RestClient.get(wechat_uri, :content_type => :json) do |response, request, result, &block|
      Wechat::Response.new(response).result      
    end
  end
end

#wechat_uriObject



29
30
31
32
33
34
35
36
# File 'lib/wechat/request.rb', line 29

def wechat_uri
  if (@path.include?'media/upload') || (@path.include?'media/get')
    base = Wechat.file_endpoint
  else
    base = Wechat.api_endpoint
  end
  [base, @path].join('/') + append_access_token 
end