Module: Yandex::Webmaster::Request

Included in:
Base
Defined in:
lib/yandex-webmaster/request.rb,
lib/yandex-webmaster.rb,
lib/yandex-webmaster/request/oauth2.rb

Overview

Defines HTTP verbs

Defined Under Namespace

Classes: OAuth2

Constant Summary collapse

METHODS =
[:get, :post, :put, :delete]
METHODS_WITH_BODIES =
[:post, :put]

Instance Method Summary collapse

Instance Method Details

#delete_request(path, params = {}, options = {}) ⇒ Object



24
25
26
# File 'lib/yandex-webmaster/request.rb', line 24

def delete_request(path, params={}, options={})
  request(:delete, path, params, options)
end

#get_request(path, params = {}, options = {}) ⇒ Object



12
13
14
# File 'lib/yandex-webmaster/request.rb', line 12

def get_request(path, params={}, options={})
  request(:get, path, params, options)
end

#post_request(path, params = {}, options = {}) ⇒ Object



16
17
18
# File 'lib/yandex-webmaster/request.rb', line 16

def post_request(path, params={}, options={})
  request(:post, path, params, options)
end

#put_request(path, params = {}, options = {}) ⇒ Object



20
21
22
# File 'lib/yandex-webmaster/request.rb', line 20

def put_request(path, params={}, options={})
  request(:put, path, params, options)
end

#request(method, path, params = {}, options = {}) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/yandex-webmaster/request.rb', line 28

def request(method, path, params = {}, options = {})
  if !METHODS.include?(method)
    raise ArgumentError, "unkown http method: #{method}"
  end

  puts "EXECUTED: #{method} - #{path} with #{params} and #{options}" if ENV['DEBUG']

  conn = connection(options)
  if conn.path_prefix != '/' && path.index(conn.path_prefix) != 0 && !path.start_with?('https://', 'http://')
    path = (conn.path_prefix + path).gsub(/\/(\/)*/, '/')
  end

  response = conn.send(method) do |request|
    case method.to_sym
    when *(METHODS - METHODS_WITH_BODIES)
      request.body = params.delete('data') if params.has_key?('data')
      request.url(path, params)
    when *METHODS_WITH_BODIES            
      request.url(path)            
      request.body = self.extract_data_from_params(params) unless params.empty?            
      request.headers['Content-Length'] = request.body.size.to_s
    end
  end        
end