yudachi 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/Gemfile +4 -0
- data/README.md +35 -0
- data/Rakefile +2 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/exe/notify-yudachi +5 -0
- data/lib/yudachi.rb +9 -0
- data/lib/yudachi/cli.rb +24 -0
- data/lib/yudachi/client.rb +32 -0
- data/lib/yudachi/runner.rb +36 -0
- data/lib/yudachi/slack_notifier.rb +24 -0
- data/lib/yudachi/version.rb +3 -0
- data/yudachi.gemspec +31 -0
- metadata +101 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 9aa05eee91c1d98d419faa1f94e4ed09f8e485dd8b1af333c0df5205ae54961f
|
4
|
+
data.tar.gz: 807fbcf654c6dcd33971a6a8dda784afc8405e8e4db1ceb8598302b9760789f9
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 1ad6c2e8d76de3db4be6c6528bbe4212a4b8c1de40e7a2deac4a5d8d061cb9b0925f4846ecaf23a728ef8e39c4876190c751e4bba9aa680326e88791f5ab0843
|
7
|
+
data.tar.gz: 2a68ac29ef28992db710bece7238ad0e67e080c93c689717c645eb196bb2e821c734370877f34011e84ea08bd5acefea463d623638816eb89d03e943dec3f6f4
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
# Yudachi
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/yudachi`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'yudachi'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle install
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install yudachi
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
+
|
31
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/pocke/yudachi.
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "yudachi"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/exe/notify-yudachi
ADDED
data/lib/yudachi.rb
ADDED
data/lib/yudachi/cli.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
module Yudachi
|
2
|
+
class CLI
|
3
|
+
def initialize
|
4
|
+
@runner = Runner.new
|
5
|
+
end
|
6
|
+
|
7
|
+
def run
|
8
|
+
notifier = SlackNotifier.new(slack_url)
|
9
|
+
Runner.new.run(app_id: app_id, location_name: location_name, notifier: notifier)
|
10
|
+
end
|
11
|
+
|
12
|
+
def app_id
|
13
|
+
ENV.fetch('YUDACHI_YAHOO_JAPAN_APP_ID')
|
14
|
+
end
|
15
|
+
|
16
|
+
def location_name
|
17
|
+
ENV.fetch('YUDACHI_LOCATION')
|
18
|
+
end
|
19
|
+
|
20
|
+
def slack_url
|
21
|
+
ENV.fetch('YUDACHI_SLACK_URL')
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'cgi'
|
2
|
+
require 'open-uri'
|
3
|
+
require 'json'
|
4
|
+
|
5
|
+
module Yudachi
|
6
|
+
class Client
|
7
|
+
def initialize(app_id:)
|
8
|
+
@app_id = app_id
|
9
|
+
end
|
10
|
+
|
11
|
+
def location_name_and_coodinated_geometry(query)
|
12
|
+
url = "https://map.yahooapis.jp/geocode/V1/geoCoder?appid=#{@app_id}&output=json&query=#{CGI.escape(query)}"
|
13
|
+
response = get url
|
14
|
+
feature = response["Feature"]&.first
|
15
|
+
[feature&.dig('Name'), feature&.dig('Geometry', 'Coordinates')]
|
16
|
+
end
|
17
|
+
|
18
|
+
def rainfall(geometry)
|
19
|
+
url = "https://map.yahooapis.jp/weather/V1/place\?appid\=#{@app_id}\&output=json&coordinates\=#{geometry}&past=1&interval=5"
|
20
|
+
response = get url
|
21
|
+
features = response["Feature"]
|
22
|
+
|
23
|
+
features&.first&.dig("Property", "WeatherList", "Weather").each do |data|
|
24
|
+
data['Date'] = Time.parse(data['Date'])
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
private def get(url)
|
29
|
+
JSON.parse(URI.open(url).read)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module Yudachi
|
2
|
+
class Runner
|
3
|
+
def run(app_id:, location_name:, notifier:)
|
4
|
+
client = Client.new(app_id: app_id)
|
5
|
+
|
6
|
+
location_name, geo = client.location_name_and_coodinated_geometry(location_name)
|
7
|
+
raise "geometry not found" unless geo
|
8
|
+
datapoints = client.rainfall(geo)
|
9
|
+
raise "something wrong" unless datapoints
|
10
|
+
|
11
|
+
observation_threshold = Time.now - 30 * 60
|
12
|
+
datapoints = datapoints.select do |data|
|
13
|
+
# Drop too old data
|
14
|
+
data['Date'] > observation_threshold
|
15
|
+
end
|
16
|
+
|
17
|
+
if beginning_of_rainfall?(datapoints)
|
18
|
+
notifier.notify(datapoints: datapoints, location_name: location_name)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
private def beginning_of_rainfall?(datapoints)
|
23
|
+
# For debug
|
24
|
+
# datapoints.select { |data| data["Type"] == 'forecast'}.each do |data|
|
25
|
+
# data['Rainfall'] = rand(1.0..20.0)
|
26
|
+
# end
|
27
|
+
|
28
|
+
# Already raining
|
29
|
+
if datapoints.select { |data| data['Type'] == 'observation' }.any? { |data| data["Rainfall"] >= RAINFALL_THRESHOLD }
|
30
|
+
return false
|
31
|
+
end
|
32
|
+
|
33
|
+
datapoints.select { |data| data['Type'] == 'forecast' }.any? { |data| data['Rainfall'] >= RAINFALL_THRESHOLD }
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'slack-notifier'
|
2
|
+
|
3
|
+
module Yudachi
|
4
|
+
class SlackNotifier
|
5
|
+
def initialize(url)
|
6
|
+
@url = url
|
7
|
+
end
|
8
|
+
|
9
|
+
def notify(datapoints:, location_name:)
|
10
|
+
n = Slack::Notifier.new(@url) do
|
11
|
+
defaults username: 'Yudachi'
|
12
|
+
end
|
13
|
+
|
14
|
+
beginning_idx = datapoints.find_index { |data| data['Type'] == 'forecast' && data['Rainfall'] >= RAINFALL_THRESHOLD }
|
15
|
+
beginning = datapoints[beginning_idx]
|
16
|
+
title = "#{location_name} で#{beginning["Date"].strftime("%H時%M分")}から雨が降り出しそうです!"
|
17
|
+
graph = datapoints[beginning_idx..].map { |data| data['Date'].strftime('%H:%M: ') + 'x' * data['Rainfall'].ceil }.join("\n")
|
18
|
+
|
19
|
+
n.ping attachments: [
|
20
|
+
{ title: title, text: graph, color: 'danger' },
|
21
|
+
]
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
data/yudachi.gemspec
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
require_relative 'lib/yudachi/version'
|
2
|
+
|
3
|
+
Gem::Specification.new do |spec|
|
4
|
+
spec.name = "yudachi"
|
5
|
+
spec.version = Yudachi::VERSION
|
6
|
+
spec.authors = ["Masataka Pocke Kuwabara"]
|
7
|
+
spec.email = ["kuwabara@pocke.me"]
|
8
|
+
|
9
|
+
spec.summary = %q{TBD}
|
10
|
+
spec.description = %q{TBD}
|
11
|
+
spec.homepage = "https://github.com/pocke/yudachi"
|
12
|
+
|
13
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
14
|
+
spec.metadata["source_code_uri"] = spec.homepage
|
15
|
+
# spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
|
16
|
+
|
17
|
+
# Specify which files should be added to the gem when it is released.
|
18
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
19
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
20
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
21
|
+
end
|
22
|
+
spec.bindir = "exe"
|
23
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
24
|
+
spec.require_paths = ["lib"]
|
25
|
+
|
26
|
+
spec.required_ruby_version = '>= 2.6'
|
27
|
+
|
28
|
+
spec.add_runtime_dependency "slack-notifier"
|
29
|
+
spec.add_development_dependency "bundler"
|
30
|
+
spec.add_development_dependency "rake"
|
31
|
+
end
|
metadata
ADDED
@@ -0,0 +1,101 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: yudachi
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Masataka Pocke Kuwabara
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-07-30 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: slack-notifier
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: TBD
|
56
|
+
email:
|
57
|
+
- kuwabara@pocke.me
|
58
|
+
executables:
|
59
|
+
- notify-yudachi
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- ".gitignore"
|
64
|
+
- Gemfile
|
65
|
+
- README.md
|
66
|
+
- Rakefile
|
67
|
+
- bin/console
|
68
|
+
- bin/setup
|
69
|
+
- exe/notify-yudachi
|
70
|
+
- lib/yudachi.rb
|
71
|
+
- lib/yudachi/cli.rb
|
72
|
+
- lib/yudachi/client.rb
|
73
|
+
- lib/yudachi/runner.rb
|
74
|
+
- lib/yudachi/slack_notifier.rb
|
75
|
+
- lib/yudachi/version.rb
|
76
|
+
- yudachi.gemspec
|
77
|
+
homepage: https://github.com/pocke/yudachi
|
78
|
+
licenses: []
|
79
|
+
metadata:
|
80
|
+
homepage_uri: https://github.com/pocke/yudachi
|
81
|
+
source_code_uri: https://github.com/pocke/yudachi
|
82
|
+
post_install_message:
|
83
|
+
rdoc_options: []
|
84
|
+
require_paths:
|
85
|
+
- lib
|
86
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
87
|
+
requirements:
|
88
|
+
- - ">="
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '2.6'
|
91
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
92
|
+
requirements:
|
93
|
+
- - ">="
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
version: '0'
|
96
|
+
requirements: []
|
97
|
+
rubygems_version: 3.0.3
|
98
|
+
signing_key:
|
99
|
+
specification_version: 4
|
100
|
+
summary: TBD
|
101
|
+
test_files: []
|