Dex 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+
6
+ coverage
7
+ rdoc
8
+ .yardoc
data/Dex.gemspec ADDED
@@ -0,0 +1,34 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ $:.push File.expand_path("../lib", __FILE__)
4
+ require "Dex/version"
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "Dex"
8
+ s.version = Dex::VERSION
9
+ s.authors = ["da99"]
10
+ s.email = ["i-hate-spam-45671204@mailinator.com"]
11
+ s.homepage = "https://github.com/da99/Dex"
12
+ s.summary = %q{Log exceptions to sqlite3}
13
+ s.description = %q{
14
+ A simple function to log errors to sqlite3.
15
+ }
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+
22
+ s.add_development_dependency 'bacon'
23
+ s.add_development_dependency 'rake'
24
+ s.add_development_dependency 'Bacon_Colored'
25
+ s.add_development_dependency 'pry'
26
+ s.add_development_dependency 'Exit_0'
27
+ s.add_development_dependency 'rack-test'
28
+
29
+ # Specify any dependencies here; for example:
30
+ s.add_runtime_dependency 'sqlite3'
31
+ s.add_runtime_dependency 'sequel'
32
+ s.add_runtime_dependency 'sinatra'
33
+ s.add_runtime_dependency 'markaby'
34
+ end
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ source "http://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in Bob.gemspec
6
+ gemspec
data/README.md ADDED
@@ -0,0 +1,48 @@
1
+
2
+ Dex
3
+ ================
4
+
5
+ A Ruby gem providing a function to
6
+ log exceptions to sqlite3.
7
+
8
+ Requirements/Installation
9
+ ------------
10
+
11
+ Required:
12
+
13
+ * Ruby 1.9.3
14
+
15
+ Install:
16
+
17
+ sudo apt-get install sqlite3
18
+ gem install Dex
19
+
20
+ Usage
21
+ ------
22
+
23
+ require "Dex"
24
+
25
+ Dex.db "./my_log.db"
26
+
27
+ begin
28
+ raise
29
+ rescue Object => e
30
+ Dex.log $!
31
+ raise e
32
+ end
33
+
34
+
35
+ Run Tests
36
+ ---------
37
+
38
+ git clone git@github.com:da99/Dex.git
39
+ cd Dex
40
+ bundle update
41
+ bundle exec bacon spec/libs/main.rb
42
+
43
+ "I hate writing."
44
+ -----------------------------
45
+
46
+ If you know of existing software that makes the above redundant,
47
+ please tell me. The last thing I want to do is maintain code.
48
+
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/gem_tasks"
4
+
5
+ task :bacon do
6
+ exec "bundle exec ruby libs/main.rb"
7
+ end
data/lib/Dex.rb ADDED
@@ -0,0 +1,101 @@
1
+ require 'Dex/version'
2
+ require 'sequel'
3
+
4
+ class Dex
5
+
6
+ def self.default_db
7
+ "dex_exceptions.db"
8
+ end
9
+
10
+ def self.default_table
11
+ :dex_exceptions
12
+ end
13
+
14
+ module DSL
15
+
16
+ def db_file f = :_R_
17
+ return @db_file if f == :_R_
18
+ @db_file = f
19
+ end
20
+
21
+ def keep_only n = 250
22
+ c = table.count
23
+ return false unless c > 250
24
+ table.filter( :id=> Dex.table.select(:id).limit( c-n ) ).delete
25
+ end
26
+
27
+ def db name = :_RETURN_
28
+ if name != :_RETURN_
29
+ @db = begin
30
+ db_file name
31
+ db = Sequel.connect "sqlite://#{db_file}"
32
+ db.create_table?(Dex.default_table) {
33
+
34
+ primary_key :id
35
+ String :message
36
+ String :exception
37
+ Text :backtrace
38
+ Integer :status
39
+ DateTime :created_at
40
+
41
+ }
42
+ db
43
+ end
44
+ end
45
+
46
+ @db ||= db(Dex.default_db)
47
+ end
48
+
49
+ def table name = :_RETURN_
50
+ if name != :_RETURN_
51
+ @table = db[name]
52
+ end
53
+ @table ||= table(:dex_exceptions)
54
+ end
55
+
56
+ def insert e
57
+ table.insert \
58
+ :message => e.message, \
59
+ :exception => e.exception.class.name, \
60
+ :backtrace => e.backtrace.join("\n"),
61
+ :status => 0,
62
+ :created_at => Time.now.utc
63
+ end
64
+
65
+ def recent n = 10
66
+ ds = table.reverse_order(:created_at, :id).limit(n)
67
+ if n < 2
68
+ ds.first
69
+ else
70
+ ds
71
+ end
72
+ end
73
+
74
+ def method_missing *args, &blok
75
+ if table.respond_to?(args.first)
76
+ table.send *args, &blok
77
+ else
78
+ super
79
+ end
80
+ end
81
+
82
+ end # === DSL
83
+
84
+ extend DSL
85
+
86
+ end # === class Dex
87
+
88
+
89
+
90
+
91
+
92
+
93
+
94
+
95
+
96
+
97
+
98
+
99
+
100
+
101
+
@@ -0,0 +1,93 @@
1
+
2
+ require 'sinatra/base'
3
+ require 'Dex'
4
+ require 'markaby'
5
+
6
+ class Dex
7
+
8
+ class Rack_App < Sinatra::Base
9
+
10
+ get "/" do
11
+ recent
12
+ end
13
+
14
+ get "/recent/:num" do | num |
15
+ recent Integer(num)
16
+ end
17
+
18
+ get "/:id" do | id |
19
+ r = Dex.filter(:id=>id).first
20
+ view_record Hash[:title=>r[:message], :record=>r]
21
+ end
22
+
23
+ put "/:id/status" do |id|
24
+ Dex.filter(:id=>id).update(:status=>params[:status].to_i)
25
+ redirect to('/'), 303
26
+ end
27
+
28
+ delete "/:id" do | id |
29
+ Dex.filter(:id=>id).delete
30
+ redirect to('/'), 303
31
+ end
32
+
33
+ def recent num = 10
34
+ vars = Hash[
35
+ :title => "Dex List",
36
+ :list => Dex.recent
37
+ ]
38
+ view_index vars
39
+ end
40
+
41
+ def layout vars, &b
42
+ mab = Markaby::Builder.new
43
+ mab.html do
44
+ head {
45
+ title vars[:title]
46
+ }
47
+ body {
48
+ instance_eval(&b)
49
+ }
50
+ end
51
+
52
+ mab.to_s
53
+ end
54
+
55
+ def view_index vars
56
+ layout(vars) {
57
+ vars[:list].each { |db|
58
+ div {
59
+ p "test"
60
+ p db[:message]
61
+ p db[:exception]
62
+ }
63
+ }
64
+ }
65
+ end
66
+
67
+ def view_record vars
68
+ r = vars[:record]
69
+ s = self
70
+ layout(vars) {
71
+ div {
72
+ p {
73
+ span "#{r[:exception]}: #{r[:message]}"
74
+ span.status s.status_to_word(r[:status])
75
+ }
76
+ pre { r[:backtrace] }
77
+
78
+ }
79
+ }
80
+ end
81
+
82
+ def status_to_word num
83
+ case num
84
+ when 0
85
+ "Unresolved"
86
+ when 1
87
+ "Resolved"
88
+ end
89
+ end
90
+ end # === Rack_App < Sinatra::Base
91
+
92
+ end # === Dex
93
+
@@ -0,0 +1,3 @@
1
+ class Dex
2
+ VERSION = "0.1.0"
3
+ end
data/spec/Dex.rb ADDED
@@ -0,0 +1,101 @@
1
+
2
+ describe "Dex :recent" do
3
+
4
+ it "returns first result if n = 1" do
5
+ transact {
6
+ e = except "One"
7
+ Dex.insert e
8
+ Dex.recent( 1 )[:message].should == "One"
9
+ }
10
+ end
11
+
12
+ it "returns a dataset if n > 1" do
13
+ transact {
14
+ e1 = except "One"
15
+ e2 = except "Two"
16
+ Dex.insert e1
17
+ Dex.insert e2
18
+ r = Dex.recent
19
+ r.to_a.size.should == 2
20
+ }
21
+ end
22
+
23
+ it "returns results in reverse order" do
24
+ transact {
25
+ 3.times do |i|
26
+ Dex.insert( except i.to_s )
27
+ end
28
+ Dex.recent.map { |d| d[:message] }.should == %w{ 2 1 0 }
29
+ }
30
+ end
31
+
32
+ end # === Dex :recent
33
+
34
+
35
+ describe "Dex :insert" do
36
+
37
+ it "saves message of exception" do
38
+ transact {
39
+ e = except "My Name"
40
+ Dex.insert e
41
+ Dex.recent( 1 )[:message].should == e.message
42
+ }
43
+ end
44
+
45
+ it "returns id of new record" do
46
+ transact {
47
+ e = except "My record"
48
+ id = Dex.insert(e)
49
+ Dex.filter(:id=>id).first[:message].should == "My record"
50
+ }
51
+ end
52
+
53
+ it "sets :status to 0 by default" do
54
+ transact {
55
+ id = Dex.insert(except "Another record")
56
+ Dex.filter(:id=>id).first[:status].should == 0
57
+ }
58
+ end
59
+ end # === Dex :insert
60
+
61
+ describe "Dex :keep_only" do
62
+
63
+ it "deletes oldest records leaving most recent 250" do
64
+ transact {
65
+ 300.times { |i| Dex.insert except(i.to_s) }
66
+ Dex.keep_only
67
+ Dex.count.should == 250
68
+ Dex.recent(1)[:message].should == '299'
69
+ }
70
+ end
71
+
72
+ it "accepts a limit argument" do
73
+ transact {
74
+ 300.times { |i| Dex.insert except(i.to_s) }
75
+ Dex.keep_only 12
76
+ Dex.table.count.should == 12
77
+ }
78
+ end
79
+
80
+ end # === Dex :keep_only
81
+
82
+ describe "Dex missing_method" do
83
+
84
+ it "sends message to :table if it responds to message" do
85
+ transact {
86
+ (rand 10).times { |i| Dex.insert except(i.to_s) }
87
+ Dex.count.should == Dex.table.count
88
+ }
89
+ end
90
+
91
+ it "raises super :missing_method if :table does not respond to message" do
92
+ transact {
93
+ (rand 10).times { |i| Dex.insert except(i.to_s) }
94
+ should.raise(NoMethodError) {
95
+ Dex.xyz
96
+ }.message.should.match %r!undefined method `xyz' for Dex:Class!
97
+ }
98
+ end
99
+
100
+ end # === Dex missing_method
101
+
@@ -0,0 +1,87 @@
1
+
2
+ describe "get /" do
3
+
4
+ it "renders a recent list of 10" do
5
+ transact {
6
+ 5.times { |i| Dex.insert except("Err: #{i}") }
7
+
8
+ get "/"
9
+ 5.times { |i|
10
+ last_response.body.should.match %r!Err: #{i}!
11
+ }
12
+ }
13
+ end
14
+
15
+ end # === get /
16
+
17
+ describe "get /recent/:num" do
18
+
19
+ it "/n renders a list equal to n" do
20
+ transact {
21
+ n = rand(10) + 1
22
+ n.times { |i| Dex.insert except("N: #{i}") }
23
+
24
+ get "/recent/#{n}"
25
+ n.times { |i|
26
+ last_response.body.should.match %r!N: #{i}!
27
+ }
28
+ }
29
+ end
30
+
31
+ end # === get /recent/num
32
+
33
+ describe "get /:id" do
34
+
35
+ it "displays record" do
36
+ transact {
37
+ id = Dex.insert(except "rand err")
38
+ get "/#{id}"
39
+ should_render "rand err"
40
+ }
41
+ end
42
+
43
+ end # === get /:id
44
+
45
+ describe "put /:id/status" do
46
+
47
+ it "updates status of record" do
48
+ transact {
49
+ id = Dex.insert(except "rand err")
50
+ put "/#{id}/status", :status=>2
51
+ Dex.filter(:id=>id).first[:status]
52
+ .should == 2
53
+ }
54
+ end
55
+
56
+ it "redirects to /:id 303" do
57
+ transact {
58
+ id = Dex.insert(except "rand err")
59
+ put "/#{id}/status", :status=>2
60
+ should_redirect_to "/"
61
+ }
62
+ end
63
+
64
+ end # === put /:id/status
65
+
66
+ describe "delete /:id" do
67
+
68
+ it "deletes record" do
69
+ transact {
70
+ Dex.insert(except("Deleted"))
71
+ r = Dex.recent(1)
72
+ delete "/#{r[:id]}"
73
+ Dex.count.should == 0
74
+ }
75
+ end
76
+
77
+ it "redirects to / 303" do
78
+ transact {
79
+ Dex.insert(except("Deleted"))
80
+ r = Dex.recent(1)
81
+ delete "/#{r[:id]}"
82
+ should_redirect_to "/"
83
+ }
84
+ end
85
+
86
+ end # === delete /:id
87
+
data/spec/bin.rb ADDED
@@ -0,0 +1,13 @@
1
+
2
+ bins = Dir.glob("bin/*")
3
+
4
+ unless bins.empty?
5
+ describe "permissions of bin/" do
6
+ bins.each { |file|
7
+ it "should chmod 755 for: #{file}" do
8
+ `stat -c %a #{file}`.strip
9
+ .should.be == "755"
10
+ end
11
+ }
12
+ end # === permissions of bin/
13
+ end # === unless bins.empty?
data/spec/libs/main.rb ADDED
@@ -0,0 +1,76 @@
1
+
2
+ require 'rubygems'
3
+ require 'bundler'
4
+ begin
5
+ Bundler.setup(:default, :development)
6
+ rescue Bundler::BundlerError => e
7
+ $stderr.print e.message, "\n"
8
+ $stderr.print "Run `bundle install` to install missing gems\n"
9
+ exit e.status_code
10
+ end
11
+ require 'bacon'
12
+
13
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
14
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '/../..', 'lib'))
15
+
16
+ Bacon.summary_on_exit
17
+
18
+ require 'Bacon_Colored'
19
+ require 'pry'
20
+ require 'Exit_0'
21
+
22
+ ENV['RACK_ENV']='test'
23
+ require 'rack/test'
24
+ require 'Dex'
25
+ require 'Dex/Rack_App'
26
+
27
+ class Bacon::Context
28
+ include Rack::Test::Methods
29
+
30
+ def app
31
+ Dex::Rack_App
32
+ end
33
+
34
+ def should_render txt
35
+ last_response.should.be.ok
36
+ r = txt.respond_to?(:~) ? txt : %r!#{txt}!
37
+ last_response.body.should.match r
38
+ end
39
+
40
+ def should_redirect_to url, status = 303
41
+ last_response.status.should == status
42
+ last_response['Location'].sub( %r!http://(www.)?example.(com|org)!, '' )
43
+ .should == '/'
44
+ end
45
+
46
+ end # === class Bacon::Context
47
+
48
+ Exit_0 "rm /tmp/dex.test.db" if File.exists?("/tmp/dex.test.db")
49
+ Dex.db "/tmp/dex.test.db"
50
+
51
+ def transact
52
+ Dex.db.transaction(:rollback=>:always) {
53
+ yield
54
+ }
55
+ end
56
+
57
+
58
+ def except name
59
+ err = nil
60
+ begin
61
+ raise name
62
+ rescue Object => e
63
+ err = e
64
+ end
65
+ err
66
+ end
67
+
68
+
69
+ # ======== Include the tests.
70
+ if ARGV.size > 1 && ARGV[1, ARGV.size - 1].detect { |a| File.exists?(a) }
71
+ # Do nothing. Bacon grabs the file.
72
+ else
73
+ Dir.glob('./spec/*.rb').each { |file|
74
+ require file.sub('.rb', '') if File.file?(file)
75
+ }
76
+ end
metadata ADDED
@@ -0,0 +1,217 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: Dex
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - da99
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-05-04 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bacon
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: Bacon_Colored
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: pry
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: Exit_0
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ - !ruby/object:Gem::Dependency
95
+ name: rack-test
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ - !ruby/object:Gem::Dependency
111
+ name: sqlite3
112
+ requirement: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ! '>='
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ - !ruby/object:Gem::Dependency
127
+ name: sequel
128
+ requirement: !ruby/object:Gem::Requirement
129
+ none: false
130
+ requirements:
131
+ - - ! '>='
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ type: :runtime
135
+ prerelease: false
136
+ version_requirements: !ruby/object:Gem::Requirement
137
+ none: false
138
+ requirements:
139
+ - - ! '>='
140
+ - !ruby/object:Gem::Version
141
+ version: '0'
142
+ - !ruby/object:Gem::Dependency
143
+ name: sinatra
144
+ requirement: !ruby/object:Gem::Requirement
145
+ none: false
146
+ requirements:
147
+ - - ! '>='
148
+ - !ruby/object:Gem::Version
149
+ version: '0'
150
+ type: :runtime
151
+ prerelease: false
152
+ version_requirements: !ruby/object:Gem::Requirement
153
+ none: false
154
+ requirements:
155
+ - - ! '>='
156
+ - !ruby/object:Gem::Version
157
+ version: '0'
158
+ - !ruby/object:Gem::Dependency
159
+ name: markaby
160
+ requirement: !ruby/object:Gem::Requirement
161
+ none: false
162
+ requirements:
163
+ - - ! '>='
164
+ - !ruby/object:Gem::Version
165
+ version: '0'
166
+ type: :runtime
167
+ prerelease: false
168
+ version_requirements: !ruby/object:Gem::Requirement
169
+ none: false
170
+ requirements:
171
+ - - ! '>='
172
+ - !ruby/object:Gem::Version
173
+ version: '0'
174
+ description: ! "\n A simple function to log errors to sqlite3.\n "
175
+ email:
176
+ - i-hate-spam-45671204@mailinator.com
177
+ executables: []
178
+ extensions: []
179
+ extra_rdoc_files: []
180
+ files:
181
+ - .gitignore
182
+ - Dex.gemspec
183
+ - Gemfile
184
+ - README.md
185
+ - Rakefile
186
+ - lib/Dex.rb
187
+ - lib/Dex/Rack_App.rb
188
+ - lib/Dex/version.rb
189
+ - spec/Dex.rb
190
+ - spec/Dex_Rack_App.rb
191
+ - spec/bin.rb
192
+ - spec/libs/main.rb
193
+ homepage: https://github.com/da99/Dex
194
+ licenses: []
195
+ post_install_message:
196
+ rdoc_options: []
197
+ require_paths:
198
+ - lib
199
+ required_ruby_version: !ruby/object:Gem::Requirement
200
+ none: false
201
+ requirements:
202
+ - - ! '>='
203
+ - !ruby/object:Gem::Version
204
+ version: '0'
205
+ required_rubygems_version: !ruby/object:Gem::Requirement
206
+ none: false
207
+ requirements:
208
+ - - ! '>='
209
+ - !ruby/object:Gem::Version
210
+ version: '0'
211
+ requirements: []
212
+ rubyforge_project:
213
+ rubygems_version: 1.8.23
214
+ signing_key:
215
+ specification_version: 3
216
+ summary: Log exceptions to sqlite3
217
+ test_files: []