Class: MyanimelistClient::SearchResponse

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/myanimelist_client/search_response.rb

Overview

This class represents a response to a search request. It is returned by #search_anime and #search_manga.

SearchResponse is responsible for parsing and wraping the XML response from the myanimelist API (or the error message).

It wraps every entry in a SearchEntry instance and it includes the Ruby's Enumerable module so it is easy to iterate over, sort it, etc.

Examples:

require 'myanimelist_client'

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

results = client.search_anime 'anime name'                      # => SearchResponse


# It provides two predicates for error handling:
results.ok?           # => true or false
results.error?        # => true or false

# On error, results.raw may contain an error message:
if results.error?
  results.raw         # => String or nil
end

# On success you can browse the results:
if results.ok?  # It provides basic informations:

  results.length      # => Fixnum
  results.empty?      # => true or false

  # It is browsable more or less like an Array would be:
  results.first == results[0]                      # => true


  # It is enumerable so you can use #each, #map, #sort_by, #select, etc.
  # An exemple:
  top3 = results.sort_by(&:score).reverse!.take(3).to_a
  top3.each_with_index do |entry, index|
    puts "#{index+1}. #{entry.title} - #{entry.score}"
  end

  # Yes, it is awesome ! :3

end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw_xml, error = false) ⇒ SearchResponse


53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/myanimelist_client/search_response.rb', line 53

def initialize raw_xml, error=false
  @raw       = raw_xml
  @error     = !!error
  @entries   = []

  parsed_xml = Nokogiri::XML raw_xml
  animes     = parsed_xml.css 'anime entry'
  mangas     = parsed_xml.css 'manga entry'

  (animes + mangas).each do |entry|
    @entries.push MyanimelistClient::SearchEntry.new(
      id:         entry.at_css('id')&.content,
      title:      entry.at_css('title')&.content,
      english:    entry.at_css('english')&.content,
      synonyms:   entry.at_css('synonyms')&.content,
      episodes:   entry.at_css('episodes')&.content&.to_i,
      chapters:   entry.at_css('chapters')&.content&.to_i,
      volumes:    entry.at_css('volumes')&.content&.to_i,
      score:      entry.at_css('score')&.content&.to_f,
      type:       entry.at_css('type')&.content,
      status:     entry.at_css('status')&.content,
      start_date: entry.at_css('start_date')&.content,
      end_date:   entry.at_css('end_date')&.content,
      synopsis:   entry.at_css('synopsis')&.content,
      image:      entry.at_css('image')&.content
    )
  end
end

Instance Attribute Details

#rawString?

Returns the raw response from the API or the error message.


47
48
49
# File 'lib/myanimelist_client/search_response.rb', line 47

def raw
  @raw
end

Instance Method Details

#[](index) ⇒ SearchEntry?

Allows to browse the entries like with an array.


109
110
111
# File 'lib/myanimelist_client/search_response.rb', line 109

def [] index
  @entries[index]
end

#each(&block) ⇒ self, Enumerator

Similar to Array's #each.


84
85
86
87
88
89
90
91
# File 'lib/myanimelist_client/search_response.rb', line 84

def each &block
  if block_given?
    @entries.each &block
    self
  else
    @entries.each
  end
end

#empty?Boolean

Is it empty?


102
103
104
# File 'lib/myanimelist_client/search_response.rb', line 102

def empty?
  @entries.empty?
end

#error?Boolean

Returns true if an error occured.


114
115
116
# File 'lib/myanimelist_client/search_response.rb', line 114

def error?
  @error
end

#lengthFixnum Also known as: size

Returns the number of entries returned by the search request.


95
96
97
# File 'lib/myanimelist_client/search_response.rb', line 95

def length
  @entries.size
end

#ok?Boolean

Returns true if no error occured.


119
120
121
# File 'lib/myanimelist_client/search_response.rb', line 119

def ok?
  not error?
end