Skip to content

Commit e5f26d9

Browse files
committed
Merge pull request #391 from alphagov/update-api
Some tweaks to the API
2 parents bf1ae40 + d33ccbc commit e5f26d9

8 files changed

Lines changed: 77 additions & 8 deletions

File tree

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,28 @@
11
class Archived::PetitionsController < ApplicationController
2+
respond_to :html, :json
3+
4+
before_action :fetch_petitions, only: [:index]
5+
before_action :fetch_petition, only: [:show]
6+
27
def index
8+
respond_with(@petitions)
9+
end
10+
11+
def show
12+
respond_with(@petition)
13+
end
14+
15+
private
16+
17+
def fetch_petitions
318
if params[:q].blank?
419
@petitions = ArchivedPetition.search(params.merge({state: 'by_most_signatures'}))
520
else
621
@petitions = ArchivedPetition.search(params)
722
end
823
end
924

10-
def show
25+
def fetch_petition
1126
@petition = ArchivedPetition.find(params[:id])
1227
end
1328
end

app/presenters/petition_csv_presenter.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ def self.attributes
3636
def self.timestamps
3737
[
3838
:created_at, :updated_at, :open_at, :closed_at, :government_response_at, :scheduled_debate_date,
39-
:debate_threshold_reached_at, :rejected_at, :debate_outcome_at, :moderation_threshold_reached_at,
40-
:government_response_created_at, :government_response_updated_at
39+
:response_threshold_reached_at, :debate_threshold_reached_at, :rejected_at, :debate_outcome_at,
40+
:moderation_threshold_reached_at, :government_response_created_at, :government_response_updated_at
4141
]
4242
end
4343

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
json.type "archived-petition"
2+
json.id petition.id
3+
4+
json.links do
5+
json.self archived_petition_url(petition, format: params[:format])
6+
end if defined?(is_collection)
7+
8+
json.attributes do
9+
json.title petition.title
10+
json.description petition.description
11+
json.state petition.state
12+
json.signature_count petition.signature_count
13+
json.opened_at api_date_format(petition.opened_at)
14+
json.closed_at api_date_format(petition.closed_at)
15+
json.created_at api_date_format(petition.created_at)
16+
json.updated_at api_date_format(petition.updated_at)
17+
18+
if petition.rejected?
19+
json.rejection do
20+
json.details petition.reason_for_rejection
21+
end
22+
else
23+
json.rejection nil
24+
end
25+
26+
if petition.response?
27+
json.government_response do
28+
json.details petition.response
29+
end
30+
else
31+
json.government_response nil
32+
end
33+
end
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
json.links do
2+
json.self request.url
3+
json.merge! ApiPaginationLinksPresenter.new(@petitions).serialize
4+
end
5+
6+
json.data @petitions do |petition|
7+
json.partial! 'petition', petition: petition, is_collection: true
8+
end
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
json.cache! @petition, expires_in: 5.minutes do
2+
json.links do
3+
json.self request.url
4+
end
5+
6+
json.data do
7+
json.partial! 'petition', petition: @petition
8+
end
9+
end

app/views/petitions/_petition.json.jbuilder

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ json.attributes do
2222
json.rejected_at api_date_format(petition.rejected_at)
2323
json.debate_outcome_at api_date_format(petition.debate_outcome_at)
2424
json.moderation_threshold_reached_at api_date_format(petition.moderation_threshold_reached_at)
25+
json.response_threshold_reached_at api_date_format(petition.response_threshold_reached_at)
2526

2627
if petition.open?
2728
json.creator_name petition.creator_name
Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
json.links do
2-
json.self request.url
3-
end
1+
json.cache! @petition, expires_in: 5.minutes do
2+
json.links do
3+
json.self request.url
4+
end
45

5-
json.data do
6-
json.partial! 'petition', petition: @petition
6+
json.data do
7+
json.partial! 'petition', petition: @petition
8+
end
79
end

spec/presenters/petition_csv_presenter_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ def csvd_petition(petition)
5454
timestampify(petition.closed_at),
5555
timestampify(petition.government_response_at),
5656
datestampify(petition.scheduled_debate_date),
57+
timestampify(petition.response_threshold_reached_at),
5758
timestampify(petition.debate_threshold_reached_at),
5859
timestampify(petition.rejected_at),
5960
timestampify(petition.debate_outcome_at),

0 commit comments

Comments
 (0)