Class: Jikanrb::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/jikanrb/client.rb

Overview

Main HTTP client for interacting with the Jikan API v4. Handles requests, rate limiting, retries, and error handling.

Examples:

Basic usage

client = Jikanrb::Client.new
anime = client.anime(1)

With custom configuration

client = Jikanrb::Client.new do |config|
  config.read_timeout = 15
  config.max_retries = 5
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize {|config| ... } ⇒ Client

Initializes the client with optional configuration

Examples:

With default configuration

client = Jikanrb::Client.new

With custom configuration

client = Jikanrb::Client.new do |config|
  config.base_url = "https://api.jikan.moe/v4"
  config.read_timeout = 15
end

Yields:

  • (config)

    Optional block to configure the client

Yield Parameters:



36
37
38
39
# File 'lib/jikanrb/client.rb', line 36

def initialize
  @config = Configuration.new
  yield(@config) if block_given?
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



21
22
23
# File 'lib/jikanrb/client.rb', line 21

def config
  @config
end

Instance Method Details

#anime(id, full: false) ⇒ Hash

Anime information by ID

Parameters:

  • id (Integer)

    Anime ID on MyAnimeList

  • full (Boolean) (defaults to: false)

    If true, returns extended information

Returns:

  • (Hash)

    Anime data



55
56
57
58
# File 'lib/jikanrb/client.rb', line 55

def anime(id, full: false)
  path = full ? "/anime/#{id}/full" : "/anime/#{id}"
  get(path)
end

#character(id, full: false) ⇒ Hash

Character information by ID

Parameters:

  • id (Integer)

    Character ID on MyAnimeList

  • full (Boolean) (defaults to: false)

    If true, returns extended information

Returns:

  • (Hash)

    Character data



75
76
77
78
# File 'lib/jikanrb/client.rb', line 75

def character(id, full: false)
  path = full ? "/characters/#{id}/full" : "/characters/#{id}"
  get(path)
end

#get(path, params = {}) ⇒ Hash

Performs a GET request

Parameters:

  • path (String)

    Endpoint path (e.g., “/anime/1”)

  • params (Hash) (defaults to: {})

    Query string parameters

Returns:

  • (Hash)

    Response parsed as Hash



46
47
48
# File 'lib/jikanrb/client.rb', line 46

def get(path, params = {})
  request(:get, path, params)
end

#manga(id, full: false) ⇒ Hash

Manga information by ID

Parameters:

  • id (Integer)

    Manga ID on MyAnimeList

  • full (Boolean) (defaults to: false)

    If true, returns extended information

Returns:

  • (Hash)

    Manga data



65
66
67
68
# File 'lib/jikanrb/client.rb', line 65

def manga(id, full: false)
  path = full ? "/manga/#{id}/full" : "/manga/#{id}"
  get(path)
end

#paginate(method, **params) ⇒ Pagination::Paginator

Create a paginator for iterating through all pages of a paginated endpoint

Examples:

Iterate through all top anime

client.paginate(:top_anime, type: 'tv').each do |anime|
  puts anime['title']
end

Get all items as array

all_anime = client.paginate(:search_anime, 'Naruto').all

Get first 3 pages only

anime = client.paginate(:top_anime).take_pages(3)

Parameters:

  • method (Symbol)

    Method name to paginate (e.g., :top_anime, :search_anime)

  • params (Hash)

    Parameters to pass to the method

Returns:



177
178
179
# File 'lib/jikanrb/client.rb', line 177

def paginate(method, **params)
  Pagination::Paginator.new(self, method, **params)
end

#pagination_info(response) ⇒ Pagination::PaginationInfo

Extract pagination information from a response

Examples:

result = client.top_anime(page: 1)
pagination = client.pagination_info(result)
puts "Page #{pagination.current_page} of #{pagination.total_pages}"

Parameters:

  • response (Hash)

    API response with pagination data

Returns:



190
191
192
# File 'lib/jikanrb/client.rb', line 190

