Skip to content
This repository has been archived by the owner on May 28, 2019. It is now read-only.

Commit

Permalink
Merge pull request #5 from harveyico/fullapi
Browse files Browse the repository at this point in the history
Full API
  • Loading branch information
harveyico committed May 2, 2017
2 parents 4146eef + 9eb0d08 commit de8f121
Show file tree
Hide file tree
Showing 10 changed files with 169 additions and 101 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
.bundle
.config
.yardoc
.rvmrc
Gemfile.lock
InstalledFiles
_yardoc
Expand Down
75 changes: 60 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
# MyAnimeList
An API for http:https://myanimelist.net for searching animes and mangas up to date!
A gem wrapper for http:https://myanimelist.net that covers all the methods available on the current API
For additional documentation, visit: http:https://myanimelist.net/modules.php?go=api

## Installation

Add this line to your application's Gemfile:

gem 'myanimelist', '~> 0.0.6'
gem 'myanimelist', '~> 1.0'

And then execute:

Expand All @@ -16,37 +17,81 @@ Or install it yourself as:
$ gem install myanimelist

### Configuration
MyAnimeList API requires authentication from the actual site, so you need to sign up first
<a href="http:https://myanimelist.net/register.php">here</a>
MyAnimeList API requires authentication from the actual site, so you need to sign up first [here](http:https://myanimelist.net/register.php).

After signing up and verifying your email, you need to assign the username and password on your respective myanimelist API,
here's what you need to do,
If you are using Rails: setup `config/initializers/myanimelist.rb`:
```ruby
MyAnimeList.configure do |config|
config.username = "username"
config.password = "password"
config.username = ENV['username']
config.password = ENV['password']
end
```
If you are not using rails make sure to set this up before you can start searching.

## Usage
#####For Anime:
```ruby
MyAnimeList.search_anime("Anime name here.")
#### Verify Credentials
Additional step for verifying credentials from myanimelist.
```
####For Manga:
```ruby
MyAnimeList.search_manga("Manga name here.")
MyAnimeList.verify_crendetials
```

#### Search Anime/Manga
Returns an Array of Hashes for the result.
```
# Anime
> MyAnimeList.search_anime('Fairy Tail')
> MyAnimeList::Anime.search('Fairy Tail')
# Manga
> MyAnimeList.search_manga('Fairy Tail')
> MyAnimeList::Manga.search('Fairy Tail'
```

#### Add Anime/Manga
Adds anime/manga on your list. For additional `Anime` parameters, please refer [here](https://myanimelist.net/modules.php?go=api#animevalues). For `Manga`, please refer [here](https://myanimelist.net/modules.php?go=api#mangavalues).
Just imagine that the xml value would be the key:value pair in ruby, as seen below.
```
# Anime
> MyAnimeList::Anime.add(anime_id, { episode: 1, status: 2 })
=> true
Simple isn't it?
# Manga
> MyAnimeList::Manga.add(manga_id, { chapter: 1 })
=> true
```

#### Update Anime/Manga
Updates anime/manga on your list. For additional `Anime` parameters, please refer [here](https://myanimelist.net/modules.php?go=api#animevalues). For `Manga`, please refer [here](https://myanimelist.net/modules.php?go=api#mangavalues).
Just imagine that the xml value would be the key:value pair in ruby, as seen below.
```
# Anime
> MyAnimeList::Anime.update(anime_id, { episode: 1, status: 2 })
=> true
# Manga
> MyAnimeList::Manga.update(manga_id, { chapter: 1 })
=> true
```

#### Remove Anime/Manga
Remove anime/manga on your list.
```
# Anime
> MyAnimeList::Anime.remove(anime_id)
=> true
# Manga
> MyAnimeList::Manga.remove(manga_id)
=> true
```

##Example
## Example
```ruby
MyAnimeList.search_anime('Fairy Tail')

#will result to the followinghash
# will result to the followinghash

=> [{"id"=>"6702",
"title"=>"Fairy Tail",
Expand Down
20 changes: 9 additions & 11 deletions lib/myanimelist.rb
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
31 changes: 8 additions & 23 deletions lib/myanimelist/anime.rb
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
63 changes: 63 additions & 0 deletions lib/myanimelist/api.rb
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
6 changes: 4 additions & 2 deletions lib/myanimelist/credentials.rb
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
33 changes: 9 additions & 24 deletions lib/myanimelist/manga.rb
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
27 changes: 8 additions & 19 deletions lib/myanimelist/serializer.rb
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
4 changes: 2 additions & 2 deletions lib/myanimelist/version.rb
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
10 changes: 5 additions & 5 deletions myanimelist.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -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

0 comments on commit de8f121

Please sign in to comment.