Class: Wanikani::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/wanikani/client.rb

Constant Summary collapse

API_ENDPOINT =
"https://api.wanikani.com"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Wanikani::Client

Initialize a client which will be used to communicate with WaniKani.

Parameters:

  • options (Hash) (defaults to: {})

    the API key (required) and API version (optional) used to communicate with the WaniKani API.

Raises:

  • (ArgumentError)


16
17
18
19
20
21
22
23
# File 'lib/wanikani/client.rb', line 16

def initialize(options = {})
  raise ArgumentError, "You must specify a WaniKani API key before querying the API." if options[:api_key].nil? || options[:api_key].empty?
  raise ArgumentError, "API revision should be one of the following: #{Wanikani::VALID_API_REVISIONS.join(', ')}." unless Wanikani::VALID_API_REVISIONS.include?(options[:api_revision]) || options[:api_revision].nil?

  @api_key = options[:api_key]
  @api_revision = options[:api_revision] ||= Wanikani::DEFAULT_API_REVISION

end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



9
10
11
# File 'lib/wanikani/client.rb', line 9

def api_key
  @api_key
end

#api_revisionObject

Returns the value of attribute api_revision.



9
10
11
# File 'lib/wanikani/client.rb', line 9

def api_revision
  @api_revision
end

Class Method Details

.valid_api_key?(api_key = nil) ⇒ Boolean

Verifies if the specified API key is valid by checking WaniKani’s API.

Parameters:

  • api_key (String) (defaults to: nil)

    the API key to validate in WaniKani.

Returns:

  • (Boolean)

    whether the API key is valid.

Raises:

  • (ArgumentError)


43
44
45
46
47
48
# File 'lib/wanikani/client.rb', line 43

def self.valid_api_key?(api_key = nil)
  raise ArgumentError, "You must specify a WaniKani API key before querying the API." if api_key.nil? || api_key.empty?

  @client = Wanikani::Client.new(api_key: api_key)
  return @client.valid_api_key?
end

Instance Method Details

#api_endpointString

API endpoint at WaniKani

Returns:

  • (String)

    URL of endpoint



53
54
55
# File 'lib/wanikani/client.rb', line 53

def api_endpoint
  API_ENDPOINT
end

#get(resource, parameters = nil) ⇒ Hash

Contacts the WaniKani API and returns the data specified.

Parameters:

  • resource (String)

    the resource to access.

  • parameters (Hash) (defaults to: nil)

    optional arguments for the specified resource.

Returns:

  • (Hash)

    the parsed API response.

Raises:

  • (ArgumentError)


62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/wanikani/client.rb', line 62

def get(resource, parameters = nil)
  raise ArgumentError, "You must define a resource to query WaniKani" if resource.nil? || resource.empty?

  begin
    res = client.get("/v2/#{resource}", parameters)

    if !res.success? || res.body.has_key?("error")
      raise_exception(res)
    else
      return res.body
    end
  rescue => error
    raise Exception, "There was an error: #{error.message}"
  end
end

#valid_api_key?(api_key = nil) ⇒ Boolean

Verifies if the client’s API key is valid by checking WaniKani’s API.

Parameters:

  • api_key (String) (defaults to: nil)

    the API key to validate in WaniKani.

Returns:

  • (Boolean)

    whether the API key is valid.



29
30
31
32
33
34
35
36
37
# File 'lib/wanikani/client.rb', line 29

def valid_api_key?(api_key = nil)
  api_key ||= @api_key
  return false if api_key.empty?

  res = client.get("/v2/user/")

  return false if !res.success? || res.body.has_key?("error")
  return true
end