Sha256: 4f74e6ee21c901d5f65def98045df06b46ca4736537bc2c8bd402f256ab0d2a8
Contents?: true
Size: 1.57 KB
Versions: 6
Compression:
Stored size: 1.57 KB
Contents
# frozen_string_literal: true require 'codat/errors' require 'codat/camelizer' module Codat class BaseModel class << self def get(path, params = {}) result = Codat.client.get(path.strip, params) raise APIKeyError, result.body[:error] if result.status == 401 result end def post(path, params = {}) result = Codat.client.post(path.strip, params) raise APIKeyError, result.body[:error] if result.status == 401 result end def attributes(*attributes) @attributes ||= [] return @attributes unless attributes attr_accessor(*attributes) @attributes += attributes end def format_url(url, params) formatted = url.dup.strip params.each { |key, value| formatted.sub!(":#{key}", value) } formatted end # As per Codat API doc # https://docs.codat.io/reference/errors def successful_response?(result) result.status < 400 end end # Sets all the instance variables by reading the JSON from Codat and converting the keys from # camelCase to snake_case, as it's the standard in Ruby. def initialize(json: {}, key_transformer: Camelizer) self.class.attributes.each do |attr| send("#{attr}=", json[key_transformer.transform(attr)]) end end def get(path, params = {}) self.class.get(path, params) end def post(path, params = {}) self.class.post(path, params) end def format_url(url, params) self.class.format_url(url, params) end end end
Version data entries
6 entries across 6 versions & 1 rubygems