gds-api-adapters 7.9.1 → 7.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,17 @@
1
+ require_relative 'base'
2
+
3
+ class GdsApi::Organisations < GdsApi::Base
4
+
5
+ def organisations
6
+ get_list! "#{base_url}/organisations"
7
+ end
8
+
9
+ def organisation(organisation_slug)
10
+ get_json "#{base_url}/organisations/#{organisation_slug}"
11
+ end
12
+
13
+ private
14
+ def base_url
15
+ "#{endpoint}/api"
16
+ end
17
+ end
@@ -9,6 +9,13 @@ module GdsApi
9
9
  end
10
10
  end
11
11
 
12
+ # expects a slug like "ministry-of-funk"
13
+ # returns an acronym like "MOF"
14
+ def acronymize_slug(slug)
15
+ initials = slug.gsub(/\b\w+/) {|m| m[0] }.gsub("-", "")
16
+ initials.upcase
17
+ end
18
+
12
19
  def response_base
13
20
  {
14
21
  "_response_info" => {
@@ -0,0 +1,101 @@
1
+ require 'gds_api/test_helpers/json_client_helper'
2
+ require 'gds_api/test_helpers/common_responses'
3
+
4
+ module GdsApi
5
+ module TestHelpers
6
+ module Organisations
7
+ include GdsApi::TestHelpers::CommonResponses
8
+
9
+ ORGANISATIONS_API_ENDPOINT = Plek.current.find('whitehall-admin')
10
+
11
+ # Sets up the index endpoints for the given organisation slugs
12
+ # The stubs are setup to paginate in chunks of 20
13
+ #
14
+ # This also sets up the individual endpoints for each slug
15
+ # by calling organisations_api_has_organisation below
16
+ def organisations_api_has_organisations(organisation_slugs)
17
+ organisation_slugs.each {|s| organisations_api_has_organisation(s) }
18
+ pages = []
19
+ organisation_slugs.each_slice(20) do |slugs|
20
+ pages << slugs.map {|s| organisation_details_for_slug(s) }
21
+ end
22
+
23
+ pages.each_with_index do |page, i|
24
+ page_details = plural_response_base.merge({
25
+ "results" => page,
26
+ "total" => organisation_slugs.size,
27
+ "pages" => pages.size,
28
+ "current_page" => i + 1,
29
+ "page_size" => 20,
30
+ "start_index" => i * 20 + 1,
31
+ })
32
+
33
+ links = {:self => "#{ORGANISATIONS_API_ENDPOINT}/api/organisations?page=#{i + 1}" }
34
+ links[:next] = "#{ORGANISATIONS_API_ENDPOINT}/api/organisations?page=#{i + 2}" if pages[i+1]
35
+ links[:previous] = "#{ORGANISATIONS_API_ENDPOINT}/api/organisations?page=#{i}" unless i == 0
36
+ page_details["_response_info"]["links"] = []
37
+ link_headers = []
38
+ links.each do |rel, href|
39
+ page_details["_response_info"]["links"] << {"rel" => rel, "href" => href}
40
+ link_headers << "<#{href}>; rel=\"#{rel}\""
41
+ end
42
+
43
+ stub_request(:get, links[:self]).
44
+ to_return(:status => 200, :body => page_details.to_json, :headers => {"Link" => link_headers.join(", ")})
45
+ if i == 0
46
+ # First page exists at URL with and without page param
47
+ stub_request(:get, links[:self].sub(/\?page=1/, '')).
48
+ to_return(:status => 200, :body => page_details.to_json, :headers => {"Link" => link_headers.join(", ")})
49
+ end
50
+ end
51
+ end
52
+
53
+ def organisations_api_has_organisation(organisation_slug, details=nil)
54
+ details ||= organisation_for_slug(organisation_slug)
55
+ stub_request(:get, "#{ORGANISATIONS_API_ENDPOINT}/api/organisations/#{organisation_slug}").
56
+ to_return(:status => 200, :body => details.to_json)
57
+ end
58
+
59
+ def organisations_api_does_not_have_organisation(organisation_slug)
60
+ stub_request(:get, "#{ORGANISATIONS_API_ENDPOINT}/api/organisations/#{organisation_slug}").to_return(:status => 404)
61
+ end
62
+
63
+ def organisation_for_slug(slug)
64
+ singular_response_base.merge(organisation_details_for_slug(slug))
65
+ end
66
+
67
+ # Constructs a sample organisation
68
+ #
69
+ # if the slug contains 'ministry' the format will be set to 'Ministerial department'
70
+ # otherwise it will be set to 'Executive agency'
71
+ def organisation_details_for_slug(slug)
72
+ {
73
+ "id" => "https://www.gov.uk/api/organisations/#{slug}",
74
+ "title" => titleize_slug(slug, :title_case => true),
75
+ "format" => (slug =~ /ministry/ ? "Ministerial department" : "Executive agency"),
76
+ "updated_at" => "2013-03-25T13:06:42+00:00",
77
+ "web_url" => "https://www.gov.uk/government/organisations/#{slug}",
78
+ "details" => {
79
+ "slug" => slug,
80
+ "acronym" => acronymize_slug(slug),
81
+ "closed_at" => nil,
82
+ "govuk_status" => (slug =~ /ministry/ ? "live" : "joining"),
83
+ },
84
+ "parent_organisations" => [
85
+ {
86
+ "id" => "https://www.gov.uk/api/organisations/#{slug}-parent-1",
87
+ "web_url" => "https://www.gov.uk/government/organisations/#{slug}-parent-1"
88
+ },
89
+ ],
90
+ "child_organisations" => [
91
+ {
92
+ "id" => "https://www.gov.uk/api/organisations/#{slug}-child-1",
93
+ "web_url" => "https://www.gov.uk/government/organisations/#{slug}-child-1"
94
+ },
95
+ ],
96
+ }
97
+ end
98
+
99
+ end
100
+ end
101
+ end
@@ -1,3 +1,3 @@
1
1
  module GdsApi
2
- VERSION = '7.9.1'
2
+ VERSION = '7.10.0'
3
3
  end
@@ -0,0 +1,53 @@
1
+ require_relative 'test_helper'
2
+ require 'gds_api/organisations'
3
+ require 'gds_api/test_helpers/organisations'
4
+
5
+ describe GdsApi::Organisations do
6
+ include GdsApi::TestHelpers::Organisations
7
+
8
+ before do
9
+ @base_api_url = GdsApi::TestHelpers::Organisations::ORGANISATIONS_API_ENDPOINT
10
+ @api = GdsApi::Organisations.new(@base_api_url)
11
+ end
12
+
13
+ describe "fetching list of organisations" do
14
+ it "should get the organisations" do
15
+ organisation_slugs = %w(ministry-of-fun, tea-agency)
16
+ organisations_api_has_organisations(organisation_slugs)
17
+
18
+ response = @api.organisations
19
+ assert_equal organisation_slugs, response.map {|r| r.details.slug }
20
+ assert_equal "Tea Agency", response.results[1].title
21
+ end
22
+
23
+ it "should handle the pagination" do
24
+ organisation_slugs = (1..50).map {|n| "organisation-#{n}" }
25
+ organisations_api_has_organisations(organisation_slugs)
26
+
27
+ response = @api.organisations
28
+ assert_equal organisation_slugs, response.with_subsequent_pages.map {|r| r.details.slug }
29
+ end
30
+
31
+ it "should raise error if endpoint 404s" do
32
+ stub_request(:get, "#{@base_api_url}/api/organisations").to_return(:status => 404)
33
+ assert_raises GdsApi::HTTPNotFound do
34
+ @api.organisations
35
+ end
36
+ end
37
+ end
38
+
39
+ describe "fetching an organisation" do
40
+ it "should return the details" do
41
+ organisations_api_has_organisation('ministry-of-fun')
42
+
43
+ response = @api.organisation('ministry-of-fun')
44
+ assert_equal 'Ministry Of Fun', response.title
45
+ end
46
+
47
+ it "should return nil for a non-existent organisation" do
48
+ organisations_api_does_not_have_organisation('non-existent')
49
+
50
+ assert_nil @api.organisation('non-existent')
51
+ end
52
+ end
53
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gds-api-adapters
3
3
  version: !ruby/object:Gem::Version
4
- version: 7.9.1
4
+ version: 7.10.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-10-09 00:00:00.000000000 Z
12
+ date: 2013-10-10 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: plek
@@ -273,6 +273,7 @@ files:
273
273
  - lib/gds_api/test_helpers/need_api.rb
274
274
  - lib/gds_api/test_helpers/content_api/artefact_stub.rb
275
275
  - lib/gds_api/test_helpers/licence_application.rb
276
+ - lib/gds_api/test_helpers/organisations.rb
276
277
  - lib/gds_api/test_helpers/imminence.rb
277
278
  - lib/gds_api/test_helpers/worldwide.rb
278
279
  - lib/gds_api/test_helpers/mapit.rb
@@ -284,6 +285,7 @@ files:
284
285
  - lib/gds_api/test_helpers/publisher.rb
285
286
  - lib/gds_api/test_helpers/common_responses.rb
286
287
  - lib/gds_api/test_helpers/content_api.rb
288
+ - lib/gds_api/organisations.rb
287
289
  - lib/gds_api/needotron.rb
288
290
  - lib/gds_api/null_cache.rb
289
291
  - lib/gds_api/imminence.rb
@@ -313,6 +315,7 @@ files:
313
315
  - test/json_client_test.rb
314
316
  - test/content_api_test.rb
315
317
  - test/response_test.rb
318
+ - test/organisations_api_test.rb
316
319
  - test/imminence_api_test.rb
317
320
  - test/asset_manager_test.rb
318
321
  - test/fixtures/hello.txt
@@ -335,7 +338,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
335
338
  version: '0'
336
339
  segments:
337
340
  - 0
338
- hash: 584452250631713984
341
+ hash: 4439822392201911705
339
342
  required_rubygems_version: !ruby/object:Gem::Requirement
340
343
  none: false
341
344
  requirements:
@@ -344,7 +347,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
344
347
  version: '0'
345
348
  segments:
346
349
  - 0
347
- hash: 584452250631713984
350
+ hash: 4439822392201911705
348
351
  requirements: []
349
352
  rubyforge_project:
350
353
  rubygems_version: 1.8.23
@@ -365,6 +368,7 @@ test_files:
365
368
  - test/json_client_test.rb
366
369
  - test/content_api_test.rb
367
370
  - test/response_test.rb
371
+ - test/organisations_api_test.rb
368
372
  - test/imminence_api_test.rb
369
373
  - test/asset_manager_test.rb
370
374
  - test/fixtures/hello.txt