eventbrite-api-v3 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (53) hide show
  1. checksums.yaml +7 -0
  2. data/.document +5 -0
  3. data/.rspec +2 -0
  4. data/.ruby-version +1 -0
  5. data/.travis.yml +9 -0
  6. data/Gemfile +15 -0
  7. data/Gemfile.lock +96 -0
  8. data/LICENSE +22 -0
  9. data/README.md +187 -0
  10. data/Rakefile +51 -0
  11. data/VERSION +1 -0
  12. data/eventbrite-api-v3.gemspec +97 -0
  13. data/lib/eventbrite-api.rb +21 -0
  14. data/lib/eventbrite/api/client.rb +64 -0
  15. data/lib/eventbrite/api/helper.rb +27 -0
  16. data/lib/eventbrite/api/model/attendee.rb +11 -0
  17. data/lib/eventbrite/api/model/base.rb +84 -0
  18. data/lib/eventbrite/api/model/category.rb +11 -0
  19. data/lib/eventbrite/api/model/event.rb +23 -0
  20. data/lib/eventbrite/api/model/event_ticket_class.rb +11 -0
  21. data/lib/eventbrite/api/model/format.rb +11 -0
  22. data/lib/eventbrite/api/model/order.rb +11 -0
  23. data/lib/eventbrite/api/model/organizer.rb +11 -0
  24. data/lib/eventbrite/api/model/owned_event.rb +11 -0
  25. data/lib/eventbrite/api/model/owned_event_attendee.rb +11 -0
  26. data/lib/eventbrite/api/model/owned_event_order.rb +11 -0
  27. data/lib/eventbrite/api/model/user.rb +11 -0
  28. data/lib/eventbrite/api/model/user_contact.rb +11 -0
  29. data/lib/eventbrite/api/model/user_contact_list.rb +11 -0
  30. data/lib/eventbrite/api/model/user_order.rb +11 -0
  31. data/lib/eventbrite/api/model/user_organizer.rb +11 -0
  32. data/lib/eventbrite/api/model/user_venue.rb +11 -0
  33. data/lib/eventbrite/api/model/venue.rb +11 -0
  34. data/spec/eventbrite/api/client_spec.rb +46 -0
  35. data/spec/eventbrite/api/model/base_spec.rb +11 -0
  36. data/spec/eventbrite/api/model/event_spec.rb +50 -0
  37. data/spec/eventbrite/api/model/owned_event_attendee_spec.rb +32 -0
  38. data/spec/eventbrite/api/model/owned_event_order_spec.rb +19 -0
  39. data/spec/eventbrite/api/model/owned_event_spec.rb +19 -0
  40. data/spec/fixtures/events/13270934723/publish.json +129 -0
  41. data/spec/fixtures/events/13270934723/unpublish.json +129 -0
  42. data/spec/fixtures/events_page1.json +2405 -0
  43. data/spec/fixtures/events_page2.json +2096 -0
  44. data/spec/fixtures/events_page3.json +1996 -0
  45. data/spec/fixtures/owned_event_attendees/page1.json +3119 -0
  46. data/spec/fixtures/owned_event_attendees/page2.json +3074 -0
  47. data/spec/fixtures/owned_event_attendees/page3.json +2789 -0
  48. data/spec/fixtures/users/133925426255/owned_event_attendees.json +11 -0
  49. data/spec/fixtures/users/133925426255/owned_event_orders.json +327 -0
  50. data/spec/fixtures/users/133925426255/owned_events.json +181 -0
  51. data/spec/fixtures/users/me.json +13 -0
  52. data/spec/spec_helper.rb +98 -0
  53. metadata +139 -0
