Class: Brauchbar::Request

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

Class Method Summary collapse

Class Method Details

.execute(params = {}) ⇒ Object

Raises:

  • (ArgumentError)


2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/brauchbar/request.rb', line 2

def self.execute(params = {})
  # validation
  raise ArgumentError.new('Method option required') if params[:method].nil?
  raise ArgumentError.new('Unsupported Method') unless %w{ get }.include?(params[:method].to_s)

  raise ArgumentError.new('URI option required') if params[:uri].nil?
  begin
    URI.parse(params[:uri])
  rescue URI::InvalidURIError => e
    raise ArgumentError.new('Invalid URI')
  end

  # clear headers
  params[:headers] ||= {}

  # create request
  session = Patron::Session.new

  # make request
  response = session.request(params[:method], params[:uri], params[:headers])

  # create brauchbar response object
  Brauchbar::Response.new response
end