Module: RestClient

Defined in:
lib/restclient.rb,
lib/restclient/payload.rb,
lib/restclient/request.rb,
lib/restclient/resource.rb,
lib/restclient/response.rb,
lib/restclient/exceptions.rb,
lib/restclient/raw_response.rb,
lib/restclient/mixin/response.rb

Overview

This module's static methods are the entry point for using the REST client.

# GET
xml = RestClient.get 'http://example.com/resource'
jpg = RestClient.get 'http://example.com/resource', :accept => 'image/jpg'

# authentication and SSL
RestClient.get 'https://user:[email protected]/private/resource'

# POST or PUT with a hash sends parameters as a urlencoded form body
RestClient.post 'http://example.com/resource', :param1 => 'one'

# nest hash parameters
RestClient.post 'http://example.com/resource', :nested => { :param1 => 'one' }

# POST and PUT with raw payloads
RestClient.post 'http://example.com/resource', 'the post body', :content_type => 'text/plain'
RestClient.post 'http://example.com/resource.xml', xml_doc
RestClient.put 'http://example.com/resource.pdf', File.read('my.pdf'), :content_type => 'application/pdf'

# DELETE
RestClient.delete 'http://example.com/resource'

# retreive the response http code and headers
res = RestClient.get 'http://example.com/some.jpg'
res.code                    # => 200
res.headers[:content_type]  # => 'image/jpg'

# HEAD
RestClient.head('http://example.com').headers

To use with a proxy, just set RestClient.proxy to the proper http proxy:

RestClient.proxy = "http://proxy.example.com/"

Or inherit the proxy from the environment:

RestClient.proxy = ENV['http_proxy']

For live tests of RestClient, try using rest-test.heroku.com, which echoes back information about the rest call:

>> RestClient.put 'http://rest-test.heroku.com/resource', :foo => 'baz'
=> "PUT http://rest-test.heroku.com/resource with a 7 byte payload, content type application/x-www-form-urlencoded {\"foo\"=>\"baz\"}"

Defined Under Namespace

Modules: Exceptions, Mixin, Payload Classes: Exception, ExceptionWithResponse, RawResponse, Redirect, Request, RequestFailed, Resource, Response, ServerBrokeConnection

Constant Summary collapse

@@env_log =
create_log ENV['RESTCLIENT_LOG']
@@log =
nil

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.proxyObject

Returns the value of attribute proxy


88
89
90
# File 'lib/restclient.rb', line 88

def proxy
  @proxy
end

Class Method Details

.create_log(param) ⇒ Object

Create a log that respond to << like a logger param can be 'stdout', 'stderr', a string (then we will log to that file) or a logger (then we return it)


109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/restclient.rb', line 109

def self.create_log param
  if param
    if param.is_a? String
      if param == 'stdout'
        stdout_logger = Class.new do
          def << obj
            STDOUT.puts obj
          end
        end
        stdout_logger.new
      elsif param == 'stderr'
        stderr_logger = Class.new do
          def << obj
            STDERR.puts obj
          end
        end
        stderr_logger.new
      else
        file_logger = Class.new do
          attr_writer :target_file
          def << obj
            File.open(@target_file, 'a') { |f| f.puts obj }
          end
        end
        logger = file_logger.new
        logger.target_file = param
        logger
      end
    else
      param
    end
  end
end

.delete(url, headers = {}, &block) ⇒ Object


79
80
81
# File 'lib/restclient.rb', line 79

def self.delete(url, headers={}, &block)
  Request.execute(:method => :delete, :url => url, :headers => headers, &block)
end

.get(url, headers = {}, &block) ⇒ Object


67
68
69
# File 'lib/restclient.rb', line 67

def self.get(url, headers={}, &block)
  Request.execute(:method => :get, :url => url, :headers => headers, &block)
end

.head(url, headers = {}, &block) ⇒ Object


83
84
85
# File 'lib/restclient.rb', line 83

def self.head(url, headers={}, &block)
  Request.execute(:method => :head, :url => url, :headers => headers, &block)
end

.logObject

:nodoc:


147
148
149
# File 'lib/restclient.rb', line 147

def self.log # :nodoc:
  @@env_log || @@log
end

.log=(log) ⇒ Object

Setup the log for RestClient calls. Value should be a logger but can can be stdout, stderr, or a filename. You can also configure logging by the environment variable RESTCLIENT_LOG.


94
95
96
97
98
99
# File 'lib/restclient.rb', line 94

def self.log= log
  if log.is_a? String
    warn "[warning] You should set the log with a logger"
  end
  @@log = create_log log
end

.post(url, payload, headers = {}, &block) ⇒ Object


71
72
73
# File 'lib/restclient.rb', line 71

def self.post(url, payload, headers={}, &block)
  Request.execute(:method => :post, :url => url, :payload => payload, :headers => headers, &block)
end

.put(url, payload, headers = {}, &block) ⇒ Object


75
76
77
# File 'lib/restclient.rb', line 75

def self.put(url, payload, headers={}, &block)
  Request.execute(:method => :put, :url => url, :payload => payload, :headers => headers, &block)
end

.versionObject


101
102
103
104
105
# File 'lib/restclient.rb', line 101

def self.version
  version_path = File.dirname(__FILE__) + "/../VERSION"
  return File.read(version_path).chomp if File.file?(version_path)
  "0.0.0"
end