Module: Rested::BaseMethods

Included in:
Base, Base
Defined in:
lib/rested/base.rb

Instance Method Summary collapse

Instance Method Details

#clientObject



29
30
31
# File 'lib/rested/base.rb', line 29

def client
    @client ||= setup_client()
end

#decode_json_response(message) ⇒ Object



74
75
76
77
78
79
80
# File 'lib/rested/base.rb', line 74

def decode_json_response(message)
    begin
        JSON.load(message.content)
    rescue => ex
        nil
    end
end

#decode_response(message) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/rested/base.rb', line 62

def decode_response(message)
    ct = message.contenttype
    if ct =~ /json|javascript/ then
        decode_json_response(message)
    elsif ct =~ /xml/ then
        decode_xml_response(message)
    else
        Rested. { "\n" + message.content }
        raise "Unknown response type"
    end
end

#decode_xml_response(message) ⇒ Object

Raises:

  • (NotImplementedError)


82
83
84
# File 'lib/rested/base.rb', line 82

def decode_xml_response(message)
    raise NotImplementedError # TODO
end

#delete(uri) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/rested/base.rb', line 47

def delete(uri)
    url = self.base_url + uri
    Rested.log_out{"DELETE #{url}"}
    res = self.client.delete(url)
    handle_error(res) if message.status >= 400
    return res.status == 200
end

#get(uri, params = nil) ⇒ Object



33
34
35
36
37
38
# File 'lib/rested/base.rb', line 33

def get(uri, params = nil)
    url = self.base_url + uri
    Rested.log_out{"GET #{url}"}
    Rested.log_out{"    params: " + params.inspect}
    handle_response(self.client.get(url, params))
end

#handle_error(message) ⇒ Object

Raises:



86
87
88
89
90
# File 'lib/rested/base.rb', line 86

def handle_error(message)
    Rested.{ puts; "HTTP/#{message.version} #{message.status} #{message.reason}"}
    Rested.{ message.header.all.each{|h| Rested.("#{h[0]}: #{h[1]}")}; puts }
    raise Rested::Error.new(message)
end

#handle_response(message) ⇒ Object



55
56
57
58
59
60
# File 'lib/rested/base.rb', line 55

def handle_response(message)
    handle_error(message) if message.status >= 400
    Rested.{ puts; "HTTP/#{message.version} #{message.status} #{message.reason}"}
    Rested.{ message.header.all.each{|h| Rested.("#{h[0]}: #{h[1]}")}; puts }
    decode_response(message)
end

#post(uri, params = nil) ⇒ Object



40
41
42
43
44
45
# File 'lib/rested/base.rb', line 40

def post(uri, params = nil)
    url = self.base_url + uri
    Rested.log_out{"POST #{url}"}
    Rested.log_out{"     params: " + params.inspect}
    handle_response(self.client.post(url, params))
end

#setup_clientObject

override this method to customize HTTPClient



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/rested/base.rb', line 11

def setup_client
    @client = ::HTTPClient.new
    if self.user and self.pass then
        @client.set_auth(self.base_url, self.user, self.pass)
    end
    if not Rested.debug.nil?
        @client.debug_dev = (Rested.debug.respond_to?("<<") ? Rested.debug : STDOUT)
    end
    if Object.const_defined? "Rack" then
        handler = lambda { |path|
            path =~ /(\..*?)$/
            Rack::Mime.mime_type(($1.nil? ? nil : $1.downcase))
        }
        HTTP::Message.mime_type_handler = handler
    end
    @client
end