rubotnik 0.2.0 → 0.2.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 33af86f4563629621c9a60e41d8ed6e18b57a7bea3049771f1dc4a7d231f09af
4
- data.tar.gz: f8231e56a6526c0f371360c5824a148db4c302e5c1b58436598ad4fb0d554b83
3
+ metadata.gz: dfec0763c4edd0680ea995ab6d4de3f8c18ab2bfb15f4e267068b546c9e39390
4
+ data.tar.gz: 06d5e0d7d702ccb63ba4a173f705da25f64c76e189ef35d1bc59994257005734
5
5
  SHA512:
6
- metadata.gz: e5b74060181a95947438daca6a069e45d31f29bdda0e352ca51a1aa47b6003a4f4948bb7b5014511cc0fa50bbaa8c80a4a03458a0ef176f7f09c14bb1a01aa44
7
- data.tar.gz: d86d3550536e110c6aa76c1fea981860c002b7b7e183a55e1dd68cd2c2872fd882518b81e98b78df2388af5038a67cc678dd3e5c5e173ef30aa508cdc3ca269f
6
+ metadata.gz: af0c8bad43c4a19c71ef3cffd0e9dd94f5bb07ae23245c3038738da56b3fede8bb98d8e642544265c6def6ec0760c86eca6216d31614188ee335094a0b6a9366
7
+ data.tar.gz: 8047dfa3db4aaf51a102cf29100b818030ef1a4c93cbe48f9126af83f601eaebcdee839bacd9beed0791e31c7ea55cd200b03c4e06b14a96312ee83594282f60
data/lib/rubotnik.rb CHANGED
@@ -23,14 +23,17 @@ module Rubotnik
23
23
  end
24
24
 
25
25
  def self.route(event, &block)
26
- Bot.on(event, &block) unless [:message, :postback].include?(event)
27
- Bot.on event do |e|
28
- case e
29
- when Facebook::Messenger::Incoming::Message
30
- Rubotnik::MessageDispatch.new(e).route(&block)
31
- when Facebook::Messenger::Incoming::Postback
32
- Rubotnik::PostbackDispatch.new(e).route(&block)
26
+ if [:message, :postback].include?(event)
27
+ Bot.on event do |e|
28
+ case e
29
+ when Facebook::Messenger::Incoming::Message
30
+ Rubotnik::MessageDispatch.new(e).route(&block)
31
+ when Facebook::Messenger::Incoming::Postback
32
+ Rubotnik::PostbackDispatch.new(e).route(&block)
33
+ end
33
34
  end
35
+ else
36
+ Bot.on(event, &block)
34
37
  end
35
38
  end
36
39
 
@@ -9,7 +9,7 @@ module Rubotnik
9
9
  GRAPH_URL = 'https://graph.facebook.com/v2.8/'.freeze
10
10
 
11
11
  # abstraction over Bot.deliver to send messages declaratively and directly
