class Codat::BaseModel

Public Class Methods

attributes(*attributes) click to toggle source
# File lib/codat/base_model.rb, line 25
def attributes(*attributes)
  @attributes ||= []

  return @attributes unless attributes

  attr_accessor(*attributes)

  @attributes += attributes
end
format_url(url, params) click to toggle source
# File lib/codat/base_model.rb, line 35
def format_url(url, params)
  formatted = url.dup.strip

  params.each { |key, value| formatted.sub!(":#{key}", value) }

  formatted
end
get(path, params = {}) click to toggle source
# File lib/codat/base_model.rb, line 9
def get(path, params = {})
  result = Codat.client.get(path.strip, params)

  raise APIKeyError, result.body[:error] if result.status == 401

  result
end
new(json: {}, key_transformer: Camelizer) click to toggle source

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.

# File lib/codat/base_model.rb, line 52
def initialize(json: {}, key_transformer: Camelizer)
  self.class.attributes.each do |attr|
    send("#{attr}=", json[key_transformer.transform(attr)])
  end
end
post(path, params = {}) click to toggle source
# File lib/codat/base_model.rb, line 17
def post(path, params = {})
  result = Codat.client.post(path.strip, params)

  raise APIKeyError, result.body[:error] if result.status == 401

  result
end
successful_response?(result) click to toggle source

As per Codat API doc docs.codat.io/reference/errors

# File lib/codat/base_model.rb, line 45
def successful_response?(result)
  result.status < 400
end

Public Instance Methods

format_url(url, params) click to toggle source
# File lib/codat/base_model.rb, line 66
def format_url(url, params)
  self.class.format_url(url, params)
end
get(path, params = {}) click to toggle source
# File lib/codat/base_model.rb, line 58
def get(path, params = {})
  self.class.get(path, params)
end
post(path, params = {}) click to toggle source
# File lib/codat/base_model.rb, line 62
def post(path, params = {})
  self.class.post(path, params)
end