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

Class Method Details

.channels_for_url(channels, should_encode = true) ⇒ Object

Returns string with all channels separated by comma or single coma



135
136
137
138
139
# File 'lib/pubnub/formatter.rb', line 135

def channels_for_url(channels, should_encode = true)
  channel = channels.map { |c| should_encode ? encode(c) : c }.join(',')
  channel = ',' if channel.empty?
  channel
end

.classify_method(method) ⇒ Object

Quite lazy way, but good enough for current usage



68
69
70
# File 'lib/pubnub/formatter.rb', line 68

def classify_method(method)
  method.split('_').map(&:capitalize).join
end

.encode(string) ⇒ Object



72
73
74
# File 'lib/pubnub/formatter.rb', line 72

def encode(string)
  URI.encode_www_form_component(string).gsub('+', '%20')
end

.format_channel(channel, should_encode = false) ⇒ Object

Returns array of encoded channels if should_encode is true, otherwise returns just array of channels



12
13
14
15
16
17
18
19
20
# File 'lib/pubnub/formatter.rb', line 12

def format_channel(channel, should_encode = false)
  make_channel_array(channel).map do |chan|
    if should_encode
      encode(chan)
    else
      chan.to_s
    end
  end
end

.format_group(group, should_encode = true) ⇒ Object



22
23
24
25
26
# File 'lib/pubnub/formatter.rb', line 22

def format_group(group, should_encode = true)
  format_channel(group, should_encode).map do |g|
    g.gsub '%3A', ':'
  end
end

.format_message(message, crypto = nil, uri_escape = true) ⇒ String?

Transforms message to json and encode it.

Parameters:

  • message (Hash, String, Integer, Boolean)

    Message data which should be formatted.

  • crypto (Crypto::CryptoProvider, nil) (defaults to: nil)

    Crypto which should be used to encrypt message data.

  • uri_escape (Boolean, nil) (defaults to: true)

    Whether formatted message should escape to be used as part of URI or not.

Returns:

  • (String, nil)

    Formatted message data.



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/pubnub/formatter.rb', line 53

def format_message(message, crypto = nil, uri_escape = true)
  json_message = message.to_json
  if crypto
    encrypted_data = crypto&.encrypt(json_message)
    json_message = Base64.strict_encode64(encrypted_data).to_json unless encrypted_data.nil?
  end

  if uri_escape
    json_message = Formatter.encode(json_message) if crypto.nil?
    json_message = Addressable::URI.escape(json_message).to_s unless crypto.nil?
  end
  json_message
end

.format_presence_channel(presence) ⇒ Object



28
29
30
31
32
# File 'lib/pubnub/formatter.rb', line 28

def format_presence_channel(presence)
  format_channel(
    make_channel_array(presence).map { |p| p + '-pnpres' }
  )
end

.format_uuid(uuids, should_encode = true) ⇒ Object



34
35
36
37
38
39
40
41
42
# File 'lib/pubnub/formatter.rb', line 34

def format_uuid(uuids, should_encode = true)
  make_uuid_array(uuids).map do |uuid|
    if should_encode
      encode(uuid)
    else
      uuid.to_s
    end
  end
end

.make_channel_array(channel) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/pubnub/formatter.rb', line 76

def make_channel_array(channel)
  case channel.class.to_s
  when 'String'
    channel.to_s.split(',')
  when 'Symbol'
    channel.to_s.split(',')
  when 'Array'
    channel.map(&:to_s)
  when 'NilClass'
    []
  else
    raise Pubnub::ArgumentError.new(
      message: 'Channel has to be String, Symbol or Array'
    ), 'Channel has to be String, Symbol or Array'
  end
end

.make_uuid_array(uuid) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/pubnub/formatter.rb', line 93

def make_uuid_array(uuid)
  case uuid.class.to_s
  when 'String'
    uuid.to_s.split(',')
  when 'Symbol'
    uuid.to_s.split(',')
  when 'Array'
    uuid.map(&:to_s)
  when 'NilClass'
    []
  else
    raise Pubnub::ArgumentError.new(
      message: 'UUID has to be String, Symbol or Array'
    ), 'UUID has to be String, Symbol or Array'
  end
end

.params_hash_to_url_params(hash) ⇒ Object

Formats hash to params string



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/pubnub/formatter.rb', line 118

def params_hash_to_url_params(hash)
  params = ''
  hash.sort_by { |k, _v| k.to_s }.to_h.each do |key, value|
    if %w[meta ortt filter].include?(key.to_s)
      value_for_encoding = value.is_a?(String) ? value : value.to_json
      encoded_value = URI.encode_www_form_component(value_for_encoding).gsub('+', '%20')
      params << "#{key}=#{encoded_value}&"
    elsif %w[t state filter-expr].include?(key.to_s)
      params << "#{key}=#{value}&"
    else
      params << "#{key}=#{URI.encode_www_form_component(value).gsub('+', '%20')}&"
    end
  end
  params.chop! if params[-1] == '&'
end

.parse_json(string) ⇒ Object

Parses string to JSON



111
112
113
114
115
# File 'lib/pubnub/formatter.rb', line 111

def parse_json(string)
  [JSON.parse(string), nil]
rescue JSON::ParserError => _e
  [nil, JSON::ParserError]
end