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, #inspect

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.

Parameters:

  • image (String, WeebImage, PreviewImage, #resolve_id)

    the image being referred

  • tags (Array<String, Tag>)

    the affected tags

Returns:

  • (WeebImage)

    Returns the image with the given ID.


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 id.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.

Parameters:

Returns:

  • (WeebImage)

    Returns the image with the given ID.


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.

Parameters:

Returns:

  • (WeebImage)

    Returns the image with the given ID.


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.

Parameters:

  • type (String) (defaults to: nil)

    the type of images to recieve

  • tags (Array<String, Tag>) (defaults to: [])

    the tags a image should require to be for selection

  • hidden (true, false) (defaults to: false)

    if true, you can only get back hidden tags you added

  • nsfw (true, false, String) (defaults to: false)

    whether or not preview images could be NSFW. Setting this to “only” restricts it to NSFW images.

  • file_type (String) (defaults to: nil)

    the file type an image is required to be for selection

  • page (Number) (defaults to: 0)

    the page number of images to use

  • account (String) (defaults to: nil)

    the account ID to get images from

Returns:

  • (WeebImage)

    Returns image that was randomly selected.


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.

Parameters:

  • type (String) (defaults to: nil)

    the type of images to recieve

  • tags (Array<String, Tag>) (defaults to: [])

    the tags a image should require to be for selection

  • hidden (true, false) (defaults to: false)

    if true, you can only get back hidden tags you added

  • nsfw (true, false, String) (defaults to: false)

    whether or not preview images could be NSFW. Setting this to “only” restricts it to NSFW images.

  • file_type (String) (defaults to: nil)

    the file type an image is required to be for selection

Returns:

  • (WeebImage)

    Returns image that was randomly selected.


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.

Parameters:

  • image (String, WeebImage, PreviewImage, #resolve_id)

    the image being referred

  • tags (Array<String, Tag>)

    the affected tags

Returns:

  • (WeebImage)

    Returns the image with the given ID.


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 id.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.

Parameters:

  • hidden (true, false) (defaults to: false)

    if true, you only get back hidden tags you added

  • nsfw (true, false, String) (defaults to: false)

    whether or not preview images could be NSFW. Setting this to “only” restricts it to NSFW images.

Returns:

  • (Array<String>, Array<PreviewImage>)

    Returns all available tags.


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.

Parameters:

  • preview (true, false) (defaults to: false)

    whether or not to return an array ofpreview images instead of and array of strings

  • hidden (true, false) (defaults to: false)

    if true, you only get back hidden preview images you uploaded

  • nsfw (true, false, String) (defaults to: false)

    whether or not preview images could be NSFW. Setting this to “only” restricts it to NSFW images.

Returns:

  • (Array<String>, Array<PreviewImage>)

    Returns all available types.


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.

Parameters:

  • resource (File, String)

    the file to upload, this can either be a File, a URL or a path.

  • type (String)

    the type of image this is in.

  • hidden (true, false) (defaults to: false)

    whether or not this image will be only seen to the uploader.

  • tags (Array<String, Tag>) (defaults to: [])

    the tags that this image applies to.

  • nsfw (true, false) (defaults to: false)

    whether or not this image is NSFW.

  • source (String) (defaults to: nil)

    the source URL of the image

Returns:

  • (Image)

    Returns image that was uploaded.


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