lib/papercall/rest_fetcher.rb in papercall-0.15.2 vs lib/papercall/rest_fetcher.rb in papercall-1.0.0
- old
+ new
@@ -1,16 +1,23 @@
require 'json'
require 'rest-client'
require 'parallel'
+require 'active_support/core_ext/hash/indifferent_access'
+require 'papercall/models/submission'
+require 'papercall/models/rating'
+require 'papercall/models/feedback'
module Papercall
# Fetches submissions from Papercall REST API
# Params:
class RestFetcher < Fetcher
SUBMISSIONS_URL = 'https://www.papercall.io/api/v1/submissions'.freeze
- def initialize(api_key)
+ def initialize()
+ @output = Papercall.configuration.output
+ api_key = Papercall.configuration.API_KEY
+ raise ArgumentError, 'Missing API_KEY for access to Papercall. Please refer to documentation for instructions on setting this value.' unless api_key
@auth_hash = { Authorization: api_key }
@submitted = []
@accepted = []
@rejected = []
@waitlist = []
@@ -25,52 +32,55 @@
raw_results =
RestClient::Request.execute(method: :get,
url: papercall_url,
headers: @auth_hash) # :timeout => 120
if raw_results
- JSON.parse raw_results
+ JSON.parse(raw_results).map {|r| r.with_indifferent_access}
else
[]
end
end
def fetch(*states)
states = [%i[submitted accepted rejected waitlist declined]] if states == [[:all]]
states.flatten.each do |state|
next unless state
start_time = Time.now
- print "Fetching #{state} submissions from PaperCall API..."
- instance_variable_set("@#{state}", papercall(submission_url(state.to_s)))
- puts "finished in #{Time.now - start_time} seconds."
+ print "Fetching #{state} submissions from PaperCall API..." if @output
+ submissions = papercall(submission_url(state.to_s))
+ instance_variable_set("@#{state}_raw", submissions)
+ instance_variable_set("@#{state}", submissions.map {|s| Submission.new(s)})
+ puts "finished in #{Time.now - start_time} seconds." if @output
end
fetch_ratings
fetch_feedback
end
def fetch_ratings
start_time = Time.now
- print 'Fetching ratings for all submissions from Papercall API...'
+ print 'Fetching ratings for all submissions from Papercall API...' if @output
- Parallel.each(analysis, in_threads: 32) do |submission|
- unless submission['ratings']
- ratings_url = "#{SUBMISSIONS_URL}/#{submission['id']}/ratings"
- submission['ratings'] = papercall(ratings_url)
+ Parallel.each(analysis, in_threads: 128) do |submission|
+ if submission.ratings.empty?
+ ratings_url = "#{SUBMISSIONS_URL}/#{submission.id}/ratings"
+ ratings = papercall(ratings_url).map {|r| Rating.new(r)}
+ submission.ratings = ratings
end
- submission['ratings'] = [] unless submission['ratings']
+ #submission.ratings = [] unless submission.ratings
end
- puts "finished in #{Time.now - start_time} seconds."
+ puts "finished in #{Time.now - start_time} seconds." if @output
end
def fetch_feedback
start_time = Time.now
- print 'Fetching feedback for all submissions from Papercall API...'
- Parallel.each(analysis, in_threads: 32) do |submission|
- unless submission['feedback']
- feedback_url = "#{SUBMISSIONS_URL}/#{submission['id']}/feedback"
- submission['feedback'] = papercall(feedback_url)
+ print 'Fetching feedback for all submissions from Papercall API...' if @output
+ Parallel.each(analysis, in_threads: 128) do |submission|
+ if submission.feedback.empty?
+ feedback_url = "#{SUBMISSIONS_URL}/#{submission.id}/feedback"
+ submission.feedback = papercall(feedback_url).map {|f| Feedback.new(f)}
end
- submission['feedback'] = [] unless submission['feedback']
+ #submission.feedback = [] unless submission.feedback
end
- puts "finished in #{Time.now - start_time} seconds."
+ puts "finished in #{Time.now - start_time} seconds." if @output
end
end
end