Module: Wanikani

Defined in:
lib/wanikani.rb,
lib/wanikani/srs.rb,
lib/wanikani/user.rb,
lib/wanikani/level.rb,
lib/wanikani/version.rb,
lib/wanikani/exceptions.rb,
lib/wanikani/study_queue.rb,
lib/wanikani/critical_items.rb,
lib/wanikani/recent_unlocks.rb

Defined Under Namespace

Classes: CriticalItems, Exception, InvalidKey, Level, RecentUnlocks, SRS, StudyQueue, User

Constant Summary collapse

API_ENDPOINT =
"https://www.wanikani.com"
DEFAULT_API_VERSION =
"v1.4"
VALID_API_VERSIONS =
%w(v1 v1.1 v1.2 v1.3 v1.4)
VERSION =
'1.5.0'

Class Method Summary collapse

Class Method Details

.api_keyObject



25
26
27
# File 'lib/wanikani.rb', line 25

def self.api_key
  @api_key
end

.api_key=(api_key) ⇒ Object



21
22
23
# File 'lib/wanikani.rb', line 21

def self.api_key=(api_key)
  @api_key = api_key
end

.api_response(resource, optional_arg = nil) ⇒ Object

Raises:

  • (ArgumentError)


38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/wanikani.rb', line 38

def self.api_response(resource, optional_arg = nil)
  raise ArgumentError, "You must define a resource to query Wanikani" if resource.nil? || resource.empty?
  raise ArgumentError, "You must set your Wanikani API key before querying the API" if Wanikani.api_key.nil? || Wanikani.api_key.empty?

  begin
    res = client.get("/api/#{Wanikani.api_version}/user/#{Wanikani.api_key}/#{resource}/#{optional_arg}")

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

.api_versionObject



34
35
36
# File 'lib/wanikani.rb', line 34

def self.api_version
  @api_version ||= DEFAULT_API_VERSION
end

.api_version=(api_version) ⇒ Object

Raises:

  • (ArgumentError)


29
30
31
32
# File 'lib/wanikani.rb', line 29

def self.api_version=(api_version)
  raise ArgumentError, "API version should be one of the following: #{VALID_API_VERSIONS.join(', ')}." unless VALID_API_VERSIONS.include?(api_version) || api_version.nil?
  @api_version = api_version
end

.valid_api_key?(api_key = nil) ⇒ Boolean

Returns:

  • (Boolean)


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

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

  res = client.get("/api/#{Wanikani.api_version}/user/#{api_key}/user-information")

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