ruboty-livedoor_weather 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: da6520490b0f16287cfb7cc58d3376f5529ac88a
4
+ data.tar.gz: 2b488e9828d97369488ebda73c1a43cbec378f41
5
+ SHA512:
6
+ metadata.gz: 05a4201ad60484e1115f5aa5910c215291163b8580da5d00241829e4918718a6637d2d2b5cfa43a5b9ccf44b02cad8629f4a822bf4124ff2da7e6fa429a5e1dd
7
+ data.tar.gz: 5e0d88ff055152b4efdbb5ab56013f5fedff28e2710ae4bec682b8845cadd4555f45e8fd952295e929d7db7203f4a70a380b01a23e89b9d28cf983ed772721f9
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ /vendor/bundle
12
+ .DS_Store
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.0
5
+ before_install: gem install bundler -v 1.13.7
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ruboty-livedoor_weather.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 SHIOYA, Hiromu
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,34 @@
1
+ # Ruboty::LivedoorWeather
2
+
3
+ Ruboty handler to show weather forecast from [Weather Hacks](http://weather.livedoor.com/weather_hacks/) by Livedoor.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your ruboty's Gemfile:
8
+
9
+ ```
10
+ gem 'ruboty-livedoor_weather'
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ Talk to your ruboty:
16
+
17
+ ```
18
+ ruboty livedoor_weather
19
+ ```
20
+
21
+ Then shows forecasts specified by `ENV['LDW_CITY']` (default : '東京').
22
+
23
+ You can specify city to show forecast:
24
+
25
+ ```
26
+ ruboty livedoor_weather 種子島
27
+ ```
28
+
29
+ List of cities is [here](http://weather.livedoor.com/forecast/rss/primary_area.xml).
30
+
31
+ ## License
32
+
33
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
34
+
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "ruboty/livedoor_weather"
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
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,15 @@
1
+ require 'json'
2
+ require 'nokogiri'
3
+ require 'rest-client'
4
+
5
+ require 'ruboty'
6
+ require 'ruboty/livedoor_weather/version'
7
+ require 'ruboty/livedoor_weather/handlers/livedoor_weather'
8
+ require 'ruboty/livedoor_weather/client'
9
+
10
+ module Ruboty
11
+ module LivedoorWeather
12
+ ENDPOINT = 'http://weather.livedoor.com/forecast/webservice/json/v1'
13
+ PRIMARY_AREA = 'http://weather.livedoor.com/forecast/rss/primary_area.xml'
14
+ end
15
+ end
@@ -0,0 +1,75 @@
1
+ module Ruboty::LivedoorWeather
2
+ class Client
3
+ def initialize(message)
4
+ @city = city(message)
5
+ @locations = locations(message)
6
+ @brain = message.robot.brain
7
+ @primary_area = retrieve_primary_area
8
+ end
9
+
10
+ def report
11
+ request
12
+ end
13
+
14
+ private
15
+
16
+ def request
17
+ cc = city_code
18
+ return "*#{@city}* の予報は見当たりません。" if cc.nil?
19
+
20
+ response = RestClient.get(ENDPOINT, params: { city: cc })
21
+ json = JSON.parse(response)
22
+
23
+ p json['forecasts']
24
+ forecasts = format_forecasts(json['forecasts'])
25
+
26
+ "*#{@city}* の天気予報です。\n#{forecasts}\n\n#{json['description']['text']}"
27
+ end
28
+
29
+ private
30
+
31
+ def city(message)
32
+ message[:city] || ENV['LDW_CITY'] || '東京'
33
+ end
34
+
35
+ def locations(message)
36
+ (message[:locations] || ENV['LDW_LOCATIONS'] || '').strip.split(/\s/)
37
+ end
38
+
39
+ def city_code
40
+ city = @brain.data[:ldw_primary_area].find { |c| c['title'] == @city }
41
+ city&.fetch('id')
42
+ end
43
+
44
+ def format_forecasts(forecasts)
45
+ forecasts.each_with_object([]) { |forecast, r|
46
+ r.push "#{forecast['dateLabel']} #{forecast['telop']} #{format_temperature(forecast['temperature'])}"
47
+ }.join("\n")
48
+ end
49
+
50
+ def format_temperature(temp, unit: 'celsius')
51
+ min = temp['min']&.fetch(unit) || '-'
52
+ max = temp['max']&.fetch(unit) || '-'
53
+ "気温:#{min}/#{max} #{unit == 'celsius' ? 'C' : 'F' }"
54
+ end
55
+
56
+ def retrieve_primary_area
57
+ # cache 1.day
58
+ expire = @brain.data[:ldw_primary_area_expire] || 1.day.ago
59
+ @brain.data[:ldw_primary_area] = nil if Time.now > expire
60
+
61
+ @brain.data[:ldw_primary_area] ||= Array.new.tap do |r|
62
+ response = RestClient.get(PRIMARY_AREA)
63
+ xml = Nokogiri::XML(response)
64
+ xml.xpath('//ldWeather:source//pref').each do |pref|
65
+ pref.xpath('city').each do |city|
66
+ c = { 'pref' => pref.attr('title') }
67
+ city.attributes.each { |k, _| c[k] = city.attr(k) }
68
+ r.push c
69
+ end
70
+ end
71
+ @brain.data[:ldw_primary_area_expire] = 1.day.since
72
+ end
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,10 @@
1
+ module Ruboty::Handlers
2
+ class LivedoorWeather < Base
3
+ on(/livedoor_weather(\s(?<city>\S+)(\s(?<locations>.+\Z)|\Z)|\Z)/, name: 'livedoor_weather', description: 'Show weather report from Livedoor Weather')
4
+
5
+ def livedoor_weather(message)
6
+ client = Ruboty::LivedoorWeather::Client.new(message)
7
+ message.reply(client.report)
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,5 @@
1
+ module Ruboty
2
+ module LivedoorWeather
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'ruboty/livedoor_weather/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'ruboty-livedoor_weather'
8
+ spec.version = Ruboty::LivedoorWeather::VERSION
9
+ spec.authors = ['SHIOYA, Hiromu']
10
+ spec.email = ['kwappa.856@gmail.com']
11
+
12
+ spec.summary = 'Ruboty handler to show Livedoor Weather'
13
+ spec.description = spec.summary
14
+ spec.homepage = 'https://github.com/kwappa/ruboty-livedoor_weather'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = 'exe'
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ['lib']
23
+
24
+ spec.add_dependency 'nokogiri'
25
+ spec.add_dependency 'rest-client'
26
+
27
+ spec.add_development_dependency 'bundler', '~> 1.13'
28
+ spec.add_development_dependency 'rake', '~> 12.0'
29
+ spec.add_development_dependency 'minitest', '~> 5.0'
30
+ spec.add_development_dependency 'pry-byebug'
31
+ end
metadata ADDED
@@ -0,0 +1,141 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruboty-livedoor_weather
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - SHIOYA, Hiromu
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-01-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: nokogiri
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: rest-client
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
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: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.13'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.13'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '12.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '12.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: minitest
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '5.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '5.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: pry-byebug
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: Ruboty handler to show Livedoor Weather
98
+ email:
99
+ - kwappa.856@gmail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - ".travis.yml"
106
+ - Gemfile
107
+ - LICENSE.txt
108
+ - README.md
109
+ - Rakefile
110
+ - bin/console
111
+ - bin/setup
112
+ - lib/ruboty/livedoor_weather.rb
113
+ - lib/ruboty/livedoor_weather/client.rb
114
+ - lib/ruboty/livedoor_weather/handlers/livedoor_weather.rb
115
+ - lib/ruboty/livedoor_weather/version.rb
116
+ - ruboty-livedoor_weather.gemspec
117
+ homepage: https://github.com/kwappa/ruboty-livedoor_weather
118
+ licenses:
119
+ - MIT
120
+ metadata: {}
121
+ post_install_message:
122
+ rdoc_options: []
123
+ require_paths:
124
+ - lib
125
+ required_ruby_version: !ruby/object:Gem::Requirement
126
+ requirements:
127
+ - - ">="
128
+ - !ruby/object:Gem::Version
129
+ version: '0'
130
+ required_rubygems_version: !ruby/object:Gem::Requirement
131
+ requirements:
132
+ - - ">="
133
+ - !ruby/object:Gem::Version
134
+ version: '0'
135
+ requirements: []
136
+ rubyforge_project:
137
+ rubygems_version: 2.6.8
138
+ signing_key:
139
+ specification_version: 4
140
+ summary: Ruboty handler to show Livedoor Weather
141
+ test_files: []