Class: Hydrogen::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/hydrogen/request.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Request

Returns a new instance of Request.



5
6
7
8
# File 'lib/hydrogen/request.rb', line 5

def initialize(options={})
	@options = {:mash => true}.merge(options)
	@auth = {:username => @options[:username], :password => @options[:password]} if @options[:username]
end

Instance Method Details

#perform_get(path, query = {}) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/hydrogen/request.rb', line 11

def perform_get(path, query={})
	url = URI.parse("#{@options[:base_uri]}#{path}")
	req = Net::HTTP::Get.new(url.path)
	req.basic_auth @auth[:username], @auth[:password]
	res = Net::HTTP.start(url.host, url.port) do |http|
		response = http.request(req)
		perform(response)
	end
end

#perform_post(path, query = {}) ⇒ Object



22
23
24
25
26
27
28
29
30
31
# File 'lib/hydrogen/request.rb', line 22

def perform_post(path, query={})
	url = URI.parse("#{@options[:base_uri]}#{path}")
	req = Net::HTTP::Post.new(url.path)
	req.basic_auth @auth[:username], @auth[:password]
	req.set_form_data(query)
	res = Net::HTTP.start(url.host, url.port) do |http|
		response = http.request(req)
		perform(response)
	end
end