Class: MaxBotApi::Resources::Uploads

Inherits:
Object
  • Object
show all
Defined in:
lib/max_bot_api/resources/uploads.rb

Overview

Upload helpers for media and photos.

Constant Summary collapse

UPLOAD_TYPES =
{
  photo: 'image',
  video: 'video',
  audio: 'audio',
  file: 'file'
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Uploads

Returns a new instance of Uploads.



17
18
19
# File 'lib/max_bot_api/resources/uploads.rb', line 17

def initialize(client)
  @client = client
end

Instance Method Details

#upload_media_from_file(type:, filename:) ⇒ Object

Upload media from a local file.

Parameters:

  • type (Symbol, String)
  • filename (String)


24
25
26
27
28
# File 'lib/max_bot_api/resources/uploads.rb', line 24

def upload_media_from_file(type:, filename:)
  File.open(filename, 'rb') do |fh|
    upload_media_from_reader_with_name(type: type, io: fh, name: File.basename(filename))
  end
end

#upload_media_from_reader(type:, io:) ⇒ Object

Upload media from an IO object.

Parameters:

  • type (Symbol, String)
  • io (#read)


46
47
48
# File 'lib/max_bot_api/resources/uploads.rb', line 46

def upload_media_from_reader(type:, io:)
  upload_media_from_reader_with_name(type: type, io: io, name: nil)
end

#upload_media_from_reader_with_name(type:, io:, name: nil) ⇒ Object

Upload media from IO with explicit name.

Parameters:

  • type (Symbol, String)
  • io (#read)
  • name (String, nil) (defaults to: nil)


54
55
56
# File 'lib/max_bot_api/resources/uploads.rb', line 54

def upload_media_from_reader_with_name(type:, io:, name: nil)
  upload_media_from_reader_internal(type: type, io: io, name: name)
end

#upload_media_from_url(type:, url:) ⇒ Object

Upload media from a remote URL.

Parameters:

  • type (Symbol, String)
  • url (String)


33
34
35
36
37
38
39
40
41
# File 'lib/max_bot_api/resources/uploads.rb', line 33

def upload_media_from_url(type:, url:)
  response = Faraday.get(url.to_s)
  raise Error, "fetch URL failed: HTTP #{response.status}" unless response.status.between?(200, 299)

  name = attachment_name(response.headers)
  upload_media_from_reader_with_name(type: type, io: StringIO.new(response.body), name: name)
rescue Faraday::Error => e
  raise NetworkError.new(op: 'GET upload source', original_error: e)
end

#upload_photo_from_base64_string(code:) ⇒ Object

Upload a photo from base64 content.

Parameters:

  • code (String)


66
67
68
69
# File 'lib/max_bot_api/resources/uploads.rb', line 66

def upload_photo_from_base64_string(code:)
  decoded = Base64.decode64(code)
  upload_media_from_reader_with_name(type: :photo, io: StringIO.new(decoded), name: nil)
end

#upload_photo_from_file(path:) ⇒ Object

Upload a photo from a local file.

Parameters:

  • path (String)


60
61
62
# File 'lib/max_bot_api/resources/uploads.rb', line 60

def upload_photo_from_file(path:)
  upload_media_from_file(type: :photo, filename: path)
end

#upload_photo_from_reader(io:) ⇒ Object

Upload a photo from an IO object.

Parameters:

  • io (#read)


79
80
81
# File 'lib/max_bot_api/resources/uploads.rb', line 79

def upload_photo_from_reader(io:)
  upload_media_from_reader(type: :photo, io: io)
end

#upload_photo_from_reader_with_name(io:, name:) ⇒ Object

Upload a photo from IO with explicit name.

Parameters:

  • io (#read)
  • name (String)


86
87
88
# File 'lib/max_bot_api/resources/uploads.rb', line 86

def upload_photo_from_reader_with_name(io:, name:)
  upload_media_from_reader_with_name(type: :photo, io: io, name: name)
end

#upload_photo_from_url(url:) ⇒ Object

Upload a photo from a remote URL.

Parameters:

  • url (String)


73
74
75
# File 'lib/max_bot_api/resources/uploads.rb', line 73

def upload_photo_from_url(url:)
  upload_media_from_url(type: :photo, url: url)
end