Class: Rest::Resource

Inherits:
Object
  • Object
show all
Defined in:
lib/rest/resource.rb

Direct Known Subclasses

Database, Document, Server

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection, path = '/', username = nil, password = nil) ⇒ Resource

Returns a new instance of Resource.



9
10
11
12
13
14
15
16
17
18
# File 'lib/rest/resource.rb', line 9

def initialize(connection, path = '/', username=nil, password=nil)
  @connection = connection
  uri = [ 'http://',
          [connection.address,
           connection.port].join(':'), 
          path ].join
  @uri = URI.parse(uri)
  @username = username
  @password = password
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



7
8
9
# File 'lib/rest/resource.rb', line 7

def connection
  @connection
end

Class Method Details

.nested_resource(name) ⇒ Object



20
21
22
23
24
25
# File 'lib/rest/resource.rb', line 20

def self.nested_resource(name)
  define_method name.to_sym do |*args|
    resource = [name.to_s, *args.first].compact.join('/')
    Resource.new(@connection, nested_resource_path(resource))
  end
end

Instance Method Details

#delete(options_or_body = '') ⇒ Object



42
43
44
# File 'lib/rest/resource.rb', line 42

def delete(options_or_body = '')
  do_request(options_or_body, Net::HTTP::Delete)
end

#get(options_or_body = '') ⇒ Object



38
39
40
# File 'lib/rest/resource.rb', line 38

def get(options_or_body = '')
  do_request(options_or_body, Net::HTTP::Get)
end

#nested_resource_path(nested_resource = '') ⇒ Object



27
28
29
30
31
32
# File 'lib/rest/resource.rb', line 27

def nested_resource_path(nested_resource = '')
  components = @uri.request_uri.split('/')
  components << "" if components.empty?
  components << nested_resource
  components.join('/')
end

#post(options_or_body = '') ⇒ Object



46
47
48
# File 'lib/rest/resource.rb', line 46

def post(options_or_body = '')
  do_request(options_or_body, Net::HTTP::Post)
end

#put(options_or_body = '') ⇒ Object



34
35
36
# File 'lib/rest/resource.rb', line 34

def put(options_or_body = '')
  do_request(options_or_body, Net::HTTP::Put)
end