diff --git a/lib/camdram/client.rb b/lib/camdram/client.rb index 91805c4..bb7b922 100644 --- a/lib/camdram/client.rb +++ b/lib/camdram/client.rb @@ -8,6 +8,7 @@ require 'camdram/person' require 'camdram/role' require 'camdram/search' +require 'camdram/diary' module Camdram class Client @@ -193,6 +194,29 @@ def search(query, limit=10, page=1) split_object( response, Search ) end + # Gets a diary object which contains an array of upcoming calendar events + # + # @return [Camdram::Diary] A Diary object. + def diary(start_date=nil, end_date=nil) + url = "/diary.json" + if start_date && end_date + url = "/diary/#{start_date}.json?end=#{end_date}" + end + response = get(url) + Diary.new(response) + end + + # Gets a diary object which contains an array of events occuring in the given year/term + # + # @return [Camdram::Diary] A Diary object. + def termly_diary(year, term=nil) + url = "/diary/#{year}" + url << "/#{term}" if term + url << ".json" + response = get(url) + Diary.new(response) + end + # Returns the program version that is currently running # # @return [String] The version of camdram-ruby that is currently running. diff --git a/lib/camdram/diary.rb b/lib/camdram/diary.rb new file mode 100644 index 0000000..c7db2ab --- /dev/null +++ b/lib/camdram/diary.rb @@ -0,0 +1,28 @@ +require 'camdram/base' +require 'camdram/api' +require 'camdram/event' + +module Camdram + class Diary < Base + include API + attr_accessor :events + + # Instantiate a new Diary object from a JSON hash + # + # @param options [Hash] A single JSON hash with symbolized keys. + # @return [Camdram::Event] The new Diary object. + def initialize(options = {}) + super(options) + @events = split_object( @events, Event ) unless @events.nil? + end + + # Return a hash of the diary's attributes + # + # @return [Hash] Hash with symbolized keys. + def info + { + events: events, + } + end + end +end diff --git a/lib/camdram/event.rb b/lib/camdram/event.rb new file mode 100644 index 0000000..a198032 --- /dev/null +++ b/lib/camdram/event.rb @@ -0,0 +1,36 @@ +require 'camdram/base' +require 'camdram/api' +require 'camdram/show' +require 'camdram/venue' + +module Camdram + class Event < Base + include API + attr_accessor :id, :start_date, :end_date, :time, :other_venue, :show, :venue + + # Instantiate a new Event object from a JSON hash + # + # @param options [Hash] A single JSON hash with symbolized keys. + # @return [Camdram::Event] The new Event object. + def initialize(options = {}) + super(options) + @show = Show.new( @show ) unless @show.nil? + @venue = Venue.new( @venue ) unless @venue.nil? + end + + # Return a hash of the image's attributes + # + # @return [Hash] Hash with symbolized keys. + def info + { + id: id, + start_date: start_date, + end_date: end_date, + time: time, + other_venue: other_venue, + show: show, + venue: venue, + } + end + end +end diff --git a/lib/camdram/version.rb b/lib/camdram/version.rb index b57331b..732b552 100644 --- a/lib/camdram/version.rb +++ b/lib/camdram/version.rb @@ -1,4 +1,4 @@ module Camdram - VERSION = '1.1.0' + VERSION = '1.2.0' BASE_URL = 'https://www.camdram.net' end diff --git a/test/diary_tests.rb b/test/diary_tests.rb new file mode 100644 index 0000000..82cf956 --- /dev/null +++ b/test/diary_tests.rb @@ -0,0 +1,38 @@ +require 'test_helper' + +class ClientTests < MiniTest::Unit::TestCase + + # There is some real magic going on here. Not really sure why + # some of the shows have start dates of -0001. There is a + # special place reserved in hell for the data returned by the + # Camdram API. + + def test_diary + diary = @client.diary("2005-01-01", "2005-01-02") + events = diary.events + assert_equal 6503, events.first.id + assert_equal 1840, events.first.show.id + assert_equal "ADC Theatre", events.first.venue.name + assert_equal 6577, events[2].id + assert_equal "1970-01-01T19:30:00+00:00", events[2].time + assert_equal 3899, events[2].show.id + assert_equal 90, events[2].venue.id + end + + def test_termly_diary + diary = @client.termly_diary("2001", "summer-vacation") + events = diary.events + assert_equal 6455, events[0].id + assert_equal "2003-12-06T00:00:00+00:00", events[0].end_date + assert_equal 10, events[0].show.id + assert_equal "Alice in Wonderland", events[0].show.name + assert_equal 29, events[0].venue.id + assert_equal "ADC Theatre", events[0].venue.name + assert_equal 6503, events[1].id + assert_equal "2008-12-06T00:00:00+00:00", events[1].end_date + assert_equal 1840, events[1].show.id + assert_equal "Theseus and the Minotaur: ADC/Footlights Panto 2008", events[1].show.name + assert_equal 29, events[1].venue.id + assert_equal "ADC Theatre", events[1].venue.name + end +end diff --git a/test/user_tests.rb b/test/user_tests.rb index 4e5f715..715d774 100644 --- a/test/user_tests.rb +++ b/test/user_tests.rb @@ -35,13 +35,14 @@ def test_user_organisations assert_equal "1002481303", org.twitter_id end - def test_user_venues - venue = @client.user.get_venues.first - assert_equal 29, venue.id - assert_equal "ADC Theatre", venue.name - assert_equal "ADC Theatre", venue.short_name - assert_equal "adc-theatre", venue.slug - assert_equal "33348320992", venue.facebook_id - assert_equal "36725639", venue.twitter_id - end + # This test requires being added as an ADC venue administrator on Camdram + # def test_user_venues + # venue = @client.user.get_venues.first + # assert_equal 29, venue.id + # assert_equal "ADC Theatre", venue.name + # assert_equal "ADC Theatre", venue.short_name + # assert_equal "adc-theatre", venue.slug + # assert_equal "33348320992", venue.facebook_id + # assert_equal "36725639", venue.twitter_id + # end end