wanikani 1.3 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 47162b1fe2e86d5d805dae4c0e248c0f562ee8ba
4
- data.tar.gz: ce663341e1e1a9f753a04d629eb3062c08335406
2
+ SHA256:
3
+ metadata.gz: a313e7eca6981bd4ba6baf4f2dd0360a5e013bfde51a1bfc8d7f77184afee9ff
4
+ data.tar.gz: 2e13e29b6b19234142639720afa85578c7f036ba64e958c16102fbcde9c10990
5
5
  SHA512:
6
- metadata.gz: 76585b1c58d2148d2ad6ace39e9be05e7f7f348e33d5ee73893a95c06496e679043e7df77567ff094d9f8c8c900d92257d766559c72ccc62904eae621790581e
7
- data.tar.gz: 0d6ff0ef83595072ea0010594a3656a6022980d56fae0627ae9ec205349036cea08bf9ebbc00d427a1851e9572f5f4bf11fcaca6a1623f16ee281cddd84710dd
6
+ metadata.gz: 9bdbca8d53bed166a13720ba58e14564788d6a3c5e868d7e3469209940ac904c88b576514a860ac8926357f069523ed1da87ff86dff405de64f50684d29a080f
7
+ data.tar.gz: 1dceaa8c18c3eeb7cdf4e92fdb5b629b40ddd78627b648c8d8f9585fe6786ed109e26904483fbf9ec0781fabd10bd4f419a2fac39609610456aeaf9890db0185
@@ -1,69 +1,40 @@
1
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
- require 'wanikani/critical_items'
11
- require 'wanikani/exceptions'
2
+ require 'wanikani/client'
3
+ require 'wanikani/response'
4
+ require 'wanikani/configuration'
5
+ require 'wanikani/models/shared'
6
+ require 'wanikani/models/assignment'
7
+ require 'wanikani/models/level_progression'
8
+ require 'wanikani/models/reset'
9
+ require 'wanikani/models/review'
10
+ require 'wanikani/models/review_statistic'
11
+ require 'wanikani/models/spaced_repetition_system'
12
+ require 'wanikani/models/study_material'
13
+ require 'wanikani/models/subject'
14
+ require 'wanikani/models/summary'
15
+ require 'wanikani/models/user'
16
+ require 'wanikani/models/voice_actor'
12
17
 
13
18
  Encoding.default_external = Encoding::UTF_8
14
19
  Encoding.default_internal = Encoding::UTF_8
15
20
 
16
21
  module Wanikani
17
- API_ENDPOINT = "https://www.wanikani.com/api"
18
- DEFAULT_API_VERSION = "v1.4"
19
- VALID_API_VERSIONS = %w(v1 v1.1 v1.2 v1.3 v1.4)
22
+ DEFAULT_API_REVISION = '20170710'
23
+ VALID_API_REVISIONS = %w(20170710)
20
24
 
21
- def self.api_key=(api_key)
22
- @api_key = api_key
23
- end
25
+ class InvalidKey < Exception; end
26
+ class Exception < Exception; end
24
27
 
25
- def self.api_key
26
- @api_key
27
- end
28
+ DEFAULT_CONFIG = {
29
+ api_revision: DEFAULT_API_REVISION
30
+ }
28
31
 
29
- def self.api_version=(api_version)
30
- raise ArgumentError, "API version should be one of the following: #{VALID_API_VERSIONS.join(', ')}." unless VALID_API_VERSIONS.include?(api_version) || api_version.nil?
31
- @api_version = api_version
32
+ def self.configure
33
+ yield @config ||= Wanikani::Configuration.new(DEFAULT_CONFIG)
32
34
  end
33
35
 
