Class: Rtrac::Base
- Inherits:
-
Object
- Object
- Rtrac::Base
- Defined in:
- lib/rtrac/base.rb
Class Method Summary collapse
- .columns ⇒ Object
- .config_path ⇒ Object
- .configuration_options ⇒ Object
- .connection ⇒ Object
- .create_new_config ⇒ Object
- .custom(*args) ⇒ Object
- .extract_account_info ⇒ Object
- .find_or_create_config ⇒ Object
- .get_by_milestone(milestone) ⇒ Object
-
.get_tickets(count, order = :priority) ⇒ Object
Returns an array of ticket ids Valid order types include:.
- .home_dir ⇒ Object
- .invalid_config?(username, password, url, columns) ⇒ Boolean
-
.list_methods ⇒ Object
list all methods supported by the trac api.
- .logger ⇒ Object
-
.method_help(method) ⇒ Object
returns method help.
-
.open_connection ⇒ Object
:stopdoc:.
- .optional_columns ⇒ Object
- .required_columns ⇒ Object
-
.send_query(*args) ⇒ Object
handles the sending of queries to Trac with exception handling.
Class Method Details
.columns ⇒ Object
97 98 99 100 |
# File 'lib/rtrac/base.rb', line 97 def columns extract_account_info @columns ||= required_columns + optional_columns end |
.config_path ⇒ Object
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.[:config_filename]) else File.join(home_dir, ".#{Rtrac::Configuration.[:config_filename]}") # hide it if it goes in home end end |
.configuration_options ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/rtrac/base.rb', line 81 def 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 |
.connection ⇒ Object
21 22 23 |
# File 'lib/rtrac/base.rb', line 21 def connection @connection ||= self.open_connection end |
.create_new_config ⇒ Object
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.[: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_info ⇒ Object
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 extract_account_info username = password = url = columns = nil .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_config ⇒ Object
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) extract_account_info order = :priority unless columns.include?(order) send_query('ticket.query', "order=#{order.to_s}")[0,count].collect{ |id| id } end |
.home_dir ⇒ Object
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
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_methods ⇒ Object
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 |
.logger ⇒ Object
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_connection ⇒ Object
:stopdoc:
52 53 54 55 56 57 58 59 |
# File 'lib/rtrac/base.rb', line 52 def open_connection if ( = extract_account_info) @username, @password, @url = 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_columns ⇒ Object
107 108 109 110 |
# File 'lib/rtrac/base.rb', line 107 def optional_columns extract_account_info @configuration_options["columns"]["optional"] end |
.required_columns ⇒ Object
102 103 104 105 |
# File 'lib/rtrac/base.rb', line 102 def required_columns extract_account_info @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 |