Permalink
Browse files

Refactor the AMA Fetching rake task

  • Loading branch information...
1 parent 8f91daa commit 69ba5e1fb147bb2c52fb99677b150627169c7c9e @andey committed Feb 27, 2014
Showing with 31 additions and 63 deletions.
  1. +5 −3 app/models/ama_processing.rb
  2. +26 −60 lib/tasks/find.rake
@@ -88,11 +88,13 @@ def comment_has_children?(data)
end
end
+ # Check if AMA data meets the requirements
+ def meets_requirements(json)
+ return json["data"]["score"].to_i > 100 && !json["data"]["title"].to_s.match(/ama request/i) && !Trash.find_by_key(json["data"]["id"])
+ end
+
# Process JSON response
def process_it(response)
- puts '===================='
- puts 'PROCESS IT'
- puts response
self.archive_it(response)
if self.date
self.find_responses(response[1]["data"]["children"])
View
@@ -2,57 +2,40 @@ require 'api/reddit.com'
namespace :find do
- # Find NEW AMAs
- task :new => :environment do
- reddit = Reddit.new
- result = reddit.getIAMAs()
- if result
- puts "YES THERE IS A RESULT"
- result["data"]["children"].each do |a|
- puts "LOOP"
+ # Create AMA if :
+ # * Karma greater than 100
+ # * Isn't an AMA Request
+ # * Isn't in the Trash
+ def create(ama)
+ @ama = Ama.new
+ @ama.create_by_json(ama["data"]) unless Ama.find_by_key(ama["data"]["id"])
+ end
- # Create AMA if :
- # * Karma greater than 100
- # * Isn't an AMA Request
- # * Isn't in the Trash
- if a["data"]["score"].to_i > 100 && !a["data"]["title"].to_s.match(/ama request/i) && !Trash.find_by_key(a["data"]["id"])
- @ama = Ama.new
- @ama.create_by_json(a["data"]) unless Ama.find_by_key(a["data"]["id"])
- puts "SCORE OVER 100"
- #@ama.fetch() unless !@ama
- end
- end
+ def process(amas)
+ amas["data"]["children"].each do |ama|
+ create(ama) if good?(ama)
end
+ end
+
+ def good?(ama)
+ return ama["data"]["score"].to_i > 100 && !ama["data"]["title"].to_s.match(/ama request/i) && !Trash.find_by(key: ama["data"]["id"])
+ end
+ # Find NEW AMAs
+ task :new => :environment do
+ reddit = Reddit.new
+ amas = reddit.getIAMAs()
+ process(amas) if amas
end
# Find AMAs FROM SPECIFIED URL
task :in_url, [:count, :after] => :environment do |t, args|
reddit = Reddit.new
- result = reddit.get("/r/IAmA.json?count=#{args[:count]}&after=#{args[:after]}")
- if result
- puts "YES THERE IS A RESULT (2)"
- result["data"]["children"].each do |a|
- puts "LOOP"
-
- # Create AMA if :
- # * Karma greater than 100
- # * Isn't an AMA Request
- # * Isn't in the Trash
- if a["data"]["score"].to_i > 100 && !a["data"]["title"].to_s.match(/ama request/i) && !Trash.find_by(key: a["data"]["id"])
- @ama = Ama.new
- @ama.create_by_json(a["data"]) unless Ama.find_by_key(a["data"]["id"])
- puts "SCORE OVER 100"
- #@ama.fetch() unless !@ama
- end
- end
- end
-
+ amas = reddit.get("/r/IAmA.json?count=#{args[:count]}&after=#{args[:after]}")
+ process(amas) if amas
end
-
# Find OLD AMAs
- # NOTE: This function does not work
task :old => :environment do
#get query variables to build reddit query
@@ -61,28 +44,11 @@ namespace :find do
time = Metum.find_by_name("find_old_ama-time")
category = Metum.find_by_name("find_old_ama-category")
- puts "========================"
- puts "find_old_amas"
- puts "category: " + category.value
- puts "count: " + count.value
- puts "after: " + after.value
-
reddit = Reddit.new
- result = reddit.get("/r/IAmA/#{category.value}/.json?sort=top&count=" + count.value + "&after=" + after.value + "&t=" + time.value)
-
- if result
- result["data"]["children"].each do |a|
- puts "LOOP"
+ amas = reddit.get("/r/IAmA/#{category.value}/.json?sort=top&count=" + count.value + "&after=" + after.value + "&t=" + time.value)
- # Create AMA if :
- # * Karma greater than 100
- # * Isn't an AMA Request
- # * Isn't in the Trash
- if a["data"]["score"].to_i > 100 && !a["data"]["title"].to_s.match(/ama request/i) && !Trash.find_by_key(a["data"]["id"])
- @ama = Ama.new
- @ama.create_by_json(a["data"]) unless Ama.find_by_key(a["data"]["id"])
- end
- end
+ if amas
+ process(amas)
# Update meta the query variables for the next run
count.update_attribute(:value, count.value.to_i + 25)

0 comments on commit 69ba5e1

Please sign in to comment.