Class: AniApi

Inherits:
Object
  • Object
show all
Defined in:
lib/ani-api.rb

Constant Summary collapse

API_URI =
URI("https://graphql.anilist.co/").freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#rate_limitObject (readonly)

Returns the value of attribute rate_limit.



8
9
10
# File 'lib/ani-api.rb', line 8

def rate_limit
  @rate_limit
end

Class Method Details

.make_request(query, vars) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/ani-api.rb', line 69

def self.make_request(query, vars)
  payload = {
    query: query,
    variables: vars
  }

  http = Net::HTTP.new(API_URI.host, API_URI.port)
  http.use_ssl = true

  request = Net::HTTP::Post.new(API_URI.path, {'Content-Type' => 'application/json'})
  request.body = payload.to_json

  http.request(request)
end

.media(id) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/ani-api.rb', line 9

def self.media(id)
  query = <<~GRAPHQL
    query ($id: Int) {
      Media (id: $id) {
        id
        type
        format
        episodes
        duration
        chapters
        volumes
        genres
        averageScore
        title {
          romaji
          english
          native
        }
        description
        seasonYear
        startDate {
          day, month, year
        }
        endDate {
          day, month, year
        }
        coverImage {
          large, medium          
        }         
      }
    }
  GRAPHQL

  response = make_request(query, { id: id })
  @rate_limit = response['X-RateLimit-Remaining'].to_i

  JSON.parse(response.body)["data"]["Media"].compact
end

.rate_limit_remainingObject



84
85
86
# File 'lib/ani-api.rb', line 84

def self.rate_limit_remaining
  @rate_limit
end

.search(title) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/ani-api.rb', line 48

def self.search(title)
  query = <<~GRAPHQL
    query ($search: String) {
      Page (perPage: 5) {
        media (search: $search) {
          id
          type
        }
      }
    }
  GRAPHQL

  response = make_request(query, { search: title })
  data = JSON.parse(response.body)["data"]["Page"]["media"]
  @rate_limit = response['X-RateLimit-Remaining'].to_i

  data.map { |entry| entry }
end