Class: MyanimelistClient::SearchEntry

Inherits:
Object
  • Object
show all
Defined in:
lib/myanimelist_client/search_entry.rb

Overview

This class is used to reprensent an entry within a SearchResponse. It can describe either an anime or a manga.

Examples:

require 'myanimelist_client'

client = MyanimelistClient.new 'username', 'password'

client.search_anime('anime name').each do |entry|
  # entry is a SearchResponse.

  # It exposes all its attributes:
  entry.id                # => String or nil
  entry.title             # => String or nil
  entry.english           # => String or nil
  entry.synonyms          # => String or nil
  entry.episodes          # => Fixnum or nil
  entry.chapters          # => Fixnum or nil
  entry.volumes           # => Fixnum or nil
  entry.score             # => Float or nil
  entry.type              # => String or nil
  entry.status            # => String or nil
  entry.start_date        # => String or nil
  entry.end_date          # => String or nil
  entry.synopsis          # => String or nil
  entry.image             # => String or nil

  # It provides some useful predicates:
  entry.manga?            # => true or false
  entry.anime?            # => true or false
  entry.upcoming?         # => true or false
  entry.ongoing?          # => true or false
  entry.finished?         # => true or false

  # It can be converted to a hash:
  entry.to_h              # => Hash
end

Constant Summary collapse

ATTRIBUTES =

The list of attributes defining the entry for internal usage.

%w(
id
title
english
synonyms
episodes
chapters
volumes
score
type
status
start_date
end_date
synopsis
image

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ SearchEntry

Returns a new instance of SearchEntry.

Parameters:

  • options (Hash) (defaults to: {})

    A hash used to hydrate the object.


77
78
79
80
81
# File 'lib/myanimelist_client/search_entry.rb', line 77

def initialize options={}
  ATTRIBUTES.each do |attribute|
    instance_variable_set "@#{attribute}", options[attribute] || options[attribute.to_sym]
  end
end

Instance Attribute Details

#chaptersFixnum?

Returns the number of chapters (nil when the entry describes an anime).

Returns:

  • (Fixnum, nil)

    the current value of chapters


53
54
55
# File 'lib/myanimelist_client/search_entry.rb', line 53

def chapters
  @chapters
end

#end_dateString?

Returns the end date.

Returns:

  • (String, nil)

    the current value of end_date


53
54
55
# File 'lib/myanimelist_client/search_entry.rb', line 53

def end_date
  @end_date
end

#englishString?

Returns the english title.

Returns:

  • (String, nil)

    the current value of english


53
54
55
# File 'lib/myanimelist_client/search_entry.rb', line 53

def english
  @english
end

#episodesFixnum?

Returns the number of episodes (nil when the entry describes a manga).

Returns:

  • (Fixnum, nil)

    the current value of episodes


53
54
55
# File 'lib/myanimelist_client/search_entry.rb', line 53

def episodes
  @episodes
end

#idString?

Returns the ID.

Returns:

  • (String, nil)

    the current value of id


53
54
55
# File 'lib/myanimelist_client/search_entry.rb', line 53

def id
  @id
end

#imageString?

Returns the image's URL.

Returns:

  • (String, nil)

    the current value of image


53
54
55
# File 'lib/myanimelist_client/search_entry.rb', line 53

def image
  @image
end

#scoreFloat?

Returns the score.

Returns:

  • (Float, nil)

    the current value of score


53
54
55
# File 'lib/myanimelist_client/search_entry.rb', line 53

def score
  @score
end

#start_dateString?

Returns the start date.

Returns:

  • (String, nil)

    the current value of start_date


53
54
55
# File 'lib/myanimelist_client/search_entry.rb', line 53

def start_date
  @start_date
end

#statusString?

Returns the status.

Returns:

  • (String, nil)

    the current value of status


53
54
55
# File 'lib/myanimelist_client/search_entry.rb', line 53

def status
  @status
end

#synonymsString?

Returns the synonyms.

Returns:

  • (String, nil)

    the current value of synonyms


53
54
55
# File 'lib/myanimelist_client/search_entry.rb', line 53

def synonyms
  @synonyms
end

#synopsisString?

Returns the synopsis.

Returns:

  • (String, nil)

    the current value of synopsis


53
54
55
# File 'lib/myanimelist_client/search_entry.rb', line 53

def synopsis
  @synopsis
end

#titleString?

Returns the title.

Returns:

  • (String, nil)

    the current value of title


53
54
55
# File 'lib/myanimelist_client/search_entry.rb', line 53

def title
  @title
end

#typeString?

Returns the type.

Returns:

  • (String, nil)

    the current value of type


53
54
55
# File 'lib/myanimelist_client/search_entry.rb', line 53

def type
  @type
end

#volumesFixnum?

Returns the number of volumes (nil when the entry describes an anime).

Returns:

  • (Fixnum, nil)

    the current value of volumes


53
54
55
# File 'lib/myanimelist_client/search_entry.rb', line 53

def volumes
  @volumes
end

Instance Method Details

#==(other) ⇒ Object

Performs a loose equality based on the value of the attributes (see ATTRIBUTES).


91
92
93
94
95
96
97
98
# File 'lib/myanimelist_client/search_entry.rb', line 91

def == other
  ATTRIBUTES.each do |attribute|
    if !other.respond_to?(attribute) || send(attribute) != other.send(attribute)
      return false
    end
  end
  true
end

#anime?Boolean

Returns true when the entry describes an anime.

Returns:

  • (Boolean)

101
102
103
# File 'lib/myanimelist_client/search_entry.rb', line 101

def anime?
  !!@episodes
end

#finished?Boolean

Returns true when the anime or manga is finished.

Returns:

  • (Boolean)

121
122
123
# File 'lib/myanimelist_client/search_entry.rb', line 121

def finished?
  !!(/finished/i =~ @status)
end

#manga?Boolean

Returns true when the entry describes a manga.

Returns:

  • (Boolean)

106
107
108
# File 'lib/myanimelist_client/search_entry.rb', line 106

def manga?
  !!(@chapters && @volumes)
end

#ongoing?Boolean

Returns true when the anime is currently airing / when the manga is currently publishing.

Returns:

  • (Boolean)

116
117
118
# File 'lib/myanimelist_client/search_entry.rb', line 116

def ongoing?
  !!(/currently|publishing/i =~ @status)
end

#to_hHash

Creates a hash representing the entry.

Returns:

  • (Hash)

85
86
87
88
# File 'lib/myanimelist_client/search_entry.rb', line 85

def to_h
  values = ATTRIBUTES.map{|attribute| send attribute }
  Hash[ATTRIBUTES.zip values]
end

#upcoming?Boolean

Returns true when the anime is not yet aired / when the manga is not yet published.

Returns:

  • (Boolean)

111
112
113
# File 'lib/myanimelist_client/search_entry.rb', line 111

def upcoming?
  !!(/not\s+yet/i =~ @status)
end