Top Level Namespace

Defined Under Namespace

Classes: TicketMaster

Instance Method Summary collapse

Instance Method Details

#add(options) ⇒ Object

Called on –add. It adds a new entry to the config file and will refuse if it already exists



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/ticketmaster/cli/commands/config.rb', line 40

def add(options)
  require_provider unless options[:provider]
  provider = options[:provider]
  config_file = File.expand_path(options[:config])
  config = if File.exists?(config_file)
    YAML.load_file(config_file)
    else
    {}
    end
  if config[provider]
    puts "#{provider} has already been specfied in #{options[:config]}. Refusing to add. Use --edit instead."
    exit 1
  end
  config[provider] = {}
  config[provider]['authentication'] = options[:authentication] || {}
  config[provider]['project'] = options[:project] if options[:project]
  File.open(config_file, 'w') do |out|
    YAML.dump(config, out)
  end
  puts "Wrote #{provider} to #{config_file}"
  exit
end

#attributes_hash(kvlist) ⇒ Object

A utility method used to separate name:value pairs



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/ticketmaster/cli/common.rb', line 13

def attributes_hash(kvlist)
  require 'enumerator' if RUBY_VERSION < "1.8.7"
  if kvlist.is_a?(String)
    kvlist.split(',').inject({}) do |mem, kv|
      key, value = kv.split(':')
      mem[key] = value
      mem
    end
  elsif kvlist.is_a?(Array)
    mem = {}
    kvlist.each_slice(2) do |k, v|
      mem[k] = v
    end
    mem
  end
end

#config(options) ⇒ Object

The command method call implementation This sets the option parser and passes the parsed options to the subcommands



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/ticketmaster/cli/commands/config.rb', line 3

def config(options)
  ARGV << '--help' if ARGV.length == 0
  begin
    OptionParser.new do |opts|
      opts.banner = 'Usage: ticket -p PROVIDER [options] config [config_options]'
      opts.separator ''
      opts.separator 'Options:'
      
      opts.on('-a', '--add', 'Add a new entry to the configuration file based on ticketmaster options.') do
        options[:subcommand] = 'add'
      end
      
      opts.on('-e', '--edit', 'Edit an existing entry to the configuration file based on ticketmaster options') do
        options[:subcommand] = 'edit'
      end
      
      opts.on('-p', '--set-default-provider', 'Set the current provider as the default.', 'Requires provider to be specified, otherwise unsets the default') do
        options[:subcommand] = 'set_default_provider'
      end
      
      opts.separator ''
      opts.separator 'Other options:'
      
      opts.on_tail('-h', '--help', 'Show this message') do
        puts opts
        exit
      end
    end.parse(ARGV)
  rescue OptionParser::MissingArgument => exception
    puts "ticket #{options[:original_argv].join(' ')}\n\n"
    puts "Error: An option was called that requires an argument, but was not given one"
    puts exception.message
  end
  send(options[:subcommand], options)
end

#console(options) ⇒ Object

the console command



2
3
4
# File 'lib/ticketmaster/cli/commands/console.rb', line 2

def console(options)
  send(:open_irb, options, ARGV)
end

#copy_skeleton(options) ⇒ Object

Copy over the skeleton files



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/ticketmaster/cli/commands/generate.rb', line 57

def copy_skeleton(options)
  skeleton_path = File.dirname(__FILE__) + '/generate/'
  provider = File.read(skeleton_path + 'provider.rb').gsub('yoursystem', options[:provider].downcase)
  create_file(options, options[:provider_dir] + '.rb', provider)
  skeleton_path << 'provider/'
  provider = File.read(skeleton_path + 'provider.rb').gsub('Yoursystem', options[:provider].capitalize).gsub('yoursystem', options[:provider].downcase)
  create_file(options, 'provider/' + options[:provider].downcase + '.rb', provider)
  %w(project.rb ticket.rb comment.rb).each do |p|
    provider = File.read(skeleton_path + p).gsub('Yoursystem', options[:provider].capitalize).gsub('yoursystem', options[:provider].downcase)
    create_file(options, 'provider/' + p, provider)
  end
