Class: WeChat::REST::Request

Inherits:
Object
  • Object
show all
Includes:
Symbolizable
Defined in:
lib/we_chat/rest/request.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(_client, request_method, path, options = {}) ⇒ Request

Returns a new instance of Request.



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/we_chat/rest/request.rb', line 17

def initialize(_client, request_method, path, options = {})
  @client = _client
  request_params = options.delete(:params) unless options[:params].nil?
  request_params ||= {}
  request_params[:access_token] = @client.access_token if @client.access_token?
  template = ::Addressable::Template.new(path.start_with?('http') ? path : WeChat.wechat_api_base_url + path + "{?query*}")
  @uri = template.expand({
    "query" => request_params
  })
  set_multipart_options!(request_method, options)
  @path = uri.path
  @options = options
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



13
14
15
# File 'lib/we_chat/rest/request.rb', line 13

def client
  @client
end

#headersObject

Returns the value of attribute headers.



13
14
15
# File 'lib/we_chat/rest/request.rb', line 13

def headers
  @headers
end

#optionsObject

Returns the value of attribute options.



13
14
15
# File 'lib/we_chat/rest/request.rb', line 13

def options
  @options
end

#pathObject

Returns the value of attribute path.



13
14
15
# File 'lib/we_chat/rest/request.rb', line 13

def path
  @path
end

#request_methodObject Also known as: verb

Returns the value of attribute request_method.



13
14
15
# File 'lib/we_chat/rest/request.rb', line 13

def request_method
  @request_method
end

#uriObject

Returns the value of attribute uri.



13
14
15
# File 'lib/we_chat/rest/request.rb', line 13

def uri
  @uri
end

Instance Method Details

#performArray, Hash

Returns:

  • (Array, Hash)


32
33
34
35
36
# File 'lib/we_chat/rest/request.rb', line 32

def perform
  options_key = @request_method == :get ? :params : @request_method == :post ? :json : :form
  response = HTTP.with(@headers).public_send(@request_method, @uri.to_s, options_key => @options)
  fail_or_return_response_body(response)
end