Class: DataMapperRest::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/dm-rest-adapter/connection.rb

Overview

Somewhat stolen from ActiveResource TODO: Support https?

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri, format) ⇒ Connection

Returns a new instance of Connection.



9
10
11
12
# File 'lib/dm-rest-adapter/connection.rb', line 9

def initialize(uri, format)
  @uri = uri
  @format = Format.new(format)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object

this is used to run the http verbs like http_post, http_put, http_delete etc. TODO: handle nested resources, see prefix in ActiveResource



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/dm-rest-adapter/connection.rb', line 16

def method_missing(method, *args)
  if verb = method.to_s.match(/\Ahttp_(get|post|put|delete|head)\z/)

    orig_uri, @uri = @uri, @uri.dup
    begin
      path, query = args[0].split('?', 2)
      @uri.path = "#{path}.#{@format.extension}#{'?' << query if query}" # Should be the form of /resources
      run_verb(verb.to_s.split('_').last, args[1])
    ensure
      @uri = orig_uri
    end
  end
end

Instance Attribute Details

#formatObject

Returns the value of attribute format.



7
8
9
# File 'lib/dm-rest-adapter/connection.rb', line 7

def format
  @format
end

#uriObject

Returns the value of attribute uri.



7
8
9
# File 'lib/dm-rest-adapter/connection.rb', line 7

def uri
  @uri
end