Class: Snapcat::Requestor

Inherits:
Object
  • Object
show all
Includes:
HTTMultiParty
Defined in:
lib/snapcat/requestor.rb

Constant Summary collapse

SECRET =
'iEk21fuwZApXlz93750dmW22pw389dPwOk'
STATIC_TOKEN =
'm198sOkJEn37DjqZ32lpRu76xmw288xSQ9'
HASH_PATTERN =
'0001110111101110001111010101111011010001001110011000110001000110'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(username) ⇒ Requestor

Returns a new instance of Requestor.



13
14
15
16
# File 'lib/snapcat/requestor.rb', line 13

def initialize(username)
  @auth_token = STATIC_TOKEN
  @username = username
end

Instance Attribute Details

#auth_tokenObject

Returns the value of attribute auth_token.



9
10
11
# File 'lib/snapcat/requestor.rb', line 9

def auth_token
  @auth_token
end

Instance Method Details

#request(endpoint, data = {}) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
# 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) ⇒ Object



32
33
34
35
36
37
38
39
40
# 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) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# 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) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# 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 = {}) ⇒ Object



42
43
44
# File 'lib/snapcat/requestor.rb', line 42

def request_with_username(endpoint, data = {})
  request(endpoint, data.merge({ username: @username }))
end