Class: Tito::Base
- Inherits:
-
Object
show all
- Defined in:
- lib/tito/base.rb
Direct Known Subclasses
Account, Answer, DiscountCode, Event, EventSettings, InterestedUser, Question, Registration, Release, Ticket, WebhookEndpoint, WebhookEndpointIncludedTrigger
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.
96
97
98
99
100
101
|
# File 'lib/tito/base.rb', line 96
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
111
112
113
114
115
116
117
|
# File 'lib/tito/base.rb', line 111
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_prefix ⇒ Object
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
|
.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_name ⇒ Object
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
|
.site ⇒ Object
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_context ⇒ Object
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_name ⇒ Object
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
#attributes ⇒ Object
103
104
105
|
# File 'lib/tito/base.rb', line 103
def attributes
@attributes ||= {}
end
|
#attributes=(attrs) ⇒ Object
107
108
109
|
# File 'lib/tito/base.rb', line 107
def attributes=(attrs)
@attributes = attrs
end
|
#auth(api_key: nil) ⇒ Object
127
128
129
|
# File 'lib/tito/base.rb', line 127
def auth(api_key: nil)
self.class.auth(api_key: api_key)
end
|
#destroy(api_key: nil) ⇒ Object
156
157
158
|
# File 'lib/tito/base.rb', line 156
def destroy(api_key: nil)
http(api_key: api_key).delete(put_url, ssl_context: ssl_context)
end
|
#http(api_key: nil) ⇒ Object
131
132
133
|
# File 'lib/tito/base.rb', line 131
def http(api_key: nil)
self.class.http(api_key: api_key)
end
|
#new_record? ⇒ Boolean
119
120
121
|
# File 'lib/tito/base.rb', line 119
def new_record?
attributes["id"] == nil
end
|
#persisted? ⇒ Boolean
123
124
125
|
# File 'lib/tito/base.rb', line 123
def persisted?
!new_record?
end
|
#post_url ⇒ Object
143
144
145
|
# File 'lib/tito/base.rb', line 143
def post_url
[Tito::Base.site, post_path].join("/")
end
|
#put_url ⇒ Object
139
140
141
|
# File 'lib/tito/base.rb', line 139
def put_url
[Tito::Base.site, put_path].join("/")
end
|
#save(api_key: nil) ⇒ Object
147
148
149
150
151
152
153
154
|
# File 'lib/tito/base.rb', line 147
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 ⇒ Object
135
136
137
|
# File 'lib/tito/base.rb', line 135
def ssl_context
self.class.ssl_context
end
|