Sha256: 715e5284ec7b583dddc48662831beb5ac14e380e4adc796dbc12c7d819e16099

Contents?: true

Size: 771 Bytes

Versions: 6

Compression:

Stored size: 771 Bytes

Contents

# frozen_string_literal: true

require "json"
require "net/http"

module BrasilAPI
  class Base
    BASE_URL = "https://brasilapi.com.br/api"

    class << self
      def get(path)
        uri = URI("#{BASE_URL}#{path}")

        response = Net::HTTP.get_response(uri)

        case status_for(response)
        when 200
          parsed_body(response)
        when 404
          raise BrasilAPI::NotFound
        else
          raise BrasilAPI::Error
        end
      end

      protected

      def status_for(response)
        status = {
          Net::HTTPOK => 200,
          Net::HTTPNotFound => 404
        }

        status[response.class]
      end

      def parsed_body(response)
        JSON.parse(response.body) if response.body
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
brasilapi-0.6.0 lib/brasil_api/base.rb
brasilapi-0.5.1 lib/brasil_api/base.rb
brasilapi-0.4.0 lib/brasil_api/base.rb
brasilapi-0.3.0 lib/brasil_api/base.rb
brasilapi-0.2.0 lib/brasil_api/base.rb
brasilapi-0.1.1 lib/brasil_api/base.rb