Class: Puppet::Indirector::Request
- Defined in:
- lib/vendor/puppet/indirector/request.rb
Overview
This class encapsulates all of the information you need to make an Indirection call, and as a a result also handles REST calls. It’s somewhat analogous to an HTTP Request object, except tuned for our Indirector.
Constant Summary collapse
- OPTION_ATTRIBUTES =
[:ip, :node, :authenticated, :ignore_terminus, :ignore_cache, :instance, :environment]
Instance Attribute Summary collapse
-
#authenticated ⇒ Object
Returns the value of attribute authenticated.
-
#ignore_cache ⇒ Object
Returns the value of attribute ignore_cache.
-
#ignore_terminus ⇒ Object
Returns the value of attribute ignore_terminus.
-
#indirection_name ⇒ Object
Returns the value of attribute indirection_name.
-
#instance ⇒ Object
Returns the value of attribute instance.
-
#ip ⇒ Object
Returns the value of attribute ip.
-
#key ⇒ Object
Returns the value of attribute key.
-
#method ⇒ Object
Returns the value of attribute method.
-
#node ⇒ Object
Returns the value of attribute node.
-
#options ⇒ Object
Returns the value of attribute options.
-
#port ⇒ Object
Returns the value of attribute port.
-
#protocol ⇒ Object
Returns the value of attribute protocol.
-
#server ⇒ Object
Returns the value of attribute server.
-
#uri ⇒ Object
Returns the value of attribute uri.
Instance Method Summary collapse
-
#authenticated? ⇒ Boolean
Is this an authenticated request?.
- #environment ⇒ Object
- #environment=(env) ⇒ Object
- #escaped_key ⇒ Object
-
#ignore_cache? ⇒ Boolean
LAK:NOTE This is a messy interface to the cache, and it’s only used by the Configurer class.
- #ignore_terminus? ⇒ Boolean
-
#indirection ⇒ Object
Look up the indirection based on the name provided.
-
#initialize(indirection_name, method, key_or_instance, options_or_instance = {}) ⇒ Request
constructor
A new instance of Request.
- #model ⇒ Object
-
#plural? ⇒ Boolean
Are we trying to interact with multiple resources, or just one?.
-
#query_string ⇒ Object
Create the query string, if options are present.
- #remote? ⇒ Boolean
- #to_hash ⇒ Object
- #to_s ⇒ Object
-
#use_cache? ⇒ Boolean
Should we allow use of the cached object?.
Constructor Details
#initialize(indirection_name, method, key_or_instance, options_or_instance = {}) ⇒ Request
Returns a new instance of Request.
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 |
# File 'lib/vendor/puppet/indirector/request.rb', line 52 def initialize(indirection_name, method, key_or_instance, = {}) if .is_a? Hash = @instance = nil else = {} @instance = end self.indirection_name = indirection_name self.method = method set_attributes() @options = .inject({}) { |hash, ary| hash[ary[0].to_sym] = ary[1]; hash } if key_or_instance.is_a?(String) || key_or_instance.is_a?(Symbol) key = key_or_instance else @instance ||= key_or_instance end if key # If the request key is a URI, then we need to treat it specially, # because it rewrites the key. We could otherwise strip server/port/etc # info out in the REST class, but it seemed bad design for the REST # class to rewrite the key. if key.to_s =~ /^\w+:\// and not Puppet::Util.absolute_path?(key.to_s) # it's a URI set_uri_key(key) else @key = key end end @key = @instance.name if ! @key and @instance end |
Instance Attribute Details
#authenticated ⇒ Object
Returns the value of attribute authenticated.
9 10 11 |
# File 'lib/vendor/puppet/indirector/request.rb', line 9 def authenticated @authenticated end |
#ignore_cache ⇒ Object
Returns the value of attribute ignore_cache.
9 10 11 |
# File 'lib/vendor/puppet/indirector/request.rb', line 9 def ignore_cache @ignore_cache end |
#ignore_terminus ⇒ Object
Returns the value of attribute ignore_terminus.
9 10 11 |
# File 'lib/vendor/puppet/indirector/request.rb', line 9 def ignore_terminus @ignore_terminus end |
#indirection_name ⇒ Object
Returns the value of attribute indirection_name.
13 14 15 |
# File 'lib/vendor/puppet/indirector/request.rb', line 13 def indirection_name @indirection_name end |
#instance ⇒ Object
Returns the value of attribute instance.
9 10 11 |
# File 'lib/vendor/puppet/indirector/request.rb', line 9 def instance @instance end |
#ip ⇒ Object
Returns the value of attribute ip.
9 10 11 |
# File 'lib/vendor/puppet/indirector/request.rb', line 9 def ip @ip end |
#key ⇒ Object
Returns the value of attribute key.
9 10 11 |
# File 'lib/vendor/puppet/indirector/request.rb', line 9 def key @key end |
#method ⇒ Object
Returns the value of attribute method.
9 10 11 |
# File 'lib/vendor/puppet/indirector/request.rb', line 9 def method @method end |
#node ⇒ Object
Returns the value of attribute node.
9 10 11 |
# File 'lib/vendor/puppet/indirector/request.rb', line 9 def node @node end |
#options ⇒ Object
Returns the value of attribute options.
9 10 11 |
# File 'lib/vendor/puppet/indirector/request.rb', line 9 def @options end |
#port ⇒ Object
Returns the value of attribute port.
11 12 13 |
# File 'lib/vendor/puppet/indirector/request.rb', line 11 def port @port end |
#protocol ⇒ Object
Returns the value of attribute protocol.
11 12 13 |
# File 'lib/vendor/puppet/indirector/request.rb', line 11 def protocol @protocol end |
#server ⇒ Object
Returns the value of attribute server.
11 12 13 |
# File 'lib/vendor/puppet/indirector/request.rb', line 11 def server @server end |
#uri ⇒ Object
Returns the value of attribute uri.
11 12 13 |
# File 'lib/vendor/puppet/indirector/request.rb', line 11 def uri @uri end |
Instance Method Details
#authenticated? ⇒ Boolean
Is this an authenticated request?
18 19 20 21 |
# File 'lib/vendor/puppet/indirector/request.rb', line 18 def authenticated? # Double negative, so we just get true or false ! ! authenticated end |
#environment ⇒ Object
23 24 25 |
# File 'lib/vendor/puppet/indirector/request.rb', line 23 def environment @environment ||= Puppet::Node::Environment.new end |
#environment=(env) ⇒ Object
27 28 29 30 31 32 33 |
# File 'lib/vendor/puppet/indirector/request.rb', line 27 def environment=(env) @environment = if env.is_a?(Puppet::Node::Environment) env else Puppet::Node::Environment.new(env) end end |
#escaped_key ⇒ Object
35 36 37 |
# File 'lib/vendor/puppet/indirector/request.rb', line 35 def escaped_key URI.escape(key) end |
#ignore_cache? ⇒ Boolean
LAK:NOTE This is a messy interface to the cache, and it’s only used by the Configurer class. I decided it was better to implement it now and refactor later, when we have a better design, than to spend another month coming up with a design now that might not be any better.
44 45 46 |
# File 'lib/vendor/puppet/indirector/request.rb', line 44 def ignore_cache? ignore_cache end |
#ignore_terminus? ⇒ Boolean
48 49 50 |
# File 'lib/vendor/puppet/indirector/request.rb', line 48 def ignore_terminus? ignore_terminus end |
#indirection ⇒ Object
Look up the indirection based on the name provided.
91 92 93 |
# File 'lib/vendor/puppet/indirector/request.rb', line 91 def indirection Puppet::Indirector::Indirection.instance(indirection_name) end |
#model ⇒ Object
100 101 102 103 |
# File 'lib/vendor/puppet/indirector/request.rb', line 100 def model raise ArgumentError, "Could not find indirection '#{indirection_name}'" unless i = indirection i.model end |
#plural? ⇒ Boolean
Are we trying to interact with multiple resources, or just one?
115 116 117 |
# File 'lib/vendor/puppet/indirector/request.rb', line 115 def plural? method == :search end |
#query_string ⇒ Object
Create the query string, if options are present.
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/vendor/puppet/indirector/request.rb', line 120 def query_string return "" unless and ! .empty? "?" + .collect do |key, value| case value when nil; next when true, false; value = value.to_s when Fixnum, Bignum, Float; value = value # nothing when String; value = CGI.escape(value) when Symbol; value = CGI.escape(value.to_s) when Array; value = CGI.escape(YAML.dump(value)) else raise ArgumentError, "HTTP REST queries cannot handle values of type '#{value.class}'" end "#{key}=#{value}" end.join("&") end |
#remote? ⇒ Boolean
153 154 155 |
# File 'lib/vendor/puppet/indirector/request.rb', line 153 def remote? self.node or self.ip end |
#to_hash ⇒ Object
138 139 140 141 142 143 144 145 146 147 |
# File 'lib/vendor/puppet/indirector/request.rb', line 138 def to_hash result = .dup OPTION_ATTRIBUTES.each do |attribute| if value = send(attribute) result[attribute] = value end end result end |
#to_s ⇒ Object
149 150 151 |
# File 'lib/vendor/puppet/indirector/request.rb', line 149 def to_s return(uri ? uri : "/#{indirection_name}/#{key}") end |
#use_cache? ⇒ Boolean
Should we allow use of the cached object?
106 107 108 109 110 111 112 |
# File 'lib/vendor/puppet/indirector/request.rb', line 106 def use_cache? if defined?(@use_cache) ! ! use_cache else true end end |