class VkMusic::Request::Base
Base
class for most of requests
Attributes
data[R]
@return [Hash]
headers[R]
@return [Hash]
method[R]
@return [String]
path[R]
@return [String]
response[R]
@return [Mechanize::File?]
Public Class Methods
new(path, data = {}, method = 'GET', headers = {})
click to toggle source
Initialize new request @param path [String] @param data [Hash] @param method [String] @param headers [Hash]
# File lib/vk_music/request/base.rb, line 25 def initialize(path, data = {}, method = 'GET', headers = {}) @path = path @data = data @method = method.upcase @headers = headers @response = nil end
Public Instance Methods
call(agent)
click to toggle source
@param agent [Mechanize] @return [self]
# File lib/vk_music/request/base.rb, line 36 def call(agent) before_call log @response = case method when 'GET' then get(agent) when 'POST' then post(agent) else raise(ArgumentError, "unsupported method #{method}") end after_call self end
Private Instance Methods
after_call()
click to toggle source
# File lib/vk_music/request/base.rb, line 72 def after_call; end
before_call()
click to toggle source
# File lib/vk_music/request/base.rb, line 70 def before_call; end
get(agent)
click to toggle source
# File lib/vk_music/request/base.rb, line 59 def get(agent) uri = URI(path) uri.query = URI.encode_www_form(data) agent.get(uri, [], nil, headers) end
log()
click to toggle source
# File lib/vk_music/request/base.rb, line 53 def log VkMusic.log.debug('request') do "#{method} to '#{path}', with data: #{data}, headers: #{headers}" end end
post(agent)
click to toggle source
# File lib/vk_music/request/base.rb, line 65 def post(agent) uri = URI(path) agent.post(uri, data, headers) end