Class: Wanikani::Client
- Inherits:
-
Object
- Object
- Wanikani::Client
- Defined in:
- lib/wanikani/client.rb
Constant Summary collapse
- API_ENDPOINT =
"https://api.wanikani.com"
Instance Attribute Summary collapse
-
#api_key ⇒ Object
Returns the value of attribute api_key.
-
#api_revision ⇒ Object
Returns the value of attribute api_revision.
Class Method Summary collapse
-
.valid_api_key?(api_key = nil) ⇒ Boolean
Verifies if the specified API key is valid by checking WaniKani’s API.
Instance Method Summary collapse
-
#api_endpoint ⇒ String
API endpoint at WaniKani.
-
#get(resource, parameters = nil) ⇒ Hash
Contacts the WaniKani API and returns the data specified.
-
#initialize(options = {}) ⇒ Wanikani::Client
constructor
Initialize a client which will be used to communicate with WaniKani.
-
#valid_api_key?(api_key = nil) ⇒ Boolean
Verifies if the client’s API key is valid by checking WaniKani’s API.
Constructor Details
#initialize(options = {}) ⇒ Wanikani::Client
Initialize a client which will be used to communicate with WaniKani.
16 17 18 19 20 21 22 23 |
# File 'lib/wanikani/client.rb', line 16 def initialize( = {}) raise ArgumentError, "You must specify a WaniKani API key before querying the API." if [:api_key].nil? || [:api_key].empty? raise ArgumentError, "API revision should be one of the following: #{Wanikani::VALID_API_REVISIONS.join(', ')}." unless Wanikani::VALID_API_REVISIONS.include?([:api_revision]) || [:api_revision].nil? @api_key = [:api_key] @api_revision = [:api_revision] ||= Wanikani::DEFAULT_API_REVISION end |
Instance Attribute Details
#api_key ⇒ Object
Returns the value of attribute api_key.
9 10 11 |
# File 'lib/wanikani/client.rb', line 9 def api_key @api_key end |
#api_revision ⇒ Object
Returns the value of attribute api_revision.
9 10 11 |
# File 'lib/wanikani/client.rb', line 9 def api_revision @api_revision end |
Class Method Details
.valid_api_key?(api_key = nil) ⇒ Boolean
Verifies if the specified API key is valid by checking WaniKani’s API.
43 44 45 46 47 48 |
# File 'lib/wanikani/client.rb', line 43 def self.valid_api_key?(api_key = nil) raise ArgumentError, "You must specify a WaniKani API key before querying the API." if api_key.nil? || api_key.empty? @client = Wanikani::Client.new(api_key: api_key) return @client.valid_api_key? end |
Instance Method Details
#api_endpoint ⇒ String
API endpoint at WaniKani
53 54 55 |
# File 'lib/wanikani/client.rb', line 53 def api_endpoint API_ENDPOINT end |
#get(resource, parameters = nil) ⇒ Hash
Contacts the WaniKani API and returns the data specified.
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/wanikani/client.rb', line 62 def get(resource, parameters = nil) raise ArgumentError, "You must define a resource to query WaniKani" if resource.nil? || resource.empty? begin res = client.get("/v2/#{resource}", parameters) if !res.success? || res.body.has_key?("error") raise_exception(res) else return res.body end rescue => error raise Exception, "There was an error: #{error.}" end end |
#valid_api_key?(api_key = nil) ⇒ Boolean
Verifies if the client’s API key is valid by checking WaniKani’s API.
29 30 31 32 33 34 35 36 37 |
# File 'lib/wanikani/client.rb', line 29 def valid_api_key?(api_key = nil) api_key ||= @api_key return false if api_key.empty? res = client.get("/v2/user/") return false if !res.success? || res.body.has_key?("error") return true end |