Module: Ahora::Resource

Defined in:
lib/ahora/resource.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#document_parser=(value) ⇒ Object

Sets the attribute document_parser

Parameters:

  • value

    the value to set the attribute document_parser to.



5
6
7
# File 'lib/ahora/resource.rb', line 5

def document_parser=(value)
  @document_parser = value
end

Instance Method Details

#collection(*args, &block) ⇒ Object



65
66
67
68
# 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

#connectionObject



40
41
42
43
44
45
46
47
48
# 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) ⇒ Object

This method is abstract.

override to use custom Faraday middleware



51
52
53
# File 'lib/ahora/resource.rb', line 51

def extend_middleware(builder)
  super if defined? super
end

#get(url, params = nil) ⇒ Object



7
8
9
10
11
12
13
14
15
16
# 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

#headersObject

This method is abstract.

override to set custome headers

FIXME test (FakeWeb cannot test request headers) returns a hash with a string for each key



58
59
60
61
62
63
# 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) ⇒ Object

FIXME test



19
20
21
22
23
24
25
26
27
# 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) ⇒ Object

FIXME test



30
31
32
33
34
35
36
37
38
# 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) ⇒ Object



70
71
72
73
# 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