Sha256: 8558c2e3c5692a392fbd5c1c9c94f41f89c4c5904e0bdec0881d268db31b899c

Contents?: true

Size: 1.43 KB

Versions: 1

Compression:

Stored size: 1.43 KB

Contents

module Coinsetter
  module Net
    def self.uri
      Coinsetter.configuration.uri
    end

    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={})
      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={})
      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={})
      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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
coinsetter-0.0.4 lib/coinsetter/net.rb