From 9f73ec61bd678cdb48b33d3be145957dd3e85e56 Mon Sep 17 00:00:00 2001 From: Kyle Drake Date: Wed, 22 Mar 2017 11:39:06 -0700 Subject: [PATCH] Fixes for Ruby 2.4 support --- Gemfile | 48 +++----- Gemfile.lock | 80 ++++++------- environment.rb | 1 + ext/ago.rb | 169 +++++++++++++++++++++++++++ ext/time.rb | 10 +- migrations/081_stats_bigint.rb | 4 +- migrations/082_daily_stats_bigint.rb | 8 +- 7 files changed, 232 insertions(+), 88 deletions(-) create mode 100644 ext/ago.rb diff --git a/Gemfile b/Gemfile index 9eb155e4..b4fb1fce 100644 --- a/Gemfile +++ b/Gemfile @@ -3,7 +3,7 @@ source 'https://rubygems.org' gem 'sinatra' gem 'redis' gem 'redis-namespace' -gem 'sequel', '4.8.0' +gem 'sequel' gem 'bcrypt' gem 'sinatra-flash', require: 'sinatra/flash' gem 'sinatra-xsendfile', require: 'sinatra/xsendfile' @@ -11,12 +11,10 @@ gem 'puma', require: nil gem 'rack-recaptcha', require: 'rack/recaptcha' gem 'rmagick', require: nil gem 'sidekiq', '~> 4.1.2' -gem 'ago' gem 'mail' gem 'tilt' gem 'erubis' gem 'stripe', '1.15.0' #, source: 'https://code.stripe.com/' -#gem 'screencap', '~> 0.1.4' gem 'cocaine' gem 'zipruby' gem 'sass', require: nil @@ -48,33 +46,16 @@ gem 'image_optim_pack' gem 'ipaddress' gem 'feedjira' gem 'monetize' +gem 'xmlrpc' +gem 'magic' +gem 'pg' +gem 'sequel_pg', require: nil +gem 'hiredis' +gem 'activesupport' -platform :mri, :rbx do - gem 'magic' # sudo apt-get install file, For OSX: brew install libmagic - gem 'pg' - gem 'sequel_pg', require: nil - gem 'hiredis' - gem 'posix-spawn' - - group :development, :test do - gem 'pry' - end -end - -platform :mri do - group :development, :test do - gem 'pry-byebug', platform: 'mri' - end -end - -platform :jruby do - gem 'jruby-openssl' - gem 'json' - gem 'jdbc-postgres' - - group :development do - gem 'ruby-debug', require: nil - end +group :development, :test do + gem 'pry' + gem 'pry-byebug' end group :development do @@ -92,15 +73,12 @@ group :test do gem 'rake', require: nil gem 'poltergeist' gem 'capybara_minitest_spec' - gem 'capybara', '2.10.1', require: nil + gem 'capybara', require: nil #, '2.10.1', require: nil gem 'rack_session_access', require: nil gem 'webmock', require: nil gem 'stripe-ruby-mock', '2.0.1', require: 'stripe_mock' gem 'timecop' gem 'mock_redis' - - platform :mri, :rbx do - gem 'simplecov', require: nil - gem 'm' - end + gem 'simplecov', require: nil + gem 'm' end diff --git a/Gemfile.lock b/Gemfile.lock index bba22b4d..deb6945c 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -14,24 +14,22 @@ GEM specs: acme-client (0.4.0) faraday (~> 0.9, >= 0.9.1) - activesupport (4.2.6) + activesupport (5.0.2) + concurrent-ruby (~> 1.0, >= 1.0.2) i18n (~> 0.7) - json (~> 1.7, >= 1.7.7) minitest (~> 5.1) - thread_safe (~> 0.3, >= 0.3.4) tzinfo (~> 1.1) addressable (2.5.0) public_suffix (~> 2.0, >= 2.0.2) - ago (0.1.5) annoy (0.5.6) highline (>= 1.5.0) ansi (1.5.0) base32 (0.3.2) bcrypt (3.1.11) blankslate (3.1.3) - builder (3.2.2) + builder (3.2.3) byebug (8.2.4) - capybara (2.10.1) + capybara (2.13.0) addressable mime-types (>= 1.16) nokogiri (>= 1.3.3) @@ -48,14 +46,14 @@ GEM cocaine (0.5.8) climate_control (>= 0.0.3, < 1.0) coderay (1.1.1) - concurrent-ruby (1.0.2) + concurrent-ruby (1.0.5) connection_pool (2.2.0) - coveralls (0.8.13) - json (~> 1.8) - simplecov (~> 0.11.0) + coveralls (0.8.19) + json (>= 1.8, < 3) + simplecov (~> 0.12.0) term-ansicolor (~> 1.3) thor (~> 0.19.1) - tins (~> 1.6.0) + tins (~> 1.6) crack (0.4.3) safe_yaml (~> 1.0.0) crass (1.0.2) @@ -83,12 +81,12 @@ GEM ffi (>= 1.0.0) rake filesize (0.1.1) - fspath (3.0.1) - gandi (2.1.3) + fspath (3.0.3) + gandi (3.3.27) hashie geoip (1.6.1) - hashdiff (0.3.0) - hashie (3.4.3) + hashdiff (0.3.2) + hashie (3.5.5) highline (1.7.8) hiredis (0.6.1) hoe (3.14.2) @@ -122,7 +120,7 @@ GEM multi_json (~> 1.0) rack (~> 1.4) rest-client (~> 1.0) - json (1.8.3) + json (1.8.6) loofah (2.0.3) nokogiri (>= 1.5.9) m (1.4.2) @@ -136,7 +134,7 @@ GEM method_source (0.8.2) mime-types (2.99.3) mini_portile2 (2.1.0) - minitest (5.8.4) + minitest (5.10.1) minitest-reporters (1.1.8) ansi builder @@ -150,25 +148,24 @@ GEM money (6.8.1) i18n (>= 0.6.4, <= 0.7.0) sixarm_ruby_unaccent (>= 1.1.1, < 2) - msgpack (0.7.5) + msgpack (1.1.0) multi_json (1.12.1) multipart-post (2.0.0) net-scp (1.2.1) net-ssh (>= 2.6.5) net-ssh (3.1.1) netrc (0.11.0) - nokogiri (1.6.8.1) + nokogiri (1.7.0.1) mini_portile2 (~> 2.1.0) nokogumbo (1.4.7) nokogiri ox (2.4.6) paypal-recurring (1.1.0) - pg (0.18.4) + pg (0.20.0) poltergeist (1.11.0) capybara (~> 2.1) cliver (~> 0.3.1) websocket-driver (>= 0.2.0) - posix-spawn (0.3.11) progress (3.2.2) pry (0.10.3) coderay (~> 1.1.0) @@ -177,7 +174,7 @@ GEM pry-byebug (3.3.0) byebug (~> 8.0) pry (~> 0.10) - public_suffix (2.0.4) + public_suffix (2.0.5) puma (3.4.0) rack (1.6.5) rack-cache (1.6.1) @@ -192,7 +189,7 @@ GEM builder (>= 2.0.0) rack (>= 1.0.0) rake (10.5.0) - redis (3.2.2) + redis (3.3.3) redis-namespace (1.5.2) redis (~> 3.0, >= 3.0.4) rest-client (1.8.0) @@ -219,8 +216,8 @@ GEM scrypt (2.1.1) ffi-compiler (>= 0.0.2) rake - sequel (4.8.0) - sequel_pg (1.6.16) + sequel (4.44.0) + sequel_pg (1.6.17) pg (>= 0.8.0) sequel (>= 4.0.0) shotgun (0.9.1) @@ -229,13 +226,13 @@ GEM concurrent-ruby (~> 1.0) connection_pool (~> 2.2, >= 2.2.0) redis (~> 3.2, >= 3.2.1) - simplecov (0.11.2) + simplecov (0.12.0) docile (~> 1.1.0) - json (~> 1.8) + json (>= 1.8, < 3) simplecov-html (~> 0.10.0) simplecov-html (0.10.0) simpleidn (0.0.6) - sinatra (1.4.7) + sinatra (1.4.8) rack (~> 1.5) rack-protection (~> 1.4) tilt (>= 1.3, < 3) @@ -257,21 +254,21 @@ GEM sysinfo (0.8.1) drydock storable - term-ansicolor (1.3.2) + term-ansicolor (1.4.0) tins (~> 1.0) - thor (0.19.1) + thor (0.19.4) thread (0.2.2) - thread_safe (0.3.5) - tilt (2.0.2) + thread_safe (0.3.6) + tilt (2.0.6) timecop (0.8.1) - tins (1.6.0) + tins (1.13.2) tzinfo (1.2.2) thread_safe (~> 0.1) unf (0.1.4) unf_ext unf_ext (0.0.7.2) uuidtools (2.1.5) - webmock (2.1.0) + webmock (2.3.2) addressable (>= 2.3.6) crack (>= 0.3.2) hashdiff @@ -279,6 +276,7 @@ GEM websocket-extensions (>= 0.1.0) websocket-extensions (0.1.2) will_paginate (3.1.0) + xmlrpc (0.3.0) xpath (2.0.0) nokogiri (~> 1.3) zipruby (0.3.6) @@ -288,11 +286,11 @@ PLATFORMS DEPENDENCIES acme-client + activesupport addressable - ago base32 bcrypt - capybara (= 2.10.1) + capybara capybara_minitest_spec certified cocaine @@ -313,9 +311,6 @@ DEPENDENCIES image_optim_pack io-extra ipaddress - jdbc-postgres - jruby-openssl - json m magic mail @@ -328,7 +323,6 @@ DEPENDENCIES paypal-recurring pg poltergeist - posix-spawn pry pry-byebug puma @@ -342,12 +336,11 @@ DEPENDENCIES rest-client rinku rmagick - ruby-debug rye sanitize sass scrypt - sequel (= 4.8.0) + sequel sequel_pg shotgun sidekiq (~> 4.1.2) @@ -363,7 +356,8 @@ DEPENDENCIES timecop webmock will_paginate + xmlrpc zipruby BUNDLED WITH - 1.13.7 + 1.14.6 diff --git a/environment.rb b/environment.rb index 43344bfd..fcc5ce93 100644 --- a/environment.rb +++ b/environment.rb @@ -12,6 +12,7 @@ Bundler.require Bundler.require :development if ENV['RACK_ENV'] == 'development' require 'tilt/erubis' +require 'active_support/core_ext/integer/time' Dir['./ext/**/*.rb'].each {|f| require f} diff --git a/ext/ago.rb b/ext/ago.rb new file mode 100644 index 00000000..de2a2335 --- /dev/null +++ b/ext/ago.rb @@ -0,0 +1,169 @@ + +module Ago + module VERSION + MAJOR = 0 + MINOR = 1 + TINY = 5 + + class << self + def pretty + "#{MAJOR}.#{MINOR}.#{TINY}" + end + alias_method :print, :pretty + end + end + + Ago::Order = [:year, :month, :week, :day, :hour, :minute, :second] + Ago::Units = { + :year => { + :basic => 60 * 60 * 24 * 365, + :gregorian => 86400 * 365.2425, + }, + :month => { + :basic => 60 * 60 * 24 * 30, + :gregorian => 86400 * 30.436875, + }, + :week => { + :basic => 60 * 60 * 24 * 7, + :gregorian => 86400 * 7.02389423076923, + }, + :day => { + :basic => 60 * 60 * 24 + }, + :hour => { + :basic => 60 * 60 + }, + :minute => { + :basic => 60 + }, + :second => { + :basic => 1 + } + } + + def Ago.calendar_check(calendar) + error = ":calendar => value must be either :basic or :gregorian." + unless calendar == :basic || calendar == :gregorian + raise ArgumentError, error + end + end + + + module Ago::TimeAgo + # Generate List of valid unit :symbols + valids = "" + Ago::Order.each do |u| + unless u == :second + valids += ":#{u.to_s}, " + else + valids += "and :#{u.to_s}." + end + end + Valids = valids + + def ago_in_words(opts={}) + # Process options {hash} + focus = opts[:focus] ? opts[:focus] : 0 + start_at = opts[:start_at] ? opts[:start_at] : :year + now = opts[:now] ? opts[:now] : Time.now + in_time = opts[:in_time] ? opts[:in_time] : :past + calendar = opts[:calendar] ? opts[:calendar] : :basic + + # Filter out invalid arguments for :in_time + in_time_error = ":in_time => value must be either :past or :future, " \ + + "depending on whether the Time object is before or after Time.now." + unless in_time == :past || in_time == :future + raise ArgumentError, in_time_error + end + + # Filter out invalid arguments for :calendar + Ago.calendar_check(calendar) + + # Filter out invalid arguments for :start_at and :focus + base_error = " => value must either be a number " + + "between 0 and 6 (inclusive),\nor one of the following " + + "symbols: " + Valids + {:focus => focus, :start_at => start_at}.each do |key, opt| + opt_error = ":" + key.to_s + base_error + if opt.class == Integer + raise ArgumentError, opt_error unless opt >= 0 && opt <= 6 + elsif opt.class == Symbol + raise ArgumentError, opt_error unless Ago::Units[opt] + else + raise ArgumentError, opt_error + end + end + + # Create Variables necessary for processing. + frags = [] + output = "" + count = 0 + + now = calendar == :basic ? now.to_i : now.to_f + my_time = calendar == :basic ? self.to_i : self.to_f + if now > my_time + diff = now - my_time + tail = " ago" + elsif my_time > now + diff = my_time - now + tail = " from now" + else + diff = 0 + tail = "just now" + end + + # Begin Ago.ago processing + Ago::Order.each do |u| + if calendar == :gregorian && Ago::Units[u][:gregorian] + value = Ago::Units[u][:gregorian] + else + value = Ago::Units[u][:basic] + end + count += 1 + + # Move further ahead in the Ago::Units array if start_at is farther back than + # the current point in the array. + if start_at.class == Integer + next if count <= start_at + elsif start_at.class == Symbol + next if Ago::Order.index(u) < Ago::Order.index(start_at) + end + + n = (diff/value).floor + if n > 0 + plural = n > 1 ? "s" : "" + frags << "#{n} #{u.to_s + plural}" + + # If the argument passed into ago() is a symbol, focus the ago statement + # down to the level specified in the symbol + if focus.class == Symbol + break if u == focus || u == :second + elsif focus.class == Fixnum + if focus == 0 || u == :second + break + else + focus -= 1 + end + end + diff -= n * value + end + end + + # Der Kommissar + frags.size.times do |n| + output += frags[n] + output += ", " unless n == frags.size - 1 + end + + return output + "#{tail}" + end + + def from_now_in_words(opts={}) + ago_in_words(opts) + end + end +end + +class Time + include Ago::TimeAgo +end diff --git a/ext/time.rb b/ext/time.rb index d76dacf3..4b777cae 100644 --- a/ext/time.rb +++ b/ext/time.rb @@ -1,7 +1,9 @@ class Time - alias_method :ago_original, :ago - def ago - ago_original.downcase.gsub('right now, this very moment.', 'just now') + self.ago_in_words end -end \ No newline at end of file + + def from_now + self.from_now_in_words + end +end diff --git a/migrations/081_stats_bigint.rb b/migrations/081_stats_bigint.rb index f8f0081b..e468fc91 100644 --- a/migrations/081_stats_bigint.rb +++ b/migrations/081_stats_bigint.rb @@ -1,8 +1,8 @@ Sequel.migration do up { alter_table(:stats) do - set_column_type :hits, Bignum - set_column_type :views, Bignum + set_column_type :hits, :bigint + set_column_type :views, :bigint end } diff --git a/migrations/082_daily_stats_bigint.rb b/migrations/082_daily_stats_bigint.rb index 5cefebed..5441c615 100644 --- a/migrations/082_daily_stats_bigint.rb +++ b/migrations/082_daily_stats_bigint.rb @@ -1,10 +1,10 @@ Sequel.migration do up { alter_table(:daily_site_stats) do - set_column_type :hits, Bignum - set_column_type :views, Bignum - set_column_type :bandwidth, Bignum - set_column_type :site_updates, Bignum + set_column_type :hits, :bigint + set_column_type :views, :bigint + set_column_type :bandwidth, :bigint + set_column_type :site_updates, :bigint end }