Module: GmailComposeEncoder

Defined in:
lib/gmail_compose_encoder.rb,
lib/gmail_compose_encoder/encoder.rb,
lib/gmail_compose_encoder/version.rb

Constant Summary collapse

FULL_ALPHABET =
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
RESTRICTED_ALPHABET =
'BCDFGHJKLMNPQRSTVWXZbcdfghjklmnpqrstvwxz'
THREAD_PREFIX =
'thread-'
MESSAGE_PREFIX =
'msg-'
VERSION =
"0.1.0"

Class Method Summary collapse

Class Method Details

.compose_draft_url(from_address, thread_id, message_id) ⇒ Object



5
6
7
8
# File 'lib/gmail_compose_encoder.rb', line 5

def self.compose_draft_url(from_address, thread_id, message_id)
  tmp=encode("thread-f:#{thread_id.to_i(16)}+msg-a:#{message_id}")
  "https://mail.google.com/mail/u/#{from_address}/#all?compose=#{tmp}"
end

.encode(url_parameter) ⇒ Object

for whatever reason, google choose to Base64.encode64 the string, and then further map it into a restricted alphabet with no vowels or numbers.



11
12
13
# File 'lib/gmail_compose_encoder/encoder.rb', line 11

def self.encode(url_parameter)
  transliterate(Base64.strict_encode64(url_parameter.gsub(THREAD_PREFIX,'')).gsub(/=/,''), FULL_ALPHABET, RESTRICTED_ALPHABET)
end

.transliterate(string, input_alphabet = FULL_ALPHABET, output_alphabet = RESTRICTED_ALPHABET) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/gmail_compose_encoder/encoder.rb', line 15

def self.transliterate(string, input_alphabet=FULL_ALPHABET, output_alphabet=RESTRICTED_ALPHABET)
  string_indices = string.chars.reverse.map {|char| input_alphabet.index(char)}
  # string_indices.each {|xxx| puts "inputIndices #{xxx}"}
  result_indices = []
  string_indices.reverse.each_with_index do |string_index,iii|
    offset=0
    result_indices.each.with_index do |result_index,j|
      index = result_index * input_alphabet.size + offset
      if(index >=input_alphabet.size)
        remainder = index % output_alphabet.size
        offset = (index - remainder) / output_alphabet.size
        index=remainder
      else
        offset = 0
      end
      result_indices[j]=index
    end
    while(!offset.zero?)
      remainder = offset % output_alphabet.size
      result_indices.push(remainder)
      offset = (offset - remainder) / output_alphabet.size
    end
    jjj=0
    offset=string_index
    while(!offset.zero?)
      result_indices[jjj] = 0 if result_indices[jjj].nil?
      index = result_indices[jjj] + offset
      if(index >=output_alphabet.size)
        remainder = index % output_alphabet.size
        offset = (index - remainder) / output_alphabet.size
        result_indices[jjj]=remainder
      else
        offset=0
        result_indices[jjj]=index
      end
      jjj+=1
    end
  end

  result_indices.reverse.map {|idx| output_alphabet[idx]}.join
end