wanikani 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/lib/wanikani.rb ADDED
@@ -0,0 +1,45 @@
1
+ # -*- encoding : utf-8 -*-
2
+ require 'rest_client'
3
+ require 'multi_json'
4
+
5
+ require 'wanikani/user'
6
+ require 'wanikani/study_queue'
7
+ require 'wanikani/level'
8
+ require 'wanikani/srs'
9
+ require 'wanikani/recent_unlocks'
10
+
11
+ module Wanikani
12
+ API_ENDPOINT = "http://www.wanikani.com/api/user"
13
+
14
+ def self.api_key=(api_key)
15
+ @api_key = api_key
16
+ end
17
+
18
+ def self.api_key
19
+ @api_key
20
+ end
21
+
22
+ def self.api_response(resource, optional_arg = nil)
23
+ raise ArgumentError, "You must define a resource to query Wanikani" if resource.nil? || resource.empty?
24
+ raise ArgumentError, "You must set your Wanikani API key before querying the API" if Wanikani.api_key.nil? || Wanikani.api_key.empty?
25
+
26
+ begin
27
+ response = RestClient.get("#{Wanikani::API_ENDPOINT}/#{Wanikani.api_key}/#{resource}/#{optional_arg}")
28
+ parsed_response = MultiJson.load(response)
29
+
30
+ if parsed_response.has_key?("error")
31
+ self.raise_exception(parsed_response["error"]["message"])
32
+ else
33
+ return parsed_response
34
+ end
35
+ rescue => error
36
+ self.raise_exception(error.message)
37
+ end
38
+ end
39
+
40
+ private
41
+
42
+ def self.raise_exception(message)
43
+ raise Exception, "There was an error fetching the data from Wanikani (#{message})"
44
+ end
45
+ end
@@ -0,0 +1,12 @@
1
+ # -*- encoding : utf-8 -*-
2
+ module Wanikani
3
+ class Level
4
+ # Gets the user's current level progression (radicals and Kanji).
5
+ #
6
+ # @return [Hash] Progress and total of radicals and Kanji for the user's current level.
7
+ def self.progression
8
+ api_response = Wanikani.api_response("level-progression")
9
+ return api_response["requested_information"]
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,40 @@
1
+ # -*- encoding : utf-8 -*-
2
+ module Wanikani
3
+ class RecentUnlocks
4
+ # Gets the recent unlocked items (radicals, Kanji and vocabulary).
5
+ #
6
+ # @param [Integer] limit the total number of items returned.
7
+ # @return [Array] Returns hashes of unlocked items and related information.
8
+ def self.list(limit = 10)
9
+ api_response = Wanikani.api_response("recent-unlocks", limit)
10
+ return api_response["requested_information"]
11
+ end
12
+
13
+ # Gets the recent unlocked radicals.
14
+ #
15
+ # @param [Integer] limit the total number of items returned.
16
+ # @return [Array] Returns hashes of unlocked radicals and related information.
17
+ def self.radicals(limit = 10)
18
+ unlock_list = self.list(limit)
19
+ return unlock_list.select { |unlock| unlock["type"] == "radical" }
20
+ end
21
+
22
+ # Gets the recent unlocked vocabulary.
23
+ #
24
+ # @param [Integer] limit the total number of items returned.
25
+ # @return [Array] Returns hashes of unlocked vocabulary and related information.
26
+ def self.vocabulary(limit = 10)
27
+ unlock_list = self.list(limit)
28
+ return unlock_list.select { |unlock| unlock["type"] == "vocabulary" }
29
+ end
30
+
31
+ # Gets the recent unlocked Kanji.
32
+ #
33
+ # @param [Integer] limit the total number of items returned.
34
+ # @return [Array] Returns hashes of unlocked Kanji and related information.
35
+ def self.kanji(limit = 10)
36
+ unlock_list = self.list(limit)
37
+ return unlock_list.select { |unlock| unlock["type"] == "kanji" }
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,20 @@
1
+ # -*- encoding : utf-8 -*-
2
+ module Wanikani
3
+ class SRS
4
+ ITEM_TYPES = %w(apprentice guru master enlighten burned all)
5
+
6
+ # Gets the counts for each SRS level and item types.
7
+ #
8
+ # @param [String] item_type the SRS level that will be returned.
9
+ # @return [Hash] Returns the SRS information for each level for the user.
10
+ def self.distribution(item_type = "all")
11
+ raise ArgumentError, "Please use a valid SRS type (or none for all types)" if !ITEM_TYPES.include?(item_type)
12
+
13
+ api_response = Wanikani.api_response("srs-distribution")
14
+ srs_distribution = api_response["requested_information"]
15
+
16
+ return srs_distribution if item_type == "all"
17
+ return srs_distribution[item_type]
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,26 @@
1
+ # -*- encoding : utf-8 -*-
2
+ module Wanikani
3
+ class StudyQueue
4
+ # Get the number of lessons and reviews that are currently in the user's queue.
5
+ #
6
+ # @return [Hash] Returns the number of lessons and reviews pending, along with upcoming review information.
7
+ def self.queue
8
+ api_response = Wanikani.api_response("study-queue")
9
+ return api_response["requested_information"]
10
+ end
11
+
12
+ # Check if there are lessons available.
13
+ #
14
+ # @return [Boolean] Returns true if there's at least one lesson available, or false if there are none.
15
+ def self.lessons_available?
16
+ return !self.queue["lessons_available"].zero?
17
+ end
18
+
19
+ # Check if there are reviews available.
20
+ #
21
+ # @return [Boolean] Returns true if there's at least one review available, or false if there are none.
22
+ def self.reviews_available?
23
+ return !self.queue["reviews_available"].zero?
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,12 @@
1
+ # -*- encoding : utf-8 -*-
2
+ module Wanikani
3
+ class User
4
+ # Gets the user information from WaniKani.
5
+ #
6
+ # @return [Hash] The user information for the WaniKani account by from the API key.
7
+ def self.information
8
+ api_response = Wanikani.api_response("user-information")
9
+ return api_response["user_information"]
10
+ end
11
+ end
12
+ end
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: wanikani
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.2
6
+ platform: ruby
7
+ authors:
8
+ - Dennis Martinez
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-12-31 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ type: :runtime
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.6'
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ~>
27
+ - !ruby/object:Gem::Version
28
+ version: '1.6'
29
+ name: rest-client
30
+ - !ruby/object:Gem::Dependency
31
+ type: :runtime
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: 1.5.0
38
+ prerelease: false
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ~>
43
+ - !ruby/object:Gem::Version
44
+ version: 1.5.0
45
+ name: multi_json
46
+ - !ruby/object:Gem::Dependency
47
+ type: :development
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: '2.12'
54
+ prerelease: false
55
+ version_requirements: !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ~>
59
+ - !ruby/object:Gem::Version
60
+ version: '2.12'
61
+ name: rspec
62
+ - !ruby/object:Gem::Dependency
63
+ type: :development
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: 1.3.0
70
+ prerelease: false
71
+ version_requirements: !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ~>
75
+ - !ruby/object:Gem::Version
76
+ version: 1.3.0
77
+ name: fakeweb
78
+ description: Use WaniKani's (http://www.wanikani.com/) API in your Ruby applications
79
+ email: dennis@dennmart.com
80
+ executables: []
81
+ extensions: []
82
+ extra_rdoc_files: []
83
+ files:
84
+ - lib/wanikani/study_queue.rb
85
+ - lib/wanikani/recent_unlocks.rb
86
+ - lib/wanikani/srs.rb
87
+ - lib/wanikani/user.rb
88
+ - lib/wanikani/level.rb
89
+ - lib/wanikani.rb
90
+ homepage: http://github.com/dennmart/wanikani-gem
91
+ licenses: []
92
+ post_install_message:
93
+ rdoc_options: []
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
+ none: false
104
+ requirements:
105
+ - - ! '>='
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ requirements: []
109
+ rubyforge_project:
110
+ rubygems_version: 1.8.24
111
+ signing_key:
112
+ specification_version: 3
113
+ summary: Add Japanese Kanji learning goodness to your Ruby projects!
114
+ test_files: []
115
+ has_rdoc: