From 061f3f4e0573a24fda77e8fcac0321c390c47a76 Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Wed, 1 Jan 2020 01:17:44 -0500 Subject: [PATCH] Ruby 2.6 or give me :skull: --- .gitignore | 1 + .rubocop.yml | 6 ++++++ .rubocop_todo.yml | 2 +- cpi_fetcher.rb | 30 +++++++++++++++--------------- mini_s3put.rb | 15 ++++++++------- 5 files changed, 31 insertions(+), 23 deletions(-) diff --git a/.gitignore b/.gitignore index 02c1928..5f91bce 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ .*env *.csv +.ruby-version diff --git a/.rubocop.yml b/.rubocop.yml index 60da610..d6fa38e 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -5,3 +5,9 @@ AllCops: Style/Documentation: Enabled: false + +Style/AccessModifierDeclarations: + EnforcedStyle: inline + +Layout/MultilineMethodCallIndentation: + EnforcedStyle: indented diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 456ee5d..f94de40 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -1,6 +1,6 @@ # This configuration was generated by # `rubocop --auto-gen-config` -# on 2019-12-31 10:11:37 -0500 using RuboCop version 0.78.0. +# on 2020-01-01 01:01:20 -0500 using RuboCop version 0.78.0. # The point is for the user to remove these configuration records # one by one as the offenses are removed from the code base. # Note that changes in the inspected code, or installation of new diff --git a/cpi_fetcher.rb b/cpi_fetcher.rb index 8efb05b..069f48d 100644 --- a/cpi_fetcher.rb +++ b/cpi_fetcher.rb @@ -12,6 +12,7 @@ class CPIFetcher ) private_constant :DEFAULT_CPI_SERIES_URL attr_reader :url, :bls_token + private :url, :bls_token def initialize @url = URI(ENV['CPI_SERIES_URL'] || DEFAULT_CPI_SERIES_URL) @@ -20,28 +21,27 @@ class CPIFetcher def cpi resp = fetch_raw_response - return nil unless resp['Results']['series'] - data = resp['Results']['series'].first['data'] - data.sort! { |a, b| a['year'] <=> b['year'] } - data.last['value'] + last_cpi_value(resp['Results']['series'].first['data']) end - private + private def last_cpi_value(data) + data + .sort { |a, b| a['year'] <=> b['year'] } + .then { |d| d.last['value'] } + end - def fetch_raw_response - http = Net::HTTP.new(url.hostname, url.port) - http.use_ssl = true - http.verify_mode = OpenSSL::SSL::VERIFY_PEER - res = http.request(build_request) - JSON.parse(res.body) + private def fetch_raw_response + Net::HTTP.new(url.hostname, url.port) + .tap { |h| h.use_ssl = true } + .tap { |h| h.verify_mode = OpenSSL::SSL::VERIFY_PEER } + .then { |h| JSON.parse(h.request(build_request).body) } end - def build_request - req = Net::HTTP::Get.new(url) - req['Authorization'] = "token #{bls_token}" - req + private def build_request + Net::HTTP::Get.new(url) + .tap { |r| r['Authorization'] = "token #{bls_token}" } end end diff --git a/mini_s3put.rb b/mini_s3put.rb index 27f4698..cbc6b14 100644 --- a/mini_s3put.rb +++ b/mini_s3put.rb @@ -9,14 +9,16 @@ class MiniS3put @instream = instream end + attr_reader :bucket, :key, :instream + private :bucket, :key, :instream + def put Aws::S3::Resource.new.bucket(bucket).object(key).put( body: instream.read ).on_success(&method(:on_put_success)) - 0 end - def on_put_success(response) + private def on_put_success(response) puts response.data Aws::S3::Client.new.put_object_acl( @@ -25,10 +27,9 @@ class MiniS3put puts acl_response.data end end - - private - - attr_reader :bucket, :key, :instream end -exit(MiniS3put.new(key: ARGV.first).put) if $PROGRAM_NAME == __FILE__ +if $PROGRAM_NAME == __FILE__ + MiniS3put.new(key: ARGV.first).put + exit 0 +end