-
Notifications
You must be signed in to change notification settings - Fork 0
/
enriched-petition.ts
139 lines (130 loc) · 4.2 KB
/
enriched-petition.ts
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/// <reference path="../node.d.ts" />
import util = require('util');
import raw = require('./raw-petition');
/// <reference path="./raw-petition.ts" />
function stringToDate(dateString: string): Date {
if (dateString) {
var date:Date = new Date(dateString);
if (isNaN(date.getTime())) {
throw new Error('A date string of the petition could not be parsed');
}
return date;
}
else {
return null;
}
}
var keysToTransform = {
created_at : stringToDate,
government_response_at : stringToDate,
response_threshold_reached_at : stringToDate,
debate_threshold_reached_at : stringToDate,
scheduled_debate_date : stringToDate
};
export module UkPetitions {
/**
* A petition enriched with additional properties and methods.
*/
export class EnrichedPetition {
/**
* Unique ID of the petiton.
*/
id: number;
/**
* The call to action.
*/
action: string;
/**
* The background to the petition.
*/
background: string;
/**
* The additional details about petition.
*/
additional_details: string;
/**
* The number of signatures.
*/
signature_count: number;
/**
* The state of the petition.
*/
state: string;
/**
* URL for HTML version of the petition.
*/
html_url: string;
/**
* URL for JSON version of the petition.
*/
json_url: string;
/**
* URL for HTML version of the petition showing the additional_details.
*/
html_detail_url: string;
/**
* URL for HTML version of the petition showing the response.
*/
html_response_url: string;
/**
* URL for HTML version of the petition showing the debate.
*/
html_debate_url: string;
/**
* If the petition has detailed information.
*/
detailed: boolean;
/**
* Breakdown of signatures by country.
*/
signatures_by_country: raw.UkPetitions.SignaturesByCountry[];
/**
* Breakdown of signatures by constituency.
*/
signatures_by_constituency: raw.UkPetitions.SignaturesByConstituency[];
/**
* The timestamp petition was created.
*/
created_at: Date;
/**
* The timestamp the government responded.
*/
government_response_at: Date;
/**
* The timestamp 10000 signatures was reached.
*/
response_threshold_reached_at: Date;
/**
* The timestamp 100000 signatures was reached.
*/
debate_threshold_reached_at: Date;
/**
* The date the debate has been scheduled for.
*/
scheduled_debate_date: Date;
/**
* The constructor for enriched petitions.
* @param rawPetition The petiton from the data source.
*/
constructor(rawPetition: raw.UkPetitions.Petition) {
this.id = rawPetition.id;
var transform = null;
for (var key in rawPetition.attributes) {
transform = keysToTransform[key];
if (transform) {
this[key] = transform(rawPetition.attributes[key]);
transform = null;
}
else {
this[key] = rawPetition.attributes[key];
}
}
this.html_url = util.format('https://petition.parliament.uk/petitions/%d', this.id);
this.json_url = util.format('https://petition.parliament.uk/petitions/%d.json', this.id);
this.html_detail_url = util.format('https://petition.parliament.uk/petitions/%d#details-content-0', this.id);
this.html_response_url = util.format('https://petition.parliament.uk/petitions/%d?reveal_response=yes#response-threshold', this.id);
this.html_debate_url = util.format('https://petition.parliament.uk/petitions/%d?#debate-threshold', this.id);
this.detailed = this.signatures_by_country !== undefined;
}
}
}