tito_ruby 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (36) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +12 -0
  3. data/LICENSE.txt +21 -0
  4. data/README.md +119 -0
  5. data/lib/tito/admin/client.rb +98 -0
  6. data/lib/tito/admin/collection_proxy.rb +119 -0
  7. data/lib/tito/admin/query_builder.rb +110 -0
  8. data/lib/tito/admin/resources/activity.rb +32 -0
  9. data/lib/tito/admin/resources/answer.rb +23 -0
  10. data/lib/tito/admin/resources/checkin_list.rb +34 -0
  11. data/lib/tito/admin/resources/discount_code.rb +36 -0
  12. data/lib/tito/admin/resources/event.rb +62 -0
  13. data/lib/tito/admin/resources/interested_user.rb +19 -0
  14. data/lib/tito/admin/resources/opt_in.rb +23 -0
  15. data/lib/tito/admin/resources/question.rb +30 -0
  16. data/lib/tito/admin/resources/refund.rb +20 -0
  17. data/lib/tito/admin/resources/registration.rb +49 -0
  18. data/lib/tito/admin/resources/release.rb +67 -0
  19. data/lib/tito/admin/resources/release_invitation.rb +32 -0
  20. data/lib/tito/admin/resources/rsvp_list.rb +26 -0
  21. data/lib/tito/admin/resources/ticket.rb +59 -0
  22. data/lib/tito/admin/resources/venue.rb +19 -0
  23. data/lib/tito/admin/resources/waitlisted_person.rb +35 -0
  24. data/lib/tito/admin/resources/webhook_endpoint.rb +20 -0
  25. data/lib/tito/admin/scope.rb +71 -0
  26. data/lib/tito/configuration.rb +9 -0
  27. data/lib/tito/connection.rb +42 -0
  28. data/lib/tito/errors.rb +40 -0
  29. data/lib/tito/resource.rb +172 -0
  30. data/lib/tito/types/integer_array.rb +15 -0
  31. data/lib/tito/types/json.rb +16 -0
  32. data/lib/tito/types/string_array.rb +15 -0
  33. data/lib/tito/version.rb +3 -0
  34. data/lib/tito.rb +63 -0
  35. data/lib/tito_ruby.rb +1 -0
  36. metadata +180 -0
