Class: NetworkFacade::REST::Client
- Inherits:
-
Base::Client
- Object
- Base::Client
- NetworkFacade::REST::Client
- Defined in:
- lib/network-facade/rest.rb
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ Client
constructor
A new instance of Client.
- #method_missing(name, *args) ⇒ Object
Methods inherited from Base::Client
#__read__, #__write__, inherited, uri=
Constructor Details
#initialize(options = {}) ⇒ Client
Returns a new instance of Client.
25 26 27 28 |
# File 'lib/network-facade/rest.rb', line 25 def initialize( = {}) super @cookies = {} end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/network-facade/rest.rb', line 30 def method_missing(name, *args) # Build query string params = args.first || {} raise "Params must be an Hash table" unless params.is_a? Hash qs = params.keys.collect do |k| "#{k}=#{CGI::escape(params[k].to_s)}" end.join('&') # Cutom method mapping defined ? if @options[:mapping] and @options[:mapping][name] @uri.path = @options[:mapping][name] end format = @options[:append_slash] ? '%s/?%s' : '%s?%s' Net::HTTP.start(@uri.host, @uri.port) do |http| # Build header fields header = @options[:header] || {} header['User-Agent'] = @options[:user_agent] || NetworkFacade::NAME + '/' + NetworkFacade::VERSION unless @cookies.empty? header['Cookie'] = @cookies.keys.collect do |k| "#{k}=#{@cookies[k]}" end.join('; ') end # Build request path if @options[:method] == :path path = format % [ "#{@uri.path}/#{name}", qs ] elsif @options[:method] == :param and @options[:method_param] path = format % [ @uri.path, "#{qs}&#{@options[:method_param]}=#{CGI::escape(name.to_s)}" ] else path = format % [ @uri.path, qs ] end # POST or GET if @options[:post].is_a? Array and @options[:post].include? name req = Net::HTTP::Post.new(path, header) req.form_data = params else req = Net::HTTP::Get.new(path, header) NetworkFacade.log(:info, "GET #{req.path}") end res = http.request(req) # Remember cookies CGI::Cookie::parse(res['set-cookie']).each do |k,v| next if k == 'path' or k == 'expires' @cookies[k] = v.value end # Return the correct data case res.code when '200' case res.content_type when /xml/ if defined? XML parser = XML::Parser.new parser.string = res.body data = parser.parse else data = REXML::Document.new(res.body) end when /json/ if defined? JSON data = JSON.parse res.body else data = res.body end when /php/ #TODO: http://php.net/serialize, http://hurring.com/code/perl/serialize/ raise "Not implemented yet" else data = res.body end when /3\d\d/ # FIXME raise "Redirection not supported yet" else NetworkFacade.log(:error, "An error occured when calling #{name} (returned code = #{res.code})") end # Return the header if asked @options[:header] ? [res.to_hash, data] : data end end |