From e61fdcbef92b56958435691fe6a106f450c690b5 Mon Sep 17 00:00:00 2001 From: Andrey Deryabin Date: Thu, 4 Dec 2014 02:18:36 +0300 Subject: [PATCH 1/4] replace hash syntax to ruby 1.9 --- repasties.rb | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/repasties.rb b/repasties.rb index 5011099..fd90063 100755 --- a/repasties.rb +++ b/repasties.rb @@ -20,9 +20,9 @@ if service = services["rethinkdb"].first creds = service["credentials"] rdb_config = { - :host => creds["hostname"] || creds["host"], - :port => creds["port"], - :db => creds["name"] || 'repasties' + host: creds["hostname"] || creds["host"], + port: creds["port"], + db: creds["name"] || 'repasties' } end end @@ -31,9 +31,9 @@ # variables for location of RethinkDB server. Otherwise default # to a locally running server. rdb_config ||= { - :host => ENV['RDB_HOST'] || 'localhost', - :port => ENV['RDB_PORT'] || 28015, - :db => ENV['RDB_DB'] || 'repasties' + host: ENV['RDB_HOST'] || 'localhost', + port: ENV['RDB_PORT'] || 28015, + db: ENV['RDB_DB'] || 'repasties' } # A friendly shortcut for accessing ReQL functions @@ -51,8 +51,7 @@ configure do set :db, rdb_config[:db] begin - connection = r.connect(:host => rdb_config[:host], - :port => rdb_config[:port]) + connection = r.connect(host: rdb_config[:host], port: rdb_config[:port]) rescue Exception => err puts "Cannot connect to RethinkDB database #{rdb_config[:host]}:#{rdb_config[:port]} (#{err.message})" Process.exit(1) @@ -65,7 +64,7 @@ end begin - r.db(rdb_config[:db]).table_create('snippets').run(connection) + r.db(rdb_config[:db]).table_create(:snippets).run(connection) rescue RethinkDB::RqlRuntimeError => err puts "Table `snippets` already exists." ensure @@ -84,8 +83,7 @@ before do begin # When opening a connection we can also specify the database: - @rdb_connection = r.connect(:host => rdb_config[:host], :port => - rdb_config[:port], :db => settings.db) + @rdb_connection = r.connect(host: rdb_config[:host], port: rdb_config[:port], db: settings.db) rescue Exception => err logger.error "Cannot connect to RethinkDB database #{rdb_config[:host]}:#{rdb_config[:port]} (#{err.message})" halt 501, 'This page could look nicer, unfortunately the error is the same: database not available.' @@ -111,9 +109,9 @@ # [`table.insert`](http://www.rethinkdb.com/api/ruby/insert/). post '/' do @snippet = { - :title => params[:snippet_title], - :body => params[:snippet_body], - :lang => (params[:snippet_lang] || 'text').downcase, + title: params[:snippet_title], + body: params[:snippet_body], + lang: (params[:snippet_lang] || 'text').downcase, } if @snippet[:body].empty? erb :new From 49ca02a01764a8bae92ffff5361804a88c72fa86 Mon Sep 17 00:00:00 2001 From: Andrey Deryabin Date: Thu, 4 Dec 2014 02:20:31 +0300 Subject: [PATCH 2/4] replace using Strings to Symbols in queries --- repasties.rb | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/repasties.rb b/repasties.rb index fd90063..1711b76 100755 --- a/repasties.rb +++ b/repasties.rb @@ -125,7 +125,7 @@ @snippet[:created_at] = Time.now.to_i @snippet[:formatted_body] = pygmentize(@snippet[:body], @snippet[:lang]) - result = r.table('snippets').insert(@snippet).run(@rdb_connection) + result = r.table(:snippets).insert(@snippet).run(@rdb_connection) # The `insert` operation returns a single object specifying the number # of successfully created objects and their corresponding IDs. @@ -152,7 +152,7 @@ # for a single document by its ID, we use the # [`get`](http://www.rethinkdb.com/api/ruby/get/) command. get '/:id' do - @snippet = r.table('snippets').get(params[:id]).run(@rdb_connection) + @snippet = r.table(:snippets).get(params[:id]).run(@rdb_connection) if @snippet @snippet['created_at'] = Time.at(@snippet['created_at']) @@ -171,10 +171,10 @@ get '/lang/:lang' do @lang = params[:lang].downcase max_results = params[:limit] || 10 - results = r.table('snippets'). - filter('lang' => @lang). - pluck('id', 'title', 'created_at'). - order_by(r.desc('created_at')). + results = r.table(:snippets). + filter(lang: @lang). + pluck(:id, :title, :created_at). + order_by(r.desc(:created_at)). limit(max_results). run(@rdb_connection) From 874c683364de0ed38472765f5fe6156b6c54e316 Mon Sep 17 00:00:00 2001 From: Andrey Deryabin Date: Thu, 4 Dec 2014 02:24:32 +0300 Subject: [PATCH 3/4] remove unnecessary setting --- repasties.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/repasties.rb b/repasties.rb index 1711b76..56f3a2f 100755 --- a/repasties.rb +++ b/repasties.rb @@ -49,7 +49,6 @@ # and # [`table_create`](http://www.rethinkdb.com/api/ruby/table_create/) commands. configure do - set :db, rdb_config[:db] begin connection = r.connect(host: rdb_config[:host], port: rdb_config[:port]) rescue Exception => err @@ -83,7 +82,7 @@ before do begin # When opening a connection we can also specify the database: - @rdb_connection = r.connect(host: rdb_config[:host], port: rdb_config[:port], db: settings.db) + @rdb_connection = r.connect(host: rdb_config[:host], port: rdb_config[:port], db: rdb_config[:db]) rescue Exception => err logger.error "Cannot connect to RethinkDB database #{rdb_config[:host]}:#{rdb_config[:port]} (#{err.message})" halt 501, 'This page could look nicer, unfortunately the error is the same: database not available.' From e5e130704abe70abe811ebd009d69438c2cc7a5d Mon Sep 17 00:00:00 2001 From: Andrey Deryabin Date: Thu, 4 Dec 2014 02:25:30 +0300 Subject: [PATCH 4/4] fix messages --- repasties.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/repasties.rb b/repasties.rb index 56f3a2f..363183e 100755 --- a/repasties.rb +++ b/repasties.rb @@ -52,7 +52,7 @@ begin connection = r.connect(host: rdb_config[:host], port: rdb_config[:port]) rescue Exception => err - puts "Cannot connect to RethinkDB database #{rdb_config[:host]}:#{rdb_config[:port]} (#{err.message})" + puts "Cannot connect to RethinkDB #{rdb_config[:host]}:#{rdb_config[:port]} (#{err.message})" Process.exit(1) end @@ -84,7 +84,7 @@ # When opening a connection we can also specify the database: @rdb_connection = r.connect(host: rdb_config[:host], port: rdb_config[:port], db: rdb_config[:db]) rescue Exception => err - logger.error "Cannot connect to RethinkDB database #{rdb_config[:host]}:#{rdb_config[:port]} (#{err.message})" + logger.error "Cannot connect to RethinkDB database `#{rdb_config[:db]}` #{rdb_config[:host]}:#{rdb_config[:port]} (#{err.message})" halt 501, 'This page could look nicer, unfortunately the error is the same: database not available.' end end