Class: Statwhore::Connection
- Inherits:
-
Object
- Object
- Statwhore::Connection
- Defined in:
- lib/statwhore/connection.rb
Instance Method Summary collapse
- #get(resource, args = nil) ⇒ Object
-
#initialize(base_url, args = {}) ⇒ Connection
constructor
A new instance of Connection.
- #post(resource, args = nil) ⇒ Object
- #request(resource, method = "get", args = nil) ⇒ Object
Constructor Details
#initialize(base_url, args = {}) ⇒ Connection
Returns a new instance of Connection.
4 5 6 7 8 |
# File 'lib/statwhore/connection.rb', line 4 def initialize(base_url, args = {}) @base_url = base_url @username = args[:username] @password = args[:password] end |
Instance Method Details
#get(resource, args = nil) ⇒ Object
10 11 12 |
# File 'lib/statwhore/connection.rb', line 10 def get(resource, args = nil) request(resource, "get", args) end |
#post(resource, args = nil) ⇒ Object
14 15 16 |
# File 'lib/statwhore/connection.rb', line 14 def post(resource, args = nil) request(resource, "post", args) end |
#request(resource, method = "get", args = nil) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/statwhore/connection.rb', line 18 def request(resource, method = "get", args = nil) url = URI.join(@base_url, resource) url.query = args.map { |k,v| "%s=%s" % [URI.encode(k.to_s), URI.encode(v.to_s)] }.join("&") if args case method when "get" req = Net::HTTP::Get.new(url.request_uri) when "post" req = Net::HTTP::Post.new(url.request_uri) end req.basic_auth(@username, @password) if @username && @password http = Net::HTTP.new(url.host, url.port) http.use_ssl = (url.port == 443) res = http.start() { |conn| conn.request(req) } res.body end |