Skip to content

Commit

Permalink
updated the dummy provider to example version 0.3.0 changes
Browse files Browse the repository at this point in the history
some minor bugfixes
  • Loading branch information
Hong committed Jul 7, 2010
1 parent b35a658 commit 1e805c0
Show file tree
Hide file tree
Showing 13 changed files with 61 additions and 103 deletions.
1 change: 1 addition & 0 deletions Rakefile
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions lib/ticketmaster.rb
@@ -1,6 +1,7 @@
%w{
rubygems
hashie
active_resource
}.each {|lib| require lib }

class TicketMaster
Expand Down
2 changes: 1 addition & 1 deletion lib/ticketmaster/cli/commands/console.rb
Expand Up @@ -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} "
Expand Down
3 changes: 2 additions & 1 deletion lib/ticketmaster/cli/common.rb
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/ticketmaster/cli/init.rb
Expand Up @@ -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."
}

Expand Down
7 changes: 3 additions & 4 deletions lib/ticketmaster/common.rb
Expand Up @@ -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

Expand Down
27 changes: 27 additions & 0 deletions 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
39 changes: 1 addition & 38 deletions lib/ticketmaster/dummy/dummy.rb
Expand Up @@ -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
37 changes: 8 additions & 29 deletions lib/ticketmaster/dummy/project.rb
Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -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
Expand Down
25 changes: 7 additions & 18 deletions lib/ticketmaster/dummy/ticket.rb
Expand Up @@ -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',
Expand Down
6 changes: 3 additions & 3 deletions lib/ticketmaster/helper.rb
Expand Up @@ -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
Expand All @@ -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}"
}
Expand Down
2 changes: 1 addition & 1 deletion spec/ticketmaster_spec.rb
Expand Up @@ -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
Expand Down
12 changes: 5 additions & 7 deletions ticketmaster.gemspec
Expand Up @@ -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}
Expand Down Expand Up @@ -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}
Expand All @@ -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
Expand All @@ -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<hashie>, [">= 0"])
s.add_runtime_dependency(%q<activeresource>, [">= 0"])
s.add_development_dependency(%q<shoulda>, [">= 0"])
else
s.add_dependency(%q<hashie>, [">= 0"])
s.add_dependency(%q<activeresource>, [">= 0"])
s.add_dependency(%q<shoulda>, [">= 0"])
end
else
s.add_dependency(%q<hashie>, [">= 0"])
s.add_dependency(%q<activeresource>, [">= 0"])
s.add_dependency(%q<shoulda>, [">= 0"])
end
end
Expand Down

0 comments on commit 1e805c0

Please sign in to comment.