Class: TotenDev::Controller
- Inherits:
-
Object
- Object
- TotenDev::Controller
- Defined in:
- lib/tdev_metrics/controllers/controller.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#base_url ⇒ Object
Returns the value of attribute base_url.
-
#password ⇒ Object
Returns the value of attribute password.
-
#username ⇒ Object
Returns the value of attribute username.
Instance Method Summary collapse
- #get(endpoint, params = {}) ⇒ Object
-
#initialize(&block) ⇒ Controller
constructor
A new instance of Controller.
- #post(endpoint, params = {}) ⇒ Object
- #request(uri, req) ⇒ Object
Constructor Details
#initialize(&block) ⇒ Controller
Returns a new instance of Controller.
14 15 16 |
# File 'lib/tdev_metrics/controllers/controller.rb', line 14 def initialize( &block ) instance_eval &block end |
Instance Attribute Details
#base_url ⇒ Object
Returns the value of attribute base_url.
12 13 14 |
# File 'lib/tdev_metrics/controllers/controller.rb', line 12 def base_url @base_url end |
#password ⇒ Object
Returns the value of attribute password.
11 12 13 |
# File 'lib/tdev_metrics/controllers/controller.rb', line 11 def password @password end |
#username ⇒ Object
Returns the value of attribute username.
10 11 12 |
# File 'lib/tdev_metrics/controllers/controller.rb', line 10 def username @username end |
Instance Method Details
#get(endpoint, params = {}) ⇒ Object
18 19 20 21 22 |
# File 'lib/tdev_metrics/controllers/controller.rb', line 18 def get( endpoint, params = {} ) uri = URI "#{parsed_base_url}#{endpoint}" uri.query = URI.encode_www_form params unless params.empty? request uri, Net::HTTP::Get.new( uri.request_uri ) end |
#post(endpoint, params = {}) ⇒ Object
24 25 26 27 28 29 |
# File 'lib/tdev_metrics/controllers/controller.rb', line 24 def post( endpoint, params = {} ) uri = URI "#{parsed_base_url}#{endpoint}" req = Net::HTTP::Post.new uri.path req.set_form_data params unless params.empty? request uri, req end |
#request(uri, req) ⇒ Object
31 32 33 34 |
# File 'lib/tdev_metrics/controllers/controller.rb', line 31 def request( uri, req ) req.basic_auth @username, @password Net::HTTP.start( uri.hostname, uri.port ) {|http| http.request(req) } end |