class Tito::Base

Attributes

path_prefix[RW]

Public Class Methods

all(params = {}) click to toggle source
# File lib/tito/base.rb, line 83
def self.all(params = {})
  api_key = params.delete(:api_key)
  path_prefix = params.delete(:path_prefix)
  response = http(api_key: api_key).get(all_url(path_prefix: path_prefix), params: ParamNester.encode(params), ssl_context: ssl_context).parse
  all_records = response[self.resource_path(:all)]
  meta = response["meta"]
  out = ResponseArray.new((all_records || []).collect do |record|
    new record
  end)
  out.meta = meta
  out
end
all_path(path_prefix: nil) click to toggle source
# File lib/tito/base.rb, line 75
def self.all_path(path_prefix: nil)
  [path_prefix, resource_path(:all)].compact.join("/")
end
all_url(path_prefix: nil) click to toggle source
# File lib/tito/base.rb, line 79
def self.all_url(path_prefix: nil)
  [site, all_path(path_prefix: path_prefix)].join("/")
end
auth(api_key: nil) click to toggle source
# File lib/tito/base.rb, line 12
def self.auth(api_key: nil)
  token = api_key || Tito.api_key
  raise "API key is required" if !token
  if token.length > 30
    %(Bearer #{token})
  else
    %(Token token="#{token}")
  end
end
event_ids(params = {}) click to toggle source
# File lib/tito/base.rb, line 96
def self.event_ids(params = {})
  api_key = params.delete(:api_key)
  url = [site, "my/events/ids"].join("/")
  http(api_key: api_key).get(url, ssl_context: ssl_context).parse
end
get(path, params = {}) click to toggle source
# File lib/tito/base.rb, line 69
def self.get(path, params = {})
  api_key = params.delete(:api_key)
  path_prefix = params.delete(:path_prefix)
  new http(api_key: api_key).get(get_url(path, path_prefix: path_prefix), params: ParamNester.encode(params), ssl_context: ssl_context).parse[resource_name]
end
get_path(path, path_prefix: nil) click to toggle source
# File lib/tito/base.rb, line 61
def self.get_path(path, path_prefix: nil)
  [path_prefix, self.resource_path, path].compact.join("/")
end
get_url(path, path_prefix: nil) click to toggle source
# File lib/tito/base.rb, line 65
def self.get_url(path, path_prefix: nil)
  [Tito::Base.site, get_path(path, path_prefix: path_prefix)].join("/")
end
http(api_key: nil) click to toggle source
# File lib/tito/base.rb, line 33
def self.http(api_key: nil)
  HTTP[user_agent: "Tito Ruby API #{Tito::VERSION}"].auth(auth(api_key: api_key)).accept("application/json")
end
new(attrs = {}) click to toggle source
# File lib/tito/base.rb, line 102
def initialize(attrs = {})
  self.path_prefix = (attrs ||= {}).delete(:path_prefix)
  attrs.each do |key, val|
    attributes[key.to_s] = val
  end
end
resource_name() click to toggle source
# File lib/tito/base.rb, line 53
def self.resource_name
  @resource_name ||= underscore_class_name.split("/").last
end
resource_name=(val) click to toggle source
# File lib/tito/base.rb, line 41
def self.resource_name=(val)
  @resource_name = val
end
resource_path(*args) click to toggle source
# File lib/tito/base.rb, line 57
def self.resource_path(*args)
  @resource_path ||= "#{resource_name}s"
end
resource_path=(val) click to toggle source
# File lib/tito/base.rb, line 37
def self.resource_path=(val)
  @resource_path = val
end
site() click to toggle source
# File lib/tito/base.rb, line 4
def self.site
  @site || ENV['TITO_SITE'] || "https://api.tito.io/v2"
end
site=(val) click to toggle source
# File lib/tito/base.rb, line 8
def self.site=(val)
  @site = val
end
ssl_context() click to toggle source
# File lib/tito/base.rb, line 22
def self.ssl_context
  @ssl_context ||= begin
    ctx = OpenSSL::SSL::SSLContext.new
    ctx
  end
end
ssl_context=(ctx) click to toggle source
# File lib/tito/base.rb, line 29
def self.ssl_context=(ctx)
  @ssl_context = ctx
end
underscore_class_name() click to toggle source
# File lib/tito/base.rb, line 45
def self.underscore_class_name
  self.to_s.gsub(/::/, '/').
  gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
  gsub(/([a-z\d])([A-Z])/,'\1_\2').
  tr("-", "_").
  downcase
end

Public Instance Methods

attributes() click to toggle source
# File lib/tito/base.rb, line 109
def attributes
  @attributes ||= {}
end
attributes=(attrs) click to toggle source
# File lib/tito/base.rb, line 113
def attributes=(attrs)
  @attributes = attrs
end
auth(api_key: nil) click to toggle source
# File lib/tito/base.rb, line 133
def auth(api_key: nil)
  self.class.auth(api_key: api_key)
end
destroy(api_key: nil) click to toggle source
# File lib/tito/base.rb, line 162
def destroy(api_key: nil)
  http(api_key: api_key).delete(put_url, ssl_context: ssl_context)
end
http(api_key: nil) click to toggle source
# File lib/tito/base.rb, line 137
def http(api_key: nil)
  self.class.http(api_key: api_key)
end
method_missing(method_name, val = nil) click to toggle source
# File lib/tito/base.rb, line 117
def method_missing(method_name, val = nil)
  if method_name.to_s[-1] == "="
    self.attributes[method_name[0, (method_name.length - 1)]] = val
  else
    self.attributes[method_name.to_s]
  end
end
new_record?() click to toggle source
# File lib/tito/base.rb, line 125
def new_record?
  attributes["id"] == nil
end
persisted?() click to toggle source
# File lib/tito/base.rb, line 129
def persisted?
  !new_record?
end
post_url() click to toggle source
# File lib/tito/base.rb, line 149
def post_url
  [Tito::Base.site, post_path].join("/")
end
put_url() click to toggle source
# File lib/tito/base.rb, line 145
def put_url
  [Tito::Base.site, put_path].join("/")
end
save(api_key: nil) click to toggle source
# File lib/tito/base.rb, line 153
def save(api_key: nil)
  if persisted?
    attrs = http(api_key: api_key).put(put_url, json: {self.class.resource_name => attributes}, ssl_context: ssl_context).parse[self.class.resource_name]
  else
    attrs = http(api_key: api_key).post(post_url, json: {self.class.resource_name => attributes}, ssl_context: ssl_context).parse[self.class.resource_name]
  end
  self.attributes = attrs
end
ssl_context() click to toggle source
# File lib/tito/base.rb, line 141
def ssl_context
  self.class.ssl_context
end