12
- def say(text = 'What was I talking about?', quick_replies: [], user: @user)
12
+ def say(text, quick_replies: [], user: @user)
13
13
  message_options = {
14
14
  recipient: { id: user.id },
15
15
  message: { text: text }
@@ -18,11 +18,14 @@ module Rubotnik
18
18
  message_options[:message][:quick_replies] = UI::QuickReplies
19
19
  .build(*quick_replies)
20
20
  end
21
- Bot.deliver(message_options, access_token: ENV['ACCESS_TOKEN'])
21
+
22
+ send_message(message_options)
22
23
  end
23
24
 
24
25
  def show(ui_element, user: @user)
25
- ui_element.send(user)
26
+ payload = ui_element.build(user)
27
+
28
+ send_message(payload)
26
29
  end
27
30
 
28
31
  def next_command(command)
@@ -38,7 +41,7 @@ module Rubotnik
38
41
  end
39
42
 
40
43
  def message_contains_location?
41
- @message.attachments && @message.attachments.first['type'] == 'location'
44
+ !@message.attachments.nil? && @message.attachments.first['type'] == 'location'
42
45
  end
43
46
 
44
47
  # Get user info from Graph API. Takes names of required fields as symbols
@@ -49,8 +52,7 @@ module Rubotnik
49
52
  ENV['ACCESS_TOKEN']
50
53
  begin
51
54
  return call_graph_api(url)
52
- rescue
53
- puts "Couldn't access URL" # logging
55
+ rescue => e
54
56
  return false
55
57
  end
56
58
  end
@@ -61,11 +63,16 @@ module Rubotnik
61
63
  @message.typing_off
62
64
  case response.code
63
65
  when 200
64
- puts "User data received from Graph API: #{response.body}" # logging
66
+ puts "Data received from Graph API: #{response.body}" # logging
65
67
  return JSON.parse(response.body, symbolize_names: true)
66
68
  else
69
+ puts "Request failed: #{response.body}"
67
70
  return false
68
71
  end
69
72
  end
73
+
74
+ def send_message(payload)
75
+ Bot.deliver(payload, access_token: ENV['ACCESS_TOKEN'])
76
+ end
70
77
  end
71
78
  end
@@ -13,14 +13,14 @@ module Rubotnik
13
13
  @message = message
14
14
  p @message.class
15
15
  p @message
16
- @user = UserStore.instance.find_or_create_user(@message.sender['id'])
16
+ @user = Rubotnik::UserStore.instance.find_or_create_user(@message.sender['id'])
17
17
  end
18
18
 
19
19
  def route(&block)
20
20
  if @user.current_command
21
21
  command = @user.current_command
22
22
  execute(command)
23
- puts "Command #{command} is executed for user #{@user.id}" # log
23
+ puts "Command #{command} is executed for user #{@user.id}"
24
24
  else
25
25
  bind_commands(&block)
26
26
  end
@@ -13,7 +13,7 @@ module Rubotnik
13
13
  @postback = postback
14
14
  p @postback.class
15
15
  p @postback
16
- @user = UserStore.instance.find_or_create_user(@postback.sender['id'])
16
+ @user = Rubotnik::UserStore.instance.find_or_create_user(@postback.sender['id'])
17
17
  end
18
18
 
19
19
  def route(&block)
@@ -36,10 +36,10 @@ module Rubotnik
36
36
  yield
37
37
  return
38
38
  end
39
- handle_commands(to, reply_with)
39
+ handle_command(to, reply_with)
40
40
  end
41
41
 
42
- def handle_commands(to, reply_with)
42
+ def handle_command(to, reply_with)
43
43
  if reply_with.empty?
44
44
  execute(to)
45
45
  puts "Command #{to} is executed for user #{@user.id}"
data/lib/rubotnik/user.rb CHANGED
@@ -1,4 +1,4 @@
1
- class User
1
+ class Rubotnik::User
2
2
  attr_reader :id
3
3
  attr_accessor :session
4
4
 
@@ -1,7 +1,7 @@
1
1
  require 'singleton'
2
2
  require_relative 'user'
3
3
  # In-memory storage for users
4
- class UserStore
4
+ class Rubotnik::UserStore
5
5
  include Singleton
6
6
  attr_reader :users
7
7
 
@@ -10,7 +10,7 @@ class UserStore
10
10
  end
11
11
 
12
12
  def find_or_create_user(id)
13
- find(id) || add(User.new(id))
13
+ find(id) || add(Rubotnik::User.new(id))
14
14
  end
15
15
 
16
16
  def add(user)
@@ -1,3 +1,3 @@
1
1
  module Rubotnik
2
- VERSION = "0.2.0"
2
+ VERSION = "0.2.1"
3
3
  end
@@ -4,12 +4,6 @@
4
4
 
5
5
  module UI
6
6
  class BaseUiElement
7
- # Sends the valid JSON to Messenger API
8
- def send(user)
9
- formed = build(user)
10
- Bot.deliver(formed, access_token: ENV['ACCESS_TOKEN'])
11
- end
12
-
13
7
  # Use this method to return a valid hash and save it for later
14
8
  def build(user)
15
9
  @template[:recipient][:id] = user.id
@@ -4,6 +4,8 @@ module UI
4
4
  ########################### BUTTON TEMPLATE #############################
5
5
  # https://developers.facebook.com/docs/messenger-platform/send-api-reference/button-template
6
6
  class FBButtonTemplate < UI::BaseUiElement
7
+ include UI::Common::HasButtons
8
+
7
9
  def initialize(text, buttons)
8
10
  @template = {
9
11
  recipient: {
@@ -23,19 +23,22 @@ module UI
23
23
 
24
24
  # set image aspect ratio to 'square'
25
25
  def square_images
26
- @template[:message][:attachment][:payload][:image_aspect_ratio] = 'square'
26
+ self.image_aspect_ratio = 'square'
27
27
  self
28
28
  end
29
29
 
30
- # set image aspect ratio to 'square'
30
+ # set image aspect ratio to 'horizontal'
31
31
  def horizontal_images
32
- hrz = 'horizontal'
33
- @template[:message][:attachment][:payload][:image_aspect_ratio] = hrz
32
+ self.image_aspect_ratio = 'horizontal'
34
33
  self
35
34
  end
36
35
 
37
36
  private
38
37
 
38
+ def image_aspect_ratio=(type)
39
+ @template[:message][:attachment][:payload][:image_aspect_ratio] = type
40
+ end
41
+
39
42
  def parse_elements(elements)
40
43
  elements = [elements] if elements.class == Hash
41
44
  elements.map do |elt|
@@ -20,8 +20,7 @@ module UI
20
20
  private_class_method def self.build_from_hash(reply)
21
21
  unless reply.key?(:content_type)
22
22
  reply[:content_type] = 'text'
23
- error_msg = "type 'text' should have a payload"
24
- raise ArgumentError, error_msg unless reply.key?(:payload)
23
+ raise ArgumentError, "type 'text' should have a payload" unless reply.key?(:payload)
25
24
  end
26
25
  reply
27
26
  end
data/rubotnik.gemspec CHANGED
@@ -20,7 +20,6 @@ Gem::Specification.new do |spec|
20
20
  spec.require_paths = ["lib"]
21
21
 
22
22
  spec.add_dependency 'facebook-messenger'
23
- spec.add_dependency 'addressable'
24
23
  spec.add_dependency 'dotenv'
25
24
  spec.add_dependency 'httparty'
26
25
  spec.add_dependency 'puma'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubotnik
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andy Barnov
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-05-14 00:00:00.000000000 Z
11
+ date: 2018-06-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: facebook-messenger
@@ -24,20 +24,6 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
- - !ruby/object:Gem::Dependency
28
- name: addressable
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - ">="
32
- - !ruby/object:Gem::Version
33
- version: '0'
34
- type: :runtime
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - ">="
39
- - !ruby/object:Gem::Version
40
- version: '0'
41
27
  - !ruby/object:Gem::Dependency
42
28
  name: dotenv
43
29
  requirement: !ruby/object:Gem::Requirement