end

#create(options) ⇒ Object

The create subcommand



77
78
79
80
81
82
# File 'lib/ticketmaster/cli/commands/ticket.rb', line 77

def create(options)
  tm = TicketMaster.new(options[:provider], options[:authentication])
  ticket = tm.ticket.create(options[:ticket_attributes].merge({:project_id => options[:project]}))
  read_ticket ticket
  exit
end

#create_directories(options) ⇒ Object

Create the directories so copy_skeleton can do its job



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/ticketmaster/cli/commands/generate.rb', line 71

def create_directories(options)
  if options[:jeweler]
    jeweler_options = options[:jeweler].inject('') do |mem, j|
      j="'#{j}'" if j.include?(' ')
      mem + j + ' '
    end
    puts "Running jeweler #{jeweler_options} #{options[:provider_dir]}"
    puts `jeweler #{jeweler_options} #{options[:provider_dir]}`
  elsif options[:mkdir]
    begin 
      Dir.mkdir(options[:provider_dir])
      puts "\tcreate\t#{options[:provider_dir]}"
    rescue Exception => e
      puts "\t#{e.message}"
    end
    begin
      Dir.mkdir(options[:lib])
      puts "\tcreate\t#{options[:lib]}"
    rescue Exception => e
      puts "\t#{e.message}"
    end
  end
  begin
    Dir.mkdir(options[:lib] + '/provider')
    puts "\tcreate\t#{options[:lib] + 'provider'}"
  rescue Exception => e
    puts "\t#{e.message}"
  end
end

#create_file(options, filename, data) ⇒ Object

Create files



102
103
104
105
106
107
108
109
110
111
112
# File 'lib/ticketmaster/cli/commands/generate.rb', line 102

def create_file(options, filename, data)
  file_path = options[:lib] + '/' + filename
  if File.exist?(file_path) and File.size(file_path) > 0
    puts "\texists with content...skipping\t#{filename}"
    return false;
  end
  puts "\tcreate\t#{filename}"
  f = File.open(file_path, 'a+')
  f.write data
  f.close
end

#destroy(options) ⇒ Object

The destroy subcommand.



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/ticketmaster/cli/commands/ticket.rb', line 108

def destroy(options)
  tm = TicketMaster.new(options[:provider], options[:authentication])
  project = tm.project(options[:project])
  ticket = project.ticket(options[:ticket])
  puts "Are you sure you want to delete Ticket #{ticket.title} (#{ticket.id})? (yes/no) [no]"
  ARGV.clear
  confirm = readline.chomp.downcase
  if confirm != 'y' and confirm != 'yes'
    puts "Did not receive a 'yes' confirmation. Exiting..."
    exit
  elsif ticket.destroy
    puts "Successfully deleted Ticket #{ticket.title} (#{ticket.id})"
  else
    puts "Sorry, it seems there was an error when trying to delete the project"
  end
  exit
end

#edit(options) ⇒ Object

Called on –edit. It updates and edits an entry. If the entry is non-existent, it will add it.



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/ticketmaster/cli/commands/config.rb', line 64

def edit(options)
  require_provider unless options[:provider]
  provider = options[:provider]
  config_file = File.expand_path(options[:config])
  config = if File.exist?(config_file)
    YAML.load_file(config_file)
  else
    {}
  end
  config[provider] ||= {}
  config[provider]['authentication'] = options[:authentication] || {}
  config[provider]['project'] = options[:project] if options[:project]
  File.open(config_file, 'w') do |out|
    YAML.dump(config, out)
  end
  puts "Wrote #{provider} to #{config_file}"
  exit
end

#generate(options) ⇒ Object

The generate command



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/ticketmaster/cli/commands/generate.rb', line 2

