Skip to content

Commit

Permalink
Merge pull request #27 from centosadmin/develop
Browse files Browse the repository at this point in the history
Release 2.1.0
  • Loading branch information
vladislav-yashin authored Oct 18, 2019
2 parents 542dcaa + 619b8a3 commit 0bb23fb
Show file tree
Hide file tree
Showing 256 changed files with 2,865 additions and 684 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
language: ruby
rvm:
- 2.3.8
- 2.6.0
- 2.4.9
- 2.6.5
addons:
apt:
sources:
Expand Down
5 changes: 5 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
### 2.1.0 / 2019-10-18

* Support tdlib 1.5
* Fix TD::Client#dispose race condition and client crash

### 2.0.0 / 2019-02-08

* Generated types and client functions
Expand Down
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Ruby bindings and client for TDLib (Telegram database library).

## Requirements

* Ruby 2.3+
* Ruby 2.4+
* Compiled [tdlib](https://github.com/tdlib/td)

We have precompiled versions for CentOS 6 & 7 in our repositories:
Expand All @@ -29,6 +29,7 @@ http://rpms.southbridge.ru/rhel6/stable/SRPMS/
|:-------------:|:-:| :-----------: |
| 1.x || 1.0 - 1.2 |
| 2.0 || 1.3 |
| 2.1 || 1.5 |

## Install

Expand Down Expand Up @@ -83,7 +84,7 @@ begin
when :wait_phone_number
puts 'Please, enter your phone number:'
phone = STDIN.gets.strip
client.set_authentication_phone_number(phone).wait
client.set_authentication_phone_number(phone, nil).wait
when :wait_code
puts 'Please, enter code from SMS:'
code = STDIN.gets.strip
Expand Down Expand Up @@ -154,6 +155,12 @@ TD::Client.new(database_directory: 'will override value from config',
files_directory: 'will override value from config')
```

If the tdlib schema changes, then `./bin/parse` can be run to
synchronize the Ruby types with the new schema. Please look through
`lib/tdlib/client_methods.rb` carefully, especially the set_password
method!


## License

[MIT](https://github.com/centosadmin/tdlib-ruby/blob/master/LICENSE.txt)
Expand Down
43 changes: 32 additions & 11 deletions bin/parser
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#!/usr/bin/env ruby

require 'open-uri'
require 'active_support/all'

AUTOLOAD_BASE_DIR = 'tdlib/types'
TD_API_TL_LOCATION = File.join(File.expand_path("../../", __FILE__), %w(td td generate scheme td_api.tl))
TD_API_TL_LOCATION = "https://raw.githubusercontent.com/tdlib/td/v1.5.0/td/generate/scheme/td_api.tl"


def parse_tl_type(type)
if (vector = type.scan(/[vV]ector<(.*)>/)).length > 0
Expand Down Expand Up @@ -207,9 +208,12 @@ def attrs_to_yard_comment(attrs, key = 'attr')
end.join("\n")
end

puts "Creating types directory"
puts "Removing lib/tdlib/types"
# `find lib/tdlib/types | grep -v base.rb | xargs rm -rf`
`rm -rf lib/tdlib/types`

FileUtils.mkdir_p "types"
puts "MKDIR 'lib/tdlib/types'"
FileUtils.mkdir_p "lib/tdlib/types"

puts "Writing Base class"

Expand Down Expand Up @@ -245,12 +249,13 @@ module TD::Types
end
RUBY

puts "Writing 'lib/tdlib/types/base.rb'"
File.write 'lib/tdlib/types/base.rb', klass

puts "Parsing td_api.tl"
puts "Parsing #{TD_API_TL_LOCATION}"

# Reading the TL file and splitting it into classes and functions
@classes, @functions = File.read(TD_API_TL_LOCATION).split("\n\n---functions---\n\n")
@classes, @functions = open(TD_API_TL_LOCATION).read.split("\n\n---functions---\n\n")

# First entry in td_api.tl is typecasting, it's worthless for us.
# Last entry before functions is a testing class, once again worthless
Expand All @@ -267,6 +272,7 @@ puts "Converting classes into Ruby types"
@lookup_table = build_lookup_table(@classes)
@lookup_regex = Regexp.union(@lookup_table.keys.select { |k| k.match?(/[a-z]+[A-Z][a-z]+/) }.map { |k| /\W#{k}\W/ })


@classes.each do |class_info|
class_name = normalize_class_name(class_info[:class], class_info[:super_class])
attributes = class_info[:arguments]
Expand All @@ -292,7 +298,8 @@ puts "Converting classes into Ruby types"
file_name = "#{super_class_name.underscore}/#{class_name.sub(super_class_name, '').underscore}"
class_name = "#{super_class_name}::#{class_name.sub(super_class_name, '')} < #{super_class_name}"

FileUtils.mkdir_p "types/#{super_class_name.underscore}"
puts "MKDIR 'lib/tdlib/types/#{super_class_name.underscore}'"
FileUtils.mkdir_p "lib/tdlib/types/#{super_class_name.underscore}"
end

if attributes.blank?
Expand All @@ -317,6 +324,7 @@ module TD::Types#{description}#{attributes_doc}
end
RUBY

puts "Writing 'lib/tdlib/types/#{file_name}.rb'"
File.write "lib/tdlib/types/#{file_name}.rb", klass
end

Expand Down Expand Up @@ -384,6 +392,7 @@ module TD::Types
end
RUBY

puts "Writing 'lib/tdlib/types.rb'"
File.write 'lib/tdlib/types.rb', klass

puts "Converting functions"
Expand All @@ -407,10 +416,21 @@ puts "Converting functions"
else
params_doc = "\n" + attrs_to_yard_comment(params, 'param')

# TODO: Fix mixup of =nil params (only in set_password)
method_params = params.map do |attr, info|
"#{attr}#{ " = nil" if info[:optional] }"
end.join(", ")
if method_name == 'set_password'
method_params = params.map do |attr, info|
#set default value to false
if attr == 'set_recovery_email_address'
"#{attr}: false"
else
# use named arguments
"#{attr}#{ ": nil" if info[:optional] }"
end
end.join(", ")
else
method_params = params.map do |attr, info|
"#{attr}#{ " = nil" if info[:optional] }"
end.join(", ")
end

method_params = "(#{wrap_params(method_params, 118 - (method_name.length + 5), method_name.length + 7)})"

Expand Down Expand Up @@ -442,6 +462,7 @@ module TD::ClientMethods
#{@functions.join(" \n")}end
RUBY

puts "Writing 'lib/tdlib/client_methods.rb'"
File.write 'lib/tdlib/client_methods.rb', klass

puts "Done. Please look through client_methods.rb carefully, especially the set_password method!"
15 changes: 10 additions & 5 deletions lib/tdlib/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def connect
end
end

@update_manager.run
@update_manager.run(callback: method(:handle_update))
ready
end

Expand Down Expand Up @@ -149,10 +149,7 @@ def on_ready(&action)
# Stops update manager and destroys TDLib client
def dispose
return if dead?
@update_manager.stop
@alive = false
@ready = false
TD::Api.client_destroy(@td_client)
close.then { get_authorization_state }
end

def alive?
Expand All @@ -169,6 +166,14 @@ def ready?

private

def handle_update(update)
return unless update.is_a?(TD::Types::AuthorizationState::Closed)
@alive = false
@ready = false
TD::Api.client_destroy(@td_client)
throw(:client_closed)
end

def send_to_td_client(query)
return unless alive?
TD::Api.client_send(@td_client, query)
Expand Down
Loading

0 comments on commit 0bb23fb

Please sign in to comment.