Class: Rubyhexagon::Artist

Inherits:
Object
  • Object
show all
Defined in:
lib/rubyhexagon/artist.rb,
lib/rubyhexagon/api/artist.rb

Overview

A class to interact with the e621 web interface.

Author:

  • Maxine Michalski

Since:

  • 2.0.0

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(artist) ⇒ Object

Initializer for Artist

Examples:

Get an artist instance

E621::Artist.new(id: 1) #=> E621::Artist instance
E621::Artist.new(name: 'artist') #=> E621::Artist instance

Parameters:

  • artist (Hash)

    artist data

Author:

  • Maxine Michalski

Since:

  • 1.0.0



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/rubyhexagon/artist.rb', line 77

def initialize(artist)
  unless artist.is_a?(Hash)
    raise ArgumentError, "#{artist.class} is not a Hash"
  end
  if artist[:id].nil? && artist[:name].nil?
    raise ArgumentError, 'At least :id or :name must be given!'
  end
  artist.each do |k, v|
    if i[id name group_name version is_active].include?(k)
      if k == :id && !(v.is_a?(Integer) && v.positive?)
        raise InvalidIDError, "ID out of range: #{v}"
      end
      instance_variable_set("@#{k}".to_sym, v)
    elsif i[other_names urls updater_id].include?(k)
      __send__("setup_#{k}".to_sym, v)
    end
  end
end

Instance Attribute Details

#group_nameString (readonly)

Group name for artist

Examples:

Get group name for artist

artist.group_name #=> String

Returns:

  • (String)

    group name for artist

Since:

  • 1.0.0



48
49
50
# File 'lib/rubyhexagon/artist.rb', line 48

def group_name
  @group_name
end

#idInteger (readonly)

Artist ID

Examples:

Get artist ID

artist.id #=> Integer

Returns:

  • (Integer)

    id of artist

Since:

  • 1.0.0



30
31
32
# File 'lib/rubyhexagon/artist.rb', line 30

def id
  @id
end

#nameString (readonly)

Artist name

Examples:

Get artist name

artist.name #=> String

Returns:

  • (String)

    name of artist

Since:

  • 1.0.0



36
37
38
# File 'lib/rubyhexagon/artist.rb', line 36

def name
  @name
end

#other_namesArray<String> (readonly)

Alternative names for artist

Examples:

Get alternative names for artist

artist.other_names #=> [] or Array<String>

Returns:

  • (Array<String>)

    alternative names for artist

Since:

  • 1.0.0



42
43
44
# File 'lib/rubyhexagon/artist.rb', line 42

def other_names
  @other_names
end

#updaterE621::User (readonly)

Updating user for this artist

Examples:

Get the user, who last updated this artist

artist.updater #=> E621::User

Returns:

Since:

  • 1.0.0



66
67
68
# File 'lib/rubyhexagon/artist.rb', line 66

def updater
  @updater
end

#urlsArray<URI> (readonly)

URLs to find artist

Examples:

Get parsed URIs for artist

artist.urls #=> Array<URI> or []

Returns:

  • (Array<URI>)

    links to find artist

Since:

  • 1.0.0



54
55
56
# File 'lib/rubyhexagon/artist.rb', line 54

def urls
  @urls
end

#versionInteger (readonly)

Version of artist information

Examples:

Get version of this artist’s information

artist.version #=> Integer

Returns:

  • (Integer)

    version of artist information

Since:

  • 1.0.0



60
61
62
# File 'lib/rubyhexagon/artist.rb', line 60

def version
  @version
end

Class Method Details

.list(query) ⇒ Array<E621::Artist>

Fetch a list of artists

Examples:

Get list of artists

E621::Artist.list(name: 'artist') #=> Array<E621::Artist>

Parameters:

  • query (Hsah)

    Query for fetching artist data

Returns:

Raises:

  • (ArgumentError)

See Also:

Author:

  • Maxine Michalski

Since:

  • 2.0.0



35
36
37
38
39
40
# File 'lib/rubyhexagon/api/artist.rb', line 35

def self.list(query)
  raise ArgumentError, 'A Hash is required' unless query.is_a?(Hash)
  E621::API.fetch(:artist, :index, query).map do |artist|
    new(artist)
  end
end

Instance Method Details

#==(other) ⇒ TrueClass, FalseClass

Comparison method for artists

Examples:

Compare two tags, that are unequal

Tag.new(id: 1) == Tag.new(id: 2) #=> false

Returns:

  • (TrueClass, FalseClass)

Author:

  • Maxine Michalski

Since:

  • 1.0.0



102
103
104
105
# File 'lib/rubyhexagon/artist.rb', line 102

def ==(other)
  return false unless other.is_a?(Artist)
  @id == other.id && @name == other.name && @version == other.version
end

#active?TrueClass|FalseClass

Check if this artist is active

Examples:

Is artist active?

artist.active? #=> true or false

Returns:

  • (TrueClass|FalseClass)

    depending on active status

Author:

  • Maxine Michalski

Since:

  • 1.0.0



113
114
115
# File 'lib/rubyhexagon/artist.rb', line 113

def active?
  @is_active
end

#to_hashHash

Turn object into a hash representation of itself

Examples:

Turn a User into a Hash

Tag.new(id: 1).to_hash #=> { id: 1 }

Returns:

  • (Hash)

Author:

  • Maxine Michalski

Since:

  • 1.0.0



124
125
126
127
128
129
130
# File 'lib/rubyhexagon/artist.rb', line 124

def to_hash
  hash = {}
  instance_variables.each do |i|
    hash.store(i.to_s.sub(/^@/, '').to_sym, instance_variable_get(i))
  end
  hash
end