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, Level, RecentUnlocks, SRS, StudyQueue, User

Constant Summary collapse

API_ENDPOINT =
"http://www.wanikani.com/api/user"
VERSION =
'0.0.9'

Class Method Summary collapse

Class Method Details

.api_keyObject



23
24
25
# File 'lib/wanikani.rb', line 23

def self.api_key
  @api_key
end

.api_key=(api_key) ⇒ Object



19
20
21
# File 'lib/wanikani.rb', line 19

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

.api_response(resource, optional_arg = nil) ⇒ Object

Raises:

  • (ArgumentError)


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/wanikani.rb', line 27

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
    response = RestClient.get("#{Wanikani::API_ENDPOINT}/#{Wanikani.api_key}/#{resource}/#{optional_arg}")
    parsed_response = MultiJson.load(response)

    if parsed_response.has_key?("error")
      self.raise_exception(parsed_response["error"]["message"])
    else
      return parsed_response
    end
  rescue => error
    self.raise_exception(error.message)
  end
end

.valid_api_key?(api_key = nil) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
48
49
50
51
# File 'lib/wanikani.rb', line 45

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

  response = RestClient.get("#{Wanikani::API_ENDPOINT}/#{api_key}/user-information/")
  !MultiJson.load(response).has_key?("error")
end