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 =
"https://www.wanikani.com/api"
- DEFAULT_API_VERSION =
"v1.2"
- VALID_API_VERSIONS =
%w(v1 v1.1 v1.2)
- VERSION =
'1.1'
Class Method Summary
collapse
Class Method Details
.api_key ⇒ Object
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
# 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
response = RestClient.get("#{Wanikani::API_ENDPOINT}/#{Wanikani.api_version}/user/#{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
|
.api_version ⇒ Object
34
35
36
|
# File 'lib/wanikani.rb', line 34
def self.api_version
@api_version ||= DEFAULT_API_VERSION
end
|
.api_version=(api_version) ⇒ Object
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
56
57
58
59
60
61
62
|
# File 'lib/wanikani.rb', line 56
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}/#{Wanikani.api_version}/user/#{api_key}/user-information/")
!MultiJson.load(response).has_key?("error")
end
|