telegram-rails 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 32045c7d423551f7c28bf5cb2a2461e70f04cf07
4
- data.tar.gz: ae807568c5f7806f718f4177a3bd52b2abb6b6d6
3
+ metadata.gz: 182d125795a4e088af6c27402934a4fa901a4c06
4
+ data.tar.gz: aeb4a85c703ab577fbc520727a1afa00d420d8ab
5
5
  SHA512:
6
- metadata.gz: d77da4d52528321a44a3815f188062f5ef935873c010a1afc869013043ad4af2247b6997fce9602e01f441fe89c5bfb9b866c91ffda4a31f49c723a22b1bb8ab
7
- data.tar.gz: e773a1c9ab1dd94de058d7fb3e206bfb360c753f689c7fa88428f9987220f66dc2dd636636ebe963155c08876ae9f1398d35e10c23c3eb08e1caf43f6f0de289
6
+ metadata.gz: b11c81be68b1188df5afd01b5976f450c602c4bc84c8ab451c9d901514501a196efbbbfb7de86fbdff0a3cc7f8cd7c9cd8aec0006e59874ced2d0cb5488f796d
7
+ data.tar.gz: 797894a095d7ad53c76fe65a7f706c51c6511800baa8e4055b648d53f41229da9fea0d32b998bff8203737fb9be4fa5a9253272cef2dd164b79060e3c05c8a10
data/README.md CHANGED
@@ -10,14 +10,14 @@ Gives you http-like routes and controllers. Allows you to write such a code:
10
10
  #routes.rb
11
11
  Rails.application.routes.draw do
12
12
 
13
- telegram 'main/start', to: 'telegram/message#start'
14
- telegram 'main/stop', to: 'telegram/message#stop'
15
- telegram 'main', to: 'telegram/message'
13
+ telegram 'main/start', to: 'telegram/messages#start'
14
+ telegram 'main/stop', to: 'telegram/messages#stop'
15
+ telegram 'main', to: 'telegram/messages'
16
16
 
17
17
  end
18
18
 
19
- #message_controller.rb
20
- class Telegram::MessageController < Telegram::Controller
19
+ #messages_controller.rb
20
+ class Telegram::MessagesController < Telegram::Controller
21
21
 
22
22
  def start
23
23
  respond_with 'received /start'
@@ -33,6 +33,20 @@ class Telegram::MessageController < Telegram::Controller
33
33
  respond_with 'other commands'
34
34
  end
35
35
 
36
+
37
+ #rails-like implicit/explicit template rendering (only ERB support now)
38
+ def show
39
+ #app/views/telegram/messages/show.(md|html|txt).erb
40
+ #all instance variables are available in templates, like in rails
41
+ @v1 = 'some value'
42
+ end
43
+
44
+
45
+ def explicit
46
+ #explicit template rendering
47
+ render 'telegram/path/to/template'
48
+ end
49
+
36
50
  end
37
51
 