def generate(options)
  if ARGV.length == 0
    ARGV << '--help'
  else
    provider_name = ARGV.shift
    if provider_name.start_with? '_'
      options[:provider] = provider_name[1..-1]
      options[:provider_dir] = options[:provider]
    else
      options[:provider] = provider_name
      options[:provider_dir] = 'ticketmaster-' + options[:provider]
    end
  end
  options[:mkdir] = true
  begin
    OptionParser.new do |opts|
      opts.banner = 'Usage: tm generate PROVIDER_NAME [--lib-directory DIR] [--jeweler [jeweler_options]]'
      opts.separator ''
      opts.separator 'Options:'
      
      opts.on('-J', '--jeweler [JEWELER_OPTIONS]', 'Sets the working ticket') do |option|
        options[:jeweler] = ARGV
        options[:mkdir] = false
      end
      
      opts.on('-L', '--lib-directory DIR', 'Put the skeleton files inside this directory', ' * This assumes the directory already exists') do |dir|
        options[:lib] = dir
        options[:mkdir] = false
      end
      
      opts.separator ''
      opts.separator 'Other options:'
      
      opts.on_tail('-h', '--help', 'Show this message') do
        puts opts
        exit
      end
      opts.separator ''
      opts.separator 'NOTE: ticketmaster- will be prepended to your provider name'
      opts.separator 'unless you set the first character as _ (it will be removed)'
    end.order!
  rescue OptionParser::MissingArgument => exception
    puts "tm #{options[:original_argv].join(' ')}\n\n"
    puts "Error: An option was called that requires an argument, but was not given one"
    puts exception.message
  rescue OptionParser::InvalidOption => exception
    options[:jeweler] = exception.recover(ARGV)
    options[:mkdir] = false
  end
  options[:lib] ||= options[:provider_dir] + '/lib/'
  create_directories(options)
  copy_skeleton(options)
end

#help(options) ⇒ Object

The help command.



2
3
4
5
6
7
8
9
# File 'lib/ticketmaster/cli/commands/help.rb', line 2

def help(options)
  cmd = ARGV.shift || 'help'
  page = File.dirname(__FILE__) + '/help/' + cmd
  if File.exist?(page)
    puts File.read(page)
    puts "\nFor parameter listing and details, try executing the command with --help.\n\ttm #{cmd} --help"
  end  
end

#open_irb(options, argv) ⇒ Object

the actual method to do the irb opening



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/ticketmaster/cli/commands/console.rb', line 7

def open_irb(options, argv)
  tm_lib   = File.dirname(__FILE__) + '/../../../ticketmaster.rb'
  irb_name = RUBY_PLATFORM =~ /mswin32/ ? 'irb.bat' : 'irb'  
  requires = "-r rubygems -r #{tm_lib} "
  cmd = ''
  if File.exist?(config = File.expand_path(options[:config]))
    ENV['TICKETMASTER_CONFIG']=config
  end
  providers = !options[:provider].nil? ? [options[:provider]] : YAML.load_file(config).keys
  providers.delete 'default'
  require 'rubygems'
  require 'ticketmaster'
  providers.inject(requires) do |mem, p|
    begin
      require "ticketmaster-#{p}"
      requires << "-r ticketmaster-#{p} "
    rescue Exception => exception
      #puts exception
      begin
        require "#{p}"
        requires << "-r #{p} "
      rescue Exception => exception
        warn "Could not require the '#{p}' provider. Is it installed?"
      end
    end
  end
  cmd << "#{irb_name} #{requires} --simple-prompt #{ARGV.join(' ')}"
  exec cmd
end

#parse_config!(options) ⇒ Object

Parses the configuration information and puts it into options



2
3
4
5
6
7
8
9
10
# File 'lib/ticketmaster/cli/common.rb', line 2

def parse_config!(options)
  config = YAML.load_file File.expand_path(options[:config])
  provider = (options[:provider] ||= config['default'] || config.keys.first)
  if provider and provider.length > 0
    options[:project] ||= config[provider]['project']
    options[:authentication] ||= config[provider]['authentication']
  end
  options
end

#project(options) ⇒ Object

This sets the option parser and passes the parsed options to the subcommands



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/ticketmaster/cli/commands/project.rb', line 4