@@ -0,0 +1,172 @@
1
+ module Tito
2
+ class Resource
3
+ include ActiveModel::API
4
+ include ActiveModel::Attributes
5
+ include ActiveModel::Dirty
6
+
7
+ class << self
8
+ def path_template(template = nil)
9
+ if template
10
+ @_path_template = template
11
+ else
12
+ @_path_template
13
+ end
14
+ end
15
+
16
+ def member_path_template(template = nil)
17
+ if template
18
+ @_member_path_template = template
19
+ else
20
+ @_member_path_template
21
+ end
22
+ end
23
+
24
+ def resource_name(name = nil)
25
+ if name
26
+ @_resource_name = name
27
+ else
28
+ @_resource_name
29
+ end
30
+ end
31
+
32
+ def collection_name(name = nil)
33
+ if name
34
+ @_collection_name = name
35
+ else
36
+ @_collection_name || "#{@_resource_name}s"
37
+ end
38
+ end
39
+
40
+ def event_scoped!
41
+ @_event_scoped = true
42
+ end
43
+
44
+ def account_scoped!
45
+ @_event_scoped = false
46
+ end
47
+
48
+ def event_scoped?
49
+ @_event_scoped == true
50
+ end
51
+
52
+ def supports(*ops)
53
+ @_supported_operations = ops
54
+ end
55
+
56
+ def supported_operations
57
+ @_supported_operations || %i[list show create update delete]
58
+ end
59
+
60
+ def expandable(*names)
61
+ @_expandable_fields = names
62
+ end
63
+
64
+ def expandable_fields
65
+ @_expandable_fields || []
66
+ end
67
+
68
+ def action(name, method: :post, path:)
69
+ define_method(name) do |**params|
70
+ body = params.empty? ? {} : { self.class.resource_name => params }
71
+ response = _scope.request(method, "#{_scope.member_path(identifier)}/#{path}", body: body)
72
+ if response.is_a?(Hash) && response[self.class.resource_name]
73
+ _apply_response(response)
74
+ end
75
+ self
76
+ end
77
+ end
78
+
79
+ # Declare a nested resource accessor
80
+ def has_many(name, resource_class_name:, foreign_key: nil)
81
+ define_method(name) do
82
+ child_class = Tito::Admin::Resources.const_get(resource_class_name)
83
+ child_scope = _scope.nested_scope(child_class, parent_id: identifier)
84
+ Tito::Admin::CollectionProxy.new(scope: child_scope)
85
+ end
86
+ end
87
+ end
88
+
89
+ attr_reader :_scope
90
+
91
+ def initialize(_scope: nil, **attrs)
92
+ @_scope = _scope
93
+ @_persisted = !!(attrs[:id] || attrs[:slug] || attrs["id"] || attrs["slug"])
94
+ # Normalize string keys to symbols for ActiveModel
95
+ normalized = attrs.transform_keys(&:to_sym).except(:_type)
96
+ super(**normalized)
97
+ clear_changes_information if @_persisted
98
+ end
99
+
100
+ def persisted?
101
+ @_persisted
102
+ end
103
+
104
+ def new_record?
105
+ !persisted?
106
+ end
107
+
108
+ def identifier
109
+ slug = respond_to?(:slug) ? self.slug : nil
110
+ slug.present? ? slug : id
111
+ end
112
+
113
+ def save
114
+ if persisted?
115
+ _update
116
+ else
117
+ _create
118
+ end
119
+ end
120
+
121
+ def destroy
122
+ _scope.request(:delete, _scope.member_path(identifier))
123
+ @_destroyed = true
124
+ freeze
125
+ true
126
+ end
127
+
128
+ def destroyed?
129
+ @_destroyed == true
130
+ end
131
+
132
+ def reload
133
+ data = _scope.request(:get, _scope.member_path(identifier))
134
+ _apply_response(data)
135
+ self
136
+ end
137
+
138
+ private
139
+
140
+ def _create
141
+ body = { self.class.resource_name => _writable_attributes }
142
+ data = _scope.request(:post, _scope.collection_path, body: body)
143
+ _apply_response(data)
144
+ @_persisted = true
145
+ self
146
+ end
147
+
148
+ def _update
149
+ body = { self.class.resource_name => _changed_attributes_hash }
150
+ data = _scope.request(:patch, _scope.member_path(identifier), body: body)
151
+ _apply_response(data)
152
+ self
153
+ end
154
+
155
+ def _changed_attributes_hash
156
+ changed.each_with_object({}) { |attr, h| h[attr] = send(attr) }
157
+ end
158
+
159
+ def _writable_attributes
160
+ attributes.reject { |_, v| v.nil? }
161
+ end
162
+
163
+ def _apply_response(data)
164
+ attrs = data.is_a?(Hash) ? (data[self.class.resource_name] || data) : {}
165
+ attrs.each do |key, value|
166
+ setter = "#{key}="
167
+ send(setter, value) if respond_to?(setter)
168
+ end
169
+ clear_changes_information
170
+ end
171
+ end
172
+ end
@@ -0,0 +1,15 @@
1
+ module Tito
2
+ module Types
3
+ class IntegerArray < ActiveModel::Type::Value
4
+ def type = :integer_array
5
+
6
+ def cast(value)
7
+ case value
8
+ when Array then value.map(&:to_i)
9
+ when nil then nil
10
+ else [value.to_i]
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,16 @@
1
+ module Tito
2
+ module Types
3
+ class Json < ActiveModel::Type::Value
4
+ def type = :json
5
+
6
+ def cast(value)
7
+ case value
8
+ when Hash, Array then value
9
+ when String then JSON.parse(value)
10
+ when nil then nil
11
+ else value
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,15 @@
1
+ module Tito
2
+ module Types
3
+ class StringArray < ActiveModel::Type::Value
4
+ def type = :string_array
5
+
6
+ def cast(value)
7
+ case value
8
+ when Array then value.map(&:to_s)
9
+ when nil then nil
10
+ else [value.to_s]
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,3 @@
1
+ module Tito
2
+ VERSION = "0.1.0"
3
+ end
data/lib/tito.rb ADDED
@@ -0,0 +1,63 @@
1
+ require "faraday"
2
+ require "active_model"
3
+
4
+ module Tito
5
+ require "tito/version"
6
+
7
+ autoload :Configuration, "tito/configuration"
8
+ autoload :Connection, "tito/connection"
9
+ autoload :Errors, "tito/errors"
10
+ autoload :Resource, "tito/resource"
11
+
12
+ module Types
13
+ autoload :StringArray, "tito/types/string_array"
14
+ autoload :IntegerArray, "tito/types/integer_array"
15
+ autoload :Json, "tito/types/json"
16
+ end
17
+
18
+ module Admin
19
+ autoload :Client, "tito/admin/client"
20
+ autoload :CollectionProxy, "tito/admin/collection_proxy"
21
+ autoload :Scope, "tito/admin/scope"
22
+ autoload :QueryBuilder, "tito/admin/query_builder"
23
+
24
+ module Resources
25
+ autoload :Event, "tito/admin/resources/event"
26
+ autoload :Release, "tito/admin/resources/release"
27
+ autoload :Ticket, "tito/admin/resources/ticket"
28
+ autoload :Registration, "tito/admin/resources/registration"
29
+ autoload :Activity, "tito/admin/resources/activity"
30
+ autoload :Question, "tito/admin/resources/question"
31
+ autoload :Answer, "tito/admin/resources/answer"
32
+ autoload :DiscountCode, "tito/admin/resources/discount_code"
33
+ autoload :CheckinList, "tito/admin/resources/checkin_list"
34
+ autoload :OptIn, "tito/admin/resources/opt_in"
35
+ autoload :InterestedUser, "tito/admin/resources/interested_user"
36
+ autoload :RsvpList, "tito/admin/resources/rsvp_list"
37
+ autoload :ReleaseInvitation, "tito/admin/resources/release_invitation"
38
+ autoload :WaitlistedPerson, "tito/admin/resources/waitlisted_person"
39
+ autoload :WebhookEndpoint, "tito/admin/resources/webhook_endpoint"
40
+ autoload :Refund, "tito/admin/resources/refund"
41
+ autoload :Venue, "tito/admin/resources/venue"
42
+ end
43
+ end
44
+
45
+ class << self
46
+ def configure
47
+ yield configuration
48
+ end
49
+
50
+ def configuration
51
+ @configuration ||= Configuration.new
52
+ end
53
+
54
+ def reset_configuration!
55
+ @configuration = Configuration.new
56
+ end
57
+ end
58
+ end
59
+
60
+ # Register custom types
61
+ ActiveModel::Type.register(:string_array, Tito::Types::StringArray)
62
+ ActiveModel::Type.register(:integer_array, Tito::Types::IntegerArray)
63
+ ActiveModel::Type.register(:json, Tito::Types::Json)
data/lib/tito_ruby.rb ADDED
@@ -0,0 +1 @@
1
+ require "tito"
metadata ADDED
@@ -0,0 +1,180 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tito_ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Joshua Paine
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: faraday
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - "~>"
17
+ - !ruby/object:Gem::Version
18
+ version: '2.0'
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - "~>"
24
+ - !ruby/object:Gem::Version
25
+ version: '2.0'
26
+ - !ruby/object:Gem::Dependency
27
+ name: activemodel
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '7.1'
33
+ - - "<"
34
+ - !ruby/object:Gem::Version
35
+ version: '9.0'
36
+ type: :runtime
37
+ prerelease: false
38
+ version_requirements: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: '7.1'
43
+ - - "<"
44
+ - !ruby/object:Gem::Version
45
+ version: '9.0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: minitest
48
+ requirement: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - "~>"
51
+ - !ruby/object:Gem::Version
52
+ version: '5.0'
53
+ type: :development
54
+ prerelease: false
55
+ version_requirements: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - "~>"
58
+ - !ruby/object:Gem::Version
59
+ version: '5.0'
60
+ - !ruby/object:Gem::Dependency
61
+ name: vcr
62
+ requirement: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - "~>"
65
+ - !ruby/object:Gem::Version
66
+ version: '6.0'
67
+ type: :development
68
+ prerelease: false
69
+ version_requirements: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - "~>"
72
+ - !ruby/object:Gem::Version
73
+ version: '6.0'
74
+ - !ruby/object:Gem::Dependency
75
+ name: webmock
76
+ requirement: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - "~>"
79
+ - !ruby/object:Gem::Version
80
+ version: '3.0'
81
+ type: :development
82
+ prerelease: false
83
+ version_requirements: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - "~>"
86
+ - !ruby/object:Gem::Version
87
+ version: '3.0'
88
+ - !ruby/object:Gem::Dependency
89
+ name: dotenv
90
+ requirement: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - "~>"
93
+ - !ruby/object:Gem::Version
94
+ version: '3.0'
95
+ type: :development
96
+ prerelease: false
97
+ version_requirements: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - "~>"
100
+ - !ruby/object:Gem::Version
101
+ version: '3.0'
102
+ - !ruby/object:Gem::Dependency
103
+ name: rake
104
+ requirement: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - "~>"
107
+ - !ruby/object:Gem::Version
108
+ version: '13.0'
109
+ type: :development
110
+ prerelease: false
111
+ version_requirements: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - "~>"
114
+ - !ruby/object:Gem::Version
115
+ version: '13.0'
116
+ description: A Ruby client library for the Tito event management Admin API (v3). Supports
117
+ events, tickets, registrations, releases, and more.
118
+ executables: []
119
+ extensions: []
120
+ extra_rdoc_files: []
121
+ files:
122
+ - CHANGELOG.md
123
+ - LICENSE.txt
124
+ - README.md
125
+ - lib/tito.rb
126
+ - lib/tito/admin/client.rb
127
+ - lib/tito/admin/collection_proxy.rb
128
+ - lib/tito/admin/query_builder.rb
129
+ - lib/tito/admin/resources/activity.rb
130
+ - lib/tito/admin/resources/answer.rb
131
+ - lib/tito/admin/resources/checkin_list.rb
132
+ - lib/tito/admin/resources/discount_code.rb
133
+ - lib/tito/admin/resources/event.rb
134
+ - lib/tito/admin/resources/interested_user.rb
135
+ - lib/tito/admin/resources/opt_in.rb
136
+ - lib/tito/admin/resources/question.rb
137
+ - lib/tito/admin/resources/refund.rb
138
+ - lib/tito/admin/resources/registration.rb
139
+ - lib/tito/admin/resources/release.rb
140
+ - lib/tito/admin/resources/release_invitation.rb
141
+ - lib/tito/admin/resources/rsvp_list.rb
142
+ - lib/tito/admin/resources/ticket.rb
143
+ - lib/tito/admin/resources/venue.rb
144
+ - lib/tito/admin/resources/waitlisted_person.rb
145
+ - lib/tito/admin/resources/webhook_endpoint.rb
146
+ - lib/tito/admin/scope.rb
147
+ - lib/tito/configuration.rb
148
+ - lib/tito/connection.rb
149
+ - lib/tito/errors.rb
150
+ - lib/tito/resource.rb
151
+ - lib/tito/types/integer_array.rb
152
+ - lib/tito/types/json.rb
153
+ - lib/tito/types/string_array.rb
154
+ - lib/tito/version.rb
155
+ - lib/tito_ruby.rb
156
+ homepage: https://github.com/midnightmonster/tito_ruby
157
+ licenses:
158
+ - MIT
159
+ metadata:
160
+ source_code_uri: https://github.com/midnightmonster/tito_ruby
161
+ changelog_uri: https://github.com/midnightmonster/tito_ruby/blob/main/CHANGELOG.md
162
+ rubygems_mfa_required: 'true'
163
+ rdoc_options: []
164
+ require_paths:
165
+ - lib
166
+ required_ruby_version: !ruby/object:Gem::Requirement
167
+ requirements:
168
+ - - ">="
169
+ - !ruby/object:Gem::Version
170
+ version: '3.1'
171
+ required_rubygems_version: !ruby/object:Gem::Requirement
172
+ requirements:
173
+ - - ">="
174
+ - !ruby/object:Gem::Version
175
+ version: '0'
176
+ requirements: []
177
+ rubygems_version: 3.6.9
178
+ specification_version: 4
179
+ summary: Ruby client for the Tito Admin API v3.1
180
+ test_files: []