Sha256: 887883cd088407d4d71c82a547a633e8c8a432e87306c090dd603fe8b56e8e1c

Contents?: true

Size: 1.98 KB

Versions: 5

Compression:

Stored size: 1.98 KB

Contents

module Zype
  class Client
    attr_reader :headers
    include HTTParty
    class NoApiKey < StandardError; end
    class NoAppKey < StandardError; end
    class NotFound < StandardError; end
    class ServerError < StandardError; end
    class Unauthorized < StandardError; end
    class UnprocessableEntity < StandardError; end

    # for error types not explicity mapped
    class GenericError < StandardError; end
    ERROR_TYPES = {
      401 => Unauthorized,
      404 => NotFound,
      422 => UnprocessableEntity,
      500 => ServerError
    }.freeze

    # Converts all files in lib/zype/models to be used as methods
    class << self
      Models::LIST.each do |model|
        define_method model do
          constant = model.split('_').map(&:capitalize).join('')
          Module.const_get("Zype::#{constant}").new
        end
      end
    end

    def get(path:, params: {})
      self.class.get(path, query: params, headers: headers)
    end

    def post(path:, params: {})
      self.class.post(path, query: params, headers: headers)
    end

    def put(path:, params: {})
      self.class.put(path, query: params, headers: headers)
    end

    def delete(path:, params: _)
      self.class.delete(path, headers: headers)
    end

    def execute(method:, path:, params: {})
      if Zype.configuration.api_key.to_s.empty?
        raise NoApiKey if Zype.configuration.app_key.to_s.empty?
      end

      resp = send(method, path: path, params: params)
      if resp.success?
        resp['response'].nil? ? resp.parsed_response : resp['response']
      else
        error!(code: resp.code, message: resp['message'])
      end
    end

    private

    def error!(code:, message:)
      error_type = ERROR_TYPES[code] || GenericError
      raise error_type.new(message)
    end

    def authentication(auth_method)
      if auth_method.to_sym == :api_key
        { 'x-zype-key' => Zype.configuration.api_key }
      else
        { 'x-zype-app-key' => Zype.configuration.app_key }
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
zype-0.20.0 lib/zype/client.rb
zype-0.19.0 lib/zype/client.rb
zype-0.18.0 lib/zype/client.rb
zype-0.17.0 lib/zype/client.rb
zype-0.16.1 lib/zype/client.rb