Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

tests pass

  • Loading branch information...
commit 30ac02010b45aff07d1e1a7f9ed3e44892ce7528 1 parent 9e87112
@sivers sivers authored
View
1  .gitignore
@@ -1 +1,2 @@
*.swp
+lib/d50b/woodegg/config.yml
View
130 lib/d50b/woodegg.rb
@@ -3,106 +3,52 @@
class WoodEgg
@load_dir = File.expand_path('woodegg', File.dirname(__FILE__))
extend DatabaseInfo
-end
-class Person
- one_to_one :writer
- one_to_one :researcher
- one_to_one :customer
- one_to_many :test_essays, :order => :id
+ def self.config
+ if @config.nil?
+ @config = YAML.load_file(@load_dir + '/config.yml')
+ @config['formletter_thanks_buying'] = 2
+ @config['formletter_created_acct'] = 3
+ end
+ @config
+ end
end
-class Countries
+class Person
class << self
- def hsh
- {'KH' => 'Cambodia',
- 'CN' => 'China',
- 'HK' => 'Hong Kong',
- 'IN' => 'India',
- 'ID' => 'Indonesia',
- 'JP' => 'Japan',
- 'KR' => 'Korea',
- 'MY' => 'Malaysia',
- 'MN' => 'Mongolia',
- 'MM' => 'Myanmar',
- 'PH' => 'Philippines',
- 'SG' => 'Singapore',
- 'LK' => 'Sri Lanka',
- 'TW' => 'Taiwan',
- 'TH' => 'Thailand',
- 'VN' => 'Vietnam'}
- end
-
- def codes
- hsh.keys
- end
-
- # helper for routes like /cn or /jp
- def routemap
- '/(' + codes.map(&:downcase).join('|') + ')$'
- end
-
- # helper for routes like /cn/123 or /tw/321
- def routemap2
- '/(' + codes.map(&:downcase).join('|') + ')/([0-9]+)$'
- end
-
- # all WoodEgg userstats, per country
- def userstats
- statkeys = "'woodegg','" + Countries.hsh.keys.map {|x| "woodegg-#{x.downcase}"}.join("','") + "'"
- query = "SELECT statkey, statvalue, COUNT(*) FROM peeps.userstats" +
- " WHERE statkey IN (#{statkeys})" +
- " AND statvalue NOT IN ('clicked')" +
- " AND LENGTH(statvalue) > 2 AND LENGTH(statvalue) < 50" +
- " GROUP BY statkey, statvalue ORDER BY statkey, statvalue"
- Sequel.postgres('d50b', user: 'd50b').fetch(query).all
- end
-
- # make a grid out of an array of {:statkey=>"x", :statvalue=>"y", :count=>9}
- def userstats_grid
- usrsts = self.userstats
- # get all unique keys and values
- require 'set'
- k = Set.new
- v = Set.new
- usrsts.each {|u| k << u[:statkey] ; v << u[:statvalue]}
- # init a nil-filled grid
- row = {}
- v.each {|x| row[x] = nil}
- grid = {}
- k.each {|x| grid[x] = row.dup}
- # replace nil with usrsts count
- usrsts.each do |u|
- grid[u[:statkey]][u[:statvalue]] = u[:count]
+ # hash {countrycode => [array, of, people]}
+ def interviewees
+ statkeys = WoodEgg::Country.hsh.keys.map {|cc| "woodegg-#{cc.downcase}"}
+ stats = Userstat.select(:person_id, :statkey).filter(statkey: statkeys, statvalue: 'interview').map(&:values)
+ res = {}
+ # hash of bios, to add as attribute to each person, below
+ bios = {}
+ Userstat.select(:person_id, :statvalue).filter(statkey: 'woodegg-bio').each {|u| bios[u[:person_id]] = u[:statvalue]}
+ WoodEgg::Country.hsh.keys.each do |cc|
+ person_ids = stats.find_all {|x| x[:statkey] == "woodegg-#{cc.downcase}"}.map {|y| y[:person_id]}
+ pp = Person.filter(id: person_ids).all
+ # add attribute of [:interviewee_bio], if there is one (nil if not)
+ pp.each do |p|
+ p.values[:interviewee_bio] = bios[p.id]
+ end
+ res[cc] = pp
end
- grid
+ res
end
+ end
- # input 'woodegg' = output 'ANY'
- # input 'woodegg-lk' = output 'Sri Lanka'
- # input 'woodegg-qa' = output 'woodegg-qa'
- def from_userstat(statkey)
- return 'ANY' if statkey == 'woodegg'
- code = statkey.gsub('woodegg-', '').upcase
- return self.hsh[code] if self.hsh[code]
- statkey
- end
+ def interviewee_bio
+ u = userstats_dataset.filter(statkey: 'woodegg-bio').first
+ u.nil? ? nil : u.statvalue
+ end
- # maybe a weird place to put this. Format:
- # https://www.#{domain}/dp/#{asin}
- def amazon_domains
- {"amazon.com" => '$9.99',
- "amazon.ca" => '$9.99',
- "amazon.com.au" => '$9.99',
- "amazon.co.uk" => '₤5.99 +VAT',
- "amazon.de" => '€6.99 +VAT',
- "amazon.fr" => '€6.99 +VAT',
- "amazon.es" => '€6.99 +VAT',
- "amazon.it" => '€6.99 +VAT',
- "amazon.co.jp" => '¥1000',
- "amazon.in" => 'Rs 599',
- "amazon.com.br" => 'R$24.99',
- "amazon.com.mx" => '$99 MX'}
+ def set_interviewee_bio(content)
+ u = userstats_dataset.filter(statkey: 'woodegg-bio').first
+ if u
+ u.update(statvalue: content)
+ else
+ Userstat.create(person_id: id, statkey: 'woodegg-bio', statvalue: content)
end
end
+
end
View
8 lib/d50b/woodegg/book.rb
@@ -8,15 +8,15 @@ class Book < Sequel::Model(:woodegg__books)
class << self
def available
- where(id: 17..33).order(:id).all
+ where(available: true).order(:id).all
end
def done
- order(:id).all # .select {|b| b.done?}
+ exclude(asin: nil).order(:id).all
end
def not_done
- [] # Book.order(:id).all.reject {|b| b.done?}
+ where(asin: nil).order(:id).all
end
# add researchers answer count properties to book properties
@@ -90,7 +90,7 @@ def essays_finished_count
end
def essays_finished
- essays_dataset.exclude(finished_at: nil).order(:finished_at.desc).all
+ essays_dataset.exclude(finished_at: nil).order(Sequel.desc(:finished_at)).all
end
def essays_unfinished_count
View
8 lib/d50b/woodegg/config.yml.sample
@@ -0,0 +1,8 @@
+person_url:
+ http://someurl.io/person/%d
+woodegg_person_url:
+ http://anotherurl.io/person/%d
+aws_key:
+ ABCDEFG
+aws_secret:
+ abcdefg1234567
View
2  lib/d50b/woodegg/essay.rb
@@ -45,7 +45,7 @@ def for_country(cc)
# hash of country_code => howmany_essays
def country_howmany
h = {}
- join(:questions, id: :question_id).group_and_count(:country).order(:count.desc).all.each {|e| h[e.values[:country]] = e.values[:count]}
+ join(:questions, id: :question_id).group_and_count(:country).order(Sequel.desc(:count)).all.each {|e| h[e.values[:country]] = e.values[:count]}
return h
end
View
2  lib/d50b/woodegg/fixtures.yml
@@ -182,6 +182,7 @@ WoodEgg::Book:
asin: B00D1HOJII
leanpub: ChinaStartupGuide2013
salescopy: THIS IS FOR SALE NOW
+ available: true
id2:
country: JP
code: we13jp
@@ -189,6 +190,7 @@ WoodEgg::Book:
isbn: 9789810766368
leanpub: JapanStartupGuide2013
salescopy: COMING SOON
+ available: false
id3:
country: CN
code: we14cn
View
4 lib/d50b/woodegg/researcher.rb
@@ -12,7 +12,7 @@ class << self
# returns same as Researcher.all but with name & email pre-loaded
def all_people(opts = {})
ol = self.filter(opts).order(:id).all
- pp = WoodEgg::Person.select(:id, :email, :name).filter(id: ol.map(&:person_id)).all
+ pp = Person.select(:id, :email, :name).filter(id: ol.map(&:person_id)).all
ol.each do |o|
p = pp.find {|x| x[:id] == o.person_id}
o.define_singleton_method(:name) { p.name }
@@ -118,7 +118,7 @@ def answers_finished_count
end
def answers_finished
- answers_dataset.exclude(finished_at: nil).order(:finished_at.desc).all
+ answers_dataset.exclude(finished_at: nil).order(Sequel.desc(:finished_at)).all
end
def answers_unfinished_count
View
7 lib/d50b/woodegg/schema.sql
@@ -75,11 +75,14 @@ CREATE TABLE books (
country char(2) not null,
code char(6) not null UNIQUE,
title text,
- isbn text,
+ pages integer,
+ isbn char(13),
asin char(10),
leanpub varchar(30),
+ apple integer,
salescopy text,
- credits text
+ credits text,
+ available boolean
);
CREATE TABLE books_writers (
View
2  lib/d50b/woodegg/writer.rb
@@ -24,7 +24,7 @@ def essays_finished_count
end
def essays_finished
- essays_dataset.exclude(finished_at: nil).order(:finished_at.desc).all
+ essays_dataset.exclude(finished_at: nil).order(Sequel.desc(:finished_at)).all
end
def essays_unfinished_count
View
8 test/woodegg.rb
@@ -230,7 +230,7 @@ def test_question_class
assert_equal [WoodEgg::Question[3],WoodEgg::Question[4]], WoodEgg::Question.for_subtopic_and_country(3, 'CN')
assert_equal({6=>{topic: 'Country', subtopic: 'how big'}, 7=>{topic: 'Country', subtopic: 'how old'}, 8=>{topic: 'Culture', subtopic: 'is it fun?'}, 9=>{topic: 'Culture', subtopic: 'is it fun?'}, 10=>{topic: 'Culture', subtopic: 'what language?'}}, WoodEgg::Question.topichash('JP'))
th = WoodEgg::Question.topicnest(WoodEgg::Book[2].questions, WoodEgg::Question.topichash('JP'))
- assert_equal 2, th.size
+ assert_equal 3, th.size
assert_equal({'how big' => [WoodEgg::Question[6]], 'how old' => [WoodEgg::Question[7]]}, th['Country'])
assert_equal({1 => WoodEgg::Question[1], 3=> WoodEgg::Question[3]}, WoodEgg::Question.for_these(WoodEgg::Answer.where(id: [1,3]).all))
assert_equal({2 => WoodEgg::Question[2], 4=> WoodEgg::Question[4]}, WoodEgg::Question.for_these(WoodEgg::Essay.where(id: [2,4]).all))
@@ -423,9 +423,9 @@ def test_finished_person_ids
end
def test_association
- assert_equal [WoodEgg::TestEssay[1], WoodEgg::TestEssay[2]], Person[8].test_essays
- assert_equal [WoodEgg::TestEssay[3], WoodEgg::TestEssay[4]], Person[6].test_essays
- assert_equal [], Person[1].test_essays
+ assert_equal [WoodEgg::TestEssay[1], WoodEgg::TestEssay[2]], Person[8].woodegg_test_essays
+ assert_equal [WoodEgg::TestEssay[3], WoodEgg::TestEssay[4]], Person[6].woodegg_test_essays
+ assert_equal [], Person[1].woodegg_test_essays
assert_equal WoodEgg::Question[1], WoodEgg::TestEssay[3].question
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.