Class: MyanimelistClient

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

Overview

This is the main class. It represents a client that can consume the MyAnimeList.net API.

It allows to search for anime and manga titles as well as verify your username / password.

Examples:

require 'myanimelist_client'

# MyAnimeList requires a valid account in order to consume their API:
client = MyanimelistClient.new 'username', 'password'

# Verify credentials:
user = client.verify_credentials           # => UserResponse

if user.error?
  STDERR.puts "An error occured: #{user.raw}"
  exit
end

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

puts 'Error...'         if results.error?
puts 'Found nothing...' if results.ok? && results.empty?

results.sort_by(:score).reverse!.each do |anime|
  puts "#{anime.title} - #{anime.score}"
end

# Search manga:
results = client.search_manga 'manga name' # => SearchResponse

results.select{ |manga| manga.volumes < 10 }.each do |manga|
  puts "#{manga.title} - #{manga.english}"
end

Defined Under Namespace

Classes: SearchEntry, SearchResponse, UserResponse

Constant Summary collapse

VERSION =

Versioning.

'0.1.1'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(username, password) ⇒ MyanimelistClient

Returns a new instance of MyanimelistClient.

Parameters:

  • username (String)

    A valid myanimelist username

  • password (String)

    A valid myanimelist password



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

def initialize username, password
  @username = username
  @password = password
end

Instance Attribute Details

#passwordString

Returns the password

Returns:

  • (String)

    the current value of password



40
41
42
# File 'lib/myanimelist_client/myanimelist_client.rb', line 40

def password
  @password
end

#usernameString

Returns the username

Returns:

  • (String)

    the current value of username



40
41
42
# File 'lib/myanimelist_client/myanimelist_client.rb', line 40

def username
  @username
end

Instance Method Details

#search(type, query) ⇒ SearchResponse (private)

Search a manga or an anime depending on the provided type

Parameters:

  • type (String)

    The search type (+‘anime’+ or ‘manga’).

  • query (String)

    The search query.

Returns:

  • (SearchResponse)

    The API response or the error message nicely wraped in an object.



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/myanimelist_client/myanimelist_client.rb', line 92

def search type, query
  if type != 'anime' && type != 'manga'
    raise 'Invalid search type: must be anime or manga'
  end
  escaped_query = CGI::escape query
  response = RestClient::Request.execute(
    method:  :get,
    url:     "https://myanimelist.net/api/#{type}/search.xml?q=#{escaped_query}",
    user:     @username,
    password: @password
  )
  SearchResponse.new response.body
rescue RestClient::ExceptionWithResponse => e
  SearchResponse.new e.response.body, :error
rescue RuntimeError => e
  SearchResponse.new e.message, :error
end

#search_anime(query) ⇒ SearchResponse

Allows to search anime titles.

Parameters:

  • query (String)

    The search query.

Returns:

  • (SearchResponse)

    The API response or the error message nicely wraped in an object.

See Also:



72
73
74
# File 'lib/myanimelist_client/myanimelist_client.rb', line 72

def search_anime query
  search 'anime', query
end

#search_manga(query) ⇒ SearchResponse

Allows to search manga titles.

Parameters:

  • query (String)

    The search query.

Returns:

  • (SearchResponse)

    The API response or the error message nicely wraped in an object.

See Also:



81
82
83
# File 'lib/myanimelist_client/myanimelist_client.rb', line 81

def search_manga query
  search 'manga', query
end

#verify_credentialsUserResponse

Allows to check username/password.

Returns:

  • (UserResponse)

    The API response or the error message nicely wraped in an object.

See Also:



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/myanimelist_client/myanimelist_client.rb', line 55

def verify_credentials
  response = RestClient::Request.execute(
    method:  :get,
    url:     'https://myanimelist.net/api/account/verify_credentials.xml',
    user:     @username,
    password: @password
  )
  UserResponse.new response.body
rescue RestClient::ExceptionWithResponse => e
  UserResponse.new e.response.body
end