eventbrite_sdk 3.0.2

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 (37) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +2 -0
  3. data/.rspec +2 -0
  4. data/.travis.yml +7 -0
  5. data/Gemfile +12 -0
  6. data/LICENSE.txt +21 -0
  7. data/README.md +136 -0
  8. data/Rakefile +6 -0
  9. data/bin/console +16 -0
  10. data/bin/setup +6 -0
  11. data/eventbrite_sdk.gemspec +28 -0
  12. data/lib/eventbrite_sdk.rb +135 -0
  13. data/lib/eventbrite_sdk/attendee.rb +26 -0
  14. data/lib/eventbrite_sdk/category.rb +14 -0
  15. data/lib/eventbrite_sdk/event.rb +75 -0
  16. data/lib/eventbrite_sdk/exceptions.rb +42 -0
  17. data/lib/eventbrite_sdk/lists/owned_event_orders_list.rb +27 -0
  18. data/lib/eventbrite_sdk/media.rb +77 -0
  19. data/lib/eventbrite_sdk/order.rb +25 -0
  20. data/lib/eventbrite_sdk/organizer.rb +20 -0
  21. data/lib/eventbrite_sdk/report.rb +49 -0
  22. data/lib/eventbrite_sdk/resource.rb +99 -0
  23. data/lib/eventbrite_sdk/resource/attributes.rb +158 -0
  24. data/lib/eventbrite_sdk/resource/null_schema_definition.rb +13 -0
  25. data/lib/eventbrite_sdk/resource/operations/attribute_schema.rb +57 -0
  26. data/lib/eventbrite_sdk/resource/operations/endpoint.rb +101 -0
  27. data/lib/eventbrite_sdk/resource/operations/list.rb +15 -0
  28. data/lib/eventbrite_sdk/resource/operations/relationships.rb +120 -0
  29. data/lib/eventbrite_sdk/resource/schema_definition.rb +50 -0
  30. data/lib/eventbrite_sdk/resource_list.rb +86 -0
  31. data/lib/eventbrite_sdk/subcategory.rb +12 -0
  32. data/lib/eventbrite_sdk/ticket_class.rb +60 -0
  33. data/lib/eventbrite_sdk/user.rb +28 -0
  34. data/lib/eventbrite_sdk/venue.rb +22 -0
  35. data/lib/eventbrite_sdk/version.rb +5 -0
  36. data/lib/eventbrite_sdk/webhook.rb +11 -0
  37. metadata +167 -0
