Class: Rtrac::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/rtrac/base.rb

Direct Known Subclasses

Ticket, Util

Class Method Summary collapse

Class Method Details

.columnsObject



97
98
99
100
# File 'lib/rtrac/base.rb', line 97

def columns
  
  @columns ||= required_columns + optional_columns
end

.config_pathObject



125
126
127
128
129
130
131
# File 'lib/rtrac/base.rb', line 125

def config_path
  if RAILS_ROOT
    File.join(RAILS_ROOT, 'config', Rtrac::Configuration.options[:config_filename])
  else
    File.join(home_dir, ".#{Rtrac::Configuration.options[:config_filename]}") # hide it if it goes in home
  end
end

.configuration_optionsObject



81
82
83
84
85
86
87
88
89
90
91
# File 'lib/rtrac/base.rb', line 81

def configuration_options
  return @configuration_options unless @configuration_options.nil?
  if self.config.nil?
    @configuration_options = find_or_create_config
  else
    if File.exist?(config)
      @configuration_options = YAML::load(open(config))
    end
  end
  @configuration_options  
end

.connectionObject



21
22
23
# File 'lib/rtrac/base.rb', line 21

def connection
  @connection ||= self.open_connection
end

.create_new_configObject



133
134
135
136
137
138
139
140
141
142
143
# File 'lib/rtrac/base.rb', line 133

def create_new_config
  file = File.open(config_path, "w+")
  file.write(Rtrac::Configuration.options[:default_config_file])
  file.flush
  file.close
  logger.info "No config file found in #{home_dir}
A new one has been created.  Please edit it before continuing
#{config_path}
"
  exit
end

.custom(*args) ⇒ Object



30
31
32
# File 'lib/rtrac/base.rb', line 30

def custom(*args)
  send_query(*args)
end

.extract_account_infoObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/rtrac/base.rb', line 61

def 
  username = password = url = columns = nil
  configuration_options.each do |key, value|
    if key.to_sym == :username
      username = value.to_s
    elsif key.to_sym == :password
      password = value.to_s
    elsif key.to_sym == :url
      url = value.to_s
    elsif key.to_sym == :columns
      @columns = value.to_a
    end
  end
  if invalid_config?(username, password, url, @columns)
    logger.error "Invalid configuration options.  Please edit your #{config_path} file accordingly." 
    return false 
  end
  [username, password, url, @columns]
end

.find_or_create_configObject



112
113
114
115
116
117
118
119
# File 'lib/rtrac/base.rb', line 112

def find_or_create_config
  begin
    config = YAML::load(open(config_path))
  rescue
    create_new_config
  end
  config
end

.get_by_milestone(milestone) ⇒ Object



34
35
36
# File 'lib/rtrac/base.rb', line 34

def get_by_milestone(milestone)
  send_query('ticket.query', "milestone=#{milestone.strip}")
end

.get_tickets(count, order = :priority) ⇒ Object

Returns an array of ticket ids Valid order types include:



45
46
47
48
49
# File 'lib/rtrac/base.rb', line 45

def get_tickets(count, order = :priority)
  
  order = :priority unless columns.include?(order)
  send_query('ticket.query', "order=#{order.to_s}")[0,count].collect{ |id| id }
end

.home_dirObject



121
122
123
# File 'lib/rtrac/base.rb', line 121

def home_dir
  ENV['HOME'] || ENV['USERPROFILE'] || ENV['HOMEPATH']
end

.invalid_config?(username, password, url, columns) ⇒ Boolean

Returns:

  • (Boolean)


93
94
95
# File 'lib/rtrac/base.rb', line 93

def invalid_config?(username, password, url, columns)
  username.empty? || password.empty? || columns.empty? || ![url =~ /^http(s|):\/\/.+/i].any?
end

.list_methodsObject

list all methods supported by the trac api



26
27
28
# File 'lib/rtrac/base.rb', line 26

def list_methods
  send_query('system.listMethods')
end

.loggerObject



145
146
147
148
149
150
151
# File 'lib/rtrac/base.rb', line 145

def logger
  if @logger.nil?
    @logger = Logger.new(STDERR)
    @logger.level = Logger::INFO
  end
  @logger
end

.method_help(method) ⇒ Object

returns method help



39
40
41
# File 'lib/rtrac/base.rb', line 39

def method_help(method)
  send_query('system.methodHelp', method)
end

.open_connectionObject

:stopdoc:



52
53
54
55
56
57
58
59
# File 'lib/rtrac/base.rb', line 52

def open_connection
  if (options = )
    @username, @password, @url = options
    uri = URI.parse @url
    #puts "Connecting to \"#{@url}\" using username \"#{@username}\" and password \"#{@password}\""
    @connection = XMLRPC::Client.new3(:host => uri.host, :path => "#{uri.path}/login/xmlrpc", :user => @username, :password => @password, :use_ssl => true)
  end
end

.optional_columnsObject



107
108
109
110
# File 'lib/rtrac/base.rb', line 107

def optional_columns
  
  @configuration_options["columns"]["optional"]
end

.required_columnsObject



102
103
104
105
# File 'lib/rtrac/base.rb', line 102

def required_columns
  
  @configuration_options["columns"]["required"]
end

.send_query(*args) ⇒ Object

handles the sending of queries to Trac with exception handling



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/rtrac/base.rb', line 8

def send_query(*args)
  begin
    connection.call(*args)
  rescue
    # print a standard error
    logger.error "Error connecting to Trac API"
    if logger.level > Logger::INFO
      logger.error $@
    end
    exit
  end
end