Module: URI

Defined in:
lib/epitools/core_ext/uri.rb

Instance Method Summary collapse

Instance Method Details

#get(headers = {}, redirect_limit = 10) ⇒ Object

Get this URI using Net::HTTP


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/epitools/core_ext/uri.rb', line 41

def get(headers={}, redirect_limit=10)
  raise "Sorry, URI can't get from #{scheme.inspect} URIs yet" unless scheme =~ /^https?$/
  raise 'Too many HTTP redirections' if redirect_limit == 0

  # headers['User-Agent'] ||= USER_AGENT

  # response = Net::HTTP.start(host, port) do |http|
  #   # the_path = path.empty? ? "/" : path
  #   req = Net::HTTP::Get.new(self, headers)
  #   http.request(req)
  # end

  response = Net::HTTP.get_response(self)

  case response
  when Net::HTTPSuccess
    response
  when Net::HTTPRedirection
    # puts "redirect: #{response['location']}"
    URI(response['location']).get(headers, redirect_limit-1)
  else
    response.error!
  end
end

#paramsObject

Return a Hash of the variables in the query string


8
9
10
# File 'lib/epitools/core_ext/uri.rb', line 8

def params
  (@query ? @query.to_params : {})
end

#params=(key, value) ⇒ Object

Mutate the query NB: This is super slow. To make it faster, store params directly in a locally cached dict, and only call `to_query` when query is accesed, or to_s/inspect are called


16
17
18
19
20
# File 'lib/epitools/core_ext/uri.rb', line 16

def params=(key, value)
  current      = params
  current[key] = value
  self.query   = current.to_query
end

#queryObject


22
23
24
# File 'lib/epitools/core_ext/uri.rb', line 22

def query
  params.to_query
end

#to_strObject

URIs are strings, dammit!


29
30
31
# File 'lib/epitools/core_ext/uri.rb', line 29

def to_str
  to_s
end