Module: Vertebrae::Request

Included in:
API
Defined in:
lib/vertebrae/request.rb

Constant Summary collapse

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

Instance Method Summary collapse

Instance Method Details

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



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

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

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



9
10
11
# File 'lib/vertebrae/request.rb', line 9

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

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



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

def patch_request(path, params={}, options={})
  request(:patch, path, params, options)
end

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



17
18
19
# File 'lib/vertebrae/request.rb', line 17

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

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



21
22
23
# File 'lib/vertebrae/request.rb', line 21

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

#request(method, path, params, options) ⇒ Object

:nodoc:



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

def request(method, path, params, options) # :nodoc:
  if !::Vertebrae::Request::METHODS.include?(method)
    raise ArgumentError, "unknown http method: #{method}"
  end
  connection.options = default_options.merge(initialisation_options.merge(options))

  path =  connection.configuration.prefix + '/' + path

  ::Vertebrae::Base.logger.debug "EXECUTED: #{method} - #{path} with #{params} and #{options}"

  connection.connection.send(method) do |request|

    case method.to_sym
      when *(::Vertebrae::Request::METHODS - ::Vertebrae::Request::METHODS_WITH_BODIES)
        request.body = params.delete('data') if params.has_key?('data')
        request.url(path, params)
      when *::Vertebrae::Request::METHODS_WITH_BODIES
        request.path = path
        request.body = extract_data_from_params(params) unless params.empty?
    end
  end
end