@@ -0,0 +1,13 @@
1
+ module EventbriteSDK
2
+ class Resource
3
+ class NullSchemaDefinition
4
+ def writeable?(_key)
5
+ true
6
+ end
7
+
8
+ def defined_keys
9
+ []
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,57 @@
1
+ module EventbriteSDK
2
+ class Resource
3
+ module Operations
4
+ module AttributeSchema
5
+ module ClassMethods
6
+ attr_reader :prefix, :schema
7
+
8
+ def schema_definition(&block)
9
+ @schema = SchemaDefinition.new(name)
10
+ @schema.instance_eval(&block)
11
+ end
12
+
13
+ def attributes_prefix(prefix)
14
+ @prefix = prefix
15
+ end
16
+ end
17
+
18
+ module InstanceMethods
19
+ %i(changes changed?).each do |method|
20
+ define_method(method) { attrs.public_send(method) }
21
+ end
22
+
23
+ %i([] assign_attributes).each do |method|
24
+ define_method(method) { |arg| attrs.public_send(method, arg) }
25
+ end
26
+
27
+ def build_attrs(new_attrs)
28
+ @attrs = Attributes.new(
29
+ new_attrs, self.class.schema || NullSchemaDefinition.new
30
+ )
31
+ end
32
+
33
+ private
34
+
35
+ def method_missing(method_name, *_args, &_block)
36
+ if attrs.respond_to?(method_name)
37
+ attrs.public_send(method_name)
38
+ else
39
+ super
40
+ end
41
+ end
42
+
43
+ def respond_to_missing?(method_name, _include_private = false)
44
+ attrs.respond_to?(method_name) || super
45
+ end
46
+
47
+ attr_reader :attrs
48
+ end
49
+
50
+ def self.included(receiver)
51
+ receiver.extend ClassMethods
52
+ receiver.prepend InstanceMethods
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,101 @@
1
+ module EventbriteSDK
2
+ class Resource
3
+ module Operations
4
+ module Endpoint
5
+ module ClassMethods
6
+ attr_reader :path, :path_opts
7
+
8
+ # Retrieve a resource.
9
+ #
10
+ # params: Hash of supported parameters. The keys and values are
11
+ # used to build the request URL by substituting supported
12
+ # keys in the resource_path with the value defined in params.
13
+ #
14
+ # The :expand key allows the support of expansions of child
15
+ # objects. It supports strings, symbols and arrays.
16
+ #
17
+ # expand: :something
18
+ # expand: %i(something another)
19
+ # expand: %w(something another)
20
+ # expand: 'something,another'
21
+ #
22
+ # Example:
23
+ #
24
+ # class Thing < Resource
25
+ # resource_path('things/:id')
26
+ # end
27
+ #
28
+ # Thing.retrieve(id: 1234, expand: :others)
29
+ #
30
+ # This tells the resource to replace the :id placeholder with the
31
+ # value 1234. It also will pass the :expand option with the
32
+ def retrieve(params, request = EventbriteSDK)
33
+ url_path = params.reduce(@path) do |path, (key, value)|
34
+ path.gsub(":#{key}", value.to_s)
35
+ end
36
+
37
+ if params[:expand]
38
+ query = { expand: [*params[:expand]].join(',') }
39
+ end
40
+
41
+ new request.get(url: url_path, query: query)
42
+ end
43
+
44
+ # Define the url path for the resource. It also implicitly defines
45
+ # the primary key and any additional foreign keys required for this
46
+ # resource.
47
+ #
48
+ # Example:
49
+ #
50
+ # TicketClass is a resource that requires a primary key of id and a
51
+ # foreign key of event_id to be retrieved, modified or deleted.
52
+ #
53
+ # resource_path('events/:event_id/ticket_classes/:id')
54
+ #
55
+ # The resource now has #id and #event_id accessor methods, and
56
+ # requires those parameters to build the correct resource url path.
57
+ # See the retrieve method (above) for additional details.
58
+ def resource_path(path)
59
+ @path = path
60
+
61
+ path.scan(/:\w+/).each do |path_attr|
62
+ attr = path_attr.delete(':').to_sym
63
+
64
+ define_method(attr) do
65
+ @attrs[attr] if @attrs.respond_to?(attr)
66
+ end
67
+ end
68
+ end
69
+ end
70
+
71
+ module InstanceMethods
72
+ def path(postfixed_path = '')
73
+ resource_path = self.class.path.dup
74
+ tokens = resource_path.scan(/:\w+/)
75
+
76
+ full_path = tokens.reduce(resource_path) do |path_frag, token|
77
+ method = token.delete(':')
78
+ path_frag.gsub(token, send(method).to_s)
79
+ end
80
+
81
+ if postfixed_path.empty?
82
+ full_path
83
+ else
84
+ full_path += '/' unless full_path.end_with?('/')
85
+ "#{full_path}#{postfixed_path}"
86
+ end
87
+ end
88
+
89
+ def full_url(request = EventbriteSDK)
90
+ request.url path
91
+ end
92
+ end
93
+
94
+ def self.included(receiver)
95
+ receiver.extend ClassMethods
96
+ receiver.send(:include, InstanceMethods)
97
+ end
98
+ end
99
+ end
100
+ end
101
+ end
@@ -0,0 +1,15 @@
1
+ module EventbriteSDK
2
+ class Resource
3
+ module Operations
4
+ module List
5
+ def list
6
+ ResourceList.new(
7
+ key: path.split('/').first,
8
+ object_class: EventbriteSDK.const_get(name.split('::').last),
9
+ url_base: new.path.gsub(/\/\Z/, '')
10
+ )
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,120 @@
1
+ module EventbriteSDK
2
+ class Resource
3
+ module Operations
4
+ # Adds +belongs_to+ and +has_many+
5
+ # relationship functionality to Resource instances
6
+ #
7
+ # Requires that included class responds_to:
8
+ # :resource_path
9
+ # returns a path that will be given to the list_class on instantiation
10
+ # must have an arity of 1
11
+ # :resource_class_from_string
12
+ # Should constantize the string into the
13
+ # class of the operating relationship
14
+ # :list_class
15
+ # Should return the class responsible for handling many of a resource
16
+ module Relationships
17
+ module ClassMethods
18
+ # Builds a memoized single relationship to another Resource
19
+ # by dynamically defining a method on the instance
20
+ # with the given +rel_method+
21
+ # ============================================
22
+ #
23
+ # class Wheel
24
+ # include Resource::Operations::Relationships
25
+ #
26
+ # belongs_to :car, object_class: 'Car', mappings: { id: :car_id }
27
+ #
28
+ # ...
29
+ # end
30
+ #
31
+ # Wheel.new('id' => 4, 'car_id' => 1).
32
+ # car #=> EventbriteSDK::Car.retrieve('id' => 1)
33
+ #
34
+ # rel_method: Symbol of the method we are defining on this instance
35
+ # e.g. belongs_to :thing => defines self#thing
36
+ # object_class: String representation of resource
37
+ # e.g. 'Event' => EventbriteSDK::Event
38
+ #
39
+ def belongs_to(rel_method, object_class: nil)
40
+ define_method(rel_method) do
41
+ query = { id: public_send(:"#{rel_method}_id") }
42
+
43
+ relationships[rel_method] ||= begin
44
+ resource_class_from_string(object_class).retrieve(query)
45
+ end
46
+ end
47
+ end
48
+
49
+ # Builds a memoized ResourceList relationship, dynamically defining
50
+ # a method on the instance with the given +rel_method+
51
+ # ============================================
52
+ #
53
+ # class Car
54
+ # include Resource::Operations::Relationships
55
+ #
56
+ # has_many :wheels, object: 'Wheel', key: 'wheels'
57
+ #
58
+ # def resource_path(postfix)
59
+ # "my_path/1/#{postfix}"
60
+ # end
61
+ #
62
+ # ...
63
+ # end
64
+ #
65
+ # Car.new('id' => '1').wheels
66
+ #
67
+ # Would instantiate a new ResourceList
68
+ #
69
+ # ResourceList.new(
70
+ # url_base: 'my_path/1/wheels',
71
+ # object_class: Wheel,
72
+ # key: :wheels
73
+ # )
74
+ #
75
+ # rel_method: Symbol of the method we are defining on this instance
76
+ # object_class: String representation of the Class we will give
77
+ # to ResourceList
78
+ # key: key to use when ResourceList is extracting objects from
79
+ # a list payload, if nil then rel_method is used as a default
80
+ #
81
+ def has_many(rel_method, object_class: nil, key: nil)
82
+ define_method(rel_method) do
83
+ key ||= rel_method
84
+
85
+ relationships[rel_method] ||= list_class(rel_method).new(
86
+ url_base: path(rel_method),
87
+ object_class: resource_class_from_string(object_class),
88
+ key: key
89
+ )
90
+ end
91
+ end
92
+ end
93
+
94
+ module InstanceMethods
95
+ def list_class(resource_list_rel)
96
+ class_name = resource_list_rel.to_s.split('_').map(&:capitalize).join
97
+ class_name = "#{class_name}List"
98
+
99
+ if Lists.const_defined?(class_name)
100
+ Lists.const_get(class_name)
101
+ else
102
+ ResourceList
103
+ end
104
+ end
105
+
106
+ private
107
+
108
+ def relationships
109
+ @_relationships ||= {}
110
+ end
111
+ end
112
+
113
+ def self.included(receiver)
114
+ receiver.extend ClassMethods
115
+ receiver.send(:include, InstanceMethods)
116
+ end
117
+ end
118
+ end
119
+ end
120
+ end
@@ -0,0 +1,50 @@
1
+ module EventbriteSDK
2
+ class Resource
3
+ class SchemaDefinition
4
+ def initialize(resource_name)
5
+ @resource_name = resource_name
6
+ @read_only_keys = Set.new
7
+ @attrs = {}
8
+ end
9
+
10
+ %i(boolean currency datetime integer string).each do |method|
11
+ define_method(method) do |value, *opts|
12
+ options = opts.first
13
+
14
+ @read_only_keys << value if options && options[:read_only]
15
+ @attrs[value] = method
16
+ end
17
+ end
18
+
19
+ def writeable?(key)
20
+ whitelisted_attribute?(key) && !read_only?(key)
21
+ end
22
+
23
+ def type(key)
24
+ attrs[key]
25
+ end
26
+
27
+ def defined_keys
28
+ attrs.keys
29
+ end
30
+
31
+ private
32
+
33
+ attr_reader :read_only_keys, :resource_name, :attrs
34
+
35
+ def read_only?(key)
36
+ read_only_keys.member?(key)
37
+ end
38
+
39
+ def whitelisted_attribute?(key)
40
+ if attrs.has_key?(key)
41
+ true
42
+ else
43
+ raise InvalidAttribute.new(
44
+ "attribute `#{key}` not present in #{resource_name}"
45
+ )
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,86 @@
1
+ module EventbriteSDK
2
+ class ResourceList
3
+ extend Forwardable
4
+ include Enumerable
5
+
6
+ def_delegators :@objects, :[], :each, :empty?
7
+
8
+ def initialize(
9
+ url_base: nil,
10
+ object_class: nil,
11
+ key: nil,
12
+ query: {},
13
+ request: EventbriteSDK
14
+ )
15
+ @key = key
16
+ @object_class = object_class
17
+ @objects = []
18
+ @query = query
19
+ @request = request
20
+ @url_base = url_base
21
+ end
22
+
23
+ def retrieve
24
+ response = load_response
25
+
26
+ @objects = (response[key.to_s] || []).map { |raw| object_class.new(raw) }
27
+ @pagination = response['pagination']
28
+
29
+ self
30
+ end
31
+
32
+ def page(num)
33
+ pagination['page_number'] = num
34
+
35
+ retrieve
36
+ end
37
+
38
+ def next_page
39
+ pagination['page_number'] += 1 unless page_number >= (page_count || 1)
40
+
41
+ retrieve
42
+ end
43
+
44
+ def prev_page
45
+ pagination['page_number'] -= 1 unless page_number <= 1
46
+
47
+ retrieve
48
+ end
49
+
50
+ %w(object_count page_number page_size page_count).each do |method|
51
+ define_method(method) { pagination[method] }
52
+ end
53
+
54
+ def to_json(opts = {})
55
+ { key => objects.map(&:to_h), 'pagination' => @pagination }.to_json(opts)
56
+ end
57
+
58
+ def with_expansion(*args)
59
+ if args.first
60
+ @query[:expand] = args.join(',')
61
+ else
62
+ @query.delete(:expand)
63
+ end
64
+
65
+ self
66
+ end
67
+
68
+ private
69
+
70
+ def pagination
71
+ @pagination ||= { 'page_count' => 1, 'page_number' => 1 }
72
+ end
73
+
74
+ def load_response
75
+ request.get(url: url_base, query: query.merge(page: page_number))
76
+ end
77
+
78
+ attr_reader :expansion,
79
+ :key,
80
+ :object_class,
81
+ :objects,
82
+ :query,
83
+ :request,
84
+ :url_base
85
+ end
86
+ end