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.0"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(username, password) ⇒ MyanimelistClient


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

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

Instance Attribute Details

#passwordString

Returns the password


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

def password
  @password
end

#usernameString

Returns the username


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

def username
  @username
end

Instance Method Details

#search_anime(query) ⇒ SearchResponse

Allows to search anime titles.


73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/myanimelist_client/myanimelist_client.rb', line 73

def search_anime query
  begin
    escaped_query = CGI::escape query
    response = RestClient::Request.execute(
      method:  :get,
      url:     "https://myanimelist.net/api/anime/search.xml?q=#{escaped_query}",
      user:     @username,
      password: @password
    )
    SearchResponse.new response.body
  rescue RestClient::ExceptionWithResponse => e
    SearchResponse.new e.response.body, :error
  end
end

#search_manga(query) ⇒ SearchResponse

Allows to search manga titles.


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

def search_manga query
  begin
    escaped_query = CGI::escape query
    response = RestClient::Request.execute(
      method:  :get,
      url:     "https://myanimelist.net/api/manga/search.xml?q=#{escaped_query}",
      user:     @username,
      password: @password
    )
    SearchResponse.new response.body
  rescue RestClient::ExceptionWithResponse => e
    SearchResponse.new e.response.body, :error
  end
end

#verify_credentialsUserResponse

Allows to check username/password.


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

def verify_credentials
  begin
    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
end