34
- def self.api_version
35
- @api_version ||= DEFAULT_API_VERSION
36
- end
37
-
38
- def self.api_response(resource, optional_arg = nil)
39
- raise ArgumentError, "You must define a resource to query Wanikani" if resource.nil? || resource.empty?
40
- raise ArgumentError, "You must set your Wanikani API key before querying the API" if Wanikani.api_key.nil? || Wanikani.api_key.empty?
41
-
42
- begin
43
- response = RestClient.get("#{Wanikani::API_ENDPOINT}/#{Wanikani.api_version}/user/#{Wanikani.api_key}/#{resource}/#{optional_arg}")
44
- parsed_response = MultiJson.load(response)
45
-
46
- if parsed_response.has_key?("error")
47
- self.raise_exception(parsed_response["error"]["message"])
48
- else
49
- return parsed_response
50
- end
51
- rescue => error
52
- self.raise_exception(error.message)
53
- end
54
- end
55
-
56
- def self.valid_api_key?(api_key = nil)
57
- api_key ||= Wanikani.api_key
58
- return false if api_key.nil? || api_key.empty?
59
-
60
- response = RestClient.get("#{Wanikani::API_ENDPOINT}/#{Wanikani.api_version}/user/#{api_key}/user-information/")
61
- !MultiJson.load(response).has_key?("error")
62
- end
63
-
64
- private
65
-
66
- def self.raise_exception(message)
67
- raise Wanikani::Exception, "There was an error fetching the data from Wanikani (#{message})"
36
+ # global settings
37
+ def self.config
38
+ @config ||= Wanikani::Configuration.new(DEFAULT_CONFIG)
68
39
  end
69
40
  end
