class Sawyer::Relation

Attributes

agent[R]
available_methods[R]
href_template[R]
method[R]
name[R]

Public Class Methods

new(agent, name, href, method = nil) click to toggle source

A Relation represents an available next action for a resource.

agent - The Sawyer::Agent that made the request. name - The Symbol name of the relation. href - The String URL of the location of the next action. method - The Symbol HTTP method. Default: :get

# File lib/sawyer/relation.rb, line 107
def initialize(agent, name, href, method = nil)
  @agent = agent
  @name = name.to_sym
  @href = href
  @href_template = Addressable::Template.new(href.to_s)

  methods = nil

  if method.is_a? String
    if method.size.zero?
      method = nil
    else
      method.downcase!
      methods = method.split(',').map! do |m|
        m.strip!
        m.to_sym
      end
      method = methods.first
    end
  end

  @method = (method || :get).to_sym
  @available_methods = Set.new methods || [@method]
end

Public Instance Methods

call(data = nil, options = nil) click to toggle source

Public: Makes an API request with the curent Relation.

data - The Optional Hash or Resource body to be sent. :get or :head

requests can have no body, so this can be the options Hash
instead.

options - Hash of option to configure the API request.

:headers - Hash of API headers to set.
:query   - Hash of URL query params to set.
:method  - Symbol HTTP method.

Raises ArgumentError if the :method value is not in @available_methods. Returns a Sawyer::Response.

# File lib/sawyer/relation.rb, line 258
def call(data = nil, options = nil)
  m = options && options[:method]
  if m && !@agent.allow_undefined_methods? && !@available_methods.include?(m == :head ? :get : m)
    raise ArgumentError, "method #{m.inspect} is not available: #{@available_methods.to_a.inspect}"
  end

  @agent.call m || @method, @href_template, data, options
end
delete(data = nil, options = nil) click to toggle source

Public: Makes an API request with the curent Relation using DELETE.

data - The Optional Hash or Resource body to be sent. options - Hash of option to configure the API request.

:headers - Hash of API headers to set.
:query   - Hash of URL query params to set.
:method  - Symbol HTTP method.

Returns a Sawyer::Response.

# File lib/sawyer/relation.rb, line 220
def delete(data = nil, options = nil)
  options ||= {}
  options[:method] = :delete
  call data, options
end
get(options = nil) click to toggle source

Public: Makes an API request with the curent Relation using GET.

data - The Optional Hash or Resource body to be sent. :get or :head

requests can have no body, so this can be the options Hash
instead.

options - Hash of option to configure the API request.

:headers - Hash of API headers to set.
:query   - Hash of URL query params to set.
:method  - Symbol HTTP method.

Returns a Sawyer::Response.

# File lib/sawyer/relation.rb, line 160
def get(options = nil)
  options ||= {}
  options[:method] = :get
  call options
end
head(options = nil) click to toggle source

Public: Makes an API request with the curent Relation using HEAD.

data - The Optional Hash or Resource body to be sent. :get or :head

requests can have no body, so this can be the options Hash
instead.

options - Hash of option to configure the API request.

:headers - Hash of API headers to set.
:query   - Hash of URL query params to set.
:method  - Symbol HTTP method.

Returns a Sawyer::Response.

# File lib/sawyer/relation.rb, line 143
def head(options = nil)
  options ||= {}
  options[:method] = :head
  call options
end
href(options = nil) click to toggle source
# File lib/sawyer/relation.rb, line 241
def href(options = nil)
  return @href if @href_template.nil?
  @href_template.expand(options || {}).to_s
end
inspect() click to toggle source
# File lib/sawyer/relation.rb, line 267
def inspect
  %Q(#<#{self.class}: #{@name}: #{@method} #{@href_template}>)
end
options(data = nil, opt = nil) click to toggle source

Public: Makes an API request with the curent Relation using OPTIONS.

data - The Optional Hash or Resource body to be sent. options - Hash of option to configure the API request.

:headers - Hash of API headers to set.
:query   - Hash of URL query params to set.
:method  - Symbol HTTP method.

Returns a Sawyer::Response.

# File lib/sawyer/relation.rb, line 235
def options(data = nil, opt = nil)
  opt ||= {}
  opt[:method] = :options
  call data, opt
end
patch(data = nil, options = nil) click to toggle source

Public: Makes an API request with the curent Relation using PATCH.

data - The Optional Hash or Resource body to be sent. options - Hash of option to configure the API request.

:headers - Hash of API headers to set.
:query   - Hash of URL query params to set.
:method  - Symbol HTTP method.

Returns a Sawyer::Response.

# File lib/sawyer/relation.rb, line 205
def patch(data = nil, options = nil)
  options ||= {}
  options[:method] = :patch
  call data, options
end
post(data = nil, options = nil) click to toggle source

Public: Makes an API request with the curent Relation using POST.

data - The Optional Hash or Resource body to be sent. options - Hash of option to configure the API request.

:headers - Hash of API headers to set.
:query   - Hash of URL query params to set.
:method  - Symbol HTTP method.

Returns a Sawyer::Response.

# File lib/sawyer/relation.rb, line 175
def post(data = nil, options = nil)
  options ||= {}
  options[:method] = :post
  call data, options
end
put(data = nil, options = nil) click to toggle source

Public: Makes an API request with the curent Relation using PUT.

data - The Optional Hash or Resource body to be sent. options - Hash of option to configure the API request.

:headers - Hash of API headers to set.
:query   - Hash of URL query params to set.
:method  - Symbol HTTP method.

Returns a Sawyer::Response.

# File lib/sawyer/relation.rb, line 190
def put(data = nil, options = nil)
  options ||= {}
  options[:method] = :put
  call data, options
end