Class: WeebSh::Toph

Inherits:
Interface show all
Defined in:
lib/weeb/interfaces/toph.rb

Overview

Image API

Instance Attribute Summary

Attributes inherited from Interface

#api_url, #auth, #user_agent

Instance Method Summary collapse

Methods inherited from Interface

#initialize

Constructor Details

This class inherits a constructor from WeebSh::Interface

Instance Method Details

#add_tags_to_image(image, tags) ⇒ WeebImage

Add tags to an image.


101
102
103
104
105
# File 'lib/weeb/interfaces/toph.rb', line 101

def add_tags_to_image(image, tags)
  image = image.resolve_id if image.respond_to?(:resolve_id)
  tags = tags.map { |t| t.name if t.is_a(Tag) }
  WeebImage.new(API::Toph.add_tags_to_image(self, image, tags), self)
end

#delete_image(id) ⇒ WeebImage Also known as: remove_image

Delete an image.


91
92
93
94
# File 'lib/weeb/interfaces/toph.rb', line 91

def delete_image(id)
  id = id.resolve_id if id.respond_to?(:resolve_id)
  WeebImage.new(API::Toph.delete_image(self, id), self)
end

#image(id) ⇒ WeebImage

Get an image.


83
84
85
86
# File 'lib/weeb/interfaces/toph.rb', line 83

def image(id)
  id = id.resolve_id if id.respond_to?(:resolve_id)
  WeebImage.new(API::Toph.image(self, id), self)
end

#list(type: nil, tags: [], hidden: false, nsfw: false, file_type: nil, page: 0, account: nil) ⇒ WeebImage

Get a list of images based on arguments.


126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/weeb/interfaces/toph.rb', line 126

def list(type: nil, tags: [], hidden: false, nsfw: false, file_type: nil, page: 0, account: nil)
  tags = tags.map { |t| t.name if t.is_a(Tag) }
  object = {
    type: type,
    tags: tags,
    hidden: hidden,
    nsfw: nsfw,
    file_type: file_type,
    page: page
  }
  repsonse =  ? API::Toph.list_user(self, , object) : API::Toph.list(self, object)
  repsonse.map { |i| WeebImage.new(i, self) }
end

#random(type: nil, tags: [], hidden: false, nsfw: false, file_type: nil) ⇒ WeebImage

Get a random image based on type or a set of tags.


69
70
71
72
73
74
75
76
77
78
# File 'lib/weeb/interfaces/toph.rb', line 69

def random(type: nil, tags: [], hidden: false, nsfw: false, file_type: nil)
  tags = tags.map { |t| t.name if t.is_a(Tag) }
  WeebImage.new(API::Toph.random(self, {
    type: type,
    tags: tags,
    hidden: hidden,
    nsfw: nsfw,
    file_type: file_type
  }), self)
end

#remove_tags_to_image(image, tags) ⇒ WeebImage

Remove tags off of an image.


111
112
113
114
115
# File 'lib/weeb/interfaces/toph.rb', line 111

def remove_tags_to_image(image, tags)
  image = image.resolve_id if image.respond_to?(:resolve_id)
  tags = tags.map { |t| t.name if t.is_a(Tag) }
  WeebImage.new(API::Toph.remove_tags_to_image(self, image, tags), self)
end

#tags(hidden: false, nsfw: false) ⇒ Array<String>, Array<PreviewImage>

Gets a list of available types of images.


55
56
57
58
59
60
# File 'lib/weeb/interfaces/toph.rb', line 55

def tags(hidden: false, nsfw: false)
  API::Toph.types(self, {
    hidden: hidden,
    nsfw: nsfw
  })['tags']
end

#types(hidden: false, nsfw: false, preview: false) ⇒ Array<String>, Array<PreviewImage>

Gets a list of available types of images.


42
43
44
45
46
47
48
49
# File 'lib/weeb/interfaces/toph.rb', line 42

def types(hidden: false, nsfw: false, preview: false)
  response = API::Toph.types(self, {
    hidden: hidden,
    nsfw: nsfw,
    preview: preview
  })
  preview ? response['preview'].map { |p| PreviewImage.new(p, self) } : response['types']
end

#upload(resource, type, hidden: false, tags: [], nsfw: false, source: nil) ⇒ Image

Uploads an image.


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/weeb/interfaces/toph.rb', line 15

def upload(resource, type, hidden: false, tags: [], nsfw: false, source: nil)
  tags = tags.map { |t| t.name if t.is_a(Tag) }
  if resource.is_a?(String) && URI(resource).host.nil?
    begin
      resource = File.new(File.absolute_path(resource), 'rb')
    rescue Errno::ENOENT, Errno::EACCES, Errno::ENAMETOOLONG => e
      errstring = 'Unknown'
      case e.class.name
      when 'Errno::ENOENT'
        errstring = 'File Not Found'
      when 'Errno::EACCES'
        errstring = 'Permission Denied'
      when 'Errno::ENAMETOOLONG'
        errstring = 'Name Too Long'
      end
      raise WeebSh::Err::FileError, "Error initializing file '${resource}' | #{errstring} | #{e.class.name}"
    end
  end

  Image.new(API::Toph.upload(self, resource, type, hidden, tags, nsfw, source)['file'], self)
end