Sha256: b6fa7c200a28095ce4eb0d30b75a9070cf0414a67c76fb743efa54213585b8d8

Contents?: true

Size: 1.46 KB

Versions: 3

Compression:

Stored size: 1.46 KB

Contents

require 'pupper/parse_json'

module Pupper
  # Provides an interface to build an API Client, that can be used by [Model]
  class Backend
    class BaseUrlNotDefined < StandardError; end

    attr_reader :client, :model

    delegate :base_url, :headers, to: :class

    class << self
      # Sets the base URL the API client will call
      # @return [String] the URL (plus - optionally - a path)
      attr_writer :base_url, :headers

      def headers
        @headers ||= {}
      end

      def base_url
        if @base_url.nil?
          raise BaseUrlNotDefined, <<-ERR
            Add the following to #{name} to make it work:

              self.base_url = "https://example.com/some/path"

            Making sure to change the URL to something useful :)))
          ERR
        end

        @base_url
      end
    end

    %i(get put post delete patch).each do |name|
      class_eval <<-RB.strip_heredoc, __FILE__, __LINE__
        def #{name}(*args)
          client.send(:#{name}, *args).body
        end
      RB
    end

    def initialize
      @client = Faraday.new(base_url, ssl: Pupper.config.ssl) do |builder|
        builder.request :json
        builder.use Pupper::ParseJson
        builder.response :logger if Pupper.config.logging?
        builder.response :raise_error
        builder.adapter :typhoeus
        builder.headers = headers.merge!('User-Agent' => Pupper.config.user_agent)
      end
    end

    def register_model(model)
      @model = model
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
pupper-0.2.2 lib/pupper/backend.rb
pupper-0.2.1 lib/pupper/backend.rb
pupper-0.2.0 lib/pupper/backend.rb