Module: MixinBot::API::Attachment

Included in:
MixinBot::API
Defined in:
lib/mixin_bot/api/attachment.rb

Instance Method Summary collapse

Instance Method Details

#create_attachmentObject

developers.mixin.one/api/beta-mixin-message/create-attachment/ Sample Response {

"data":{
  "type":"attachment",
  "attachment_id":"7a54e394-1626-4cd4-b967-543932c2a032",
  "upload_url":"https://moments-shou-tv.s3.amazonaws.com/mixin/attachments/xxx",
  "view_url":"https://moments.shou.tv/mixin/attachments/1526305123xxxx"
}

} Once get the upload_url, use it to upload the your file via PUT request



17
18
19
20
21
22
# File 'lib/mixin_bot/api/attachment.rb', line 17

def create_attachment
  path = '/attachments'
  access_token ||= access_token('POST', path, {}.to_json)
  authorization = format('Bearer %<access_token>s', access_token: access_token)
  client.post(path, headers: { 'Authorization': authorization }, json: {})
end

#read_attachment(attachment_id) ⇒ Object



44
45
46
47
48
49
# File 'lib/mixin_bot/api/attachment.rb', line 44

def read_attachment(attachment_id)
  path = format('/attachments/%<id>s', id: attachment_id)
  access_token ||= access_token('GET', path, '')
  authorization = format('Bearer %<access_token>s', access_token: access_token)
  client.get(path, headers: { 'Authorization': authorization })
end

#upload_attachment(file) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/mixin_bot/api/attachment.rb', line 24

def upload_attachment(file)
  attachment = create_attachment['data']

  HTTP
    .timeout(connect: 5, write: 5, read: 5)
    .request(
      :put,
      attachment.delete('upload_url'),
      {
        body: file,
        headers: {
          'x-amz-acl': 'public-read',
          'Content-Type': 'application/octet-stream'
        }
      }
    )

  attachment
end