From 788853c7a0aea41303089989c3b6d8e3353e8c96 Mon Sep 17 00:00:00 2001 From: Matteo Vitali Date: Sat, 6 Feb 2021 16:43:28 +0100 Subject: [PATCH 1/2] Add find iss method --- .env_tmpl | 0 .rake_tasks~ | 3 +++ Gemfile | 2 ++ Gemfile.lock | 9 +++++++ config/locales/it.yml | 2 ++ lib/bot.rb | 2 -- lib/fortuna_luca/iss/when.rb | 41 +++++++++++++++++++++++++++++++ test/fixtures/iss_when.json | 20 +++++++++++++++ test/fortuna_luca/iss/test_iss.rb | 36 +++++++++++++++++++++++++++ test/test_helper.rb | 5 ++++ 10 files changed, 118 insertions(+), 2 deletions(-) create mode 100644 .env_tmpl create mode 100644 .rake_tasks~ create mode 100644 lib/fortuna_luca/iss/when.rb create mode 100644 test/fixtures/iss_when.json create mode 100644 test/fortuna_luca/iss/test_iss.rb diff --git a/.env_tmpl b/.env_tmpl new file mode 100644 index 0000000..e69de29 diff --git a/.rake_tasks~ b/.rake_tasks~ new file mode 100644 index 0000000..ef92d95 --- /dev/null +++ b/.rake_tasks~ @@ -0,0 +1,3 @@ +default +send_last_tweets_media +test diff --git a/Gemfile b/Gemfile index 86fda3f..6f7f9c5 100644 --- a/Gemfile +++ b/Gemfile @@ -24,7 +24,9 @@ group :development do gem 'minitest-reporters' gem 'minitest' gem 'mocha' + gem 'pry' gem 'rack-test' gem 'stackprof' + gem 'tzinfo' gem 'webmock' end diff --git a/Gemfile.lock b/Gemfile.lock index 2648097..a10a7c6 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -26,6 +26,7 @@ GEM buftok (0.2.0) builder (3.2.4) chronic (0.10.2) + coderay (1.1.3) coercible (1.0.0) descendants_tracker (~> 0.0.1) concurrent-ruby (1.1.8) @@ -101,6 +102,7 @@ GEM memoizable (0.4.2) thread_safe (~> 0.3, >= 0.3.1) memory_profiler (0.9.14) + method_source (1.0.0) mime-types (3.3.1) mime-types-data (~> 3.2015) mime-types-data (3.2020.1104) @@ -120,6 +122,9 @@ GEM naught (1.1.0) nio4r (2.5.4) os (0.9.6) + pry (0.13.1) + coderay (~> 1.1) + method_source (~> 1.0) public_suffix (4.0.6) puma (4.3.6) nio4r (~> 2.0) @@ -166,6 +171,8 @@ GEM simple_oauth (~> 0.3.1) twitter-text (1.13.4) unf (~> 0.1.0) + tzinfo (2.0.4) + concurrent-ruby (~> 1.0) uber (0.0.15) unf (0.1.4) unf_ext @@ -197,6 +204,7 @@ DEPENDENCIES minitest minitest-reporters mocha + pry puma (~> 4.3.0) rack (~> 2.2.3) rack-test @@ -206,6 +214,7 @@ DEPENDENCIES telegram-bot-ruby (~> 0.11.0) twitter (~> 6.0.0) twitter-text (~> 1.13.3) + tzinfo webmock whenever (~> 0.9.4) diff --git a/config/locales/it.yml b/config/locales/it.yml index 7a34862..273d4c3 100644 --- a/config/locales/it.yml +++ b/config/locales/it.yml @@ -12,3 +12,5 @@ it: between_hours: "tra le %{start} e le %{end}" good_for_ride: "fa uscire in bici" not_good_for_ride: "%{when} a %{where} non fa uscire in bici" + iss: + pass_time: "passa il %{date} alle %{hour} ed è visibile per %{duration_min} minuti" diff --git a/lib/bot.rb b/lib/bot.rb index f826e9b..75f41fe 100644 --- a/lib/bot.rb +++ b/lib/bot.rb @@ -61,8 +61,6 @@ def handle_message(message) # get the first web search result link when /^\/google (.+)/i # /google send_message(message.chat.id, WebSearcher.new($1).first_link) - when /^\/meteops/i - send_message(message.chat.id, "http://trottomv.suroot.com/meteo#{Time.now.strftime("%Y%m%d")}.png") # default: try AI to generate some response else text = text.sub(/\A\/\S* /, "") # remove /command part diff --git a/lib/fortuna_luca/iss/when.rb b/lib/fortuna_luca/iss/when.rb new file mode 100644 index 0000000..81f1c25 --- /dev/null +++ b/lib/fortuna_luca/iss/when.rb @@ -0,0 +1,41 @@ +# frozen_string_literal: true + +require "i18n" +require "httpclient" +require "json" +require_relative "../../logging" + +module FortunaLuca + module Iss + class When + include Logging + + DEFAULT_LAT = 43.8789729 + DEFAULT_LON = 12.9601665 + DEFAULT_NUMBER = 5 + + def initialize(lat = DEFAULT_LAT, lon = DEFAULT_LON, number = DEFAULT_NUMBER) + @api_url = "http://api.open-notify.org/iss-pass.json?lat=#{lat}&lon=#{lon}&n=#{number}" + end + + def call + result = HTTPClient.get(@api_url) + response = JSON.parse(result.body)["response"][0] + message(response) + rescue JSON::ParserError => e + Logging.logger.error "Iss JSON parse error: #{e.message}" + nil + end + + private + + def message(response) + duration_min = response["duration"] / 60 + date_time = Time.at(response["risetime"]).getlocal + date = date_time.strftime("%d/%m") + hour = date_time.strftime("%H:%M") + I18n.t('iss.pass_time', date: date, hour: hour, duration_min: duration_min) + end + end + end +end diff --git a/test/fixtures/iss_when.json b/test/fixtures/iss_when.json new file mode 100644 index 0000000..c24dc23 --- /dev/null +++ b/test/fixtures/iss_when.json @@ -0,0 +1,20 @@ +{ + "message": "success", + "request": { + "altitude": 100, + "datetime": 1612635634, + "latitude": 43.8789729, + "longitude": 12.9601665, + "passes": 3 + }, + "response": [ + { + "duration": 384, + "risetime": 1612637271 + }, + { + "duration": 274, + "risetime": 1612686050 + } + ] +} \ No newline at end of file diff --git a/test/fortuna_luca/iss/test_iss.rb b/test/fortuna_luca/iss/test_iss.rb new file mode 100644 index 0000000..1b2368b --- /dev/null +++ b/test/fortuna_luca/iss/test_iss.rb @@ -0,0 +1,36 @@ +require_relative "../../test_helper" +require_relative "../../../lib/fortuna_luca/iss/when" + +describe FortunaLuca::Iss::When do + let(:instance) { FortunaLuca::Iss::When.new() } + let(:stubbed_response) { HTTP::Message.new_response(data) } + + describe "#call" do + before do + HTTPClient.stubs(:get).with( + "http://api.open-notify.org/iss-pass.json?lat=43.8789729&lon=12.9601665&n=5", + ).returns(stubbed_response) + end + + describe "with valid data returned" do + let(:data) do + File.read(File.dirname(__FILE__) + '/../../fixtures/iss_when.json') + end + + it "returns the Iss when" do + instance.call().must_equal( + "passa il 06/02 alle 19:47 ed è visibile per 6 minuti" + ) + end + end + + describe "with invalid JSON data returned" do + let(:data) { "" } + + it "returns nil" do + instance.call().must_equal(nil) + end + end + + end +end diff --git a/test/test_helper.rb b/test/test_helper.rb index c309c78..2375210 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -1,7 +1,10 @@ +require "logger" require "minitest/autorun" require "mocha/minitest" require "minitest/reporters" +require "pry" require "rack/test" +require 'tzinfo' require "webmock/minitest" require_relative "../config/i18n" @@ -10,3 +13,5 @@ ENV["TELEGRAM_BOT_NAME"] = "@fortuna_luca" Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new + +timezone_name = 'EU/Rome' \ No newline at end of file From 5ebe3e3c032be763aa536ad4b43575e2a6ec516c Mon Sep 17 00:00:00 2001 From: Matteo Vitali Date: Sun, 7 Feb 2021 09:30:30 +0100 Subject: [PATCH 2/2] Fix timezone iss --- Gemfile | 2 +- Gemfile.lock | 5 ++--- lib/fortuna_luca/iss/when.rb | 10 +++++++--- test/fixtures/iss_when.json | 2 +- test/test_helper.rb | 4 +--- 5 files changed, 12 insertions(+), 11 deletions(-) diff --git a/Gemfile b/Gemfile index 6f7f9c5..e7cc6a7 100644 --- a/Gemfile +++ b/Gemfile @@ -17,6 +17,7 @@ gem 'sinatra', '~> 2.1.0' gem 'telegram-bot-ruby', '~> 0.11.0' gem 'twitter-text', '~> 1.13.3' gem 'twitter', '~> 6.0.0' +gem 'timezone_finder', '~> 1.5.7' gem 'whenever', '~> 0.9.4', require: false group :development do @@ -27,6 +28,5 @@ group :development do gem 'pry' gem 'rack-test' gem 'stackprof' - gem 'tzinfo' gem 'webmock' end diff --git a/Gemfile.lock b/Gemfile.lock index a10a7c6..65cb5c3 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -159,6 +159,7 @@ GEM thor (1.0.1) thread_safe (0.3.6) tilt (2.0.10) + timezone_finder (1.5.7) twitter (6.0.0) addressable (~> 2.5) buftok (~> 0.2.0) @@ -171,8 +172,6 @@ GEM simple_oauth (~> 0.3.1) twitter-text (1.13.4) unf (~> 0.1.0) - tzinfo (2.0.4) - concurrent-ruby (~> 1.0) uber (0.0.15) unf (0.1.4) unf_ext @@ -212,9 +211,9 @@ DEPENDENCIES sinatra (~> 2.1.0) stackprof telegram-bot-ruby (~> 0.11.0) + timezone_finder (~> 1.5.7) twitter (~> 6.0.0) twitter-text (~> 1.13.3) - tzinfo webmock whenever (~> 0.9.4) diff --git a/lib/fortuna_luca/iss/when.rb b/lib/fortuna_luca/iss/when.rb index 81f1c25..f2c48f3 100644 --- a/lib/fortuna_luca/iss/when.rb +++ b/lib/fortuna_luca/iss/when.rb @@ -3,6 +3,7 @@ require "i18n" require "httpclient" require "json" +require "timezone_finder" require_relative "../../logging" module FortunaLuca @@ -21,7 +22,8 @@ def initialize(lat = DEFAULT_LAT, lon = DEFAULT_LON, number = DEFAULT_NUMBER) def call result = HTTPClient.get(@api_url) response = JSON.parse(result.body)["response"][0] - message(response) + request = JSON.parse(result.body)["request"] + message(response, request) rescue JSON::ParserError => e Logging.logger.error "Iss JSON parse error: #{e.message}" nil @@ -29,11 +31,13 @@ def call private - def message(response) + def message(response, request) + ENV["TZ"] = TimezoneFinder.create.timezone_at(lng: request["longitude"], lat: request["latitude"]) duration_min = response["duration"] / 60 - date_time = Time.at(response["risetime"]).getlocal + date_time = Time.at(response["risetime"]) date = date_time.strftime("%d/%m") hour = date_time.strftime("%H:%M") + ENV["TZ"] = nil I18n.t('iss.pass_time', date: date, hour: hour, duration_min: duration_min) end end diff --git a/test/fixtures/iss_when.json b/test/fixtures/iss_when.json index c24dc23..9a19b87 100644 --- a/test/fixtures/iss_when.json +++ b/test/fixtures/iss_when.json @@ -5,7 +5,7 @@ "datetime": 1612635634, "latitude": 43.8789729, "longitude": 12.9601665, - "passes": 3 + "passes": 2 }, "response": [ { diff --git a/test/test_helper.rb b/test/test_helper.rb index 2375210..ca14151 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -4,14 +4,12 @@ require "minitest/reporters" require "pry" require "rack/test" -require 'tzinfo' require "webmock/minitest" require_relative "../config/i18n" ENV["RACK_ENV"] = "test" ENV["SECRET_WEBHOOK_PATH"] = "/the-secret-path" ENV["TELEGRAM_BOT_NAME"] = "@fortuna_luca" +ENV["TZ"] = "Europe/Rome" Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new - -timezone_name = 'EU/Rome' \ No newline at end of file