Class: Hydrogen::Request
- Inherits:
-
Object
- Object
- Hydrogen::Request
- Defined in:
- lib/hydrogen/request.rb
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ Request
constructor
A new instance of Request.
- #perform_get(path, query = {}) ⇒ Object
- #perform_post(path, query = {}) ⇒ Object
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 = {:mash => true}.merge() @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 |