$:.unshift(File.dirname(__FILE__))require'net/http'require'net/https'require'httparty/module_inheritable_attributes'require'rubygems'gem'jnunemaker-crack'require'crack'moduleHTTPartyAllowedFormats={'text/xml'=>:xml,'application/xml'=>:xml,'application/json'=>:json,'text/json'=>:json,'application/javascript'=>:json,'text/javascript'=>:json,'text/html'=>:html,'application/x-yaml'=>:yaml,'text/yaml'=>:yaml}unlessdefined?(AllowedFormats)defself.included(base)base.extendClassMethodsbase.send:include,HTTParty::ModuleInheritableAttributesbase.send(:mattr_inheritable,:default_options)base.instance_variable_set("@default_options",{})endmoduleClassMethods# Allows setting http proxy information to be used## class Foo# include HTTParty# http_proxy 'http://foo.com', 80# enddefhttp_proxy(addr=nil,port=nil)default_options[:http_proxyaddr]=addrdefault_options[:http_proxyport]=portend# Allows setting a base uri to be used for each request.# Will normalize uri to include http, etc.## class Foo# include HTTParty# base_uri 'twitter.com'# enddefbase_uri(uri=nil)returndefault_options[:base_uri]unlessuridefault_options[:base_uri]=HTTParty.normalize_base_uri(uri)end# Allows setting basic authentication username and password.## class Foo# include HTTParty# basic_auth 'username', 'password'# enddefbasic_auth(u,p)default_options[:basic_auth]={:username=>u,:password=>p}end# Allows setting default parameters to be appended to each request.# Great for api keys and such.## class Foo# include HTTParty# default_params :api_key => 'secret', :another => 'foo'# enddefdefault_params(h={})raiseArgumentError,'Default params must be a hash'unlessh.is_a?(Hash)default_options[:default_params]||={}default_options[:default_params].merge!(h)end# Allows setting a base uri to be used for each request.## class Foo# include HTTParty# headers 'Accept' => 'text/html'# enddefheaders(h={})raiseArgumentError,'Headers must be a hash'unlessh.is_a?(Hash)default_options[:headers]||={}default_options[:headers].merge!(h)enddefcookies(h={})raiseArgumentError,'Cookies must be a hash'unlessh.is_a?(Hash)default_options[:cookies]||=CookieHash.newdefault_options[:cookies].add_cookies(h)end# Allows setting the format with which to parse.# Must be one of the allowed formats ie: json, xml## class Foo# include HTTParty# format :json# enddefformat(f)raiseUnsupportedFormat,"Must be one of: #{AllowedFormats.values.join(', ')}"unlessAllowedFormats.value?(f)default_options[:format]=fend# Allows making a get request to a url.## class Foo# include HTTParty# end# # # Simple get with full url# Foo.get('http://foo.com/resource.json')# # # Simple get with full url and query parameters# # ie: http://foo.com/resource.json?limit=10# Foo.get('http://foo.com/resource.json', :query => {:limit => 10})defget(path,options={})perform_requestNet::HTTP::Get,path,optionsend# Allows making a post request to a url.## class Foo# include HTTParty# end# # # Simple post with full url and setting the body# Foo.post('http://foo.com/resources', :body => {:bar => 'baz'})## # Simple post with full url using :query option, # # which gets set as form data on the request.# Foo.post('http://foo.com/resources', :query => {:bar => 'baz'})defpost(path,options={})perform_requestNet::HTTP::Post,path,optionsenddefput(path,options={})perform_requestNet::HTTP::Put,path,optionsenddefdelete(path,options={})perform_requestNet::HTTP::Delete,path,optionsenddefdefault_options#:nodoc:@default_optionsendprivatedefperform_request(http_method,path,options)#:nodoc:process_cookies(options)Request.new(http_method,path,default_options.dup.merge(options)).performenddefprocess_cookies(options)#:nodoc:returnunlessoptions[:cookies]||default_options[:cookies]options[:headers]||={}options[:headers]["cookie"]=cookies(options[:cookies]||{}).to_cookie_stringdefault_options.delete(:cookies)options.delete(:cookies)endenddefself.normalize_base_uri(url)#:nodoc:use_ssl=(url=~/^https/)||url.include?(':443')ends_with_slash=url=~/\/$/url.chop!ifends_with_slashurl.gsub!(/^https?:\/\//i,'')"http#{'s'ifuse_ssl}://#{url}"endclassBasement#:nodoc:includeHTTPartyenddefself.get(*args)Basement.get(*args)enddefself.post(*args)Basement.post(*args)enddefself.put(*args)Basement.put(*args)enddefself.delete(*args)Basement.delete(*args)endendrequire'httparty/cookie_hash'require'httparty/core_extensions'require'httparty/exceptions'require'httparty/request'require'httparty/response'