Class: Foxit::API

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

Overview

TODO: probably split out library and anime methods

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Helpers

#objects_to_hash, #to_hash

Constructor Details

#initialize(max_threads = 200) ⇒ API

Returns a new instance of API.



18
19
20
21
# File 'lib/foxit/api.rb', line 18

def initialize max_threads=200
  @max_threads = max_threads
  @urlbuilder = URLBuilder.new()
end

Instance Attribute Details

#max_threadsObject

Returns the value of attribute max_threads.



16
17
18
# File 'lib/foxit/api.rb', line 16

def max_threads
  @max_threads
end

#urlbuilderObject (readonly)

Returns the value of attribute urlbuilder.



15
16
17
# File 'lib/foxit/api.rb', line 15

def urlbuilder
  @urlbuilder
end

Instance Method Details

#_create_library_items(user_id, library) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/foxit/api.rb', line 110

def _create_library_items user_id, library

  # get media_ids from record_ids (not in same response, additional request required)
  record_ids = []
  library.map { |entry| record_ids << entry['id'] }
  media_results = self.batch_get_results(record_ids, :get_media_relationship_by_id)
  
  library_entries = []
  library.each do |entry|
    library_entries << LibraryItem.new(user_id, entry, media_results[entry['id']])
  end

  library_entries
end

#_get_anime_by_attr(filter_attr, filter_value, rtype = :object) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/foxit/api.rb', line 142

def _get_anime_by_attr filter_attr, filter_value, rtype=:object

  # TODO: this is a bit of a mess

  case filter_attr
  when :id
    url = @urlbuilder.anime_by_id(filter_value)
  when :slug
    url = @urlbuilder.anime_by_slug(filter_value)
  else
    raise ArgumentError.new("filter_attr argument (1st) not in {:id, :slug}")
  end

  result = self.get_result(url)

  case rtype
  when :json
    return result
  when :object
    case filter_attr
    when :id
      return Anime.new(result['data'])
    when :slug
      # filtering by slug results in the 'data' attribute to be an array
      return Anime.new(result['data'][0])
    end
  else
    raise ArgumentError.new("rtype not :object or :json")
  end
end

#_get_anime_by_id_json(id) ⇒ Object



184
185
186
# File 'lib/foxit/api.rb', line 184

def _get_anime_by_id_json id
  self._get_anime_by_attr(:id, id, :json)
end

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



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/foxit/api.rb', line 30

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

#batch_get_anime(anime_ids) ⇒ Object



189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/foxit/api.rb', line 189

def batch_get_anime anime_ids
  results = self.batch_get_results(anime_ids, :_get_anime_by_id_json)
  
  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) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
# File 'lib/foxit/api.rb', line 97

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

  user_libraries.each do |user_id, library|
    all_library_entries += self._create_library_items(user_id, library)
  end

  all_library_entries
end

#batch_get_libraries_docs(user_ids) ⇒ Object



132
133
134
135
136
137
138
139
# File 'lib/foxit/api.rb', line 132

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

#batch_get_results(ids, fn) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/foxit/api.rb', line 61

def batch_get_results ids, fn
  """
  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
  """
  
  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

#get_anime_by_id(id, rtype = :object) ⇒ Object



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

def get_anime_by_id id, rtype=:object
  self._get_anime_by_attr(:id, id, rtype)
end

#get_anime_by_slug(slug, rtype = :object) ⇒ Object



179
180
181
# File 'lib/foxit/api.rb', line 179

def get_anime_by_slug slug, rtype=:object
  self._get_anime_by_attr(:slug, slug, rtype)
end

#get_anime_documents(anime_ids) ⇒ Object



203
204
205
206
# File 'lib/foxit/api.rb', line 203

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

#get_library_by_id(user_id) ⇒ Object



44
45
46
47
# File 'lib/foxit/api.rb', line 44

def get_library_by_id user_id
  url = @urlbuilder.library(user_id)
  self._get_library_by_url(url)
end

#get_media_relationship_by_id(entry_id) ⇒ Object



50
51
52
53
54
55
56
57
58
# File 'lib/foxit/api.rb', line 50

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 = @urlbuilder.media(entry_id)
  self.get_result(url)
end

#get_result(url) ⇒ Object



24
25
26
27
# File 'lib/foxit/api.rb', line 24

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

#get_user_library_by_id(user_id) ⇒ Object



126
127
128
129
# File 'lib/foxit/api.rb', line 126

def get_user_library_by_id user_id
  library = self.get_library_by_id(user_id)
  self._create_library_items(user_id, library)
end