This repository has been archived by the owner on May 28, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from harveyico/fullapi
Full API
- Loading branch information
Showing
10 changed files
with
169 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
.bundle | ||
.config | ||
.yardoc | ||
.rvmrc | ||
Gemfile.lock | ||
InstalledFiles | ||
_yardoc | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,26 @@ | ||
require 'rest-client' | ||
require 'xmlsimple' | ||
require 'myanimelist/version' | ||
require 'rest_client' | ||
require 'active_support/core_ext/hash' | ||
require 'myanimelist/api' | ||
require 'myanimelist/anime' | ||
require 'myanimelist/manga' | ||
require 'myanimelist/credentials' | ||
require 'myanimelist/serializer' | ||
|
||
module MyAnimeList | ||
def self.configure(&block) | ||
credentials = Credentials.new | ||
block.call(credentials) | ||
@myanimelist_username = credentials.username | ||
@myanimelist_password = credentials.password | ||
block.call(Credentials) if block_given? | ||
end | ||
|
||
true if block_given? | ||
def self.verify_credentials | ||
Api.new.verify_credentials! | ||
end | ||
|
||
def self.search_anime(name) | ||
animes = Anime.new(username: @myanimelist_username, password: @myanimelist_password) | ||
animes.search name | ||
MyAnimeList::Anime.search(name) | ||
end | ||
|
||
def self.search_manga(name) | ||
manga = Manga.new(username: @myanimelist_username, password: @myanimelist_password) | ||
manga.search name | ||
MyAnimeList::Manga.search(name) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,19 @@ | ||
module MyAnimeList | ||
class Anime | ||
|
||
def initialize(options={}) | ||
@myanimelist_username = options[:username] | ||
@myanimelist_password = options[:password] | ||
def self.search(name) | ||
Api.new.search('anime', name) | ||
end | ||
|
||
def search(name) | ||
get_search(name) | ||
def self.add(id, params = {}) | ||
Api.new.add('anime', id, params) | ||
end | ||
|
||
def get_search(name) | ||
response = RestClient::Request.new( | ||
method: :get, | ||
url: "https://myanimelist.net/api/anime/search.xml?q=#{CGI::escape name}", | ||
user: @myanimelist_username, | ||
password: @myanimelist_password, | ||
content_type: :xml ).execute | ||
|
||
parse_xml response | ||
def self.update(id, params = {}) | ||
Api.new.update('anime', id, params) | ||
end | ||
|
||
def parse_xml(response) | ||
serialize Hash.from_xml response | ||
def self.remove(id) | ||
Api.new.remove('anime', id) | ||
end | ||
|
||
def serialize(data) | ||
result = MyAnimeList::Serializer.new data, 'anime' | ||
result.fetch | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
module MyAnimeList | ||
class ApiException < Exception; end | ||
class Api | ||
BASE_URL = 'https://myanimelist.net/api' | ||
|
||
def search(type, name) | ||
body = request("#{BASE_URL}/#{type}/search.xml?q=#{CGI::escape name}") | ||
|
||
Serializer.new(body).entry | ||
end | ||
|
||
def add(type, id, params = {}) | ||
request("#{BASE_URL}/#{type}list/add/#{id}.xml", params) == 'Created' | ||
end | ||
|
||
def update(type, id, params = {}) | ||
request("#{BASE_URL}/#{type}list/update/#{id}.xml", params) == 'Updated' | ||
end | ||
|
||
def remove(type, id) | ||
request("#{BASE_URL}/#{type}list/delete/#{id}.xml") == 'Deleted' | ||
end | ||
|
||
def verify_credentials! | ||
body = request("#{BASE_URL}/account/verify_credentials.xml") | ||
|
||
Serializer.new(body).call | ||
end | ||
|
||
private | ||
|
||
def request(url, params = {}) | ||
RestClient::Request.new( build_params(url, params) ).execute do |resp| | ||
if resp.code == 200 || resp.code == 201 | ||
resp.body | ||
else | ||
raise ApiException.new(resp.body) | ||
end | ||
end | ||
end | ||
|
||
def build_params(url, params) | ||
{ | ||
method: :get, | ||
url: url, | ||
user: Credentials.username, | ||
password: Credentials.password, | ||
content_type: :xml | ||
}.merge( headers(params) ) | ||
end | ||
|
||
def headers(params) | ||
return {} if params.empty? | ||
{ | ||
headers: { | ||
params: { | ||
data: XmlSimple.xml_out(params, rootname: 'entry', noattr: true, noindent: true, keeproot: false) | ||
} | ||
} | ||
} | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
module MyAnimeList | ||
class Credentials | ||
attr_accessor :username | ||
attr_accessor :password | ||
class << self | ||
attr_accessor :username | ||
attr_accessor :password | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,19 @@ | ||
module MyAnimeList | ||
class Manga | ||
|
||
def initialize(options={}) | ||
@myanimelist_username = options[:username] | ||
@myanimelist_password = options[:password] | ||
class Manga < Api | ||
def self.search(name) | ||
Api.new.search('manga', name) | ||
end | ||
|
||
def search(name) | ||
get_search(name) | ||
def self.add(id, params = {}) | ||
Api.new.add('manga', id, params) | ||
end | ||
|
||
def get_search(name) | ||
response = RestClient::Request.new( | ||
method: :get, | ||
url: "https://myanimelist.net/api/manga/search.xml?q=#{CGI::escape name}", | ||
user: @myanimelist_username, | ||
password: @myanimelist_password, | ||
content_type: :xml ).execute | ||
|
||
parse_xml response | ||
def self.update(id, params = {}) | ||
Api.new.update('manga', id, params) | ||
end | ||
|
||
def parse_xml(response) | ||
serialize Hash.from_xml response | ||
def self.remove(id) | ||
Api.new.remove('manga', id) | ||
end | ||
|
||
def serialize(data) | ||
result = MyAnimeList::Serializer.new data, 'manga' | ||
result.fetch | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,18 @@ | ||
module MyAnimeList | ||
class Serializer | ||
def initialize(data, type) | ||
@type = type | ||
@animes = data || [] | ||
end | ||
attr_accessor :response | ||
|
||
def fetch | ||
serialize_data | ||
def initialize(resp) | ||
@response = resp | ||
end | ||
|
||
def serialize_data | ||
return @animes if is_array? @animes | ||
|
||
collection = [] | ||
data = @animes[@type]['entry'] | ||
if is_array? data | ||
collection += data | ||
else | ||
collection << data | ||
end | ||
def entry | ||
hash = XmlSimple.xml_in(response, force_array: false) | ||
hash['entry'] | ||
end | ||
|
||
def is_array?(data) | ||
data.is_a? Array | ||
def call | ||
XmlSimple.xml_in(response, force_array: false) | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
module Myanimelist | ||
VERSION = '0.0.6' | ||
module MyAnimeList | ||
VERSION = '1.0.0' | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,7 @@ require 'myanimelist/version' | |
|
||
Gem::Specification.new do |spec| | ||
spec.name = 'myanimelist' | ||
spec.version = Myanimelist::VERSION | ||
spec.version = MyAnimeList::VERSION | ||
spec.authors = ['Harvey Ico'] | ||
spec.email = ['[email protected]'] | ||
spec.description = %q{The API for MyAnimeList.net} | ||
|
@@ -18,8 +18,8 @@ Gem::Specification.new do |spec| | |
spec.test_files = spec.files.grep(%r{^(test|spec|features)/}) | ||
spec.require_paths = ['lib'] | ||
|
||
spec.add_development_dependency 'bundler', '~> 1.3' | ||
spec.add_development_dependency 'rake' | ||
spec.add_dependency 'rest-client' | ||
spec.add_dependency 'activesupport' | ||
spec.add_development_dependency 'bundler', '1.3' | ||
spec.add_development_dependency 'rake', '12.0.0' | ||
spec.add_dependency 'rest-client', '2.0.2' | ||
spec.add_dependency 'xml-simple', '1.1.5' | ||
end |