Rakefile in telegram-bot-ruby-1.0.0 vs Rakefile in telegram-bot-ruby-2.0.0
- old
+ new
@@ -16,11 +16,11 @@
task default: :spec
desc 'Dump type definitions from docs to YAML'
task :dump_type_attributes do
- require File.expand_path('lib/telegram/bot', __dir__)
+ require_relative 'lib/telegram/bot'
require 'nokogiri'
require 'open-uri'
require 'yaml'
# Preload every type we have
@@ -28,33 +28,35 @@
types = Telegram::Bot::Types::Base.descendants.map { |c| c.name.split('::').last }
# Fetch and parse docs
doc = Nokogiri::HTML(URI.open('https://core.telegram.org/bots/api').read)
+ next_type_element_names = %w[table h4]
+
result = types.to_h do |type|
# This is very hacky but working way to find table containing attributes for
# given type. Basic idea is to find heading with type and then iterate until
# we find table with attributes or next heading (because sometimes type
# doesn't have any attributes).
element = doc.at_xpath(%{//h4[text() = "#{type}"]})
loop do
element = element.next_element
- break if %w[table h4].include?(element.name)
+ break if next_type_element_names.include?(element.name)
end
attributes = element.xpath('.//tbody//tr').map do |el|
cells = el.children.select { |c| c.name == 'td' }
{
'name' => cells[0].text,
- 'required' => !cells[2].text.start_with?('Optional.')
- }
+ 'type' => cells[1].text,
+ 'required' => !cells[2].text.start_with?('Optional.'),
+ 'required_value' =>
+ cells[2].text.match(/^.+, (?:must be (?<found_type>\w+)|always “(?<found_type>\w+)”)$/)&.[](:found_type)
+ }.compact
end
[type, attributes]
end
# Write everything to fixture file
- File.write(
- File.expand_path('spec/support/type_attributes.yml', __dir__),
- result.to_yaml
- )
+ File.write "#{__dir__}/spec/support/type_attributes.yml", result.to_yaml
end