@@ -0,0 +1,114 @@
1
+ # -*- encoding : utf-8 -*-
2
+ require 'faraday'
3
+ require 'faraday_middleware'
4
+
5
+ module Wanikani
6
+ class Client
7
+ API_ENDPOINT = "https://api.wanikani.com"
8
+
9
+ attr_accessor :api_key, :api_revision
10
+
11
+ # Initialize a client which will be used to communicate with WaniKani.
12
+ #
13
+ # @param options [Hash] the API key (required) and API version (optional)
14
+ # used to communicate with the WaniKani API.
15
+ # @return [Wanikani::Client] an instance of Wanikani::Client.
16
+ def initialize(options = {})
17
+ raise ArgumentError, "You must specify a WaniKani API key before querying the API." if options[:api_key].nil? || options[:api_key].empty?
18
+ 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?
19
+
20
+ @api_key = options[:api_key]
21
+ @api_revision = options[:api_revision] ||= Wanikani::DEFAULT_API_REVISION
22
+
23
+ end
24
+
25
+ # Verifies if the client's API key is valid by checking WaniKani's API.
26
+ #
27
+ # @param api_key [String] the API key to validate in WaniKani.
28
+ # @return [Boolean] whether the API key is valid.
29
+ def valid_api_key?(api_key = nil)
30
+ api_key ||= @api_key
31
+ return false if api_key.empty?
32
+
33
+ res = client.get("/v2/user/")
34
+
35
+ return false if !res.success? || res.body.has_key?("error")
36
+ return true
37
+ end
38
+
39
+ # Verifies if the specified API key is valid by checking WaniKani's API.
40
+ #
41
+ # @param api_key [String] the API key to validate in WaniKani.
42
+ # @return [Boolean] whether the API key is valid.
43
+ def self.valid_api_key?(api_key = nil)
44
+ raise ArgumentError, "You must specify a WaniKani API key before querying the API." if api_key.nil? || api_key.empty?
45
+
46
+ @client = Wanikani::Client.new(api_key: api_key)
47
+ return @client.valid_api_key?
48
+ end
49
+
50
+ # API endpoint at WaniKani
51
+ #
52
+ # @return [String] URL of endpoint
53
+ def api_endpoint
54
+ API_ENDPOINT
55
+ end
56
+
57
+ # Contacts the WaniKani API and returns the data specified.
58
+ #
59
+ # @param resource [String] the resource to access.
60
+ # @param parameters [Hash] optional arguments for the specified resource.
61
+ # @return [Hash] the parsed API response.
62
+ def get(resource, parameters = nil)
63
+ raise ArgumentError, "You must define a resource to query WaniKani" if resource.nil? || resource.empty?
64
+
65
+ begin
66
+ res = client.get("/v2/#{resource}", parameters)
67
+
68
+ if !res.success? || res.body.has_key?("error")
69
+ raise_exception(res)
70
+ else
71
+ return res.body
72
+ end
73
+ rescue => error
74
+ raise Exception, "There was an error: #{error.message}"
75
+ end
76
+ end
77
+
78
+ private
79
+
80
+ # Sets up the HTTP client for communicating with the WaniKani API.
81
+ #
82
+ # @return [Faraday::Connection] the HTTP client to communicate with the
83
+ # WaniKani API.
84
+ def client
85
+ Faraday.new(url: api_endpoint, :headers => headers) do |conn|
86
+ conn.response :json, :content_type => /\bjson$/
87
+ conn.adapter Faraday.default_adapter
88
+ end
89
+ end
90
+
91
+ def headers
92
+ {
93
+ 'Content-Type' => 'application/json',
94
+ 'Wanikani-Revision' => api_revision,
95
+ 'Authorization' => "Bearer #{api_key}"
96
+ }
97
+ end
98
+
99
+
100
+ # Handles exceptions according to the API response.
101
+ #
102
+ # @param response [Hash] the parsed API response from WaniKani's API.
103
+ def raise_exception(response)
104
+ raise Wanikani::InvalidKey, "The API key used for this request is invalid." and return if response.status == 401
105
+
106
+ message = if response.body.is_a?(Hash) and response.body.has_key?("error")
107
+ response.body["error"]
108
+ else
109
+ "Status code: #{response.status}"
110
+ end
111
+ raise Wanikani::Exception, "There was an error fetching the data from WaniKani (#{message})"
112
+ end
113
+ end
114
+ end
@@ -0,0 +1,16 @@
1
+ module Wanikani
2
+ class Configuration
3
+ attr_accessor :api_revision, :api_key
4
+
5
+ def initialize(configuration)
6
+ @api_revision = configuration.fetch(:api_revision)
7
+ end
8
+
9
+ def to_hash
10
+ {
11
+ api_revision: api_revision,
12
+ api_key: api_key
13
+ }
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,24 @@
1
+ # -*- encoding : utf-8 -*-
2
+ module Wanikani
3
+ module Assignment
4
+ extend Wanikani::Shared
5
+
6
+ PERMITTED_PARAMS = %w[available_after available_before burned hidden ids immediately_available_for_lessons immediately_available_for_review in_review levels passed srs_stages started subject_ids subject_types unlocked updated_after page_after_id page_before_id].freeze
7
+
8
+ def self.permitted_params
9
+ PERMITTED_PARAMS
10
+ end
11
+
12
+ def self.find_all
13
+ find_by
14
+ end
15
+
16
+ def self.find_by(parameters = {})
17
+ respond(client.get('assignments', filter(parameters)))
18
+ end
19
+
20
+ def self.find(id)
21
+ respond(client.get("assignments/#{id}"))
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,25 @@
1
+ # -*- encoding : utf-8 -*-
2
+ module Wanikani
3
+ module LevelProgression
4
+ extend Wanikani::Shared
5
+
6
+ PERMITTED_PARAMS = %w[ids updated_after page_after_id page_before_id].freeze
7
+
8
+ def self.find_all
9
+ find_by
10
+ end
11
+
12
+ def self.find_by(parameters = {})
13
+ respond(client.get('level_progressions',
14
+ filter(parameters)))
15
+ end
16
+
17
+ def self.find(id)
18
+ respond(client.get("level_progressions/#{id}"))
19
+ end
20
+
21
+ def self.permitted_params
22
+ PERMITTED_PARAMS
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,20 @@
1
+ # -*- encoding : utf-8 -*-
2
+ module Wanikani
3
+ module Reset
4
+ extend Wanikani::Shared
5
+
6
+ PERMITTED_PARAMS = %w[ids assignment_ids subject_ids updated_after page_after_id page_before_id].freeze
7
+
8
+ def self.find_by(parameters = {})
9
+ respond(client.get('resets', parameters))
10
+ end
11
+
12
+ def self.find(id)
13
+ respond(client.get("resets/#{id}"))
14
+ end
15
+
16
+ def self.permitted_params
17
+ PERMITTED_PARAMS
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,24 @@
1
+ # -*- encoding : utf-8 -*-
2
+ module Wanikani
3
+ module Review
4
+ extend Wanikani::Shared
5
+
6
+ PERMITTED_PARAMS = %w[ids assignment_ids subject_ids updated_after page_after_id page_before_id].freeze
7
+
8
+ def self.find_all
9
+ find_by
10
+ end
11
+
12
+ def self.find_by(parameters = {})
13
+ respond(client.get('reviews', parameters))
14
+ end
15
+
16
+ def self.find(id)
17
+ respond(client.get("reviews/#{id}"))
18
+ end
19
+
20
+ def self.permitted_params
21
+ PERMITTED_PARAMS
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,24 @@
1
+ # -*- encoding : utf-8 -*-
2
+ module Wanikani
3
+ module ReviewStatistic
4
+ extend Wanikani::Shared
5
+
6
+ PERMITTED_PARAMS = %w[ids hidden percentages_greater_than percentages_less_than subject_ids subject_types updated_after page_after_id page_before_id].freeze
7
+
8
+ def self.find_all
9
+ find_by
10
+ end
11
+
12
+ def self.find_by(parameters = {})
13
+ respond(client.get('review_statistics', parameters))
14
+ end
15
+
16
+ def self.find(id)
17
+ respond(client.get("review_statistics/#{id}"))
18
+ end
19
+
20
+ def self.permitted_params
21
+ PERMITTED_PARAMS
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,20 @@
1
+ # -*- encoding : utf-8 -*-
2
+ module Wanikani
3
+ module Shared
4
+ def respond(json)
5
+ Response.new(json)
6
+ end
7
+
8
+ def permitted_params
9
+ raise NotImplementedError
10
+ end
11
+
12
+ def filter(parameters)
13
+ parameters.keep_if { |key, value| permitted_params.include?(key.to_s) }
14
+ end
15
+
16
+ def client
17
+ @client ||= ::Wanikani::Client.new(::Wanikani.config.to_hash)
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,25 @@
1
+ # -*- encoding : utf-8 -*-
2
+ module Wanikani
3
+ module SpacedRepetitionSystem
4
+ extend Wanikani::Shared
5
+
6
+ PERMITTED_PARAMS = %w[ids updated_after].freeze
7
+
8
+ def self.find_all
9
+ find_by
10
+ end
11
+
12
+ def self.find_by(parameters = {})
13
+ respond(client.get('spaced_repetition_systems',
14
+ filter(parameters)))
15
+ end
16
+
17
+ def self.find(id)
18
+ respond(client.get("spaced_repetition_systems/#{id}"))
19
+ end
20
+
21
+ def self.permitted_params
22
+ PERMITTED_PARAMS
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,24 @@
1
+ # -*- encoding : utf-8 -*-
2
+ module Wanikani
3
+ module StudyMaterial
4
+ extend Wanikani::Shared
5
+
6
+ PERMITTED_PARAMS = %w[ids hidden subject_ids subject_types updated_after page_after_id page_before_id].freeze
7
+
8
+ def self.find_all
9
+ find_by
10
+ end
11
+
12
+ def self.find_by(parameters = {})
13
+ respond(client.get('study_materials', parameters))
14
+ end
15
+
16
+ def self.find(id)
17
+ respond(client.get("study_materials/#{id}"))
18
+ end
19
+
20
+ def self.permitted_params
21
+ PERMITTED_PARAMS
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,24 @@
1
+ # -*- encoding : utf-8 -*-
2
+ module Wanikani
3
+ module Subject
4
+ extend Wanikani::Shared
5
+
6
+ PERMITTED_PARAMS = %w[ids types slugs levels hidden updated_after page_after_id page_before_id].freeze
7
+
8
+ def self.find_all
9
+ find_by
10
+ end
11
+
12
+ def self.find_by(parameters = {})
13
+ respond(client.get('subjects', parameters))
14
+ end
15
+
16
+ def self.find(id)
17
+ respond(client.get("subjects/#{id}"))
18
+ end
19
+
20
+ def self.permitted_params
21
+ PERMITTED_PARAMS
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,10 @@
1
+ # -*- encoding : utf-8 -*-
2
+ module Wanikani
3
+ module Summary
4
+ extend Wanikani::Shared
5
+
6
+ def self.fetch
7
+ respond(client.get('summary'))
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,10 @@
1
+ # -*- encoding : utf-8 -*-
2
+ module Wanikani
3
+ module User
4
+ extend Wanikani::Shared
5
+
6
+ def self.fetch
7
+ respond(client.get('user'))
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,24 @@
1
+ # -*- encoding : utf-8 -*-
2
+ module Wanikani
3
+ module VoiceActor
4
+ extend Wanikani::Shared
5
+
6
+ PERMITTED_PARAMS = %w[ids updated_after page_after_id page_before_id].freeze
7
+
8
+ def self.find_all
9
+ find_by
10
+ end
11
+
12
+ def self.find_by(parameters = {})
13
+ respond(client.get('voice_actors', parameters))
14
+ end
15
+
16
+ def self.find(id)
17
+ respond(client.get("voice_actors/#{id}"))
18
+ end
19
+
20
+ def self.permitted_params
21
+ PERMITTED_PARAMS
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,34 @@
1
+ # -*- encoding : utf-8 -*-
2
+ module Wanikani
3
+ class Response
4
+ attr_reader :response_data
5
+
6
+ def initialize(response_data)
7
+ @response_data = response_data
8
+ end
9
+
10
+ def data
11
+ response_data['data']
12
+ end
13
+
14
+ def id
15
+ response_data['id']
16
+ end
17
+
18
+ def object
19
+ response_data['object']
20
+ end
21
+
22
+ def data_updated_at
23
+ response_data['data_updated_at']
24
+ end
25
+
26
+ def per_page
27
+ response_data['pages']['per_page']
28
+ end
29
+
30
+ def total_count
31
+ response_data['total_count']
32
+ end
33
+ end
34
+ end
@@ -1,3 +1,3 @@
1
1
  module Wanikani