38
52
  ```
@@ -0,0 +1,103 @@
1
+ require 'erubis'
2
+
3
+ require 'telegram/controller/view_object'
4
+ require 'telegram/errors/renderer/bad_template_format_error'
5
+ require 'telegram/errors/renderer/double_render_error'
6
+ require 'telegram/errors/renderer/format_not_supported_error'
7
+ require 'telegram/errors/renderer/multiple_templates_error'
8
+ require 'telegram/errors/renderer/template_not_found_error'
9
+
10
+
11
+ module Telegram
12
+ class Controller
13
+ class Renderer
14
+
15
+ def initialize(controller:, action:)
16
+ @controller, @action = controller, action
17
+ end
18
+
19
+
20
+ def render opts = {}
21
+ options = opts.merge(default_render_options)
22
+ view = Controller::ViewObject.new(@controller.expose_instance_variables)
23
+ template_file_name, response_format = resolve_template(options)
24
+
25
+ template_body = File.read template_file_name
26
+ template = Erubis::Eruby.new(template_body)
27
+ response_body = template.evaluate(view)
28
+
29
+ [response_body, response_format]
30
+ end
31
+
32
+
33
+ private
34
+
35
+ def default_render_options
36
+ controller_name = @controller.class.to_s.underscore.gsub(/_controller$/,'')
37
+ {
38
+ template: "#{controller_name}/#{@action}"
39
+ }
40
+ end
41
+
42
+
43
+ def resolve_template options
44
+ view_name = options[:template]
45
+
46
+ template_name = File.join "app", "views", view_name
47
+ template_files = Dir.glob "#{template_name}.*.erb"
48
+
49
+ if template_files.length > 1 && options[:format].nil?
50
+ raise Telegram::Errors::Renderer::MultipleTemplatesError, view_name
51
+ end
52
+
53
+ if template_files.length == 1
54
+ template_file = template_files[0]
55
+ else
56
+ template_file = template_files.find do |t|
57
+ template_file_format(t) == options[:format]
58
+ end
59
+ end
60
+
61
+ if template_file
62
+ template_format = template_file_format(template_file)
63
+ end
64
+
65
+ if !(template_file && template_format) || (options[:format] && template_format != options[:format])
66
+ raise Telegram::Errors::Renderer::BadTemplateFormatError, view_name
67
+ end
68
+
69
+ unless template_file
70
+ raise Telegram::Errors::Renderer::TemplateNotFoundError, view_name
71
+ end
72
+
73
+ [template_file, template_format]
74
+ end
75
+
76
+
77
+ def template_file_format name
78
+ extension = name.split('.')[-2]
79
+ template_format = extensions_map[extension] || extension
80
+ unless extensions_map.values.include? template_format
81
+ raise Telegram::Errors::Renderer::FormatNotSupportedError, template_format
82
+ end
83
+ template_format
84
+ end
85
+
86
+
87
+ def extensions_map
88
+ {
89
+ 'txt' => :text,
90
+ 'md' => :markdown,
91
+ 'html' => :html,
92
+ }
93
+ end
94
+
95
+
96
+ def template_extensions_whitelist
97
+ extensions_map.keys
98
+ end
99
+
100
+
101
+ end # Renderer
102
+ end # Controller
103
+ end #Telegram
@@ -0,0 +1,7 @@
1
+ module Telegram
2
+ class Controller
3
+ module Session
4
+ #WIP
5
+ end #Session
6
+ end #Controller
7
+ end #Telegram
@@ -0,0 +1,20 @@
1
+ module Telegram
2
+ class Controller
3
+ class ViewObject
4
+
5
+ def initialize(variables = {})
6
+ assign_instance_variables(variables)
7
+ end
8
+
9
+
10
+ private
11
+
12
+ def assign_instance_variables(hash)
13
+ hash.each do |key,value|
14
+ instance_variable_set "@#{key}", value
15
+ end
16
+ end
17
+
18
+ end
19
+ end # Controller
20
+ end # Telegram
@@ -1,5 +1,8 @@
1
+ require 'active_support/inflector'
2
+
1
3
  require 'telegram/utils/responder'
2
4
  require 'telegram/utils/keyboard_builder'
5
+ require 'telegram/controller/renderer'
3
6
 
4
7
 
5
8
  module Telegram
@@ -20,13 +23,69 @@ module Telegram
20
23
  end
21
24
 
22
25
 
23
- def explicit_response?
24
- @explicit_response
26
+ def dispatch action
27
+ @current_action = action
28
+ self.send action
29
+
30
+ render unless @has_response
31
+
32
+ @current_action = nil
33
+
34
+ # WIP rescue from
35
+ end
36
+
37
+
38
+ def not_exposed_instance_variables
39
+ [
40
+ :bots, :message,
41
+ :current_action, :rendered,
42
+ :bot_name, :has_response,
43
+ ]
44
+ end
45
+
46
+
47
+ # TEST
48
+ def expose_instance_variables
49
+ hash = {}
50
+ variables = instance_variables
51
+ variables -= not_exposed_instance_variables.map { |v| :"@#{v}" }
52
+
53
+ variables.each do |name|
54
+ # cut @
55
+ var_name = name[1..-1]
56
+ hash[var_name] = instance_variable_get(name)
57
+ end
58
+
59
+ hash
25
60
  end
26
61
 
27
62
 
28
63
  private
29
64
 
65
+ def has_response?
66
+ @has_response
67
+ end
68
+
69
+
70
+ def render opts = {}
71
+ raise Telegram::Errors::Renderer::DoubleRenderError if @rendered
72
+
73
+ response, response_format = Renderer.new(controller: self, action: @current_action).render(opts)
74
+ respond_with response, parse_mode: parse_modes[response_format]
75
+
76
+ @rendered = true
77
+ end
78
+
79
+
80
+ def parse_modes
81
+ {
82
+ markdown: 'Markdown',
83
+ html: 'Html',
84
+ text: nil,
85
+ }
86
+ end
87
+
88
+
30
89
  def bot
31
90
  @bots[@bot_name]
32
91
  end
@@ -44,7 +103,7 @@ module Telegram
44
103
 
45
104
  def send_message *args
46
105
  bot.api.send_message *args
47
- @explicit_response = true
106
+ @has_response = true
48
107
  end
49
108
 
50
109
 
@@ -53,7 +112,7 @@ module Telegram
53
112
  end
54
113
 
55
114
 
56
- def respond_with payload
115
+ def respond_with payload, rest = {}
57
116
  response = payload.clone
58
117
  # WIP угадывать тип, посмотреть какие есть типы ответа
59
118
  unless response.is_a? Hash
@@ -77,7 +136,7 @@ module Telegram
77
136
  response.delete :remove_keyboard
78
137
  end
79
138
 
80
- send_message default_response.merge(response)
139
+ send_message default_response.merge(response).merge(rest)
81
140
  end
82
141
 
83
142
 
@@ -0,0 +1,13 @@
1
+ require 'telegram/errors/common_error'
2
+
3
+ module Telegram
4
+ module Errors
5
+ module Renderer
6
+ class BadTemplateFormatError < Telegram::Errors::CommonError
7
+ def initialize view_name
8
+ super "Unable to find template for #{view_name}"
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ require 'telegram/errors/common_error'
2
+
3
+ module Telegram
4
+ module Errors
5
+ module Renderer
6
+ class DoubleRenderError < Telegram::Errors::CommonError
7
+ def initialize
8
+ super "Response is already rendered"
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ require 'telegram/errors/common_error'
2
+
3
+ module Telegram
4
+ module Errors
5
+ module Renderer
6
+ class FormatNotSupportedError < Telegram::Errors::CommonError
7
+ def initialize(template_format)
8
+ super "Format not supported: #{template_format}"
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ require 'telegram/errors/common_error'
2
+
3
+ module Telegram
4
+ module Errors
5
+ module Renderer
6
+ class MultipleTemplatesError < Telegram::Errors::CommonError
7
+ def initialize view_name
8
+ super "Multiple templates for #{view_name}. Please specify :format explicitly"
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ require 'telegram/errors/common_error'
2
+
3
+ module Telegram
4
+ module Errors
5
+ module Renderer
6
+ class TemplateNotFoundError < Telegram::Errors::CommonError
7
+ def initialize view_name
8
+ super "Template not found for #{view_name}"
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -65,21 +65,12 @@ module Telegram
65
65
  c.message = message
66
66
  end
67
67
 
68
- begin
69
- # WIP check explicit response...
70
- controller.send route[:action_name]
71
- rescue StandardError => e
72
- puts "RESCUE FROM"
73
- puts "RESCUE FROM"
74
- puts "RESCUE FROM"
75
- puts "RESCUE FROM"
76
- #HERE rescue_from!!!
77
- raise e
78
- end
68
+ controller.dispatch route[:action_name]
69
+ #HERE args support
79
70
  end
80
71
 
81
72
 
82
- #TODO test coverage
73
+ #TEST
83
74
  def parse_route route_data
84
75
  route_string = route_data[:route]
85
76
  options = route_data[:options]
@@ -1,5 +1,5 @@
1
1
  module Telegram
2
2
  module Rails
3
- VERSION = "0.1.0"
3
+ VERSION = "0.1.1"
4
4
  end
5
5
  end
@@ -1,7 +1,6 @@
1
1
  require 'active_support/inflector'
2
2
  require 'telegram/errors/controller/block_not_given_error'
3
- #WIP-- `ag byebug`
4
- require 'byebug'
3
+
5
4
 
6
5
  #WIP move to ...::Controller, продумать
7
6
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: telegram-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stanislav E. Govorov
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-04-25 00:00:00.000000000 Z
11
+ date: 2017-04-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: telegram-bot-ruby
@@ -53,7 +53,7 @@ dependencies:
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
55
  - !ruby/object:Gem::Dependency
56
- name: json
56
+ name: erubis
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
59
  - - ">="
@@ -120,9 +120,17 @@ files:
120
120
  - bin/setup
121
121
  - lib/telegram/client_adapter.rb
122
122
  - lib/telegram/controller.rb
123
+ - lib/telegram/controller/renderer.rb
124
+ - lib/telegram/controller/session.rb
125
+ - lib/telegram/controller/view_object.rb
123
126
  - lib/telegram/errors/common_error.rb
124
127
  - lib/telegram/errors/configuration/token_missing_error.rb
125
128
  - lib/telegram/errors/controller/block_not_given_error.rb
129
+ - lib/telegram/errors/renderer/bad_template_format_error.rb
130
+ - lib/telegram/errors/renderer/double_render_error.rb
131
+ - lib/telegram/errors/renderer/format_not_supported_error.rb
132
+ - lib/telegram/errors/renderer/multiple_templates_error.rb
133
+ - lib/telegram/errors/renderer/template_not_found_error.rb
126
134
  - lib/telegram/errors/routing/bad_route_error.rb
127
135
  - lib/telegram/errors/routing/bot_not_found_error.rb
128
136
  - lib/telegram/rails.rb