Class: Tito::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/tito/base.rb

Defined Under Namespace

Modules: ParamNester

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ Base

Returns a new instance of Base.



102
103
104
105
106
107
# 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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, val = nil) ⇒ Object



117
118
119
120
121
122
123
# 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

Instance Attribute Details

#path_prefixObject

Returns the value of attribute path_prefix.



3
4
5
# File 'lib/tito/base.rb', line 3

def path_prefix
  @path_prefix
end

Class Method Details

.all(params = {}) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
# 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) ⇒ Object



75
76
77
# 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) ⇒ Object



79
80
81
# 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) ⇒ Object



12
13
14
15
16
17
18
19
20
# 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 = {}) ⇒ Object



96
97
98
99
100
# 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 = {}) ⇒ Object



69
70
71
72
73
# 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) ⇒ Object



61
62
63
# 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) ⇒ Object



65
66
67
# 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) ⇒ Object



33
34
35
# 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

.resource_nameObject



53
54
55
# File 'lib/tito/base.rb', line 53

def self.resource_name
  @resource_name ||= underscore_class_name.split("/").last
end

.resource_name=(val) ⇒ Object



41
42
43
# File 'lib/tito/base.rb', line 41

def self.resource_name=(val)
  @resource_name = val
end

.resource_path(*args) ⇒ Object



57
58
59
# File 'lib/tito/base.rb', line 57

def self.resource_path(*args)
  @resource_path ||= "#{resource_name}s"
end

.resource_path=(val) ⇒ Object



37
38
39
# File 'lib/tito/base.rb', line 37

def self.resource_path=(val)
  @resource_path = val
end

.siteObject



4
5
6
# File 'lib/tito/base.rb', line 4

def self.site
  @site || ENV['TITO_SITE'] || "https://api.tito.io/v2"
end

.site=(val) ⇒ Object



8
9
10
# File 'lib/tito/base.rb', line 8

def self.site=(val)
  @site = val
end

.ssl_contextObject



22
23
24
25
26
27
# 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) ⇒ Object



29
30
31
# File 'lib/tito/base.rb', line 29

def self.ssl_context=(ctx)
  @ssl_context = ctx
end

.underscore_class_nameObject



45
46
47
48
49
50
51
# 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

Instance Method Details

#attributesObject



109
110
111
# File 'lib/tito/base.rb', line 109

def attributes
  @attributes ||= {}
end

#attributes=(attrs) ⇒ Object



113
114
115
# File 'lib/tito/base.rb', line 113

def attributes=(attrs)
  @attributes = attrs
end

#auth(api_key: nil) ⇒ Object



133
134
135
# File 'lib/tito/base.rb', line 133

def auth(api_key: nil)
  self.class.auth(api_key: api_key)
end

#destroy(api_key: nil) ⇒ Object



162
163
164
# 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) ⇒ Object



137
138
139
# File 'lib/tito/base.rb', line 137

def http(api_key: nil)
  self.class.http(api_key: api_key)
end

#new_record?Boolean

Returns:

  • (Boolean)


125
126
127
# File 'lib/tito/base.rb', line 125

def new_record?
  attributes["id"] == nil
end

#persisted?Boolean

Returns:

  • (Boolean)


129
130
131
# File 'lib/tito/base.rb', line 129

def persisted?
  !new_record?
end

#post_urlObject



149
150
151
# File 'lib/tito/base.rb', line 149

def post_url
  [Tito::Base.site, post_path].join("/")
end

#put_urlObject



145
146
147
# File 'lib/tito/base.rb', line 145

def put_url
  [Tito::Base.site, put_path].join("/")
end

#save(api_key: nil) ⇒ Object



153
154
155
156
157
158
159
160
# 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_contextObject



141
142
143
# File 'lib/tito/base.rb', line 141

def ssl_context
  self.class.ssl_context
end