Skip to content

Commit

Permalink
linter fix
Browse files Browse the repository at this point in the history
  • Loading branch information
seorgiy committed Oct 11, 2024
1 parent d927a78 commit a3aa859
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 39 deletions.
8 changes: 6 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ gem 'rake', '~> 13.0'
gem 'rspec', '~> 3.4'
gem 'vcr', '~> 6.0'

gem 'rubocop', '~> 1.54.1'
gem 'rubocop', '~> 1.66.1'
gem 'rubocop-performance', '~> 1.18'
gem 'rubocop-rake', '~> 0.6.0'
gem 'rubocop-rspec', '~> 2.22.0'
gem 'rubocop-rspec', '~> 3.1.0'

group :development do
gem 'openapi3_parser', '~> 0.9.2'
end
79 changes: 44 additions & 35 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -61,68 +61,61 @@ task :dump_type_attributes do
File.write "#{__dir__}/spec/support/type_attributes.yml", result.to_yaml
end

DRY_TYPES = %w[string integer float decimal array hash symbol boolean date date_time time range].freeze
desc 'Rebuild types'
task :rebuild_types do
require 'json'
require 'erb'
DRY_TYPES = %w[string integer float decimal array hash symbol boolean date date_time time range]

def underscore(camel_cased_word)
camel_cased_word.to_s.gsub(/::/, '/').
gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
gsub(/([a-z\d])([A-Z])/,'\1_\2').
tr("-", "_").
downcase
end

def typecast_default(type, obj)
type == 'Types::String' ? "'#{obj.to_s}'" : obj
end

def add_module_types(type)
DRY_TYPES.include?(type) ? "Types::#{type.capitalize}" : type
end

types = JSON.parse(File.read("#{__dir__}/spec/support/openapi_type_attributes.json"), symbolize_names: true)

types.each_pair do |name, attributes|
template = ERB.new(File.read("#{__dir__}/spec/support/type.erb"))

attributes.each_pair do |attr_name, properties|
attributes[attr_name][:type] = add_module_types(properties[:type]) unless properties[:type].kind_of?(Array)
attributes[attr_name][:type] = properties[:type].map { |type| add_module_types(type) }.join(' | ') if properties[:type].kind_of?(Array)
attributes.each_pair do |attr_name, properties|
attributes[attr_name][:type] = add_module_types(properties[:type]) unless properties[:type].is_a?(Array)
if properties[:type].is_a?(Array)
attributes[attr_name][:type] = properties[:type].map do |type|
add_module_types(type)
end.join(' | ')
end
attributes[attr_name][:type] += ".of(#{add_module_types(properties[:items])})" if properties[:items]
attributes[attr_name][:type] += ".default(#{typecast_default(properties[:type], properties[:default])})" if properties[:default]
attributes[attr_name][:type] = "Types::True" if attributes[attr_name][:type] == 'Types::Boolean.default(true)'
attributes[attr_name][:type] = "Types::Bool" if attributes[attr_name][:type] == 'Types::Boolean'
if properties[:default]
attributes[attr_name][:type] += ".default(#{typecast_default(properties[:type],
properties[:default])})"
end
attributes[attr_name][:type] = 'Types::True' if attributes[attr_name][:type] == 'Types::Boolean.default(true)'
attributes[attr_name][:type] = 'Types::Bool' if attributes[attr_name][:type] == 'Types::Boolean'
end

File.write "#{__dir__}/lib/telegram/bot/types/#{underscore(name)}.rb", template.result(binding).gsub(/ \n/,'')
File.write "#{__dir__}/lib/telegram/bot/types/#{underscore(name)}.rb", template.result(binding).gsub(" \n", '')
end
end

desc 'Parse types from public json, should be up to date https://github.com/ark0f/tg-bot-api'
task :parse_type_attributes_from_opeanapi do
require "openapi3_parser"
require 'openapi3_parser'
result = {}
document = Openapi3Parser.load_url("https://ark0f.github.io/tg-bot-api/openapi.json")
SKIP = %w[]
document = Openapi3Parser.load_url('https://ark0f.github.io/tg-bot-api/openapi.json')

document.components.schemas.each do |schema|
next if SKIP.include?(schema[0])

result_schema = {}
schema[1].properties.each do |property|
required_keys = schema[1].required.to_a || []

result_schema[property[0]] = {
type: property[1].type != 'object' ? property[1].type : property[1].name
type: property[1].type == 'object' ? property[1].name : property[1].type
}

result_schema[property[0]][:type] = property[1].any_of.map do |item|
property[1].name || item.name || item.type
end.uniq unless property[1].any_of.nil?
result_schema[property[0]][:type] = result_schema[property[0]][:type].join() if result_schema[property[0]][:type]&.length == 1
unless property[1].any_of.nil?
result_schema[property[0]][:type] = property[1].any_of.map do |item|
property[1].name || item.name || item.type
end.uniq
end
if result_schema[property[0]][:type]&.length == 1
result_schema[property[0]][:type] =
result_schema[property[0]][:type].join
end
result_schema[property[0]][:required] = true if required_keys.include?(property[0])
result_schema[property[0]][:items] = property[1].items.type if property[1]&.items
result_schema[property[0]][:items] = property[1].items.name if property[1]&.items&.type == 'object'
Expand All @@ -132,4 +125,20 @@ task :parse_type_attributes_from_opeanapi do
end

File.write "#{__dir__}/spec/support/openapi_type_attributes.json", result.to_json
end
end

def add_module_types(type)
DRY_TYPES.include?(type) ? "Types::#{type.capitalize}" : type
end

def underscore(camel_cased_word)
camel_cased_word.to_s.gsub('::', '/')
.gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
.gsub(/([a-z\d])([A-Z])/, '\1_\2')
.tr('-', '_')
.downcase
end

def typecast_default(type, obj)
type == 'Types::String' ? "'#{obj}'" : obj
end
2 changes: 1 addition & 1 deletion lib/telegram/bot/exceptions/response_error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class ResponseError < Base
def initialize(response:)
@response = response

super "Telegram API has returned the error. (#{data.map { |k, v| %(#{k}: #{v.inspect}) }.join(', ')})"
super("Telegram API has returned the error. (#{data.map { |k, v| %(#{k}: #{v.inspect}) }.join(', ')})")
end

def error_code
Expand Down
1 change: 0 additions & 1 deletion telegram-bot-ruby.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,4 @@ Gem::Specification.new do |spec|
spec.add_dependency 'faraday', '~> 2.0'
spec.add_dependency 'faraday-multipart', '~> 1.0'
spec.add_dependency 'zeitwerk', '~> 2.6'
spec.add_development_dependency 'openapi3_parser', '~> 0.9.2'
end

0 comments on commit a3aa859

Please sign in to comment.