Class: Foxit::API

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

Instance Method Summary collapse

Constructor Details

#initializeAPI

Returns a new instance of API.



13
14
15
# File 'lib/foxit/api.rb', line 13

def initialize
  @root = "https://kitsu.io/api/edge/"
end

Instance Method Details

#batch_get_anime(anime_ids, max_threads = 200) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/foxit/api.rb', line 159

def batch_get_anime anime_ids, max_threads=200
  results = self.batch_get_results(anime_ids, :get_anime_by_id, max_threads)
  
  anime_items = []
  results.each do |id, result|
    unless result.key?('errors')
      anime_items << Anime.new(result['data'])
    end
  end
  
  anime_items
end

#batch_get_libraries(user_ids, max_threads = 200) ⇒ Object

TODO: should probably assign to a @variable



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/foxit/api.rb', line 112

def batch_get_libraries user_ids, max_threads=200
  
  all_library_entries = []
  user_libraries = self.batch_get_results(user_ids, :get_library_by_id, max_threads)

  user_libraries.each do |user_id, library|

    media_ids = []
    library.map { |entry| media_ids << entry['id'] }
    media_results = self.batch_get_results(media_ids, :get_media_relationship_by_id, max_threads)
    
    library.each do |entry|
      all_library_entries << LibraryItem.new(user_id, entry, media_results[entry['id']])
    end

  end

  all_library_entries
end

#batch_get_libraries_docs(user_ids, max_threads = 200) ⇒ Object



138
139
140
141
142
143
144
145
# File 'lib/foxit/api.rb', line 138

def batch_get_libraries_docs user_ids, max_threads=200
  all_library_entries = self.batch_get_libraries(user_ids, max_threads)
  
  docs = []
  all_library_entries.map { |entry| docs << entry.to_hash }
  
  docs
end

#batch_get_results(ids, fn, max_threads) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/foxit/api.rb', line 75

def batch_get_results ids, fn, max_threads
  """
  ids: ids of results returned: user_id | library_entry_id in this case.
  fn: function name to use to return results (need to use symbol method name)
      e.g. :get_result
  max_threads: maximum number of active threads.
  """
  
  results = {}
  threads = []
  
  ids.each do |id|
  
    if Thread.list.count % max_threads != 0
      thread = Thread.new do
        # adding lock slows down considerably shouldn't matter as results are written to hash?
        results[id] = send(fn, id)
      end
      threads << thread
    else
      # wait for open threads to finish before starting new one
      threads.each(&:join)
  
      thread = Thread.new do
        results[id] = send(fn, id)
      end
      threads << thread
    end
  
  end
  
  threads.each(&:join)
  
  results
end

#build_anime_url(id) ⇒ Object



148
149
150
# File 'lib/foxit/api.rb', line 148

def build_anime_url id
  "#{@root}/anime/#{id}"
end

#build_library_url(id, type = 'Anime', status = 'completed', limit = 500) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/foxit/api.rb', line 18

def build_library_url id, type='Anime', status='completed', limit=500
  uri = Addressable::URI.parse("#{@root}library-entries")
  
  uri_query = {
    "filter[user_id]" => id,
    "filter[media_type]" => type,
    "filter[status]" => status,
    "page[limit]" => limit
  }
  uri.query_values = uri_query
  
  uri.to_s
end

#build_media_url(entry_id) ⇒ Object



33
34
35
# File 'lib/foxit/api.rb', line 33

def build_media_url entry_id
  "#{@root}/library-entries/#{entry_id}/relationships/media"
end

#get_anime_by_id(id) ⇒ Object



153
154
155
156
# File 'lib/foxit/api.rb', line 153

def get_anime_by_id id
  url = build_anime_url(id)
  self.get_result(url)
end

#get_anime_documents(anime_ids, max_threads = 200) ⇒ Object



172
173
174
175
# File 'lib/foxit/api.rb', line 172

def get_anime_documents anime_ids, max_threads=200
  anime_items = self.batch_get_anime(anime_ids, max_threads)
  self.objects_to_hash(anime_items)
end

#get_library_by_id(user_id) ⇒ Object



58
59
60
61
# File 'lib/foxit/api.rb', line 58

def get_library_by_id user_id
  url = self.build_library_url(user_id)
  self.get_library_by_url(url)
end

#get_library_by_url(url, entries = []) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/foxit/api.rb', line 44

def get_library_by_url url, entries=[]
  
  result = self.get_result(url)
  entries += result['data']

  if result['links'].key?('next')
    # recursion to retrieve additional results that have ben paginated
    result = self.get_library_by_url(result['links']['next'], entries)
  else
    return entries
  end
end

#get_media_relationship_by_id(entry_id) ⇒ Object



64
65
66
67
68
69
70
71
72
# File 'lib/foxit/api.rb', line 64

def get_media_relationship_by_id entry_id
  # adding memoisation to stop requesting same media docs
  # TODO: extend to items already existing in db?
  @get_media_relationship_by_id ||= {}
  return @get_media_relationship_by_id[entry_id] if @get_media_relationship_by_id.key?(entry_id)
  
  url = self.build_media_url(entry_id)
  self.get_result(url)
end

#get_result(url) ⇒ Object



38
39
40
41
# File 'lib/foxit/api.rb', line 38

def get_result url
  response = Net::HTTP.get(URI.parse(url))
  JSON.parse(response)
end

#get_user_libraryObject



133
134
135
# File 'lib/foxit/api.rb', line 133

def get_user_library
  # TODO: return user library using single id call to batch_get_library
end