telegram-bot-ruby 2.8.0 → 2.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.agents/skills/commit/SKILL.md +18 -0
- data/.agents/skills/update-api/SKILL.md +70 -0
- data/.claude/skills +1 -0
- data/.rubocop.yml +0 -14
- data/CHANGELOG.md +22 -0
- data/README.md +17 -3
- data/lib/telegram/bot/api/endpoints.rb +9 -0
- data/lib/telegram/bot/api.rb +1 -1
- data/lib/telegram/bot/types/area_position.rb +16 -0
- data/lib/telegram/bot/types/chat_administrator_rights.rb +1 -0
- data/lib/telegram/bot/types/chat_full_info.rb +2 -0
- data/lib/telegram/bot/types/chat_member_administrator.rb +1 -0
- data/lib/telegram/bot/types/chat_share_theme.rb +15 -0
- data/lib/telegram/bot/types/chat_share_theme_backdrop.rb +13 -0
- data/lib/telegram/bot/types/chat_share_theme_backdrops.rb +11 -0
- data/lib/telegram/bot/types/chat_share_theme_emoji.rb +13 -0
- data/lib/telegram/bot/types/chat_share_theme_unique_gift.rb +12 -0
- data/lib/telegram/bot/types/community_chat_joined.rb +11 -0
- data/lib/telegram/bot/types/disabled_button.rb +10 -0
- data/lib/telegram/bot/types/emoji_type.rb +15 -0
- data/lib/telegram/bot/types/emoji_type_custom_emoji.rb +12 -0
- data/lib/telegram/bot/types/emoji_type_emoji.rb +12 -0
- data/lib/telegram/bot/types/ephemeral_message_parameters.rb +13 -0
- data/lib/telegram/bot/types/external_reply_info.rb +1 -0
- data/lib/telegram/bot/types/game.rb +1 -1
- data/lib/telegram/bot/types/inline_keyboard_button.rb +1 -0
- data/lib/telegram/bot/types/inline_keyboard_markup.rb +1 -0
- data/lib/telegram/bot/types/input_checklist.rb +1 -1
- data/lib/telegram/bot/types/input_checklist_task.rb +1 -1
- data/lib/telegram/bot/types/input_message_content.rb +3 -1
- data/lib/telegram/bot/types/input_rich_block.rb +3 -0
- data/lib/telegram/bot/types/input_rich_block_buttons.rb +13 -0
- data/lib/telegram/bot/types/input_rich_block_document.rb +13 -0
- data/lib/telegram/bot/types/input_rich_block_expandable_block_quotation.rb +13 -0
- data/lib/telegram/bot/types/input_rich_block_map.rb +3 -3
- data/lib/telegram/bot/types/input_rich_block_table.rb +1 -0
- data/lib/telegram/bot/types/input_rich_message_media.rb +2 -1
- data/lib/telegram/bot/types/input_shared_widget_message_content.rb +16 -0
- data/lib/telegram/bot/types/input_widget_message_content.rb +21 -0
- data/lib/telegram/bot/types/message.rb +4 -0
- data/lib/telegram/bot/types/message_generation_stopped.rb +13 -0
- data/lib/telegram/bot/types/paid_messages_waived.rb +10 -0
- data/lib/telegram/bot/types/poll.rb +1 -1
- data/lib/telegram/bot/types/reply_keyboard_markup.rb +1 -0
- data/lib/telegram/bot/types/reply_parameters.rb +1 -0
- data/lib/telegram/bot/types/rich_block.rb +3 -0
- data/lib/telegram/bot/types/rich_block_buttons.rb +13 -0
- data/lib/telegram/bot/types/rich_block_document.rb +13 -0
- data/lib/telegram/bot/types/rich_block_expandable_block_quotation.rb +13 -0
- data/lib/telegram/bot/types/rich_block_table.rb +1 -0
- data/lib/telegram/bot/types/rich_message_button.rb +21 -0
- data/lib/telegram/bot/types/rich_text.rb +4 -0
- data/lib/telegram/bot/types/rich_text_button.rb +12 -0
- data/lib/telegram/bot/types/rich_text_ton_address.rb +13 -0
- data/lib/telegram/bot/types/shared_widget_query.rb +15 -0
- data/lib/telegram/bot/types/story_area.rb +1 -1
- data/lib/telegram/bot/types/text_quote.rb +1 -0
- data/lib/telegram/bot/types/ton_nft.rb +17 -0
- data/lib/telegram/bot/types/ton_nft_attribute.rb +12 -0
- data/lib/telegram/bot/types/ton_wallet_card.rb +14 -0
- data/lib/telegram/bot/types/ton_wallet_card_sticker.rb +12 -0
- data/lib/telegram/bot/types/ton_wallet_transfer.rb +14 -0
- data/lib/telegram/bot/types/unique_gift_info.rb +3 -0
- data/lib/telegram/bot/types/update.rb +3 -0
- data/lib/telegram/bot/types/widget.rb +16 -0
- data/lib/telegram/bot/types/widget_query.rb +20 -0
- data/lib/telegram/bot/types/widget_query_type.rb +16 -0
- data/lib/telegram/bot/types/widget_query_type_data.rb +12 -0
- data/lib/telegram/bot/types/widget_query_type_sent_transaction.rb +13 -0
- data/lib/telegram/bot/types/widget_query_type_sign_data.rb +13 -0
- data/lib/telegram/bot/types/widget_query_wallet.rb +14 -0
- data/lib/telegram/bot/version.rb +1 -1
- data/rakelib/README.md +116 -0
- data/rakelib/builders/type_builder.rb +11 -5
- data/rakelib/docs.rake +15 -0
- data/rakelib/parse.rake +8 -25
- data/rakelib/parsers/api_method.rb +26 -0
- data/rakelib/parsers/description.rb +62 -0
- data/rakelib/parsers/docs_page.rb +62 -0
- data/rakelib/parsers/entry.rb +45 -0
- data/rakelib/parsers/entry_grammar.rb +48 -0
- data/rakelib/parsers/error.rb +5 -0
- data/rakelib/parsers/field.rb +30 -0
- data/rakelib/parsers/object_type.rb +29 -0
- data/rakelib/parsers/page_object.rb +41 -0
- data/rakelib/parsers/readers.rb +16 -0
- data/rakelib/parsers/return_type.rb +36 -0
- data/rakelib/parsers/serializer.rb +93 -0
- data/rakelib/parsers/table.rb +14 -0
- data/rakelib/parsers/type_ref.rb +42 -0
- data/rakelib/parsers/union_type.rb +42 -0
- metadata +60 -5
- data/.claude/skills/update-api/SKILL.md +0 -70
- data/rakelib/parsers/methods_parser.rb +0 -115
- data/rakelib/parsers/types_parser.rb +0 -273
|
@@ -38,10 +38,17 @@ module Builders
|
|
|
38
38
|
end
|
|
39
39
|
|
|
40
40
|
def build_empty_type
|
|
41
|
-
attrs = attributes[:type].
|
|
41
|
+
attrs = attributes[:type].map { |member| empty_type_member(member.to_s) }
|
|
42
|
+
.join(" |\n ")
|
|
42
43
|
render_template('empty_type.erb', name: name, attributes: attrs)
|
|
43
44
|
end
|
|
44
45
|
|
|
46
|
+
def empty_type_member(member)
|
|
47
|
+
return "Types::Array.of(#{add_module_types(member.delete_prefix('array:'))})" if member.start_with?('array:')
|
|
48
|
+
|
|
49
|
+
add_module_types(member)
|
|
50
|
+
end
|
|
51
|
+
|
|
45
52
|
def build_full_type
|
|
46
53
|
process_attributes!
|
|
47
54
|
render_template('type.erb', name: name, attributes: attributes)
|
|
@@ -96,10 +103,9 @@ module Builders
|
|
|
96
103
|
def apply_min_max!(attr_name, properties)
|
|
97
104
|
return unless properties[:min_size] || properties[:max_size]
|
|
98
105
|
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
attributes[attr_name][:type] += constrain
|
|
106
|
+
bounds = properties.slice(:min_size, :max_size).compact
|
|
107
|
+
constraints = bounds.map { |key, value| "#{key}: #{value}" }.join(', ')
|
|
108
|
+
attributes[attr_name][:type] += ".constrained(#{constraints})"
|
|
103
109
|
end
|
|
104
110
|
|
|
105
111
|
def apply_default!(attr_name, properties, original_type)
|
data/rakelib/docs.rake
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'net/http'
|
|
4
|
+
require_relative 'parsers/docs_page'
|
|
5
|
+
|
|
6
|
+
namespace :docs do
|
|
7
|
+
desc 'Download the Telegram Bot API documentation to data/bot_api.html'
|
|
8
|
+
task :fetch do
|
|
9
|
+
response = Net::HTTP.get_response(URI(Parsers::DocsPage::URL))
|
|
10
|
+
raise "Failed to fetch #{Parsers::DocsPage::URL}: #{response.code}" unless response.is_a?(Net::HTTPSuccess)
|
|
11
|
+
|
|
12
|
+
File.write Parsers::DocsPage::SNAPSHOT_PATH, Parsers::DocsPage.parse(response.body).to_html
|
|
13
|
+
puts 'Written to data/bot_api.html'
|
|
14
|
+
end
|
|
15
|
+
end
|
data/rakelib/parse.rake
CHANGED
|
@@ -1,31 +1,14 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
require_relative 'parsers/
|
|
5
|
-
require_relative 'parsers/methods_parser'
|
|
3
|
+
require_relative 'parsers/docs_page'
|
|
4
|
+
require_relative 'parsers/serializer'
|
|
6
5
|
|
|
7
6
|
namespace :parse do
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
puts "Found #{result.keys.count} types"
|
|
15
|
-
|
|
16
|
-
File.write "#{__dir__}/../data/types.json", JSON.pretty_generate(result)
|
|
17
|
-
puts 'Written to data/types.json'
|
|
18
|
-
end
|
|
19
|
-
|
|
20
|
-
desc 'Parse methods from Telegram Bot API HTML documentation'
|
|
21
|
-
task :methods do
|
|
22
|
-
puts 'Parsing methods from Telegram Bot API...'
|
|
23
|
-
|
|
24
|
-
result = Parsers::MethodsParser.new.parse
|
|
25
|
-
|
|
26
|
-
puts "Found #{result.keys.count} methods"
|
|
27
|
-
|
|
28
|
-
File.write "#{__dir__}/../data/methods.json", JSON.pretty_generate(result)
|
|
29
|
-
puts 'Written to data/methods.json'
|
|
7
|
+
Parsers::Serializer::FILES.each_key do |name|
|
|
8
|
+
desc "Parse #{name} from data/bot_api.html"
|
|
9
|
+
task name do
|
|
10
|
+
File.write Parsers::Serializer.path(name), Parsers::Serializer.new(Parsers::DocsPage.load).render(name)
|
|
11
|
+
puts "Written to data/#{name}.json"
|
|
12
|
+
end
|
|
30
13
|
end
|
|
31
14
|
end
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative 'entry'
|
|
4
|
+
require_relative 'table'
|
|
5
|
+
require_relative 'return_type'
|
|
6
|
+
|
|
7
|
+
module Parsers
|
|
8
|
+
class ApiMethod < Entry
|
|
9
|
+
TABLE_HEADERS = %w[Parameter Type Required Description].freeze
|
|
10
|
+
|
|
11
|
+
blocks :paragraphs, 'p'
|
|
12
|
+
block :table, 'table', as: Table
|
|
13
|
+
|
|
14
|
+
def return_type
|
|
15
|
+
@return_type ||= ReturnType.parse(paragraphs.map(&:inner_html))
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
private
|
|
19
|
+
|
|
20
|
+
def validate!
|
|
21
|
+
raise Error, 'cannot find the return type' unless return_type
|
|
22
|
+
|
|
23
|
+
table&.expect_headers(TABLE_HEADERS)
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative 'page_object'
|
|
4
|
+
|
|
5
|
+
module Parsers
|
|
6
|
+
class Description < PageObject
|
|
7
|
+
FIXED = [/must be /i, %r{must be <em>([^<]+)</em>(?!\s+(?:for|if|when|unless)\b)}i].freeze
|
|
8
|
+
DEFAULT = [/Defaults to /i, %r{Defaults to <em>([^<]+)</em>}i].freeze
|
|
9
|
+
SIZE_RANGES = [
|
|
10
|
+
/(\d+)-(\d+) characters(?! after entities parsing)/,
|
|
11
|
+
/(?:List of |; )(\d+)-(\d+) (?:buttons|regular or custom emoji)/,
|
|
12
|
+
/must be between (\d+) and (\d+)/
|
|
13
|
+
].freeze
|
|
14
|
+
|
|
15
|
+
def text
|
|
16
|
+
@text ||= node.text.strip
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def optional?
|
|
20
|
+
text.start_with?('Optional')
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def fixed_value
|
|
24
|
+
text[/always ["“]([^"”]+)["”]/i, 1]&.delete('\\') || emphasized(FIXED)
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def default
|
|
28
|
+
text[/Defaults to “([^”]+)”/i, 1] ||
|
|
29
|
+
emphasized(DEFAULT) ||
|
|
30
|
+
text[/Defaults to (\d+)/i, 1]
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def min_size
|
|
34
|
+
size_range[0]
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def max_size
|
|
38
|
+
text[/Up to (\d+) quoted texts/, 1]&.to_i || size_range[1]
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
private
|
|
42
|
+
|
|
43
|
+
# Serializing a cell is slow, so it is only done for the few that mention the phrase.
|
|
44
|
+
def emphasized((mention, pattern))
|
|
45
|
+
node.inner_html[pattern, 1] if text.match?(mention)
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def size_range
|
|
49
|
+
@size_range ||= begin
|
|
50
|
+
match = SIZE_RANGES.filter_map { |pattern| text.match(pattern) }.first
|
|
51
|
+
if match
|
|
52
|
+
min = match[1].to_i
|
|
53
|
+
# Omit zero character minimums, but retain explicit collection bounds.
|
|
54
|
+
min = nil if min.zero? && match.regexp == SIZE_RANGES.first
|
|
55
|
+
[min, match[2].to_i]
|
|
56
|
+
else
|
|
57
|
+
[]
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative 'page_object'
|
|
4
|
+
require_relative 'entry_grammar'
|
|
5
|
+
require_relative 'api_method'
|
|
6
|
+
require_relative 'object_type'
|
|
7
|
+
require_relative 'union_type'
|
|
8
|
+
|
|
9
|
+
module Parsers
|
|
10
|
+
class DocsPage < PageObject
|
|
11
|
+
URL = 'https://core.telegram.org/bots/api'
|
|
12
|
+
SNAPSHOT_PATH = File.expand_path('../../data/bot_api.html', __dir__)
|
|
13
|
+
CONTENT = 'div#dev_page_content'
|
|
14
|
+
|
|
15
|
+
# Types, methods and prose such as "Sending files" share the <h4> level,
|
|
16
|
+
# and an entry made of paragraphs alone can be any of them, so names
|
|
17
|
+
# decide. The first matching rule wins; a heading that matches none raises.
|
|
18
|
+
GRAMMAR = EntryGrammar.new('h4') do
|
|
19
|
+
entry ApiMethod, named: /\A[a-z][A-Za-z0-9]*\z/
|
|
20
|
+
entry UnionType, named: TypeRef::NAME, with: 'ul'
|
|
21
|
+
entry ObjectType, named: TypeRef::NAME
|
|
22
|
+
skip named: /\s/
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def self.parse(html)
|
|
26
|
+
content = Nokogiri::HTML(html, nil, 'UTF-8').at_css(CONTENT)
|
|
27
|
+
raise Error, "No #{CONTENT} in the document" unless content
|
|
28
|
+
|
|
29
|
+
new(content)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def self.load(path = SNAPSHOT_PATH)
|
|
33
|
+
parse(File.read(path))
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def entries
|
|
37
|
+
@entries ||= GRAMMAR.parse(node.element_children)
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def api_types
|
|
41
|
+
@api_types ||= entries.grep_v(ApiMethod)
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def api_methods
|
|
45
|
+
@api_methods ||= entries.grep(ApiMethod)
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def api_type(name)
|
|
49
|
+
api_types.find { |type| type.name == name } || raise(Error, "Unknown type #{name}")
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def api_method(name)
|
|
53
|
+
api_methods.find { |method| method.name == name } || raise(Error, "Unknown method #{name}")
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
# Only the content is kept: without the site chrome around it, the snapshot
|
|
57
|
+
# changes when the documentation does and at no other time.
|
|
58
|
+
def to_html
|
|
59
|
+
"#{node.to_html(encoding: 'UTF-8')}\n"
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative 'error'
|
|
4
|
+
require_relative 'readers'
|
|
5
|
+
|
|
6
|
+
module Parsers
|
|
7
|
+
# No element contains an entry, so it is not a page object over one node: it
|
|
8
|
+
# is a heading and the blocks that follow it, read by tag.
|
|
9
|
+
class Entry
|
|
10
|
+
extend Readers
|
|
11
|
+
|
|
12
|
+
attr_reader :heading, :body
|
|
13
|
+
|
|
14
|
+
def initialize(heading, body)
|
|
15
|
+
@heading = heading
|
|
16
|
+
@body = body
|
|
17
|
+
validate!
|
|
18
|
+
rescue Error => e
|
|
19
|
+
raise Error, "#{name}: #{e.message}"
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def name
|
|
23
|
+
@name ||= heading.text.strip
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
class << self
|
|
27
|
+
def blocks(name, tag)
|
|
28
|
+
reader(name) { body.select { |block| block.name == tag } }
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def block(name, tag, as: nil)
|
|
32
|
+
reader(name) do
|
|
33
|
+
matches = body.select { |block| block.name == tag }
|
|
34
|
+
raise Error, "has #{matches.size} <#{tag}> blocks" if matches.size > 1
|
|
35
|
+
|
|
36
|
+
as && matches.any? ? as.new(matches.first) : matches.first
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
private
|
|
42
|
+
|
|
43
|
+
def validate!; end
|
|
44
|
+
end
|
|
45
|
+
end
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative 'error'
|
|
4
|
+
|
|
5
|
+
module Parsers
|
|
6
|
+
# The docs page is flat: a heading is followed by its content instead of
|
|
7
|
+
# wrapping it. This splits such a run of elements into entries, a heading
|
|
8
|
+
# plus the blocks up to the next heading of the same or a higher rank, and
|
|
9
|
+
# builds each entry with the first rule it matches.
|
|
10
|
+
class EntryGrammar
|
|
11
|
+
Rule = Struct.new(:entry_class, :named, :with) do
|
|
12
|
+
def match?(name, body)
|
|
13
|
+
name.match?(named) && (with.nil? || body.any? { |block| block.name == with })
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def initialize(heading, &rules)
|
|
18
|
+
@heading = heading
|
|
19
|
+
@boundary = /\Ah[1-#{heading[-1]}]\z/
|
|
20
|
+
@rules = []
|
|
21
|
+
instance_eval(&rules)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def entry(entry_class, named:, with: nil)
|
|
25
|
+
@rules << Rule.new(entry_class, named, with)
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def skip(named:)
|
|
29
|
+
@rules << Rule.new(nil, named)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def parse(elements)
|
|
33
|
+
elements.slice_before { |element| element.name.match?(@boundary) }
|
|
34
|
+
.select { |first, *| first.name == @heading }
|
|
35
|
+
.filter_map { |heading, *body| build(heading, body) }
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
private
|
|
39
|
+
|
|
40
|
+
def build(heading, body)
|
|
41
|
+
name = heading.text.strip
|
|
42
|
+
rule = @rules.find { |candidate| candidate.match?(name, body) }
|
|
43
|
+
raise Error, "Unrecognised heading #{name.inspect}" unless rule
|
|
44
|
+
|
|
45
|
+
rule.entry_class&.new(heading, body)
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative 'page_object'
|
|
4
|
+
require_relative 'type_ref'
|
|
5
|
+
require_relative 'description'
|
|
6
|
+
|
|
7
|
+
module Parsers
|
|
8
|
+
class Field < PageObject
|
|
9
|
+
cell :name, 0
|
|
10
|
+
cell :type_text, 1
|
|
11
|
+
cell :description, 2, as: Description
|
|
12
|
+
|
|
13
|
+
def type
|
|
14
|
+
@type ||= TypeRef.new(type_text)
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def required?
|
|
18
|
+
!description.optional?
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
private
|
|
22
|
+
|
|
23
|
+
# Reads the cells now, so that a bad row fails along with its entry instead
|
|
24
|
+
# of whenever it is first used.
|
|
25
|
+
def validate!
|
|
26
|
+
type
|
|
27
|
+
description
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative 'entry'
|
|
4
|
+
require_relative 'table'
|
|
5
|
+
require_relative 'field'
|
|
6
|
+
|
|
7
|
+
module Parsers
|
|
8
|
+
# An object without a table is a real thing: it holds no information.
|
|
9
|
+
class ObjectType < Entry
|
|
10
|
+
TABLE_HEADERS = %w[Field Type Description].freeze
|
|
11
|
+
|
|
12
|
+
block :table, 'table', as: Table
|
|
13
|
+
|
|
14
|
+
def fields
|
|
15
|
+
@fields ||= table ? table.rows.map { |row| Field.new(row) } : []
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
private
|
|
19
|
+
|
|
20
|
+
def validate!
|
|
21
|
+
if table
|
|
22
|
+
table.expect_headers(TABLE_HEADERS)
|
|
23
|
+
raise Error, 'has a table without rows' if fields.empty?
|
|
24
|
+
elsif body.any? { |block| block.name != 'p' }
|
|
25
|
+
raise Error, 'has no table and is not just paragraphs'
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'nokogiri'
|
|
4
|
+
require_relative 'error'
|
|
5
|
+
require_relative 'readers'
|
|
6
|
+
|
|
7
|
+
module Parsers
|
|
8
|
+
class PageObject
|
|
9
|
+
extend Readers
|
|
10
|
+
|
|
11
|
+
attr_reader :node
|
|
12
|
+
|
|
13
|
+
def initialize(node)
|
|
14
|
+
@node = node
|
|
15
|
+
validate!
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
class << self
|
|
19
|
+
def texts(name, selector)
|
|
20
|
+
reader(name) { node.css(selector).map { |match| match.text.strip } }
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def elements(name, selector)
|
|
24
|
+
reader(name) { node.css(selector).to_a }
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
# Reads a child by position, which is much cheaper than a selector per
|
|
28
|
+
# table cell.
|
|
29
|
+
def cell(name, index, as: nil)
|
|
30
|
+
reader(name) do
|
|
31
|
+
child = node.element_children[index] || raise(Error, "No cell #{index} in #{node.text.strip.inspect}")
|
|
32
|
+
as ? as.new(child) : child.text.strip
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
private
|
|
38
|
+
|
|
39
|
+
def validate!; end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Parsers
|
|
4
|
+
module Readers
|
|
5
|
+
private
|
|
6
|
+
|
|
7
|
+
def reader(name, &block)
|
|
8
|
+
ivar = "@#{name}"
|
|
9
|
+
define_method(name) do
|
|
10
|
+
return instance_variable_get(ivar) if instance_variable_defined?(ivar)
|
|
11
|
+
|
|
12
|
+
instance_variable_set(ivar, instance_exec(&block))
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative 'type_ref'
|
|
4
|
+
|
|
5
|
+
module Parsers
|
|
6
|
+
# The docs only say what a method returns in prose. The first sentence that
|
|
7
|
+
# both mentions returning and names a type wins: a linked object or an
|
|
8
|
+
# emphasized primitive.
|
|
9
|
+
module ReturnType
|
|
10
|
+
PRIMITIVES = { 'True' => 'Boolean', 'Int' => 'Integer', 'Integer' => 'Integer', 'String' => 'String' }.freeze
|
|
11
|
+
|
|
12
|
+
MENTION = /\breturn(?:s|ed)?\b/i.freeze
|
|
13
|
+
OBJECT = %r{<a[^>]*>(#{TypeRef::NAME_PATTERN})</a>}.freeze
|
|
14
|
+
ARRAY = /\barray of <a/i.freeze
|
|
15
|
+
PRIMITIVE = %r{<em>(#{Regexp.union(PRIMITIVES.keys)})</em>}.freeze
|
|
16
|
+
OR_TRUE = %r{otherwise <em>True</em> is returned}i.freeze
|
|
17
|
+
|
|
18
|
+
module_function
|
|
19
|
+
|
|
20
|
+
def parse(paragraphs)
|
|
21
|
+
sentences = paragraphs.flat_map { |html| html.split(/(?<=\.)\s+/) }
|
|
22
|
+
sentences.lazy.filter_map { |sentence| parse_sentence(sentence) }.first
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def parse_sentence(sentence)
|
|
26
|
+
return unless sentence.match?(MENTION)
|
|
27
|
+
|
|
28
|
+
if (object = sentence[OBJECT, 1])
|
|
29
|
+
type = sentence.match?(ARRAY) ? "Array<#{object}>" : object
|
|
30
|
+
sentence.match?(OR_TRUE) ? "#{type} | Boolean" : type
|
|
31
|
+
elsif (primitive = sentence[PRIMITIVE, 1])
|
|
32
|
+
PRIMITIVES.fetch(primitive)
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'json'
|
|
4
|
+
|
|
5
|
+
module Parsers
|
|
6
|
+
class Serializer
|
|
7
|
+
PRIMITIVES = {
|
|
8
|
+
'String' => 'string',
|
|
9
|
+
'Integer' => 'integer',
|
|
10
|
+
'Boolean' => 'boolean',
|
|
11
|
+
'Float' => 'number',
|
|
12
|
+
'True' => 'boolean'
|
|
13
|
+
}.freeze
|
|
14
|
+
|
|
15
|
+
FILES = { types: :types, methods: :api_methods }.freeze
|
|
16
|
+
|
|
17
|
+
def self.path(file)
|
|
18
|
+
File.expand_path("../../data/#{file}.json", __dir__)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def initialize(page)
|
|
22
|
+
@page = page
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def render(file)
|
|
26
|
+
JSON.pretty_generate(public_send(FILES.fetch(file)))
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def types
|
|
30
|
+
@page.api_types.to_h { |type| [type.name, type.is_a?(UnionType) ? union(type) : fields(type)] }
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def api_methods
|
|
34
|
+
@page.api_methods.to_h { |method| [method.name, method.return_type] }
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
private
|
|
38
|
+
|
|
39
|
+
def union(type)
|
|
40
|
+
members = []
|
|
41
|
+
members << 'string' if type.accepts_string?
|
|
42
|
+
members << "array:#{type.name}" if type.accepts_array_of_self?
|
|
43
|
+
|
|
44
|
+
{ 'type' => members + type.members }
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def fields(type)
|
|
48
|
+
type.fields.to_h { |field| [field.name, attribute(field)] }
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def attribute(field)
|
|
52
|
+
fixed = field.description.fixed_value
|
|
53
|
+
|
|
54
|
+
type_ref(field.type).merge(
|
|
55
|
+
'required' => (true if field.required?),
|
|
56
|
+
'required_value' => fixed,
|
|
57
|
+
'default' => fixed || default(field),
|
|
58
|
+
'min_size' => field.description.min_size,
|
|
59
|
+
'max_size' => field.description.max_size
|
|
60
|
+
).compact
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def type_ref(type)
|
|
64
|
+
return { 'type' => 'array', 'items' => items(type.items) } if type.array?
|
|
65
|
+
|
|
66
|
+
{ 'type' => type.union? ? type.names.map { |name| primitive(name) } : primitive(type.names.first) }
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def items(type)
|
|
70
|
+
type.array? ? type_ref(type) : type_ref(type)['type']
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def primitive(name)
|
|
74
|
+
PRIMITIVES.fetch(name, name)
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
def default(field)
|
|
78
|
+
return true if field.type.always_true?
|
|
79
|
+
|
|
80
|
+
value = field.description.default
|
|
81
|
+
cast(value, field.type) if value
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
def cast(value, type)
|
|
85
|
+
case type.to_s
|
|
86
|
+
when 'Integer' then value.to_i
|
|
87
|
+
when 'Boolean' then value.casecmp?('true')
|
|
88
|
+
when 'Float' then value.to_f
|
|
89
|
+
else value
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
end
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative 'page_object'
|
|
4
|
+
|
|
5
|
+
module Parsers
|
|
6
|
+
class Table < PageObject
|
|
7
|
+
texts :headers, 'th'
|
|
8
|
+
elements :rows, '> tbody > tr'
|
|
9
|
+
|
|
10
|
+
def expect_headers(expected)
|
|
11
|
+
raise Error, "has unexpected table headers #{headers.inspect}" if headers != expected
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
end
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative 'error'
|
|
4
|
+
|
|
5
|
+
module Parsers
|
|
6
|
+
class TypeRef
|
|
7
|
+
ARRAY_PREFIX = 'Array of '
|
|
8
|
+
UNION_SEPARATOR = ' or '
|
|
9
|
+
NAME_PATTERN = '[A-Z][A-Za-z0-9]*'
|
|
10
|
+
NAME = /\A#{NAME_PATTERN}\z/.freeze
|
|
11
|
+
|
|
12
|
+
attr_reader :names
|
|
13
|
+
|
|
14
|
+
def initialize(text)
|
|
15
|
+
@text = text.to_s.strip
|
|
16
|
+
@names = @text.sub(/\A(?:#{ARRAY_PREFIX})+/o, '').split(UNION_SEPARATOR)
|
|
17
|
+
|
|
18
|
+
raise Error, "Unsupported type #{@text.inspect}" unless @names.any? && @names.all?(NAME)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def array?
|
|
22
|
+
@text.start_with?(ARRAY_PREFIX)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def union?
|
|
26
|
+
names.size > 1
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def items
|
|
30
|
+
self.class.new(@text.delete_prefix(ARRAY_PREFIX)) if array?
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
# A "True" field is only ever sent as true, unlike a "Boolean" one.
|
|
34
|
+
def always_true?
|
|
35
|
+
@text == 'True'
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def to_s
|
|
39
|
+
@text
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|