2
- VERSION = '1.3'
2
+ VERSION = '3.0.0'
3
3
  end
metadata CHANGED
@@ -1,71 +1,71 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wanikani
3
3
  version: !ruby/object:Gem::Version
4
- version: '1.3'
4
+ version: 3.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dennis Martinez
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-02-27 00:00:00.000000000 Z
11
+ date: 2020-09-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: rest-client
14
+ name: faraday
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '1.7'
19
+ version: '0.12'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '1.7'
26
+ version: '0.12'
27
27
  - !ruby/object:Gem::Dependency
28
- name: multi_json
28
+ name: faraday_middleware
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '1.10'
33
+ version: '0.12'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '1.10'
40
+ version: '0.12'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rspec
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: '3.3'
47
+ version: '3.6'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: '3.3'
54
+ version: '3.6'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: webmock
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
59
  - - "~>"
60
60
  - !ruby/object:Gem::Version
61
- version: '1.2'
61
+ version: '3.0'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
- version: '1.2'
68
+ version: '3.0'
69
69
  description: Use WaniKani's (https://www.wanikani.com/) API in your Ruby applications
70
70
  email: dennis@dennmart.com
71
71
  executables: []
@@ -73,19 +73,29 @@ extensions: []
73
73
  extra_rdoc_files: []
