[For beginners] How to get the Ruby delayed railway line name

I'm studying ruby right now. I will write it here as a record.

Program overview

  1. Get the delayed railway information from Json of railway delay information
  2. Ask them to enter the route name you want to check if it is delayed
  3. Based on the text information, search if there is a corresponding route in the obtained json information
  4. If there is a corresponding route, output delay information ant. If not, output as not applicable


require 'open-uri'
require 'nokogiri'  #Gem for scraping
require 'json'  #Call to use json

#Parse the url
url = 'http://tetsudo.rti-giken.jp/free/delay.json'
html = open(url).read  
doc = Nokogiri::HTML(html, url)

#Parse to Json format and convert to hash
hash = JSON.parse doc.css("body").inner_text

#Enter the route name you want to check as standard and input_Store in train
puts "Please enter the route for which you want to know the delay status.Example: Keihin Tohoku Line"
input_train = gets.chomp

# delayed_prepare trains array variable
delayed_trains = Array.new
# input_Delayed route name containing train string and updated date / time delayed_Store in trains
hash.each {|train| delayed_train << train["name"] + \
            '(Update date and time: ' + Time.at(train["lastupdate_gmt"], in: "+09:00").strftime("%Y-%m-%d %H:%M:%S").to_s + ')' \
            if train["name"].include? input_train }

#If the array is empty, there is no route to be delayed, otherwise the route name is output.
if delayed_trains.empty?
  puts "Send me "#{input_train}There is no delay in the routes that include the characters"
  puts delayed_trains.join(",") + "Seems to be delayed\n Please see the railway company website for details."

There are some articles that have created parrot return with LINE BOT, but if you refer to it and put in this algorithm, I think that you can make LINE BOT that will answer if you ask the route you want to ask if it is delayed. ..

After all, I am studying and motivated to make what I make into a shape.

Recommended Posts

[For beginners] How to get the Ruby delayed railway line name
[Ruby] How to use slice for beginners
[For beginners] How to implement the delete function
Rails / Ruby: How to get HTML text for Mail
[Ruby on Rails] How to change the column name
[Ruby] How to get the tens place and the ones place
How to get the class name / method name running in Java
[IOS] How to get the table name from AWS DynamoDB
[For Ruby beginners] Explain how to freely delete array elements!
How to get the contents of Map using for statement Memorandum
How to get date data in Ruby
[Java] How to get the current directory
How to get the date in java
[For beginners] How to debug in Eclipse
How to build a Ruby on Rails environment using Docker (for Docker beginners)
[For Rails beginners] Summary of how to use RSpec (get an overview)
How to find the cause of the Ruby error
[Swift] How to implement the LINE login function
How to get today's day of the week
[Java] How to get the redirected final URL
To get Ruby Silver at the last minute
How to build the simplest blockchain in Ruby
[For super beginners] How to use autofocus: true
[Java] How to get the authority of the folder
How to implement Pagination in GraphQL (for ruby)
I want to get the value in Ruby
[Ruby basics] How to use the slice method
graphql-ruby: How to get the name of query or mutation in controller Note
[Java] How to get the URL of the transition source
How to implement login request processing (Rails / for beginners)
Get the anime name for this term by scraping
How to make LINE messaging function made with Ruby
[Ruby On Rails] How to use simple_format to display the entered text with line breaks
[Java] How to extract the file name from the path
[Ruby] How to find the sum of each digit
[Ruby] Method to easily get the receiver type .class
[For beginners] How to operate Stream API after Java 8
[Java] How to get the maximum value of HashMap
How to get the class name of the argument of LoggerFactory.getLogger when using SLF4J in Java
[Rails] How to change the column name of the table
[Android] How to get the setting language of the terminal
Ruby, Nokogiri: Get the element name of the selected node
[Rails] How to get the contents of strong parameters
How to make a mod for Slay the Spire
[Swift] How to get the document ID of Firebase
Explanation of Ruby on rails for beginners ④ ~ Naming convention and how to use form_Tag ~
How to use GitHub for super beginners (team development)
How to study kotlin for the first time ~ Part 2 ~
How to study kotlin for the first time ~ Part 1 ~
Scraping for beginners (Ruby)
I tried to introduce Bootstrap 4 to the Rails 6 app [for beginners]
How to dynamically change the column name acquired by MyBatis
SDWebImage: How to clear the cache for a particular UIImageView
How to retrieve the hash value in an array in Ruby
How to get the longest information from Twitter as of 12/12/2016
[Ruby] How to retrieve the contents of a double hash
How to get the log when install4j does not start
For Java beginners: List, Map, Iterator / Array ... How to convert?
[jsoup] How to get the full amount of a document
[Rails] How to display error messages for comment function (for beginners)
How to change the file name with Xcode (Refactor Rename)