diff --git a/lib/chiketto.rb b/lib/chiketto.rb index 44a4462..8b694ae 100644 --- a/lib/chiketto.rb +++ b/lib/chiketto.rb @@ -20,6 +20,8 @@ module Chiketto require 'chiketto/ticket_class' require 'chiketto/user' require 'chiketto/venue' + require 'chiketto/contact_list' + require 'chiketto/contact' class << self attr_writer :api_key diff --git a/lib/chiketto/attendee.rb b/lib/chiketto/attendee.rb index 8090697..baf66e1 100644 --- a/lib/chiketto/attendee.rb +++ b/lib/chiketto/attendee.rb @@ -2,6 +2,8 @@ module Chiketto class Attendee < Resource attr_accessor :quantity, :status, + :checked_in, + :costs, :ticket_class_id, :event_id, :order_id, @@ -15,5 +17,9 @@ class Attendee < Resource def profile AttendeeProfile.new @profile.to_h end + + def event + Event.new @event.to_h + end end end diff --git a/lib/chiketto/attendee_profile.rb b/lib/chiketto/attendee_profile.rb index d030c2b..956d155 100644 --- a/lib/chiketto/attendee_profile.rb +++ b/lib/chiketto/attendee_profile.rb @@ -13,6 +13,8 @@ class AttendeeProfile < Resource :blog, :gender, :birth_date, + :home_phone, + :work_phone, :cell_phone def addresses diff --git a/lib/chiketto/contact.rb b/lib/chiketto/contact.rb new file mode 100644 index 0000000..518d28c --- /dev/null +++ b/lib/chiketto/contact.rb @@ -0,0 +1,9 @@ +module Chiketto + class Contact < Resource + attr_reader :first_name, + :last_name, + :email + + attr_date :created + end +end diff --git a/lib/chiketto/contact_list.rb b/lib/chiketto/contact_list.rb new file mode 100644 index 0000000..6c81503 --- /dev/null +++ b/lib/chiketto/contact_list.rb @@ -0,0 +1,32 @@ +module Chiketto + class ContactList < Resource + attr_reader :name, + :user_id + + def contacts + raise Exception.new("ContactList must have an id and user_id to get its Contacts") unless id && user_id + + contacts = self.class.paginated(:contacts, id, { user_id: user_id }) + contacts.map { |c| Contact.new c } + end + + def add_contact(params) + raise Exception.new("ContactList must have an id and user_id to add a Contact") unless id && user_id + response = self.class.post("users/#{user_id}/contact_lists/#{id}/contacts", params) + response["created"] + end + + def delete_contact(email) + raise Exception.new("ContactList must have an id and user_id to delete its Contacts") unless id && user_id + response = self.class.delete("users/#{user_id}/contact_lists/#{id}/contacts", { email: email }) + response["deleted"] + end + + private + + def self.find_contacts(id, params) + user_id = params.delete(:user_id) + get "users/#{user_id}/contact_lists/#{id}/contacts", params + end + end +end diff --git a/lib/chiketto/resource.rb b/lib/chiketto/resource.rb index cb32f0b..c24b8c8 100644 --- a/lib/chiketto/resource.rb +++ b/lib/chiketto/resource.rb @@ -21,25 +21,28 @@ def self.get(uri, params = {}) end def self.post(uri, params = {}) - uri = URI.parse endpoint(uri) + query(params) - resource = open_post uri - JSON.parse resource + http_call(Net::HTTP::Post, uri, params) + end + + def self.delete(uri, params = {}) + http_call(Net::HTTP::Delete, uri, params) end def self.endpoint(uri) ENDPOINT + uri + token end - def self.open_post(uri) + def self.http_call(type, uri, params) + uri = URI.parse endpoint(uri) + query(params) http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true - request = Net::HTTP::Post.new(uri.request_uri) + request = type.new(uri.request_uri) request.add_field 'Authorization', "Bearer #{Chiketto.api_key}" response = http.request(request) if response.code !~ /20\d/ raise Chiketto::Exception, JSON.parse(response.body) end - response.body + JSON.parse response.body end def self.query(params) diff --git a/lib/chiketto/user.rb b/lib/chiketto/user.rb index 0bbec6d..5a5da0c 100644 --- a/lib/chiketto/user.rb +++ b/lib/chiketto/user.rb @@ -32,6 +32,21 @@ def organizers organizers['organizers'].map { |o| Organizer.new o } end + def contact_lists(params = {}) + contact_lists = User.paginated(:contact_lists, @id, params) + contact_lists.map { |cl| ContactList.new cl } + end + + def find_contact_list(contact_list_id) + contact_list = User.get "users/#{@id}/contact_lists/#{contact_list_id}" + ContactList.new contact_list + end + + def create_contact_list(params) + response = User.post "users/#{@id}/contact_lists/", params + ContactList.new response + end + private def self.find_attendees(id, params) @@ -46,6 +61,10 @@ def self.find_organizers(id) get "users/#{id}/organizers" end + def self.find_contact_lists(id, params) + get "users/#{id}/contact_lists", params + end + def self.paginated_events(id, params) paginated(:events, id, params) end diff --git a/test/attendee_profile_test.rb b/test/attendee_profile_test.rb index c680664..f3fd89a 100644 --- a/test/attendee_profile_test.rb +++ b/test/attendee_profile_test.rb @@ -16,6 +16,8 @@ def test_exposes_data assert_respond_to attendee_profile, :blog assert_respond_to attendee_profile, :gender assert_respond_to attendee_profile, :birth_date + assert_respond_to attendee_profile, :home_phone + assert_respond_to attendee_profile, :work_phone assert_respond_to attendee_profile, :cell_phone assert_respond_to attendee_profile, :addresses end diff --git a/test/attendee_test.rb b/test/attendee_test.rb index 993198e..54da8c6 100644 --- a/test/attendee_test.rb +++ b/test/attendee_test.rb @@ -6,6 +6,8 @@ def test_exposes_data assert_respond_to attendee, :ticket_class_id assert_respond_to attendee, :quantity assert_respond_to attendee, :status + assert_respond_to attendee, :checked_in + assert_respond_to attendee, :costs assert_respond_to attendee, :profile assert_respond_to attendee, :event_id assert_respond_to attendee, :order_id @@ -26,6 +28,11 @@ def test_attendee_returns_profile assert_kind_of Chiketto::AttendeeProfile, attendee.profile end + def test_attendee_returns_event + attendee = Chiketto::Attendee.new + assert_kind_of Chiketto::Event, attendee.event + end + def test_assigned_number_is_handled attendee = Chiketto::Attendee.new assigned_number: 1 assert_equal attendee.assigned_number, 1 diff --git a/test/contact_list_test.rb b/test/contact_list_test.rb new file mode 100644 index 0000000..9f4dc8d --- /dev/null +++ b/test/contact_list_test.rb @@ -0,0 +1,54 @@ +require 'test_helper' + +class ContactListTest < MiniTest::Test + USER_ID = 56727857661 + CONTACT_LIST_ID = 859438 + + def test_contact_list_exposes_data + contact_list = Chiketto::ContactList.new + assert_respond_to contact_list, :name + end + + def test_contact_list_has_contacts + contact_list = Chiketto::ContactList.new({ id: CONTACT_LIST_ID, user_id: USER_ID }) + VCR.use_cassette 'contact-list-contacts' do + assert_kind_of Chiketto::Contact, contact_list.contacts.first + end + end + + def test_contact_list_without_user_id_contacts_raises_error + contact_list = Chiketto::ContactList.new({ id: CONTACT_LIST_ID }) + assert_raises Chiketto::Exception do + contact_list.contacts + end + end + + def test_contact_list_add_contact_returns_true + contact_list = Chiketto::ContactList.new({ id: CONTACT_LIST_ID, user_id: USER_ID }) + params = { "contact.email" => "foo@bar.com", "contact.first_name" => "foo", "contact.last_name" => "bar" } + VCR.use_cassette 'contact-list-add-contact' do + assert_kind_of TrueClass, contact_list.add_contact(params) + end + end + + def test_contact_list_without_user_id_add_contact_raises_error + contact_list = Chiketto::ContactList.new({ id: CONTACT_LIST_ID }) + assert_raises Chiketto::Exception do + contact_list.add_contact({}) + end + end + + def test_contact_list_delete_contact_returns_true + contact_list = Chiketto::ContactList.new({ id: CONTACT_LIST_ID, user_id: USER_ID }) + VCR.use_cassette 'contact-list-delete-contact' do + assert_kind_of TrueClass, contact_list.delete_contact("foo@bar.com") + end + end + + def test_contact_list_without_user_id_delete_contact_raises_error + contact_list = Chiketto::ContactList.new({ id: CONTACT_LIST_ID }) + assert_raises Chiketto::Exception do + contact_list.delete_contact("") + end + end +end diff --git a/test/contact_test.rb b/test/contact_test.rb new file mode 100644 index 0000000..c1a57f4 --- /dev/null +++ b/test/contact_test.rb @@ -0,0 +1,16 @@ +require 'test_helper' + +class ContactTest < MiniTest::Test + def test_exposes_data + contact = Chiketto::Contact.new + assert_respond_to contact, :first_name + assert_respond_to contact, :last_name + assert_respond_to contact, :email + assert_respond_to contact, :created + end + + def test_attr_date_types + contact = Chiketto::Contact.new created: "2014-02-11T02:52:10Z" + assert_kind_of DateTime, contact.created + end +end diff --git a/test/user_test.rb b/test/user_test.rb index b50744a..0e66da8 100644 --- a/test/user_test.rb +++ b/test/user_test.rb @@ -2,6 +2,7 @@ class UserTest < MiniTest::Test USER_ID = 72013652427 + CONTACT_LIST_ID = 411987 def find_me VCR.use_cassette 'user-me' do @@ -64,4 +65,32 @@ def test_user_has_organizers assert_kind_of Chiketto::Organizer, @user.organizers.first end end + + def test_user_has_contact_lists + find_me + + VCR.use_cassette 'user-contact-lists' do + assert_kind_of Chiketto::ContactList, @user.contact_lists.first + end + end + + def test_user_find_contact_list_returns_contact_list + find_me + + VCR.use_cassette 'user-find-contact-list' do + assert_kind_of Chiketto::ContactList, @user.find_contact_list(CONTACT_LIST_ID) + end + end + + def test_create_contact_list + find_me + + VCR.use_cassette 'user-create-contact-list' do + contact_list = @user.create_contact_list({ + 'contact_list.name' => 'Test Contact List Creation' + }) + assert_kind_of Chiketto::ContactList, contact_list + assert_equal 'Test Contact List Creation', contact_list.name + end + end end