PageRenderTime 49ms CodeModel.GetById 36ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/wwmd/page/headers.rb

http://github.com/miketracy/wwmd
Ruby | 118 lines | 81 code | 17 blank | 20 comment | 9 complexity | 636b71f7bdcdbc935d0722405b1c174a MD5 | raw file
  1module WWMD
  2  class Page
  3
  4#:section: Header helper methods
  5
  6    def user_agent=(ua)
  7      self.headers["User-Agent"] = ua
  8    end
  9
 10    # clear header at <key>
 11    def clear_header(key)
 12      self.headers.delete_if { |k,v| k.upcase == key.upcase }
 13      return nil
 14    end
 15
 16    alias_method :delete_header, :clear_header#:nodoc:
 17
 18    # clear all headers
 19    def clear_headers
 20      self.headers.delete_if { |k,v| true }
 21      "headers cleared"
 22    end
 23
 24    # set headers from passed argument
 25    #  Nil:    set headers from WWMD::DEFAULT_HEADERS
 26    #  Symbol: entry in WWMD::HEADERS to set from
 27    #  Hash:   hash to set headers from
 28    #  String: filename (NOT IMPLEMENTED)
 29    #
 30    #  if clear == true then headers will be cleared before setting
 31    def set_headers(arg=nil,clear=false)
 32      clear_headers if clear
 33      if arg.nil?
 34        begin
 35          clear_headers
 36          WWMD::DEFAULT_HEADERS.each { |k,v| self.headers[k] = v }
 37          return "headers set from default"
 38        rescue => e
 39          putw "WARN: " + e
 40          return false
 41        end
 42      elsif arg.class == Symbol
 43        set_headers(WWMD::HEADERS[arg])
 44        putw "headers set from #{arg}"
 45        return true
 46      elsif arg.class == Hash
 47        arg.each { |k,v| self.headers[k] = v }
 48        putw "headers set from hash"
 49        return true
 50      end
 51      putw "error setting headers"
 52      return false
 53    end
 54
 55    # set headers back to default headers
 56    def default_headers(arg=nil)
 57      set_headers
 58    end
 59
 60    alias_method :set_default, :default_headers
 61
 62    # set headers from text
 63    def headers_from_array(arr)
 64      clear_headers
 65      arr = arr.split("\r\n\r\n").first if arr.class == String
 66      arr.each do |line|
 67        next if (line.empty? || line =~ /^(GET|POST)/)
 68        k,v = line.split(":",2)
 69        self.headers[k.strip] = v.strip
 70      end
 71      nil
 72    end
 73#; 
 74
 75    # set headers from paste
 76    def headers_from_paste
 77      headers_from_array(%x[pbpaste])
 78    end
 79
 80    # set headers from file
 81    def headers_from_file(fn)
 82      clear_headers
 83      headers_from_array(File.read(fn).split("\n"))
 84      return "headers set from #{fn}"
 85    end
 86
 87    # set headers to utf7 encoding post
 88    def set_utf7_headers
 89      self.headers["Content-Type"] = "application/x-www-form-urlencoded;charset=UTF-7"
 90      return "headers set to utf7"
 91    end
 92
 93    # set headers to ajax
 94    def set_ajax_headers
 95      self.headers["X-Requested-With"] = "XMLHttpRequest"
 96      self.headers["X-Prototype-Version"] = "1.5.0"
 97      return "headers set to ajax"
 98    end
 99
100    # set headers to SOAP request headers
101    def set_soap_headers
102      self.headers['Content-Type'] = "text/xml;charset=utf-8"
103      self.headers['SOAPAction'] = "\"\""
104      return "headers set to soap"
105    end
106
107    # get the current Cookie header
108    def get_cookie
109      self.headers["Cookie"]
110    end
111
112    # set the Cookie header
113    def set_cookie(cookie=nil)
114      self.headers["Cookie"] = cookie
115    end
116
117  end
118end