Skip to content

Commit

Permalink
Add coop command
Browse files Browse the repository at this point in the history
  • Loading branch information
fujiwara-e committed May 13, 2024
1 parent 958b60d commit 452a3ef
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 0 deletions.
46 changes: 46 additions & 0 deletions lib/swimmy/command/coop.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
require 'rexml/document'
require 'pp'

module Swimmy
module Command
class Coop < Swimmy::Command::Base
class ShopInfoFormatter
def format(shop)
result = ""
result += (shop.name + "\t" + shop.time + "\n")
return result
end
end #ShopInfoFormatter

command "coop" do |client, data, match|
shopinfo = Swimmy::Service::Coop.new.get_shopinfolist("https://vsign.jp/okadai/maruco/shops")
script = ""
case match[:expression]
when "open"
shopinfo.each do |shop|
if shop.open? == true
script += ShopInfoFormatter.new.format(shop)
end
end
client.say(channel: data.channel, text: script)
when "time"
shopinfo.each do |shop|
script += ShopInfoFormatter.new.format(shop)
end
client.say(channel: data.channel, text: script)
else script = "入力が間違っています\n" +
"swimmy help coop と入力してコマンドを確認してください"
client.say(channel: data.channel, text: script)
end
end #command

help do
title "coop"
desc "生協の情報を表示する"
long_desc "coop open - 空いているショップを表示する\n" +
"coop time - ショップごとの営業時間を表示する"

end #help
end #class Coop
end #module Command
end #module Swimmy
1 change: 1 addition & 0 deletions lib/swimmy/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ module Resource
autoload :Event, "#{dir}/event.rb"
autoload :Pollen, "#{dir}/pollen_resource.rb"
autoload :OpenhabMetadata, "#{dir}/openhab_metadata.rb"
autoload :CoopInfo, "#{dir}/coop_info.rb"
end
end
29 changes: 29 additions & 0 deletions lib/swimmy/resource/coop_info.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module Swimmy
module Resource
class ShopInfo
attr_accessor :f_name,:state,:n_name,:time,:ex

def initialize(f_name,state,n_name,time,ex)
@f_name, @state, @n_name ,@time ,@ex = f_name, state, n_name, time, ex
end

def open?
if @state == "OPEN"
return true
end
end

def name
s = @f_name.chomp
if s == @n_name
return s
end
return s +"/"+ @n_name
end

def time
return @time
end
end #class ShopInfo
end #module Resouce
end #module Swimmy
2 changes: 2 additions & 0 deletions lib/swimmy/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,7 @@ module Service
autoload :CityCode, "#{dir}/city_code.rb"
autoload :Openhab, "#{dir}/openhab.rb"
autoload :Newsapi, "#{dir}/newsapi.rb"
autoload :Coop, "#{dir}/coop_service.rb"

end
end
38 changes: 38 additions & 0 deletions lib/swimmy/service/coop_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
require 'open-uri'
require 'nokogiri'

module Swimmy
module Service
class Coop
def get_shopinfolist(url)
html = Nokogiri::HTML.parse(URI.open(url))
shops = []
shopdata = []
html.xpath('//h3[@class="bg-title-gray"]').each do |char|
i = 0
d = 1
f_name = char.text
f_name.slice!(0) #先頭の\nを削除
char.next_element.search('//span[@class = "text"]')
text = char.next_element.text
text = text.gsub(/\s+/,"\n")
text = text.gsub(/\n{2,}/,"\n")
text.slice!(0)
text.lines(chomp: true) do |char|
if d >4
shops << Swimmy::Resource::ShopInfo.new(f_name,shopdata[0],shopdata[1],shopdata[2],shopdata[3])
d = 1
i = 0
end
shopdata[i]= char
d = d + 1
i = i + 1
end
shops << Swimmy::Resource::ShopInfo.new(f_name,shopdata[0],shopdata[1],shopdata[2],shopdata[3])
i = i + 1
end
return shops
end
end
end
end

0 comments on commit 452a3ef

Please sign in to comment.