Class: Shelltoad::Error

Inherits:
Object
  • Object
show all
Defined in:
lib/shelltoad/error.rb

Constant Summary collapse

CACHE =

Runtime cache for http queries

{}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes, full = false) ⇒ Error

API



67
68
69
70
# File 'lib/shelltoad/error.rb', line 67

def initialize(attributes, full = false)
  @attributes = attributes
  @data = attributes if full
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &blk) ⇒ Object (protected)



148
149
150
151
152
153
154
# File 'lib/shelltoad/error.rb', line 148

def method_missing(meth, *args, &blk)
  if attr = @attributes[meth]
    attr
  else
    super(meth, *args, &blk)
  end
end

Class Method Details

.all(options = {}) ⇒ Object

Class methods



15
16
17
18
19
20
21
22
# File 'lib/shelltoad/error.rb', line 15

def self.all(options = {})
  options[:project_id] ||= Configuration.project_id if Configuration.project_id
  CACHE[options] ||= (
    (Hoptoad::Error.find(:all, options) || []).map! do |attributes|
      self.new(attributes)
    end
  )
end

.base_urlObject



58
59
60
# File 'lib/shelltoad/error.rb', line 58

def self.base_url
  URI.parse("#{Configuration.secure? ? "https" : "http"}://#{Configuration.}.hoptoadapp.com")
end

.magic_find(id) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/shelltoad/error.rb', line 24

def self.magic_find(id)
  if !id || id.to_s.empty?
    Error.all.each do |error|
      output error.to_s
    end
    output "\n"
    id = Readline.readline("Input error id: ", true)
  end

  error = id.to_s.size > 5 ? simple_finder(id) : magic_finder(id)
  if block_given?
    return yield(error)
  end
  error
end

.magic_finder(id) ⇒ Object

Raises:



40
41
42
43
44
45
46
47
# File 'lib/shelltoad/error.rb', line 40

def self.magic_finder(id)
  1.upto(3) do |page|
    self.all(:show_resolved => true, :page => page).each do |error|
      return self.new(error) if error.id.to_s =~ /#{id}$/
    end
  end
  raise ErrorNotFound, "Error with id like *#{id} not found among last 90 errors.\n Try input full id."
end

.output(*args) ⇒ Object



54
55
56
# File 'lib/shelltoad/error.rb', line 54

def self.output(*args)
  Shelltoad.output(*args)
end

.simple_finder(id) ⇒ Object



49
50
51
52
# File 'lib/shelltoad/error.rb', line 49

def self.simple_finder(id)
  attributes = Hoptoad::Error.find(id) || raise(ErrorNotFound, "Error with id #{id} not found under this account.")
  self.new(attributes, true)
end

Instance Method Details

#[](key) ⇒ Object



129
130
131
# File 'lib/shelltoad/error.rb', line 129

def [](key)
  @attributes[key] || self.data[key]
end

#commit!(custom_message) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/shelltoad/error.rb', line 89

def commit!(custom_message)
  
  message = ""
  unless custom_message.nil? || custom_message.strip.empty?
    message << custom_message + "\n"
    message << "\n"
  end
  message << url.to_s + "\n"
  if custom_message.nil? || custom_message.strip.empty?
    message << "\n"
  end
  message << self.error_message + "\n"
  message.gsub!(/`/, "'")

  output = `git commit -m "#{message}"`
  if $?.success?
    resolve!
  end
  output
end

#dataObject



72
73
74
# File 'lib/shelltoad/error.rb', line 72

def data
  @data ||= Hoptoad::Error.find(self.id)
end

#idObject



121
122
123
# File 'lib/shelltoad/error.rb', line 121

def id
  @attributes[:id]
end

#linesObject



76
77
78
# File 'lib/shelltoad/error.rb', line 76

def lines
  self.data[:backtrace][:line]
end

#resolve!Object



110
111
112
113
114
# File 'lib/shelltoad/error.rb', line 110

def resolve!
  return true if self.resolved?
  Hoptoad::Error.put(path("xml"), :body => {:group => {:resolved => 1}})
  true
end

#resolved?Boolean

Returns:

  • (Boolean)


125
126
127
# File 'lib/shelltoad/error.rb', line 125

def resolved?
  @attributes[:resolved]
end

#to_sObject



117
118
119
# File 'lib/shelltoad/error.rb', line 117

def to_s
  "[##{self.id}] #{self.rails_env.first} #{self.error_message} #{self.file}:#{self.line_number}"
end

#url(format = nil) ⇒ Object



133
134
135
# File 'lib/shelltoad/error.rb', line 133

def url(format = nil)
  URI.parse(self.class.base_url.to_s + path(format))
end

#viewObject



80
81
82
83
84
85
86
# File 'lib/shelltoad/error.rb', line 80

def view
  <<-EOI
  #{self.url.to_s}
  #{self.error_message}
  #{self.lines.join("\n")}
  EOI
end