Class: MyanimelistClient::UserResponse

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

Overview

This class represents a User. It is returned by #verify_credentials.

UserResponse is responsible for parsing and wraping the XML (or any error message) from the API.

Examples:

require 'myanimelist_client'

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

user = client.verify_credentials
                      # => UserResponse

# It provides two predicates to handle login errors:
user.error?           # => true or false
user.ok?              # => true or false

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

# On success you can access to user's attributes:
if user.ok?
  user.id             # => String or nil
  user.username       # => String or nil
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw_xml) ⇒ UserResponse

Returns a new instance of UserResponse.

Parameters:

  • raw_xml (String, nil)

    The raw response from the API or an error message (or even nil).



37
38
39
40
41
42
43
# File 'lib/myanimelist_client/user_response.rb', line 37

def initialize raw_xml
  @raw       = raw_xml

  parsed_xml = Nokogiri::XML raw_xml
  @id        = parsed_xml.at_css('id')&.content
  @username  = parsed_xml.at_css('username')&.content
end

Instance Attribute Details

#idString?

Return the MyAnimeList ID of the current account.

Returns:

  • (String, nil)

    the current value of id



33
34
35
# File 'lib/myanimelist_client/user_response.rb', line 33

def id
  @id
end

#rawString?

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

Returns:

  • (String, nil)

    the current value of raw



33
34
35
# File 'lib/myanimelist_client/user_response.rb', line 33

def raw
  @raw
end

#usernameString?

Returns the MyAnimeList username of the current account.

Returns:

  • (String, nil)

    the current value of username



33
34
35
# File 'lib/myanimelist_client/user_response.rb', line 33

def username
  @username
end

Instance Method Details

#error?Boolean

Returns true if an error occured.

Returns:

  • (Boolean)


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

def error?
  @raw.nil? || @id.nil? || @username.nil?
end

#ok?Boolean

Returns true if no error occured.

Returns:

  • (Boolean)


51
52
53
# File 'lib/myanimelist_client/user_response.rb', line 51

def ok?
  not error?
end