Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add find iss method #14

Open
wants to merge 2 commits into
base: staging
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added .env_tmpl
Empty file.
3 changes: 3 additions & 0 deletions .rake_tasks~
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
default
send_last_tweets_media
test
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ 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
gem 'derailed_benchmarks'
gem 'minitest-reporters'
gem 'minitest'
gem 'mocha'
gem 'pry'
gem 'rack-test'
gem 'stackprof'
gem 'webmock'
Expand Down
8 changes: 8 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -154,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)
Expand Down Expand Up @@ -197,13 +203,15 @@ DEPENDENCIES
minitest
minitest-reporters
mocha
pry
puma (~> 4.3.0)
rack (~> 2.2.3)
rack-test
rake
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)
webmock
Expand Down
2 changes: 2 additions & 0 deletions config/locales/it.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
2 changes: 0 additions & 2 deletions lib/bot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
45 changes: 45 additions & 0 deletions lib/fortuna_luca/iss/when.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# frozen_string_literal: true

require "i18n"
require "httpclient"
require "json"
require "timezone_finder"
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]
request = JSON.parse(result.body)["request"]
message(response, request)
rescue JSON::ParserError => e
Logging.logger.error "Iss JSON parse error: #{e.message}"
nil
end

private

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"])
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
end
end
20 changes: 20 additions & 0 deletions test/fixtures/iss_when.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"message": "success",
"request": {
"altitude": 100,
"datetime": 1612635634,
"latitude": 43.8789729,
"longitude": 12.9601665,
"passes": 2
},
"response": [
{
"duration": 384,
"risetime": 1612637271
},
{
"duration": 274,
"risetime": 1612686050
}
]
}
36 changes: 36 additions & 0 deletions test/fortuna_luca/iss/test_iss.rb
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
require "logger"
require "minitest/autorun"
require "mocha/minitest"
require "minitest/reporters"
require "pry"
require "rack/test"
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