lib/coinsetter/net.rb in coinsetter-0.0.3 vs lib/coinsetter/net.rb in coinsetter-0.0.4

- old
+ new

@@ -1,23 +1,65 @@ module Coinsetter module Net - def self.uri(path) - return "#{Coinsetter.configuration.uri}#{path}" + def self.uri + Coinsetter.configuration.uri end - def self.get(path, headers={}) - RestClient.get(uri(path), headers) + def self.get(path, args={}, headers={}) + res = connection.get do |req| + req.url path, args + req.headers["Accept"] = "application/json" + req.headers.merge!(headers) + end + + body(res) end def self.post(path, args={}, headers={}) - RestClient.post(uri(path), args, headers) + res = connection.post do |req| + req.url path + req.headers['Content-Type'] = 'application/json' + req.headers.merge!(headers) + req.body = JSON.generate(args) + end + + body(res) end def self.put(path, args={}, headers={}) - RestClient.put(uri(path), args, headers) + res = connection.put do |req| + req.url path + req.headers["Accept"] = "application/json" + req.headers.merge!(headers) + req.body = JSON.generate(args) + end + + body(res) end def self.delete(path, headers={}) - RestClient.delete(uri(path), headers) + res = connection.delete do |req| + req.url path + req.headers["Accept"] = "application/json" + req.headers.merge!(headers) + end + + body(res) + end + + def self.body(res) + if res.status == 403 + "403 - Forbidden: You don't have permission to access" + else + res.body + end + end + + def self.connection + @@connection ||= Faraday.new(url: uri) do |faraday| + faraday.request :url_encoded + faraday.response :logger + faraday.adapter Faraday.default_adapter + end end end end