def pagination_info(response)
  Pagination::PaginationInfo.new(response)
end

#person(id, full: false) ⇒ Hash

Person information by ID

Parameters:

  • id (Integer)

    Person ID on MyAnimeList

  • full (Boolean) (defaults to: false)

    If true, returns extended information

Returns:

  • (Hash)

    Person data



85
86
87
88
# File 'lib/jikanrb/client.rb', line 85

def person(id, full: false)
  path = full ? "/people/#{id}/full" : "/people/#{id}"
  get(path)
end

#schedules(day: nil) ⇒ Hash

Weekly schedule

Parameters:

  • day (String, nil) (defaults to: nil)

    Day: “monday”, “tuesday”, etc.

Returns:

  • (Hash)

    Anime schedule



156
157
158
159
# File 'lib/jikanrb/client.rb', line 156

def schedules(day: nil)
  path = day ? "/schedules/#{day}" : '/schedules'
  get(path)
end

#search_anime(query, **params) ⇒ Hash

Search anime

Parameters:

  • query (String)

    Search term

  • params (Hash)

    Additional filters (type, score, status, etc.)

Returns:

  • (Hash)

    Search results



95
96
97
# File 'lib/jikanrb/client.rb', line 95

def search_anime(query, **params)
  get('/anime', params.merge(q: query))
end

#search_manga(query, **params) ⇒ Hash

Search manga

Parameters:

  • query (String)

    Search term

  • params (Hash)

    Additional filters

Returns:

  • (Hash)

    Search results



104
105
106
# File 'lib/jikanrb/client.rb', line 104

def search_manga(query, **params)
  get('/manga', params.merge(q: query))
end

#season(year, season, page: 1) ⇒ Hash

Seasonal anime

Parameters:

  • year (Integer)

    Year

  • season (String)

    Season: “winter”, “spring”, “summer”, “fall”

  • page (Integer) (defaults to: 1)

    Page number

Returns:

  • (Hash)

    Seasonal anime



140
141
142
# File 'lib/jikanrb/client.rb', line 140

def season(year, season, page: 1)
  get("/seasons/#{year}/#{season}", page: page)
end

#season_now(page: 1) ⇒ Hash

Current season

Parameters:

  • page (Integer) (defaults to: 1)

    Page number

Returns:

  • (Hash)

    Current season anime



148
149
150
# File 'lib/jikanrb/client.rb', line 148

def season_now(page: 1)
  get('/seasons/now', page: page)
end

#top_anime(type: nil, filter: nil, page: 1) ⇒ Hash

Top anime

Parameters:

  • type (String, nil) (defaults to: nil)

    Filter: “tv”, “movie”, “ova”, etc.

  • filter (String, nil) (defaults to: nil)

    Filter: “airing”, “upcoming”, “bypopularity”, etc.

  • page (Integer) (defaults to: 1)

    Page number

Returns:

  • (Hash)

    List of top anime



114
115
116
117
118
119
# File 'lib/jikanrb/client.rb', line 114

def top_anime(type: nil, filter: nil, page: 1)
  params = { page: page }
  params[:type] = type if type
  params[:filter] = filter if filter
  get('/top/anime', params)
end

#top_manga(type: nil, filter: nil, page: 1) ⇒ Hash

Top manga

Parameters:

  • type (String, nil) (defaults to: nil)

    Filter: “manga”, “novel”, “lightnovel”, etc.

  • filter (String, nil) (defaults to: nil)

    Filter: “publishing”, “upcoming”, “bypopularity”, etc.

  • page (Integer) (defaults to: 1)

    Page number

Returns:

  • (Hash)

    List of top manga



127
128
129
130
131
132
# File 'lib/jikanrb/client.rb', line 127

def top_manga(type: nil, filter: nil, page: 1)
  params = { page: page }
  params[:type] = type if type
  params[:filter] = filter if filter
  get('/top/manga', params)
end