74
74
  files:
75
75
  - lib/wanikani.rb
76
- - lib/wanikani/critical_items.rb
77
- - lib/wanikani/exceptions.rb
78
- - lib/wanikani/level.rb
79
- - lib/wanikani/recent_unlocks.rb
80
- - lib/wanikani/srs.rb
81
- - lib/wanikani/study_queue.rb
82
- - lib/wanikani/user.rb
76
+ - lib/wanikani/client.rb
77
+ - lib/wanikani/configuration.rb
78
+ - lib/wanikani/models/assignment.rb
79
+ - lib/wanikani/models/level_progression.rb
80
+ - lib/wanikani/models/reset.rb
81
+ - lib/wanikani/models/review.rb
82
+ - lib/wanikani/models/review_statistic.rb
83
+ - lib/wanikani/models/shared.rb
84
+ - lib/wanikani/models/spaced_repetition_system.rb
85
+ - lib/wanikani/models/study_material.rb
86
+ - lib/wanikani/models/subject.rb
87
+ - lib/wanikani/models/summary.rb
88
+ - lib/wanikani/models/user.rb
89
+ - lib/wanikani/models/voice_actor.rb
90
+ - lib/wanikani/response.rb
83
91
  - lib/wanikani/version.rb
84
92
  homepage: https://github.com/dennmart/wanikani-gem
