module Ahora::Resource

Attributes

document_parser[W]

Public Instance Methods

collection(*args, &block) click to toggle source
# File lib/ahora/resource.rb, line 65
def collection(*args, &block)
  instantiator, response = extract_parser_from_args(*args, &block)
  Collection.new instantiator, document_parser, response
end
connection() click to toggle source
# File lib/ahora/resource.rb, line 40
def connection
  Faraday.new(host.dup, connection_options) do |conn|
    conn.use Faraday::Response::RaiseError
    extend_middleware(conn.builder)
    unless conn.builder.handlers.any? {|mid| mid.klass < Faraday::Adapter }
      conn.adapter Faraday.default_adapter
    end
  end
end
extend_middleware(builder) click to toggle source

@abstract override to use custom Faraday middleware

Calls superclass method
# File lib/ahora/resource.rb, line 51
def extend_middleware(builder)
  super if defined? super
end
get(url, params = nil) { |req| ... } click to toggle source
# File lib/ahora/resource.rb, line 7
def get(url, params = nil)
  begin
    connection.run_request(:get, url, nil, nil) do |req|
      req.params.update(params) if params
      yield req if block_given?
    end
  rescue => e
    handle_exception(e)
  end
end
headers() click to toggle source

FIXME test (FakeWeb cannot test request headers) @abstract override to set custome headers returns a hash with a string for each key

Calls superclass method
# File lib/ahora/resource.rb, line 58
def headers
  (defined?(super) ? super.dup : {}).update \
    :user_agent   => 'Ahora',
    :content_type => 'application/xml',
    :accept       => 'application/xml'
end
post(url, body = nil) { |req| ... } click to toggle source

FIXME test

# File lib/ahora/resource.rb, line 19
def post(url, body = nil)
  begin
    connection.run_request(:post, url, body, nil) do |req|
      yield req if block_given?
    end
  rescue => e
    handle_exception(e)
  end
end
put(url, body = nil) { |req| ... } click to toggle source

FIXME test

# File lib/ahora/resource.rb, line 30
def put(url, body = nil)
  begin
    connection.run_request(:put, url, body, nil) do |req|
      yield req if block_given?
    end
  rescue => e
    handle_exception(e)
  end
end
single(*args, &block) click to toggle source
# File lib/ahora/resource.rb, line 70
def single(*args, &block)
  instantiator, response = extract_parser_from_args(*args, &block)
  Response.new instantiator, document_parser, response
end

Private Instance Methods

connection_options() click to toggle source
Calls superclass method
# File lib/ahora/resource.rb, line 94
def connection_options
  (defined?(super) ? super.dup : {}).update \
    :headers => headers
end
document_parser() click to toggle source
# File lib/ahora/resource.rb, line 90
def document_parser
  @document_parser ||= XmlParser.method(:parse)
end
extract_parser_from_args(*args, &block) click to toggle source
# File lib/ahora/resource.rb, line 77
def extract_parser_from_args(*args, &block)
  if args.size == 2
    klass, response = args
    instantiator = lambda do |doc|
      klass.parse(doc)
    end
  else
    response = args.first
    instantiator = block
  end
  [instantiator, response]
end
handle_exception(e) click to toggle source
# File lib/ahora/resource.rb, line 99
def handle_exception(e)
  case e
  when Faraday::Error::TimeoutError, Faraday::Error::ConnectionFailed
    e.extend Ahora::Error::TimeoutError
  else
    e.extend Ahora::Error::ClientError
  end
  raise
end