Class: RESTRack::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/restrack-client.rb

Instance Method Summary collapse

Constructor Details

#initialize(uri, format = :JSON) ⇒ Client

Returns a new instance of Client.



10
11
12
13
14
# File 'lib/restrack-client.rb', line 10

def initialize(uri, format=:JSON)
			(uri.is_a? URI) ? @uri = uri : @uri = URI.parse(uri)
  @path = ''
  @format = format
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(resource_name, *args) ⇒ Object



16
17
18
19
20
# File 'lib/restrack-client.rb', line 16

def method_missing(resource_name, *args)
  @path << '/' + resource_name.to_s
  @path << '/' + args.join('/') if args.length > 0
  self
end

Instance Method Details

#deleteObject



28
29
30
31
32
# File 'lib/restrack-client.rb', line 28

def delete
  request = Net::HTTP::Delete.new(@path)
  response = send request
  parse response
end

#getObject



22
23
24
25
26
# File 'lib/restrack-client.rb', line 22

def get
  request = Net::HTTP::Get.new(@path)
  response = send request
  parse response
end

#post(data = nil) ⇒ Object



34
35
36
37
38
39
# File 'lib/restrack-client.rb', line 34

def post(data=nil)
  request = Net::HTTP::Post.new(@path, {'Content-Type' => content_type })
  request.body = prepare data unless data.nil?
  response = send request
  parse response
end

#put(data = nil) ⇒ Object



41
42
43
44
45
46
# File 'lib/restrack-client.rb', line 41

def put(data=nil)
  request = Net::HTTP::Put.new(@path, {'Content-Type' => content_type })
  request.body = prepare data unless data.nil?
  response = send request
  parse response
end