This repository has been archived by the owner on Jul 30, 2019. It is now read-only.
/
auction_parser.rb
91 lines (80 loc) · 2 KB
/
auction_parser.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
88
89
90
91
class AuctionParser < Struct.new(:params)
def attributes
general_attributes.merge(
start_datetime: start_datetime,
end_datetime: end_datetime,
start_price: start_price
)
end
def general_attributes
{
type: type,
title: title,
description: description,
summary: summary,
github_repo: github_repo,
issue_url: issue_url,
delivery_deadline: delivery_deadline,
delivery_url: delivery_url,
cap_proposal_url: cap_proposal_url,
awardee_paid_status: awardee_paid_status,
published: published,
notes: notes,
billable_to: billable_to,
result: result
}
end
[
:type,
:title,
:description,
:summary,
:github_repo,
:issue_url,
:awardee_paid_status,
:published,
:awardee_paid_at,
:delivery_url,
:cap_proposal_url,
:notes,
:billable_to,
:result
].each do |key|
define_method key do
params[:auction][key]
end
end
def delivery_deadline
if params.key?(:due_in_days)
real_days = params[:due_in_days].to_i.business_days
end_of_workday(real_days.after(end_datetime.to_date))
else
parse_time(params[:auction][:delivery_deadline])
end
end
def start_datetime
parse_time(params[:auction][:start_datetime])
end
def end_datetime
parse_time(params[:auction][:end_datetime])
end
def parse_time(time)
return nil if time.nil?
parsed_time = Chronic.parse(time)
fail ArgumentError, "Missing or poorly formatted time: '#{time}'" unless parsed_time
unless time =~ /\d{1,2}:\d{2}/
parsed_time = parsed_time.beginning_of_day
end
parsed_time.utc
end
def start_price
price = params[:auction][:start_price].to_f
price = PlaceBid::BID_LIMIT if price > PlaceBid::BID_LIMIT || price <= 0
price
end
private
def end_of_workday(date)
cob = Time.parse(BusinessTime::Config.end_of_workday)
Time.mktime(date.year, date.month, date.day, cob.hour, cob.min, cob.sec)
end
end