class DuffelAPI::Services::OrdersService

Public Instance Methods

all(options = {}) click to toggle source

Returns an ‘Enumerator` which can automatically cycle through multiple pages of `Resources::Order`s.

By default, this will use pages of 200 results under the hood, but this can be customised by specifying the ‘:limit` option in the `:params`.

@param options [Hash] options passed to ‘#list`, for example `:params` to

send an HTTP querystring with filters

@return [Enumerator] @raise [Errors::Error] when the Duffel API returns an error

# File lib/duffel_api/services/orders_service.rb, line 82
def all(options = {})
  options[:params] = DEFAULT_ALL_PARAMS.merge(options[:params] || {})

  Paginator.new(
    service: self,
    options: options,
  ).enumerator
end
create(options = {}) click to toggle source

Creates an order

@option [required, Hash] :params the payload for creating the order @return [Resources::Order] @raise [Errors::Error] when the Duffel API returns an error

# File lib/duffel_api/services/orders_service.rb, line 11
def create(options = {})
  path = "/air/orders"

  params = options.delete(:params) || {}
  options[:params] = {}
  options[:params]["data"] = params

  begin
    response = make_request(:post, path, options)

    # Response doesn't raise any errors until #body is called
    response.tap(&:raw_body)
  end

  return if response.raw_body.nil?

  Resources::Order.new(unenvelope_body(response.parsed_body), response)
end
get(id, options = {}) click to toggle source

Retrieves a single order by ID

@param id [String] @return [Resources::Order] @raise [Errors::Error] when the Duffel API returns an error

# File lib/duffel_api/services/orders_service.rb, line 96
def get(id, options = {})
  path = substitute_url_pattern("/air/orders/:id", "id" => id)

  response = make_request(:get, path, options)

  return if response.raw_body.nil?

  Resources::Order.new(unenvelope_body(response.parsed_body), response)
end
list(options = {}) click to toggle source

Lists orders, returning a single page of results.

@option [Hash] :params Parameters to include in the HTTP querystring, including

any filters

@return [ListResponse] @raise [Errors::Error] when the Duffel API returns an error

# File lib/duffel_api/services/orders_service.rb, line 60
def list(options = {})
  path = "/air/orders"

  response = make_request(:get, path, options)

  ListResponse.new(
    response: response,
    unenveloped_body: unenvelope_body(response.parsed_body),
    resource_class: Resources::Order,
  )
end
update(id, options = {}) click to toggle source

Updates an order

@option [required, Hash] :params the payload for updating the order @return [Resources::Order] @raise [Errors::Error] when the Duffel API returns an error

# File lib/duffel_api/services/orders_service.rb, line 35
def update(id, options = {})
  path = substitute_url_pattern("/air/orders/:id", "id" => id)

  params = options.delete(:params) || {}
  options[:params] = {}
  options[:params]["data"] = params

  begin
    response = make_request(:patch, path, options)

    # Response doesn't raise any errors until #body is called
    response.tap(&:raw_body)
  end

  return if response.raw_body.nil?

  Resources::Order.new(unenvelope_body(response.parsed_body), response)
end