Sha256: 4f7949e6814af252704cbd7f20d23e7a176a97aa996b9eb0a33aacd09d4680c4

Contents?: true

Size: 840 Bytes

Versions: 5

Compression:

Stored size: 840 Bytes

Contents

module Boty
  class HTTP
    attr_reader :response # available in case of non 2XX responses...

    [:get, :post, :put, :delete, :head, :patch, :options].each do |verb|
      define_method verb do |url, params = {}|
        @response = connection verb, url, params
        handle_response
      end
    end

    private

    def connection(verb, url, params)
      uri = URI url
      faraday = Faraday.new(url: "#{uri.scheme}://#{uri.host}") { |builder|
        builder.adapter(*Faraday.default_adapter)
      }
      faraday.public_send(verb) { |req| req.url uri.path, params }
    end

    def handle_response
      # TODO: use a response parser accordingly to the
      body = response.body
      if %r{application/json}.match response.headers["Content-Type"]
        JSON.parse body
      else
        body
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
boty-1.0.1 lib/boty/http.rb
boty-1.0.0 lib/boty/http.rb
boty-0.2.0 lib/boty/http.rb
boty-0.1.2 lib/boty/http.rb
boty-0.1.1 lib/boty/http.rb