-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathse.rb
87 lines (69 loc) · 1.65 KB
/
se.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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
require 'serel'
require './bounty'
class SiteDoesNotExistError < StandardError
end
class Site
def initialize(domain, se)
@domain = domain
@se = se
end
# http://kylecronin.me/blog/2012/4/22/a-clever-ruby-equality-trick.html
def ==(other)
other == @domain
end
def hash()
@domain.hash
end
def to_s()
@domain
end
def bounties()
Serel::Base.config(@domain, @se.apikey)
bounties = Serel::Question.featured.pagesize(99).request
number = bounties.length
rep = bounties.map { |bounty| bounty.bounty_amount }.reduce(:+)
rep = 0 if number == 0
[@domain, number, rep]
end
end
class SE
attr_accessor :apikey
def initialize(apikey)
@apikey = apikey
@se_sites = []
update_sites()
end
def site(url)
@se_sites[validate(url)]
end
def validate(url)
domain = extract_domain(url)
if @se_sites.include?(domain)
domain
elsif @se_sites.include?(domain[5..-1])
domain[5..-1]
elsif @se_sites.include?(domain[6..-1])
domain[6..-1]
else
raise SiteDoesNotExistError
end
end
def extract_domain(url)
m = /^(?:https?:\/\/)?([\w\.]*)/.match(url)
raise SiteDoesNotExistError if not m
m[1]
end
def update_sites()
Serel::Base.config('', @apikey)
@se_sites = Hash[Serel::Site.all.
select { |site| site.site_type == 'main_site' }.
map { |site| site.site_url }.
map { |url| extract_domain(url) }.
map { |domain| [domain, Site.new(domain, self)] }
]
end
def info()
Serel::Base.config(:apple, @apikey)
Serel::User.get.quota_remaining
end
end