diff --git a/Rakefile b/Rakefile index a2f9569..8020cd8 100644 --- a/Rakefile +++ b/Rakefile @@ -11,6 +11,7 @@ begin gem.homepage = "http://ticketrb.com" gem.authors = ["Sirupsen", "deadprogrammer"] gem.add_dependency "hashie", ">= 0" + gem.add_dependency "activeresource", ">= 0" gem.add_development_dependency "shoulda", ">= 0" end Jeweler::GemcutterTasks.new diff --git a/lib/ticketmaster.rb b/lib/ticketmaster.rb index 94511f9..6a74027 100644 --- a/lib/ticketmaster.rb +++ b/lib/ticketmaster.rb @@ -1,6 +1,7 @@ %w{ rubygems hashie + active_resource }.each {|lib| require lib } class TicketMaster diff --git a/lib/ticketmaster/cli/commands/console.rb b/lib/ticketmaster/cli/commands/console.rb index 035d973..4608fc1 100644 --- a/lib/ticketmaster/cli/commands/console.rb +++ b/lib/ticketmaster/cli/commands/console.rb @@ -16,7 +16,7 @@ def open_irb(options, argv) providers.delete 'default' require 'rubygems' require 'ticketmaster' - providers.reduce(requires) do |mem, p| + providers.inject(requires) do |mem, p| begin require "ticketmaster-#{p}" requires << "-r ticketmaster-#{p} " diff --git a/lib/ticketmaster/cli/common.rb b/lib/ticketmaster/cli/common.rb index dc9cabc..e53afc8 100644 --- a/lib/ticketmaster/cli/common.rb +++ b/lib/ticketmaster/cli/common.rb @@ -11,8 +11,9 @@ def parse_config!(options) # A utility method used to separate name:value pairs def attributes_hash(kvlist) + require 'enumerator' if RUBY_VERSION < "1.8.7" if kvlist.is_a?(String) - kvlist.split(',').reduce({}) do |mem, kv| + kvlist.split(',').inject({}) do |mem, kv| key, value = kv.split(':') mem[key] = value mem diff --git a/lib/ticketmaster/cli/init.rb b/lib/ticketmaster/cli/init.rb index 3945fea..aeb792d 100644 --- a/lib/ticketmaster/cli/init.rb +++ b/lib/ticketmaster/cli/init.rb @@ -14,7 +14,7 @@ helptext = lambda { helpmsg = "\nAvailable commands:\n" - commands.sort.reduce(helpmsg) { |mem, cmd| mem << "\t#{cmd.join("\t\t")}\n" } + commands.sort.inject(helpmsg) { |mem, cmd| mem << "\t#{cmd.join("\t\t")}\n" } helpmsg << "\nSee 'ticket help COMMAND' for more information on a specific command." } diff --git a/lib/ticketmaster/common.rb b/lib/ticketmaster/common.rb index 7a1612d..bbdb33d 100644 --- a/lib/ticketmaster/common.rb +++ b/lib/ticketmaster/common.rb @@ -29,13 +29,12 @@ def initialize(*options) @cache ||= {} first = options.shift case first - when ActiveResource::Base + when Hash + super(first.to_hash) + else @system_data[:client] = first self.prefix_options ||= @system_data[:client].prefix_options if @system_data[:client].prefix_options super(first.attributes) - - when Hash - super(first.to_hash) end end diff --git a/lib/ticketmaster/dummy/comment.rb b/lib/ticketmaster/dummy/comment.rb new file mode 100644 index 0000000..2f4633d --- /dev/null +++ b/lib/ticketmaster/dummy/comment.rb @@ -0,0 +1,27 @@ +module TicketMaster::Provider + module Dummy + # This is the Comment class for the Dummy provider + class Comment < TicketMaster::Provider::Base::Comment + + + def self.find_by_id(id) + self.new({:id => id}) + end + + def self.find_by_attributes(*options) + [self.new(*options)] + end + + # You don't need to define an initializer, this is only here to initialize dummy data + def initialize(project_id, ticket_id, *options) + data = {:id => rand(1000), :status => ['lol', 'rofl', 'lmao', 'lamo', 'haha', 'heh'][rand(6)], + :priority => rand(10), :summary => 'Tickets ticket ticket ticket', :resolution => false, + :created_at => Time.now, :updated_at => Time.now, :description => 'Ticket ticket ticket ticket laughing', + :assignee => 'lol-man'} + @system = :dummy + super(data.merge(options.first || {})) + end + + end + end +end diff --git a/lib/ticketmaster/dummy/dummy.rb b/lib/ticketmaster/dummy/dummy.rb index 0a164c3..52a9848 100644 --- a/lib/ticketmaster/dummy/dummy.rb +++ b/lib/ticketmaster/dummy/dummy.rb @@ -14,52 +14,15 @@ module TicketMaster::Provider # around the TicketMaster.new call. module Dummy include TicketMaster::Provider::Base - @system = :dummy # An example of what to do if you would like to do TicketMaster::Provider::Dummy.new(...) # rather than TicketMaster.new(:dummy, ...) def self.new(authentication = {}) TicketMaster.new(:dummy, authentication) # maybe do some other stuff end - - # It returns all projects... - def projects(*options) - return Project.find(*options) if options.length > 0 - [Project.new] - end - - # It returns all tickets... - def tickets(*options) - return Ticket.find(*options) if options.length > 0 - [Ticket.new] - end - - # Returning a single ticket based on parameters or a Ticket class if no parameters given - # - # The later is for doing: - # ticketmaster.ticket.find(...) - # ticketmaster.tickets.create(...) - # - # It's semantically nicer to use ticketmaster.tickets.find ... but people do strange things... - def ticket(*options) - return Ticket.new(*options) if options.length > 0 - TicketMaster::Provider::Dummy::Ticket - end - - # Return a single project based on parameters or the Project class if no parameters given - # - # The later is for doing: - # ticketmaster.project.find(...) - # ticketmaster.tickets.create(...) - # - # (It's semantically nicer to use ticketmaster.projects.find ... but people do strange things) - def project(*options) - return Project.new(*options) if options.length > 0 - TicketMaster::Provider::Dummy::Project - end end end -%w| project ticket |.each do |f| +%w| project ticket comment |.each do |f| require File.dirname(__FILE__) + '/' + f +'.rb' end diff --git a/lib/ticketmaster/dummy/project.rb b/lib/ticketmaster/dummy/project.rb index 3d512bd..ef15d73 100644 --- a/lib/ticketmaster/dummy/project.rb +++ b/lib/ticketmaster/dummy/project.rb @@ -2,22 +2,13 @@ module TicketMaster::Provider module Dummy # This is the Project class for the Dummy provider class Project < TicketMaster::Provider::Base::Project - # This serves to find projects - # As noted in the Project class's documentation, we should try to accept these: - # - # * find(:all) - Returns an array of all projects - # * find(##) - Returns a project based on that id or some other primary (unique) attribute - # * find(:first, :summary => 'Project name') - Returns a project based on the project's attributes - # * find(:summary => 'Test Project') - Returns all projects based on the given attribute(s) - def self.find(*options) - first = options.shift - if first.nil? or first == :all - [Project.new] - elsif first == :first - Project.new(options.shift) - elsif first.is_a?(Hash) - [Project.new(first)] - end + + def self.find_by_id(id) + self.new({:id => id}) + end + + def self.find_by_attributes(*options) + [self.new(*options)] end # You should define @system and @system_data here. @@ -31,18 +22,6 @@ def initialize(*options) super(data.merge(options.first || {})) end - # Should return all of the project's tickets - def tickets(*options) - return Ticket.find(*options) if options.length > 0 - [Ticket.new] - end - - # Point it to the Dummy's Ticket class - def ticket(*options) - return Ticket.find(:first, options.first) if options.length > 0 - Ticket - end - # Nothing to save so we always return true # ...unless it's an odd numbered second on Friday the 13th. muhaha! def save @@ -51,7 +30,7 @@ def save end # Nothing to update, so we always return true - def update(*options) + def update!(*options) return true end end diff --git a/lib/ticketmaster/dummy/ticket.rb b/lib/ticketmaster/dummy/ticket.rb index 820668b..b8236c0 100644 --- a/lib/ticketmaster/dummy/ticket.rb +++ b/lib/ticketmaster/dummy/ticket.rb @@ -4,27 +4,16 @@ module Dummy class Ticket < TicketMaster::Provider::Base::Ticket @system = :dummy - # Find a ticket - # - # The implementation should be able to accept these cases if feasible: - # - # * find(:all) - Returns an array of all tickets - # * find(##) - Returns a project based on that id or some other primary (unique) attribute - # * find(:first, :summary => 'Ticket title') - Returns a ticket based on the ticket's attributes - # * find(:summary => 'Test Ticket') - Returns all tickets based on the given attributes - def self.find(*options) - first = options.shift - if first.nil? or first == :all - [Ticket.new] - elsif first == :first - Ticket.new(options.shift) - elsif first.is_a?(Hash) - [Ticket.new(first)] - end + def self.find_by_id(project_id, ticket_id) + self.new({:project_id => project, :id => ticket_id}) + end + + def self.find_by_attributes(*ticket_attributes) + [self.new(*ticket_attributes)] end # You don't need to define an initializer, this is only here to initialize dummy data - def initialize(*options) + def initialize(project_id, *options) data = {:id => rand(1000), :status => ['lol', 'rofl', 'lmao', 'lamo', 'haha', 'heh'][rand(6)], :priority => rand(10), :summary => 'Tickets ticket ticket ticket', :resolution => false, :created_at => Time.now, :updated_at => Time.now, :description => 'Ticket ticket ticket ticket laughing', diff --git a/lib/ticketmaster/helper.rb b/lib/ticketmaster/helper.rb index 8b3444e..64a6e6e 100644 --- a/lib/ticketmaster/helper.rb +++ b/lib/ticketmaster/helper.rb @@ -22,7 +22,7 @@ def search_filter(k) # Goes through all the things and returns results that match the options attributes hash def search_by_attribute(things, options = {}, limit = 1000) things.find_all do |thing| - options.reduce(true) do |memo, kv| + options.inject(true) do |memo, kv| break unless memo key, value = kv begin @@ -40,11 +40,11 @@ def search_by_attribute(things, options = {}, limit = 1000) # # ex: filter_string({:name => 'some value', :tags => ['abc', 'def']}) = "name:'some value' tag:abc tag:def" def filter_string(filter = {}, array_join = nil) - filter.reduce('') do |mem, kv| + filter.inject('') do |mem, kv| key, value = kv if value.is_a?(Array) if !array_join.nil? - mem += value.reduce('') { |m, v| + mem += value.inject('') { |m, v| v = "\"#{v}\"" if v.to_s.include?(' ') m+= "#{key}:#{v}" } diff --git a/spec/ticketmaster_spec.rb b/spec/ticketmaster_spec.rb index 902f920..e4fa75c 100644 --- a/spec/ticketmaster_spec.rb +++ b/spec/ticketmaster_spec.rb @@ -5,7 +5,7 @@ # Also, remember to mock or stub any API calls describe "Ticketmaster" do before(:each) do - @ticketmaster = TicketMaster.new(:dummy) + @ticketmaster = TicketMaster.new(:dummy, {}) @project_class = TicketMaster::Provider::Dummy::Project @ticket_class = TicketMaster::Provider::Dummy::Ticket end diff --git a/ticketmaster.gemspec b/ticketmaster.gemspec index 5d43f53..8071704 100644 --- a/ticketmaster.gemspec +++ b/ticketmaster.gemspec @@ -9,7 +9,7 @@ Gem::Specification.new do |s| s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version= s.authors = ["Sirupsen", "deadprogrammer"] - s.date = %q{2010-07-05} + s.date = %q{2010-07-06} s.default_executable = %q{ticket} s.description = %q{Ticketmaster provides a universal API to trouble ticket and project management systems.} s.email = %q{simon@hybridgroup.com} @@ -57,9 +57,6 @@ Gem::Specification.new do |s| "spec/spec_helper.rb", "spec/ticketmaster-cli_spec.rb", "spec/ticketmaster_spec.rb", - "test/helper.rb", - "test/test_ticketmaster.rb", - "ticketmaster-0.3.2.gem", "ticketmaster.gemspec" ] s.homepage = %q{http://ticketrb.com} @@ -70,9 +67,7 @@ Gem::Specification.new do |s| s.test_files = [ "spec/spec_helper.rb", "spec/ticketmaster-cli_spec.rb", - "spec/ticketmaster_spec.rb", - "test/helper.rb", - "test/test_ticketmaster.rb" + "spec/ticketmaster_spec.rb" ] if s.respond_to? :specification_version then @@ -81,13 +76,16 @@ Gem::Specification.new do |s| if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then s.add_runtime_dependency(%q, [">= 0"]) + s.add_runtime_dependency(%q, [">= 0"]) s.add_development_dependency(%q, [">= 0"]) else s.add_dependency(%q, [">= 0"]) + s.add_dependency(%q, [">= 0"]) s.add_dependency(%q, [">= 0"]) end else s.add_dependency(%q, [">= 0"]) + s.add_dependency(%q, [">= 0"]) s.add_dependency(%q, [">= 0"]) end end