Class: MyanimelistClient
- Inherits:
-
Object
- Object
- MyanimelistClient
- 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.
Defined Under Namespace
Classes: SearchEntry, SearchResponse, UserResponse
Constant Summary collapse
- VERSION =
Versioning.
'0.1.1'
Instance Attribute Summary collapse
-
#password ⇒ String
Returns the password.
-
#username ⇒ String
Returns the username.
Instance Method Summary collapse
-
#initialize(username, password) ⇒ MyanimelistClient
constructor
A new instance of MyanimelistClient.
-
#search(type, query) ⇒ SearchResponse
private
Search a manga or an anime depending on the provided type.
-
#search_anime(query) ⇒ SearchResponse
Allows to search anime titles.
-
#search_manga(query) ⇒ SearchResponse
Allows to search manga titles.
-
#verify_credentials ⇒ UserResponse
Allows to check username/password.
Constructor Details
#initialize(username, password) ⇒ MyanimelistClient
Returns a new instance of MyanimelistClient.
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
#password ⇒ String
Returns the password
40 41 42 |
# File 'lib/myanimelist_client/myanimelist_client.rb', line 40 def password @password end |
#username ⇒ String
Returns the 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
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., :error end |
#search_anime(query) ⇒ SearchResponse
Allows to search anime titles.
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.
81 82 83 |
# File 'lib/myanimelist_client/myanimelist_client.rb', line 81 def search_manga query search 'manga', query end |
#verify_credentials ⇒ UserResponse
Allows to check username/password.
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 |