def project(options)
  ARGV << '--help' if ARGV.length == 0
  begin
    OptionParser.new do |opts|
      opts.banner = 'Usage: tm -p PROVIDER [options] project [project_options]'
      opts.separator ''
      opts.separator 'Options:'
      
      opts.on('-C', '--create ATTRIBUTES', 'Create a new project') do |attribute|
        options[:project_attributes] = {attribute => ARGV.shift}.merge(attributes_hash(ARGV))
        options[:subcommand] = 'create'
      end
      
      opts.on('-R', '--read [PROJECT]', 'Read out project and its attributes') do |id|
        options[:project] = id if id
        options[:subcommand] = 'read'
      end
      
      opts.on('-U', '--update ATTRIBUTES', 'Update project information') do |attribute|
        options[:project_attributes] = {attribute => ARGV.shift}.merge(attributes_hash(ARGV))
        options[:subcommand] = 'update'
      end
      
      opts.on('-D', '--destroy [PROJECT]', 'Destroy the project. Not reversible!') do |id|
        options[:project] = id if id
        options[:subcommand] = 'destroy'
      end
      
      opts.on('-I', '--info [PROJECT_ID]', 'Get project info. Same as --read. ') do |id|
        options[:project] = id if id
        options[:subcommand] = 'read'
      end
      
      opts.on('-S', '--search [ATTRIBUTES]', 'Search for a project based on attributes') do |attribute|
        options[:project_attributes] = attribute ? {attribute => ARGV.shift}.merge(attributes_hash(ARGV)) : {}
        options[:subcommand] = 'search'
      end
      
      opts.on('-L', '--list-all', 'List all projects. Same as --search without any parameters') do
        options[:project_attributes] = {}
        options[:subcommand] = 'search'
      end
      
      opts.on('-P', '--project [PROJECT_ID]', 'Set the project id') do |id|
        options[:project] = id
      end
      
      opts.separator ''
      opts.separator 'Other options:'
      
      opts.on_tail('-h', '--help', 'Show this message') do
        puts opts
        exit
      end
    end.order!
  rescue OptionParser::MissingArgument => exception
    puts "tm #{options[:original_argv].join(' ')}\n\n"
    puts "Error: An option was called that requires an argument, but was not given one"
    puts exception.message
  end
  parse_config!(options)
  begin
    require 'ticketmaster'
    require "ticketmaster-#{options[:provider]}"
  rescue
    require options[:provider]
  end
  send(options[:subcommand], options)
end

#read(options) ⇒ Object

The read subcommand



85
86
87
88
89
90
91
# File 'lib/ticketmaster/cli/commands/ticket.rb', line 85

def read(options)
  tm = TicketMaster.new(options[:provider], options[:authentication])
  project = tm.project(options[:project])
  ticket = project.ticket(options[:ticket])
  read_ticket ticket
  exit
end

#read_project(project) ⇒ Object

A utility method used to output project attributes



136
137
138
139
140
# File 'lib/ticketmaster/cli/commands/project.rb', line 136

def read_project(project)
  project.system_data[:client].attributes.sort.each do |key, value|
    puts "#{key} : #{value}"
  end
end

#read_ticket(ticket) ⇒ Object

A utility method used to output project attributes



141
142
143
144
145
# File 'lib/ticketmaster/cli/commands/ticket.rb', line 141

def read_ticket(ticket)
  ticket.system_data[:client].attributes.sort.each do |key, value|
    puts "#{key} : #{value}"
  end
end

#require_providerObject

Called when a provider is not given.



97
98
99
100
# File 'lib/ticketmaster/cli/commands/config.rb', line 97

def require_provider
  puts "Provider must be specified!"
  exit 1
end

#search(options) ⇒ Object

The search and list subcommands



127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/ticketmaster/cli/commands/ticket.rb', line 127

def search(options)
  tm = TicketMaster.new(options[:provider], options[:authentication])
  project = tm.project(options[:project])
  tickets = project.tickets(options[:ticket_attributes])
  puts "Found #{tickets.length} tickets"
  tickets.each_with_index do |ticket, index|
    puts "#{index+1}) Ticket #{ticket.title} (#{ticket.id})"
    #read_ticket ticket
    #puts
  end
  exit
