Class: FrOData::Service::Request

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

Overview

Encapsulates a single request to an OData service.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(service, url_chunk, options = {}) ⇒ Request

Create a new request

Parameters:

  • service (FrOData::Service)

    Where the request will be sent

  • url_chunk (String)

    Request path, relative to the service URL, including query params

  • options (Hash) (defaults to: {})

    Additional request options



18
19
20
21
22
23
24
25
# File 'lib/frodata/service/request.rb', line 18

def initialize(service, url_chunk, options = {})
  @service = service
  @url_chunk = url_chunk
  @method = options.delete(:method) || :get
  @format = options.delete(:format) || :auto
  @query  = options.delete(:query)
  @options = options
end

Instance Attribute Details

#formatObject

The request format (‘:atom`, `:json`, or `:auto`)



12
13
14
# File 'lib/frodata/service/request.rb', line 12

def format
  @format
end

#methodObject

The HTTP method for this request



10
11
12
# File 'lib/frodata/service/request.rb', line 10

def method
  @method
end

#queryObject (readonly)

The FrOData::Query that generated this request (optional)



8
9
10
# File 'lib/frodata/service/request.rb', line 8

def query
  @query
end

#serviceObject (readonly)

The OData service against which the request is performed



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

def service
  @service
end

Instance Method Details

#content_typeString

The content type for this request. Depends on format.

Returns:

  • (String)


35
36
37
38
39
40
41
42
43
# File 'lib/frodata/service/request.rb', line 35

def content_type
  if format == :auto
    MIME_TYPES.values.join(',')
  elsif MIME_TYPES.has_key? format
    MIME_TYPES[format]
  else
    raise ArgumentError, "Unknown format '#{format}'"
  end
end

#execute(request_options = {}) ⇒ FrOData::Service::Response

Execute the request

Parameters:

  • request_options (Hash) (defaults to: {})

    Request options to pass to Faraday

Returns:



49
50
51
52
53
54
55
# File 'lib/frodata/service/request.rb', line 49

def execute(request_options = {})
  Response.new(service, query) do
    connection.run_request(method, url_chunk, nil, headers) do |conn|
      conn.options.merge! request_options
    end
  end
end

#urlString

Return the full request URL (including service base)

Returns:

  • (String)


29
30
31
# File 'lib/frodata/service/request.rb', line 29

def url
  ::URI.join("#{service.service_url}/", ::URI.escape(url_chunk)).to_s
end