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) ⇒ Resources::AnimeResource

Returns an AnimeResource for fluent API access to anime sub-resources

Examples:

Get anime info

client.anime(1).info
# => { data: { mal_id: 1, title: "Cowboy Bebop", ... } }

Get full anime info

client.anime(1).info(full: true)

Get anime characters

client.anime(1).characters

Get anime episodes

client.anime(1).episodes


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

def anime(id)
  Resources::AnimeResource.new(self, id)
end

#character(id) ⇒ Resources::CharacterResource

Returns a CharacterResource for fluent API access to character sub-resources

Examples:

Get character info

client.character(1).info
# => { data: { mal_id: 1, name: "Spike Spiegel", ... } }

Get full character info

client.character(1).info(full: true)

Get character’s anime appearances

client.character(1).animes

Get character’s voice actors

client.character(1).voices


109
110
111
# File 'lib/jikanrb/client.rb', line 109

def character(id)
  Resources::CharacterResource.new(self, id)
end

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

Performs a GET request



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

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

#manga(id) ⇒ Resources::MangaResource

Returns a MangaResource for fluent API access to manga sub-resources

Examples:

Get manga info

client.manga(1).info
# => { data: { mal_id: 1, title: "Monster", ... } }

Get full manga info

client.manga(1).info(full: true)

Get manga characters

client.manga(1).characters

Get manga statistics

client.manga(1).statistics


88
89
90
# File 'lib/jikanrb/client.rb', line 88

def manga(id)
  Resources::MangaResource.new(self, id)
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)


221
222
223
# File 'lib/jikanrb/client.rb', line 221

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}"


234
235
236
# File 'lib/jikanrb/client.rb', line 234

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

#person(id) ⇒ Resources::PersonResource

Returns a PersonResource for fluent API access to person sub-resources

Examples:

Get person info

client.person(1).info
# => { data: { mal_id: 1, name: "Tomokazu Seki", ... } }

Get full person info

client.person(1).info(full: true)

Get person’s anime staff positions

client.person(1).animes

Get person’s voice acting roles

client.person(1).voices


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

def person(id)
  Resources::PersonResource.new(self, id)
end

#schedules(day: nil) ⇒ Hash

Weekly schedule



200
201
202
203
# File 'lib/jikanrb/client.rb', line 200

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

#search_anime(query, **params) ⇒ Hash

Search anime



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

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

#search_manga(query, **params) ⇒ Hash

Search manga



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

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

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

Seasonal anime



184
185
186
# File 'lib/jikanrb/client.rb', line 184

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

#season_now(page: 1) ⇒ Hash

Current season



192
193
194
# File 'lib/jikanrb/client.rb', line 192

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

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

Top anime



158
159
160
161
162
163
# File 'lib/jikanrb/client.rb', line 158

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



171
172
173
174
175
176
# File 'lib/jikanrb/client.rb', line 171

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