end

#set_default_provider(options) ⇒ Object

Called on –set-default-provider. It sets the current provider as the default



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/ticketmaster/cli/commands/config.rb', line 84

def set_default_provider(options)
  provider = options[:provider]
  config = YAML.load_file(config_file = File.expand_path(options[:config]))
  puts "Warning! #{provider} is not defined in #{config_file}" unless provider.nil? or config[provider]
  config['default'] = provider
  File.open(config_file, 'w') do |out|
    YAML.dump(config, out)
  end
  puts "Default provider has been set to '#{provider}'"
  exit
end

#ticket(options) ⇒ Object

This sets the option parser and passes the parsed options to the subcommands



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/ticketmaster/cli/commands/ticket.rb', line 4

def ticket(options)
  ARGV << '--help' if ARGV.length == 0
  begin
    OptionParser.new do |opts|
      opts.banner = 'Usage: tm -p PROVIDER -P PROJECT [options] ticket [ticket_options]'
      opts.separator ''
      opts.separator 'Options:'
      
      opts.on('-T', '--ticket TICKET', 'Sets the working ticket') do |id|
        options[:ticket] = id
      end
      opts.on('-C', '--create ATTRIBUTES', 'Create a new ticket') do |attribute|
        options[:ticket_attributes] = {attribute => ARGV.shift}.merge(attributes_hash(ARGV))
        options[:subcommand] = 'create'
      end
      
      opts.on('-R', '--read [TICKET]', 'Read out ticket and its attributes. Requires --ticket to be set') do |id|
        options[:ticket] = id if id
        options[:subcommand] = 'read'
      end
      
      opts.on('-U', '--update ATTRIBUTES', 'Update ticket information. Requires --ticket to be set') do |attribute|
        options[:ticket_attributes] = {attribute => ARGV.shift}.merge(attributes_hash(ARGV))
        options[:subcommand] = 'update'
      end
      
      opts.on('-D', '--destroy', 'Destroy/Delete the ticket. Not reversible! Requires --ticket to be set') do
        options[:subcommand] = 'destroy'
      end
      
      opts.on('-I', '--info', 'Get ticket info. Same as --read. ') do
        options[:subcommand] = 'read'
      end
      
      opts.on('-S', '--search [ATTRIBUTES]', 'Search for a ticket based on attributes') do |attribute|
        options[:ticket_attributes] = attribute ? {attribute => ARGV.shift}.merge(attributes_hash(ARGV)) : {}
        options[:subcommand] = 'search'
      end
      
      opts.on('-L', '--list-all', 'List all tickets. Same as --search without any parameters') do
        options[:ticket_attributes] = {}
        options[:subcommand] = 'search'
      end
      
      opts.on('-P', '--project [PROJECT_ID]', 'Set the project id') do |id|
        options[:project] = id
      end
      
      opts.separator ''
      opts.separator 'Other options:'
      
      opts.on_tail('-h', '--help', 'Show this message') do
        puts opts
        exit
      end
    end.order!
  rescue OptionParser::MissingArgument => exception
    puts "tm #{options[:original_argv].join(' ')}\n\n"
    puts "Error: An option was called that requires an argument, but was not given one"
    puts exception.message
  end
  parse_config!(options)
  begin
    require 'ticketmaster'
    require "ticketmaster-#{options[:provider]}"
  rescue
    require options[:provider]
  end
  send(options[:subcommand], options)
end

#update(options) ⇒ Object

The update subcommand



94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/ticketmaster/cli/commands/ticket.rb', line 94

def update(options)
  tm = TicketMaster.new(options[:provider], options[:authentication])
  project = tm.project(options[:project])
  ticket = project.ticket(options[:ticket])
  if ticket.update!(options[:ticket_attributes])
    puts "Successfully updated Ticket #{ticket.title} (#{ticket.id})"
  else
    puts "Sorry, it seems there was an error when trying to update the attributes"
  end
  read_ticket ticket
  exit
end