@@ -0,0 +1,21 @@
1
+ require 'eventbrite/api/helper.rb'
2
+ require 'eventbrite/api/client.rb'
3
+ require 'eventbrite/api/model/base.rb'
4
+ require 'eventbrite/api/model/user.rb'
5
+ require 'eventbrite/api/model/venue.rb'
6
+ require 'eventbrite/api/model/organizer.rb'
7
+ require 'eventbrite/api/model/event.rb'
8
+ require 'eventbrite/api/model/attendee.rb'
9
+ require 'eventbrite/api/model/category.rb'
10
+ require 'eventbrite/api/model/format.rb'
11
+ require 'eventbrite/api/model/event_ticket_class.rb'
12
+ require 'eventbrite/api/model/order.rb'
13
+ require 'eventbrite/api/model/user_contact.rb'
14
+ require 'eventbrite/api/model/user_contact_list.rb'
15
+ require 'eventbrite/api/model/user_order.rb'
16
+ require 'eventbrite/api/model/user_venue.rb'
17
+ require 'eventbrite/api/model/user_organizer.rb'
18
+ require 'eventbrite/api/model/owned_event.rb'
19
+ require 'eventbrite/api/model/owned_event_attendee.rb'
20
+ require 'eventbrite/api/model/owned_event_order.rb'
21
+ require 'deep_merge/rails_compat'
@@ -0,0 +1,64 @@
1
+ require 'base64'
2
+ require 'oauth2'
3
+
4
+ module Eventbrite
5
+ module Api
6
+ class Client
7
+ include Eventbrite::Api::Helpers
8
+
9
+ attr_reader :client, :access_token
10
+
11
+ def initialize(options)
12
+ model :User
13
+ model :Venue
14
+ model :Organizer
15
+ model :Order
16
+ model :Event
17
+ model :Attendee
18
+ model :Category
19
+ model :Format
20
+ model :EventTicketClass
21
+ model :UserContact
22
+ model :UserContactList
23
+ model :UserOrder
24
+ model :UserVenue
25
+ model :UserOrganizer
26
+ model :OwnedEvent
27
+ model :OwnedEventAttendee
28
+ model :OwnedEventOrder
29
+
30
+ @redirect_uri = options[:redirect_uri]
31
+ @consumer = options[:consumer]
32
+ @access_token = options[:access_token]
33
+
34
+ @client = OAuth2::Client.new(@consumer[:key], @consumer[:secret], {
35
+ :site => 'https://www.eventbrite.com',
36
+ :authorize_url => '/oauth/authorize',
37
+ :token_url => '/oauth/token',
38
+ })
39
+ end
40
+
41
+ def get_access_code_url(params = {})
42
+ @client.auth_code.authorize_url(params.merge(redirect_uri: @redirect_uri))
43
+ end
44
+
45
+ def get_access_token(access_code)
46
+ @token = @client.auth_code.get_token(access_code, redirect_uri: @redirect_uri)
47
+ @access_token = @token.token
48
+ @token
49
+ end
50
+
51
+ def headers
52
+ {
53
+ 'Accept' => 'application/json',
54
+ 'Content-Type' => 'application/json'
55
+ }
56
+ end
57
+
58
+ def connection
59
+ @auth_connection ||= OAuth2::AccessToken.new(@client, @access_token)
60
+ end
61
+
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,27 @@
1
+ class String
2
+ def underscore
3
+ self.gsub(/::/, '/').
4
+ gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
5
+ gsub(/([a-z\d])([A-Z])/,'\1_\2').
6
+ tr("-", "_").
7
+ downcase
8
+ end
9
+ end
10
+
11
+ module Eventbrite
12
+ module Api
13
+ module Helpers
14
+ def model(model_name)
15
+ method_name = model_name.to_s.underscore
16
+ variable_name = "@#{method_name}_model".to_sym
17
+ unless instance_variable_defined?(variable_name)
18
+ instance_variable_set(variable_name, Eventbrite::Api::Model.const_get("#{model_name}".to_sym).new(self, model_name.to_s))
19
+ self.define_singleton_method(method_name.to_sym) do
20
+ instance_variable_get(variable_name)
21
+ end
22
+ end
23
+ instance_variable_get(variable_name)
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,11 @@
1
+ module Eventbrite
2
+ module Api
3
+ module Model
4
+ class Attendee < Base
5
+ def model_route
6
+ 'events/:event_id/attendees'
7
+ end
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,84 @@
1
+ module Eventbrite
2
+ module Api
3
+ module Model
4
+ class Base
5
+
6
+ def initialize(client, model_name=nil)
7
+ @client = client
8
+ @model_name = model_name || 'Base'
9
+
10
+ @pagination = nil
11
+ @endpoint = nil
12
+ end
13
+
14
+ def model_route
15
+ @model_name.to_s.downcase
16
+ end
17
+
18
+ def get(opts={}, endpoint=nil)
19
+ @endpoint = endpoint
20
+ perform_request(url(opts, endpoint))
21
+ end
22
+
23
+ def find(id)
24
+ self.get({}, id)
25
+ end
26
+
27
+ def create(object, opts={})
28
+ @client.connection.post(url(opts), {:headers => @client.headers, :body => object.to_json})
29
+ end
30
+
31
+ def update(id, object, opts={})
32
+ @client.connection.post(url(opts, id), {:headers => @client.headers, :body => object.to_json})
33
+ end
34
+
35
+ def all(opts)
36
+ all_pages = get(opts)
37
+ while @pagination && @pagination['page_number'] < @pagination['page_count']
38
+ all_pages = all_pages.deeper_merge(next_page(opts))
39
+ end
40
+ all_pages
41
+ end
42
+
43
+ def next_page(opts={})
44
+ opts['params'] = {'page'=>@pagination['page_number']+1}
45
+ get(opts, @endpoint)
46
+ end
47
+
48
+ def previous_page(opts={})
49
+ opts['params'] = {'page'=>@pagination['page_number']-1}
50
+ get(opts, @endpoint)
51
+ end
52
+
53
+ protected
54
+ def url(opts={}, endpoint=nil)
55
+ if model_route == ''
56
+ target = "https://www.eventbriteapi.com/v3"
57
+ elsif endpoint
58
+ target = "#{resource_url(opts)}/#{endpoint}"
59
+ else
60
+ target = resource_url(opts)
61
+ end
62
+ if opts['params']
63
+ params = opts['params'].map { |k,v| "#{k}=#{v}" }.join('&')
64
+ target = "#{target}?#{params}"
65
+ end
66
+ target
67
+ end
68
+
69
+ def resource_url(opts={})
70
+ target = "https://www.eventbriteapi.com/v3/#{model_route}"
71
+ opts.each { |k,v| target.gsub!(":#{k}", v) if v.is_a?(String) } unless opts.empty?
72
+ target
73
+ end
74
+
75
+ def perform_request(target)
76
+ response = @client.connection.get(target, {:headers => @client.headers})
77
+ hash = JSON.parse(response.body)
78
+ @pagination = hash['pagination']
79
+ hash
80
+ end
81
+ end
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,11 @@
1
+ module Eventbrite
2
+ module Api
3
+ module Model
4
+ class Category < Base
5
+ def model_route
6
+ 'categories'
7
+ end
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,23 @@
1
+ module Eventbrite
2
+ module Api
3
+ module Model
4
+ class Event < Base
5
+ def model_route
6
+ 'events'
7
+ end
8
+
9
+ def search(opts={})
10
+ self.get(opts, 'search')
11
+ end
12
+
13
+ def publish(id)
14
+ self.update("#{id}/publish", {}, {})
15
+ end
16
+
17
+ def unpublish(id)
18
+ self.update("#{id}/unpublish", {}, {})
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,11 @@
1
+ module Eventbrite
2
+ module Api
3
+ module Model
4
+ class EventTicketClass < Base
5
+ def model_route
6
+ 'events/:event_id/ticket_classes'
7
+ end
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ module Eventbrite
2
+ module Api
3
+ module Model
4
+ class Format < Base
5
+ def model_route
6
+ 'formats'
7
+ end
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ module Eventbrite
2
+ module Api
3
+ module Model
4
+ class Order < Base
5
+ def model_route
6
+ 'orders'
7
+ end
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ module Eventbrite
2
+ module Api
3
+ module Model
4
+ class Organizer < Base
5
+ def model_route
6
+ 'users/:user_id/organizers'
7
+ end
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ module Eventbrite
2
+ module Api
3
+ module Model
4
+ class OwnedEvent < Base
5
+ def model_route
6
+ 'users/:user_id/owned_events'
7
+ end
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ module Eventbrite
2
+ module Api
3
+ module Model
4
+ class OwnedEventAttendee < Base
5
+ def model_route
6
+ 'users/:user_id/owned_event_attendees'
7
+ end
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ module Eventbrite
2
+ module Api
3
+ module Model
4
+ class OwnedEventOrder < Base
5
+ def model_route
6
+ 'users/:user_id/owned_event_orders'
7
+ end
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ module Eventbrite
2
+ module Api
3
+ module Model
4
+ class User < Base
5
+ def model_route
6
+ 'users'
7
+ end
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ module Eventbrite
2
+ module Api
3
+ module Model
4
+ class UserContact < Base
5
+ def model_route
6
+ 'users/:user_id/contact_lists/:contact_list_id/contacts'
7
+ end
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ module Eventbrite
2
+ module Api
3
+ module Model
4
+ class UserContactList < Base
5
+ def model_route
6
+ 'users/:user_id/contact_lists'
7
+ end
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ module Eventbrite
2
+ module Api
3
+ module Model
4
+ class UserOrder < Base
5
+ def model_route
6
+ 'users/:user_id/orders'
7
+ end
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ module Eventbrite
2
+ module Api
3
+ module Model
4
+ class UserOrganizer < Base
5
+ def model_route
6
+ 'users/:user_id/organizer'
7
+ end
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ module Eventbrite
2
+ module Api
3
+ module Model
4
+ class UserVenue < Base
5
+ def model_route
6
+ 'users/:user_id/venues'
7
+ end
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ module Eventbrite
2
+ module Api
3
+ module Model
4
+ class Venue < Base
5
+ def model_route
6
+ 'organizations/:organization_id/venues'
7
+ end
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,46 @@
1
+ require 'spec_helper'
2
+
3
+ describe Eventbrite::Api::Client do
4
+
5
+ let(:consumer) { {key: 'key', secret: 'secret'} }
6
+ let(:params) { {redirect_uri: 'redirect_uri', consumer: consumer, access_token: 'access_token'} }
7
+
8
+ subject { Eventbrite::Api::Client.new(params) }
9
+
10
+ describe ".get_access_code_url" do
11
+ it "generates the access code url" do
12
+ expect(subject.get_access_code_url).to eql('https://www.eventbrite.com/oauth/authorize?client_id=key&redirect_uri=redirect_uri&response_type=code')
13
+ end
14
+ end
15
+
16
+ describe ".get_access_token" do
17
+ let(:access_code) { '123' }
18
+
19
+ before { Timecop.freeze(Time.parse("2015-01-01T00:00:00")) }
20
+ after { Timecop.return }
21
+
22
+ it "requests the access_token" do
23
+ stub_request(:post, "https://www.eventbrite.com/oauth/token").
24
+ to_return(:status => 200, :body => {'access_token'=>'access_code','token_type'=>'bearer'}.to_json,
25
+ :headers => {"content-type"=>"application/json; charset=utf-8"})
26
+
27
+ subject.get_access_token(access_code)
28
+ expect(subject.access_token).to eql('access_code')
29
+ end
30
+ end
31
+
32
+ describe ".connection" do
33
+ let (:expires_in) { 1200 }
34
+
35
+ before { stub_request(:post, "https://www.eventbrite.com/oauth/token").
36
+ to_return(:status => 200, :body => {'access_token'=>'access_code','token_type'=>'bearer'}.to_json,
37
+ :headers => {"content-type"=>"application/json; charset=utf-8"}) }
38
+ before { subject.get_access_token('access_code') }
39
+
40
+ context "current token is valid" do
41
+ it "returns the current access token" do
42
+ expect(subject.connection).to be_a(OAuth2::AccessToken)
43
+ end
44
+ end
45
+ end
46
+ end