class Snapcat::Requestor

Constants

HASH_PATTERN
SECRET
STATIC_TOKEN

Attributes

auth_token[RW]

Public Class Methods

new(username) click to toggle source
# File lib/snapcat/requestor.rb, line 13
def initialize(username)
  @auth_token = STATIC_TOKEN
  @username = username
end

Public Instance Methods

request(endpoint, data = {}) click to toggle source
# File lib/snapcat/requestor.rb, line 18
def request(endpoint, data = {})
  response = self.class.post(
    "/#{endpoint}",
    body: merge_defaults_with(data),
    headers: request_headers
  )

  additional_fields = additional_fields_for(data)
  result = Response.new(response, additional_fields)

  auth_token_from(result, endpoint)
  result
end
request_media(snap_id) click to toggle source
# File lib/snapcat/requestor.rb, line 32
def request_media(snap_id)
  response = self.class.post(
    '/blob',
    body: merge_defaults_with({ id: snap_id, username: @username }),
    headers: request_headers
  )

  Response.new(response)
end
request_upload(data, type = nil) click to toggle source
# File lib/snapcat/requestor.rb, line 46
def request_upload(data, type = nil)
  encrypted_data = Crypt.encrypt(data)
  media = Media.new(encrypted_data, type)
  file_extension = media.file_extension

  begin
    file = Tempfile.new(['snap', ".#{file_extension}"])
    file.write(encrypted_data.force_encoding('utf-8'))
    file.rewind

    return request_with_username(
      'upload',
      data: file,
      media_id: media.generate_id(@username),
      type: media.type_code
    )
  ensure
    file.close
    file.unlink
  end
end
request_upload_story(data, time = nil, caption_text = nil, type = nil) click to toggle source
# File lib/snapcat/requestor.rb, line 68
def request_upload_story(data, time = nil, caption_text = nil, type = nil)
  encrypted_data = Crypt.encrypt(data)
  media = Media.new(encrypted_data, type)
  file_extension = media.file_extension

  begin
    file = Tempfile.new(['story', ".#{file_extension}"])
    file.write(encrypted_data.force_encoding('utf-8'))
    file.rewind

    return request_with_username(
      'retry_post_story',
      data: file,
      media_id: media.generate_id(@username),
      client_id: media.generate_id(@username),
      time: time || 3,
      caption_text_display: caption_text,
      type: media.type_code
    )
  ensure
    file.close
    file.unlink
  end
end
request_with_username(endpoint, data = {}) click to toggle source
# File lib/snapcat/requestor.rb, line 42
def request_with_username(endpoint, data = {})
  request(endpoint, data.merge({ username: @username }))
end

Private Instance Methods

additional_fields_for(data) click to toggle source
# File lib/snapcat/requestor.rb, line 95
def additional_fields_for(data)
  if data[:media_id]
    { media_id: data[:media_id] }
  else
    {}
  end
end
auth_token_from(result, endpoint) click to toggle source
# File lib/snapcat/requestor.rb, line 103
def auth_token_from(result, endpoint)
  if endpoint == 'logout'
    @auth_token = STATIC_TOKEN
  else
    @auth_token = result.auth_token || @auth_token
  end
end
built_token(auth_token, timestamp) click to toggle source
# File lib/snapcat/requestor.rb, line 111
def built_token(auth_token, timestamp)
  hash_a = Digest::SHA256.new << "#{SECRET}#{auth_token}"
  hash_b = Digest::SHA256.new << "#{timestamp}#{SECRET}"

  HASH_PATTERN.split(//).each_index.inject('') do |final_string, index|
    if HASH_PATTERN[index] == '1'
      final_string << hash_b.to_s[index].to_s
    else
      final_string << hash_a.to_s[index].to_s
    end
  end
end
merge_defaults_with(data) click to toggle source
# File lib/snapcat/requestor.rb, line 124
def merge_defaults_with(data)
  now = Timestamp.micro

  data.merge!({
    req_token: built_token(@auth_token, now),
    timestamp: now
  })
end
request_headers() click to toggle source
# File lib/snapcat/requestor.rb, line 133
def request_headers
  {'User-Agent' => 'Snapchat/8.1.1 (iPhone; iOS 8.1.1; gzip)',
   'Accept-Language' => 'en',
   'Accept-Locale' => 'en'}
end