Skip to content

Commit f8da531

Browse files
authored
Add an example of narrowcast (#569)
related to #533 > narrowcast Fixed a bug that caused the API to request with the replacement of reserved Ruby characters like `_and` 7eca519
1 parent 4f8cfe5 commit f8da531

File tree

3 files changed

+72
-2
lines changed

3 files changed

+72
-2
lines changed

examples/v2/kitchensink/app.rb

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1064,6 +1064,57 @@ def rich_menu_request_b
10641064

10651065
reply_text(event, "[STATS]\n#{stats}")
10661066

1067+
when 'narrowcast'
1068+
request = Line::Bot::V2::MessagingApi::NarrowcastRequest.new(
1069+
messages: [
1070+
Line::Bot::V2::MessagingApi::TextMessage.new(text: 'Hello, this is a narrowcast message')
1071+
],
1072+
filter: Line::Bot::V2::MessagingApi::Filter.new(
1073+
demographic: Line::Bot::V2::MessagingApi::OperatorDemographicFilter.new(
1074+
_or: [
1075+
Line::Bot::V2::MessagingApi::OperatorDemographicFilter.new(
1076+
_and: [
1077+
Line::Bot::V2::MessagingApi::AgeDemographicFilter.new(
1078+
gte: 'age_20',
1079+
lte: 'age_60'
1080+
),
1081+
Line::Bot::V2::MessagingApi::AppTypeDemographicFilter.new(
1082+
one_of: ['ios']
1083+
)
1084+
]
1085+
),
1086+
Line::Bot::V2::MessagingApi::OperatorDemographicFilter.new(
1087+
_and: [
1088+
Line::Bot::V2::MessagingApi::GenderDemographicFilter.new(
1089+
one_of: ['female']
1090+
),
1091+
Line::Bot::V2::MessagingApi::AreaDemographicFilter.new(
1092+
one_of: %w(jp_08 jp_09 jp_10 jp_11 jp_12 jp_13 jp_14)
1093+
),
1094+
]
1095+
)
1096+
]
1097+
)
1098+
)
1099+
)
1100+
_body, _status_code, headers = client.narrowcast_with_http_info(narrowcast_request: request)
1101+
request_id = headers['x-line-request-id']
1102+
1103+
reply_text(event, "Narrowcast requested, requestId: #{request_id}")
1104+
1105+
client.show_loading_animation(show_loading_animation_request: Line::Bot::V2::MessagingApi::ShowLoadingAnimationRequest.new(
1106+
chat_id: event.source.user_id
1107+
))
1108+
sleep 5
1109+
1110+
response = client.get_narrowcast_progress(request_id: request_id)
1111+
client.push_message(push_message_request: Line::Bot::V2::MessagingApi::PushMessageRequest.new(
1112+
to: event.source.user_id,
1113+
messages: [
1114+
Line::Bot::V2::MessagingApi::TextMessage.new(text: "Narrowcast status: #{response}")
1115+
]
1116+
))
1117+
10671118
else
10681119
if (event.message.quoted_message_id != nil)
10691120
reply_text(event, "[ECHO]\n#{event.message.text} Thanks you for quoting my message!")

lib/line/bot/v2/utils.rb

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,26 @@ def self.deep_to_hash(object)
4444
if object.is_a?(Array)
4545
object.map { |item| deep_to_hash(item) }
4646
elsif object.is_a?(Hash)
47-
object.transform_keys(&:to_sym).transform_values { |v| deep_to_hash(v) }
47+
result = object.transform_keys do |k|
48+
if k.to_s.start_with?('_') && Line::Bot::V2::RESERVED_WORDS.include?(k.to_s.delete_prefix('_').to_sym)
49+
k.to_s.delete_prefix('_').to_sym
50+
else
51+
k.to_sym
52+
end
53+
end
54+
result.transform_values { |v| deep_to_hash(v) }
4855
elsif object.instance_variables.empty?
4956
object
5057
else
5158
object.instance_variables.each_with_object({}) do |var, hash| # steep:ignore UnannotatedEmptyCollection
5259
value = object.instance_variable_get(var)
53-
key = var.to_s.delete('@').to_sym
60+
61+
key = var.to_s.delete('@')
62+
if key.start_with?('_') && Line::Bot::V2::RESERVED_WORDS.include?(key.delete_prefix('_').to_sym)
63+
key = key.delete_prefix('_')
64+
end
65+
key = key.to_sym
66+
5467
hash[key] = deep_to_hash(value)
5568
end
5669
end

spec/line/bot/v2/utils_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,12 @@ def initialize(name)
8989
expected_output = [{ name: 'Alice' }, { name: 'Bob' }]
9090
expect(Line::Bot::V2::Utils.deep_to_hash(input)).to eq(expected_output)
9191
end
92+
93+
it 'fixes reserved words' do
94+
input = { '_and' => 123, '___FILE__' => 'example', '_hoge': 123 }
95+
expected_output = { and: 123, __FILE__: 'example', _hoge: 123 }
96+
expect(Line::Bot::V2::Utils.deep_to_hash(input)).to eq(expected_output)
97+
end
9298
end
9399

94100
describe '.deep_camelize' do

0 commit comments

Comments
 (0)