class HyperionUri
Attributes
query_hash[RW]
An enhanced version of URI. Namely,
-
the base uri is a first class citizen,
-
it accepts a hash containing query key/values, and
-
it form-encodes query values that are arrays.
Public Class Methods
new(uri, query_hash={})
click to toggle source
# File lib/hyperion/types/hyperion_uri.rb, line 15 def initialize(uri, query_hash={}) @uri = make_ruby_uri(uri) query_from_uri = parse_query(@uri.query) additional_query_params = validate_query(query_hash) @query_hash = query_from_uri.merge(additional_query_params) __setobj__(@uri) end
Public Instance Methods
base()
click to toggle source
@return [String] the URI base e.g., “http://somehost:80”
# File lib/hyperion/types/hyperion_uri.rb, line 42 def base "#{scheme}://#{host}:#{port}" end
base=(uri)
click to toggle source
# File lib/hyperion/types/hyperion_uri.rb, line 46 def base=(uri) uri = uri.is_a?(HyperionUri) ? uri : HyperionUri.new(uri) self.scheme = uri.scheme self.host = uri.host self.port = uri.port end
inspect()
click to toggle source
# File lib/hyperion/types/hyperion_uri.rb, line 37 def inspect "#<HyperionUri:0x#{(object_id << 1).to_s(16)} #{to_s}>" end
query()
click to toggle source
# File lib/hyperion/types/hyperion_uri.rb, line 23 def query query_string(@query_hash) end
query=(query)
click to toggle source
# File lib/hyperion/types/hyperion_uri.rb, line 27 def query=(query) @query_hash = parse_query(query) end
to_s()
click to toggle source
# File lib/hyperion/types/hyperion_uri.rb, line 31 def to_s fixed = @uri.dup fixed.query = query make_ruby_uri(fixed).to_s end
Private Instance Methods
make_ruby_uri(x)
click to toggle source
# File lib/hyperion/types/hyperion_uri.rb, line 88 def make_ruby_uri(x) input = x.is_a?(HyperionUri) ? x.to_s : x # URI is an oddball. It's a module but also a method on Kernel. # Since this class is a SimpleDelegator and SimpleDelegator is # a BasicObject, we need to pick the method off of Kernel. # We don't want to include Kernel because that would mess up delegation. Kernel.instance_method(:URI).bind(self).call(input) end
parse_query(query)
click to toggle source
# File lib/hyperion/types/hyperion_uri.rb, line 73 def parse_query(query) query ||= '' Rack::Utils.parse_nested_query(query) end
primitive_value?(x)
click to toggle source
# File lib/hyperion/types/hyperion_uri.rb, line 69 def primitive_value?(x) x.is_a?(String) || x.is_a?(Numeric) || x.is_a?(Symbol) end
query_string(query_hash)
click to toggle source
# File lib/hyperion/types/hyperion_uri.rb, line 78 def query_string(query_hash) return nil if query_hash == {} sorted = query_hash.map{|(k, v)| [k.to_s, stringify(v)]}.sort_by(&:key).to_h Rack::Utils.build_nested_query(sorted) end
simple_value?(x)
click to toggle source
# File lib/hyperion/types/hyperion_uri.rb, line 62 def simple_value?(x) case x when Array; x.all?(&method(:primitive_value?)) else; primitive_value?(x) end end
stringify(x)
click to toggle source
# File lib/hyperion/types/hyperion_uri.rb, line 84 def stringify(x) x.is_a?(Array) ? x.map(&:to_s) : x.to_s end
validate_query(query)
click to toggle source
# File lib/hyperion/types/hyperion_uri.rb, line 55 def validate_query(query) query ||= {} query.is_a?(Hash) or fail 'query must be a hash' query.values.all?(&method(:simple_value?)) or fail 'query values must be simple' query.stringify_keys end