require'uri'require'zlib'require'stringio'beginrequire'net/https'rescueLoadError=>eraiseeunlessRUBY_PLATFORM=~/linux/raiseLoadError,"no such file to load -- net/https. Try running apt-get install libopenssl-ruby"endrequireFile.dirname(__FILE__)+'/restclient/exceptions'requireFile.dirname(__FILE__)+'/restclient/request'requireFile.dirname(__FILE__)+'/restclient/abstract_response'requireFile.dirname(__FILE__)+'/restclient/response'requireFile.dirname(__FILE__)+'/restclient/raw_response'requireFile.dirname(__FILE__)+'/restclient/resource'requireFile.dirname(__FILE__)+'/restclient/payload'requireFile.dirname(__FILE__)+'/restclient/net_http_ext'# 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:password@example.com/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 http://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\"}"#moduleRestClientdefself.get(url,headers={},&block)Request.execute(:method=>:get,:url=>url,:headers=>headers,&block)enddefself.post(url,payload,headers={},&block)Request.execute(:method=>:post,:url=>url,:payload=>payload,:headers=>headers,&block)enddefself.put(url,payload,headers={},&block)Request.execute(:method=>:put,:url=>url,:payload=>payload,:headers=>headers,&block)enddefself.delete(url,headers={},&block)Request.execute(:method=>:delete,:url=>url,:headers=>headers,&block)enddefself.head(url,headers={},&block)Request.execute(:method=>:head,:url=>url,:headers=>headers,&block)endclass<<selfattr_accessor:proxyend# 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.defself.log=logiflog.is_a?Stringwarn"[warning] You should set the log with a logger"end@@log=create_loglogenddefself.versionversion_path=File.dirname(__FILE__)+"/../VERSION"returnFile.read(version_path).chompifFile.file?(version_path)"0.0.0"end# 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)defself.create_logparamifparamifparam.is_a?Stringifparam=='stdout'stdout_logger=Class.newdodef<<objSTDOUT.putsobjendendstdout_logger.newelsifparam=='stderr'stderr_logger=Class.newdodef<<objSTDERR.putsobjendendstderr_logger.newelsefile_logger=Class.newdoattr_writer:target_filedef<<objFile.open(@target_file,'a'){|f|f.putsobj}endendlogger=file_logger.newlogger.target_file=paramloggerendelseparamendendend@@env_log=create_logENV['RESTCLIENT_LOG']@@log=nildefself.log# :nodoc:@@env_log||@@logend@@before_execution_procs=[]# Add a Proc to be called before each request in executed.# The proc parameters will be the http request and the request params.defself.add_before_execution_proc&proc@@before_execution_procs<<procenddefself.before_execution_procs# :nodoc:@@before_execution_procsendend