Module: Pubnub::Formatter
- Included in:
- Heartbeat, SubscribeEvent
- Defined in:
- lib/pubnub/formatter.rb
Overview
Bunch of useful methods that are used in many parts of Pubnub and and can be usable for any app that uses Pubnub library
Class Method Summary collapse
-
.channels_for_url(channels) ⇒ Object
Returns string with all channels separated by comma or single coma.
-
.classify_method(method) ⇒ Object
Quite lazy way, but good enough for current usage.
- .encode(string) ⇒ Object
-
.format_channel(channel, should_encode = true) ⇒ Object
Returns array of encoded channels if should_encode is true, otherwise returns just array of channels.
- .format_group(group, should_encode = true) ⇒ Object
-
.format_message(message, cipher_key, uri_escape = true) ⇒ Object
Transforms message to json and encode it.
- .format_presence_channel(presence) ⇒ Object
- .make_channel_array(channel) ⇒ Object
-
.params_hash_to_url_params(hash) ⇒ Object
Formats hash to params string.
-
.parse_json(string) ⇒ Object
Parses string to JSON.
Class Method Details
.channels_for_url(channels) ⇒ Object
Returns string with all channels separated by comma or single coma
93 94 95 96 97 |
# File 'lib/pubnub/formatter.rb', line 93 def channels_for_url(channels) channel = channels.sort.join(',') channel = ',' if channel.empty? channel end |
.classify_method(method) ⇒ Object
Quite lazy way, but good enough for current usage
51 52 53 |
# File 'lib/pubnub/formatter.rb', line 51 def classify_method(method) method.split('_').map(&:capitalize).join end |
.encode(string) ⇒ Object
55 56 57 |
# File 'lib/pubnub/formatter.rb', line 55 def encode(string) URI.encode_www_form_component(string).gsub('+', '%20') end |
.format_channel(channel, should_encode = true) ⇒ Object
Returns array of encoded channels if should_encode is true, otherwise returns just array of channels
9 10 11 12 13 14 15 16 17 |
# File 'lib/pubnub/formatter.rb', line 9 def format_channel(channel, should_encode = true) make_channel_array(channel).map do |chan| if should_encode encode(chan) else chan end end end |
.format_group(group, should_encode = true) ⇒ Object
19 20 21 22 23 |
# File 'lib/pubnub/formatter.rb', line 19 def format_group(group, should_encode = true) format_channel(group, should_encode).map do |g| g.gsub '%3A', ':' end end |
.format_message(message, cipher_key, uri_escape = true) ⇒ Object
Transforms message to json and encode it
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/pubnub/formatter.rb', line 32 def (, cipher_key, uri_escape = true) if cipher_key pc = Pubnub::Crypto.new(cipher_key) = pc.encrypt() if uri_escape URI.escape(.to_json) else .to_json end else if uri_escape Formatter.encode(.to_json) else .to_json end end end |
.format_presence_channel(presence) ⇒ Object
25 26 27 28 29 |
# File 'lib/pubnub/formatter.rb', line 25 def format_presence_channel(presence) format_channel( make_channel_array(presence).map { |p| p + '-pnpres' } ) end |
.make_channel_array(channel) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/pubnub/formatter.rb', line 59 def make_channel_array(channel) case channel.class.to_s when 'String' then channel.to_s.split(',') when 'Symbol' then channel.to_s.split(',') when 'Array' then channel.map(&:to_s) when 'NilClass' then [] else fail Pubnub::ArgumentError.new( message: 'Channel has to be String, Symbol or Array' ), 'Channel has to be String, Symbol or Array' end end |
.params_hash_to_url_params(hash) ⇒ Object
Formats hash to params string
79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/pubnub/formatter.rb', line 79 def params_hash_to_url_params(hash) params = '' hash.each do |key, value| if %w(meta).include?(key.to_s) encoded_value = URI.encode_www_form_component(value.to_json).gsub('+', '%20') params << "#{key}=#{encoded_value}&" else params << "#{key}=#{value}&" end end params.chop! if params[-1] == '&' end |
.parse_json(string) ⇒ Object
Parses string to JSON
72 73 74 75 76 |
# File 'lib/pubnub/formatter.rb', line 72 def parse_json(string) [JSON.parse(string), nil] rescue JSON::ParserError => error [nil, error] end |