85
93
  licenses:
86
94
  - MIT
87
95
  metadata: {}
88
- post_install_message:
96
+ post_install_message: |2
97
+ Version 3.0 of the wanikani gem introduces many breaking changes in support of Wanikani API 2.0.
98
+ Please view the README for more information: https://github.com/dennmart/wanikani-gem
89
99
  rdoc_options: []
90
100
  require_paths:
91
101
  - lib
@@ -93,15 +103,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
93
103
  requirements:
94
104
  - - ">="
95
105
  - !ruby/object:Gem::Version
96
- version: 1.9.2
106
+ version: '2.5'
97
107
  required_rubygems_version: !ruby/object:Gem::Requirement
98
108
  requirements:
99
109
  - - ">="
100
110
  - !ruby/object:Gem::Version
101
111
  version: '0'
102
112
  requirements: []
103
- rubyforge_project:
104
- rubygems_version: 2.4.5
113
+ rubygems_version: 3.1.2
105
114
  signing_key:
106
115
  specification_version: 4
107
116
  summary: Add Japanese Kanji learning goodness to your Ruby projects!
@@ -1,23 +0,0 @@
1
- # -*- encoding : utf-8 -*-
2
- module Wanikani
3
- class CriticalItems
4
- # Gets the user's current items under 'Critical Items'.
5
- #
6
- # @param [Integer] percentage maximum percentage
7
- # @return [Array] Returns hashes of critical items and their related information.
8
- def self.critical(percentage = 75)
9
- raise ArgumentError, "Percentage must be an Integer between 0 and 100" if !percentage.between?(0, 100)
10
- api_response = Wanikani.api_response("critical-items", percentage)
11
- return api_response["requested_information"]
12
- end
13
-
14
- # Gets the full response of the Critical Items List API call.
15
- #
16
- # @param [Integer] percentage maximum percentage
17
- # @return [Hash] Full response from the Critical Items List API call.
18
- def self.full_response(percentage = 75)
19
- raise ArgumentError, "Percentage must be an Integer between 0 and 100" if !percentage.between?(0, 100)
20
- return Wanikani.api_response("critical-items", percentage)
21
- end
22
- end
23
- end
@@ -1,3 +0,0 @@
1
- module Wanikani
2
- class Exception < Exception; end
3
- end
@@ -1,38 +0,0 @@
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
- current_level = { "current_level" => api_response["user_information"]["level"] }
10
- return api_response["requested_information"].merge(current_level)
11
- end
12
-
13
- # Gets the full response of the Level Progression API call.
14
- #
15
- # @return [Hash] Full response from the Level Progression API call.
16
- def self.full_response
17
- return Wanikani.api_response("level-progression")
18
- end
19
-
20
- private
21
-
22
- def self.method_missing(name, *args)
23
- super unless [:radicals, :kanji, :vocabulary].include?(name)
24
-
25
- levels = args.push
26
- levels = levels.join(',') if levels.is_a?(Array)
27
- api_response = Wanikani.api_response(name.to_s, levels)
28
-
29
- # The vocabulary API call without specifying levels returns a Hash instead
30
- # of an Array, so this is a hacky way of dealing with it.
31
- if api_response["requested_information"].is_a?(Hash)
32
- return api_response["requested_information"]["general"]
33
- else
34
- return api_response["requested_information"]
35
- end
36
- end
37
- end
38
- end
@@ -1,32 +0,0 @@
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 full response of the Recents Unlocks List API call.
14
- #
15
- # @param [Integer] limit the total number of items returned.
16
- # @return [Hash] Full response from the Recent Unlocks List API call.
17
- def self.full_response(limit = 10)
18
- return Wanikani.api_response("recent-unlocks", limit)
19
- end
20
-
21
- private
22
-
23
- def self.method_missing(name, *args)
24
- super unless [:radicals, :vocabulary, :kanji].include?(name)
25
-
26
- limit = args.shift || 10
27
- type = name == :radicals ? name.to_s.chop! : name.to_s
28
- unlock_list = self.list(limit)
29
- return unlock_list.select { |unlock| unlock["type"] == type }
30
- end
31
- end
32
- end
@@ -1,46 +0,0 @@
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
-
20
- # Gets all items for a specific SRS level.
21
- #
22
- # @param [String] item_type the SRS level for the items returned.
23
- # @return [Array] Returns all the items matching the specified SRS for each level for the user.
24
- def self.items_by_type(item_type)
25
- raise ArgumentError, "Please use a valid SRS type." if !ITEM_TYPES.include?(item_type) || item_type == "all"
26
-
27
- items_by_type = []
28
- %w(radicals kanji vocabulary).each do |type|
29
- items = Wanikani::Level.send(type)
30
- items.reject! { |item| item["user_specific"].nil? || item["user_specific"]["srs"] != item_type }.map! do |item|
31
- item.merge!("type" => (type == 'radicals' ? 'radical' : type))
32
- end
33
- items_by_type << items
34
- end
35
-
36
- items_by_type.flatten
37
- end
38
-
39
- # Gets the full response of the SRS Distribution API call.
40
- #
41
- # @return [Hash] Full response from the SRS Distribution API call.
42
- def self.full_response
43
- return Wanikani.api_response("srs-distribution")
44
- end
45
- end
46
- end
@@ -1,33 +0,0 @@
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
-
26
- # Gets the full response of the Study Queue API call.
27
- #
28
- # @return [Hash] Full response from the Study Queue API call.
29
- def self.full_response
30
- return Wanikani.api_response("study-queue")
31
- end
32
- end
33
- end
@@ -1,53 +0,0 @@
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
-
12
- # Checks if the user is currently in vacation mode.
13
- #
14
- # @return [Boolean] true if vacation_date contains a timestamp, false if
15
- # vacation_date is null.
16
- def self.on_vacation?
17
- api_response = Wanikani.api_response("user-information")
18
- vacation_date = api_response["user_information"]["vacation_date"]
19
- return !vacation_date.nil?
20
- end
21
-
22
- # Returns the Gravatar image URL using the Gravatar hash from the user's information.
23
- #
24
- # @param options [Hash] Optional settings for the gravatar URL.
25
- # @return [String] The Gravatar URL, with the optional size parameter, nil if
26
- # the user information contains no Gravatar hash.
27
- def self.gravatar_url(options = {})
28
- raise ArgumentError, "The size parameter must be an integer" if options[:size] && !options[:size].is_a?(Integer)
29
- api_response = Wanikani.api_response("user-information")
30
- hash = api_response["user_information"]["gravatar"]
31
-
32
- return nil if hash.nil?
33
- return build_gravatar_url(hash, options)
34
- end
35
-
36
- # Gets the full response of the User Information API call.
37
- #
38
- # @return [Hash] Full response from the User Information API call.
39
- def self.full_response
40
- return Wanikani.api_response("user-information")
41
- end
42
-
43
- private
44
-
45
- def self.build_gravatar_url(hash, options)
46
- params = "d=mm" # Use 'Mystery Man' image if no image found.
47
- params += "&size=#{options[:size]}" if options[:size]
48
-
49
- return "https://secure.gravatar.com/avatar/#{hash}?#{params}" if options[:secure]
50
- return "http://www.gravatar.com/avatar/#{hash}?#{params}"
51
- end
52
- end
53
- end