Class: OData::Request

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(method = :get, uri = '', data = nil) ⇒ Request


8
9
10
11
12
13
14
15
16
# File 'lib/odata/request.rb', line 8

def initialize(method = :get, uri = '', data = nil)
  @method  = method.to_s.downcase.to_sym
  @uri     = URI(uri)
  @data    = data
  @headers = {
    'Content-Type' => 'application/json',
    'SdkVersion' => 'Graph-ruby-0.1.0'
  }
end

Instance Attribute Details

#dataObject

Returns the value of attribute data


6
7
8
# File 'lib/odata/request.rb', line 6

def data
  @data
end

#headersObject

Returns the value of attribute headers


3
4
5
# File 'lib/odata/request.rb', line 3

def headers
  @headers
end

#methodObject

Returns the value of attribute method


5
6
7
# File 'lib/odata/request.rb', line 5

def method
  @method
end

#uriObject

Returns the value of attribute uri


4
5
6
# File 'lib/odata/request.rb', line 4

def uri
  @uri
end

Instance Method Details

#performObject

Raises:


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/odata/request.rb', line 18

def perform
  response = Net::HTTP
    .new(uri.hostname, uri.port)
    .tap { |h| h.use_ssl = true }
    .send(*send_params)
  raise ServerError.new(response) unless response.code.to_i < 500
  raise AuthenticationError.new(response) if response.code.to_i == 401
  raise AuthorizationError.new(response) if response.code.to_i == 403
  raise ClientError.new(response) unless response.code.to_i < 400
  if response.body
    begin
      OData.convert_keys_to_snake_case(JSON.parse(response.body))
    rescue JSON::ParserError => e
      {}
    end
  else
    {}
  end
end