Class: HTTPrb::Request
- Inherits:
-
Object
- Object
- HTTPrb::Request
- Defined in:
- lib/httprb/request.rb
Instance Attribute Summary collapse
-
#debug ⇒ Object
Returns the value of attribute debug.
-
#headers ⇒ Object
Returns the value of attribute headers.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#parameters ⇒ Object
Returns the value of attribute parameters.
-
#ssl ⇒ Object
Returns the value of attribute ssl.
-
#uri ⇒ Object
Returns the value of attribute uri.
-
#verify_mode ⇒ Object
Returns the value of attribute verify_mode.
Instance Method Summary collapse
-
#basic_auth(user, pass) ⇒ Object
sets up HTTP basic auth- identical to Net::HTTP request objects.
- #evaluate(&block) ⇒ Object
-
#header(key, value) ⇒ Object
set a header key/value pair.
-
#http_request ⇒ Object
generates a Net::HTTP request object.
-
#initialize(uri, options = {}) ⇒ Request
constructor
create an HTTPrb::Request object.
-
#method_missing(method, *args, &block) ⇒ Object
yep, you got it.
-
#parameter(key, value = nil) ⇒ Object
set or add to a query parameter key/value pair if you wish to set multiple values for a key, provide an array of values as the second argument.
-
#query_string ⇒ Object
generates the query string based on the provided parameter dictionary.
Constructor Details
#initialize(uri, options = {}) ⇒ Request
create an HTTPrb::Request object
the uri can be formatted however is most convenient to you- if the scheme (http://) is provided, it will be respected. if not provided, the default is http.
if https:// is given as the scheme and the necesary openssl library is installed (meaning “require ‘net/http’” doesn’t fail) then ssl will be used.
note that the ‘options’ parameter is only truly useful (or good) when not passing a block to the DSL. when a block is provided, using the accessor methods for each option (headers, parameters, etc.) is vastly preferred.
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 |
# File 'lib/httprb/request.rb', line 36 def initialize(uri, = {}) if uri.is_a? URI @uri = uri elsif uri.is_a? String @uri = URI.parse(uri) if !@uri.scheme @uri = URI.parse("http://" + uri) end else raise Exception, "Invalid URL" end @uri.path = "/" if @uri.path.empty? @ssl = true if @uri.scheme == "https" @options = @options[:type] = 'GET' unless @options[:type] @debug = [:debug] ? true : false @parameters = {} if @uri.query CGI.parse(@uri.query).each do |k,v| if v.length == 1 @parameters[k] = v.first else @parameters[k] = v end end end @headers = {} end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
yep, you got it. we’re messing with your mind here. nothing to see here, move along…
158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/httprb/request.rb', line 158 def method_missing(method, *args, &block) if method == :"ssl?" return @ssl elsif @options.keys.include?(method) return @options[method] end if @self_before_eval @self_before_eval.send method, *args, &block else super end end |
Instance Attribute Details
#debug ⇒ Object
Returns the value of attribute debug.
14 15 16 |
# File 'lib/httprb/request.rb', line 14 def debug @debug end |
#headers ⇒ Object
Returns the value of attribute headers.
14 15 16 |
# File 'lib/httprb/request.rb', line 14 def headers @headers end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
16 17 18 |
# File 'lib/httprb/request.rb', line 16 def @options end |
#parameters ⇒ Object
Returns the value of attribute parameters.
14 15 16 |
# File 'lib/httprb/request.rb', line 14 def parameters @parameters end |
#ssl ⇒ Object
Returns the value of attribute ssl.
14 15 16 |
# File 'lib/httprb/request.rb', line 14 def ssl @ssl end |
#uri ⇒ Object
Returns the value of attribute uri.
14 15 16 |
# File 'lib/httprb/request.rb', line 14 def uri @uri end |
#verify_mode ⇒ Object
Returns the value of attribute verify_mode.
14 15 16 |
# File 'lib/httprb/request.rb', line 14 def verify_mode @verify_mode end |
Instance Method Details
#basic_auth(user, pass) ⇒ Object
sets up HTTP basic auth- identical to Net::HTTP request objects.
69 70 71 72 73 |
# File 'lib/httprb/request.rb', line 69 def basic_auth(user, pass) @options[:basic_auth] = true @options[:user] = user @options[:pass] = pass end |
#evaluate(&block) ⇒ Object
149 150 151 152 153 154 |
# File 'lib/httprb/request.rb', line 149 def evaluate(&block) @self_before_eval = eval "self", block.binding r = instance_eval &block @self_before_eval = nil # clear our state r end |
#header(key, value) ⇒ Object
set a header key/value pair
83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/httprb/request.rb', line 83 def header(key, value) if @headers[key] if @headers[key].is_a? Array @headers[key] << value else cur = @headers[key] @headers[key] = [cur, value] end else @headers[key] = value end end |
#http_request ⇒ Object
generates a Net::HTTP request object. for use when the request is passed to Net::HTTP.
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/httprb/request.rb', line 118 def http_request path = "#{@uri.path}?#{query_string}" http_req = case @options[:type].upcase when 'GET' Net::HTTP::Get.new(path) when 'POST' Net::HTTP::Post.new(path) when 'PUT' Net::HTTP::Put.new(path) when 'HEAD' Net::HTTP::Head.new(path) when 'DELETE' Net::HTTP::Delete.new(path) else Net::HTTP::Get.new(path) end if @options[:basic_auth] && @options[:user] && @options[:pass] http_req.basic_auth(@options[:user], @options[:pass]) end @headers.each do |key, value| if value.is_a? Array value.each {|v| http_req.add_field(key, v)} else http_req.add_field(key, value) end end return http_req end |
#parameter(key, value = nil) ⇒ Object
set or add to a query parameter key/value pair if you wish to set multiple values for a key, provide an array of values as the second argument.
78 79 80 |
# File 'lib/httprb/request.rb', line 78 def parameter(key, value = nil) @parameters[key] = value end |
#query_string ⇒ Object
generates the query string based on the provided parameter dictionary
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/httprb/request.rb', line 98 def query_string if !@parameters.empty? # this is crabby because query strings can have more than # one value per key- the key, in that case, is simply # repeated with the additional value. queries = [] @parameters.each do |k,v| if v.is_a?(Array) v.each {|val| queries << "#{k}=#{CGI.escape(val.to_s)}"} else queries << "#{k}=#{CGI.escape(v.to_s)}" end end return queries.join('&') end return "" end |