class Sawyer::Agent

Constants

NO_BODY

Attributes

serializer[W]
allow_undefined_methods[RW]

Public Class Methods

decode(data) click to toggle source
# File lib/sawyer/agent.rb, line 23
def self.decode(data)
  serializer.decode(data)
end
encode(data) click to toggle source
# File lib/sawyer/agent.rb, line 19
def self.encode(data)
  serializer.encode(data)
end
new(endpoint, options = nil) { |conn| ... } click to toggle source

Agents handle making the requests, and passing responses to Sawyer::Response.

endpoint - String URI of the API entry point. options - Hash of options.

:allow_undefined_methods  - Allow relations to call all the HTTP verbs,
                            not just the ones defined.
:faraday                  - Optional Faraday::Connection to use.
:links_parser             - Optional parser to parse link relations
                            Defaults: Sawyer::LinkParsers::Hal.new
:serializer               - Optional serializer Class.  Defaults to
                            self.serializer_class.

Yields the Faraday::Connection if a block is given.

# File lib/sawyer/agent.rb, line 41
def initialize(endpoint, options = nil)
  @endpoint = endpoint
  @conn = (options && options[:faraday]) || Faraday.new
  @serializer = (options && options[:serializer]) || self.class.serializer
  @links_parser = (options && options[:links_parser]) || Sawyer::LinkParsers::Hal.new
  @allow_undefined_methods = (options && options[:allow_undefined_methods])
  @conn.url_prefix = @endpoint
  yield @conn if block_given?
end
serializer() click to toggle source
# File lib/sawyer/agent.rb, line 15
def self.serializer
  @serializer ||= Serializer.any_json
end

Public Instance Methods

allow_undefined_methods?() click to toggle source
# File lib/sawyer/agent.rb, line 137
def allow_undefined_methods?
  !!@allow_undefined_methods
end
call(method, url, data = nil, options = nil) click to toggle source

Makes a request through Faraday.

method - The Symbol name of an HTTP method. url - The String URL to access. This can be relative to the Agent's

endpoint.

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.

Returns a Sawyer::Response.

# File lib/sawyer/agent.rb, line 85
def call(method, url, data = nil, options = nil)
  if NO_BODY.include?(method)
    options ||= data
    data      = nil
  end

  options ||= {}
  url = expand_url(url, options[:uri])
  started = nil
  res = @conn.send method, url do |req|
    if data
      req.body = data.is_a?(String) ? data : encode_body(data)
    end
    if params = options[:query]
      req.params.update params
    end
    if headers = options[:headers]
      req.headers.update headers
    end
    started = Time.now
  end

  Response.new self, res, :sawyer_started => started, :sawyer_ended => Time.now
end
decode_body(str) click to toggle source

Decodes a String response body to a resource.

str - The String body from the response.

Returns an Object resource (Hash by default).

# File lib/sawyer/agent.rb, line 124
def decode_body(str)
  @serializer.decode(str)
end
encode_body(data) click to toggle source

Encodes an object to a string for the API request.

data - The Hash or Resource that is being sent.

Returns a String.

# File lib/sawyer/agent.rb, line 115
def encode_body(data)
  @serializer.encode(data)
end
expand_url(url, options = nil) click to toggle source
# File lib/sawyer/agent.rb, line 132
def expand_url(url, options = nil)
  tpl = url.respond_to?(:expand) ? url : Addressable::Template.new(url.to_s)
  tpl.expand(options || {}).to_s
end
inspect() click to toggle source
# File lib/sawyer/agent.rb, line 141
def inspect
  %Q(<#{self.class} #{@endpoint}>)
end
marshal_dump() click to toggle source
# File lib/sawyer/agent.rb, line 150
def marshal_dump
  [@endpoint]
end
marshal_load(dumped) click to toggle source
# File lib/sawyer/agent.rb, line 154
def marshal_load(dumped)
  @endpoint = *dumped.shift(1)
end
rels() click to toggle source

Public: Retains a reference to the root relations of the API.

Returns a Sawyer::Relation::Map.

# File lib/sawyer/agent.rb, line 54
def rels
  @rels ||= root.data._rels
end
root() click to toggle source

Public: Retains a reference to the root response of the API.

Returns a Sawyer::Response.

# File lib/sawyer/agent.rb, line 61
def root
  @root ||= start
end
start() click to toggle source

Public: Hits the root of the API to get the initial actions.

Returns a Sawyer::Response.

# File lib/sawyer/agent.rb, line 68
def start
  call :get, @endpoint
end
to_yaml_properties() click to toggle source

private

# File lib/sawyer/agent.rb, line 146
def to_yaml_properties
  [:@endpoint]
end