Class: GdsApi::ContentApi

Inherits:
Base
  • Object
show all
Defined in:
lib/gds_api/content_api.rb

Instance Attribute Summary

Attributes inherited from Base

#options

Instance Method Summary collapse

Methods inherited from Base

#client, #create_client, #url_for_slug

Constructor Details

#initialize(endpoint_url, options = {}) ⇒ ContentApi

Returns a new instance of ContentApi.



6
7
8
9
10
11
12
13
14
# File 'lib/gds_api/content_api.rb', line 6

def initialize(endpoint_url, options = {})
  # If the `web_urls_relative_to` option is given, the adapter will convert
  # any `web_url` values to relative URLs if they are from the same host.
  #
  # For example: "https://www.gov.uk"

  @web_urls_relative_to = options.delete(:web_urls_relative_to)
  super
end

Instance Method Details

#artefact(slug, params = {}) ⇒ Object



66
67
68
# File 'lib/gds_api/content_api.rb', line 66

def artefact(slug, params = {})
  get_json(artefact_url(slug, params))
end

#artefact!(slug, params = {}) ⇒ Object



70
71
72
# File 'lib/gds_api/content_api.rb', line 70

def artefact!(slug, params = {})
  get_json(artefact_url(slug, params))
end

#artefactsObject



74
75
76
# File 'lib/gds_api/content_api.rb', line 74

def artefacts
  get_list("#{base_url}/artefacts.json")
end

#business_support_schemes(facets) ⇒ Object



95
96
97
98
99
100
101
102
103
# File 'lib/gds_api/content_api.rb', line 95

def business_support_schemes(facets)
  url = "#{base_url}/business_support_schemes.json"
  query = facets.map { |k, v| "#{k}=#{v}" }
  if query.any?
    url += "?#{query.join('&')}"
  end

  get_json(url)
end

#child_tags(tag_type, parent_tag, options = {}) ⇒ Object



43
44
45
46
47
48
49
50
51
# File 'lib/gds_api/content_api.rb', line 43

def child_tags(tag_type, parent_tag, options = {})
  params = [
    "type=#{CGI.escape(tag_type)}",
    "parent_id=#{CGI.escape(parent_tag)}",
  ]
  params << "sort=#{options[:sort]}" if options.has_key?(:sort)

  get_list("#{base_url}/tags.json?#{params.join('&')}")
end

#for_need(need_id) ⇒ Object



62
63
64
# File 'lib/gds_api/content_api.rb', line 62

def for_need(need_id)
  get_list("#{base_url}/for_need/#{CGI.escape(need_id.to_s)}.json")
end

#get_json(url, &create_response) ⇒ Object



111
112
113
114
115
116
# File 'lib/gds_api/content_api.rb', line 111

def get_json(url, &create_response)
  create_response = create_response || Proc.new { |r|
    GdsApi::Response.new(r, web_urls_relative_to: @web_urls_relative_to)
  }
  super(url, &create_response)
end

#get_list(url) ⇒ Object



105
106
107
108
109
# File 'lib/gds_api/content_api.rb', line 105

def get_list(url)
  get_json(url) { |r|
    GdsApi::ListResponse.new(r, self, web_urls_relative_to: @web_urls_relative_to)
  }
end

#licences_for_ids(ids) ⇒ Object



90
91
92
93
# File 'lib/gds_api/content_api.rb', line 90

def licences_for_ids(ids)
  ids = ids.map(&:to_s).sort.join(',')
  get_json("#{@endpoint}/licences.json?ids=#{ids}")
end

#local_authorities_by_name(name) ⇒ Object



82
83
84
# File 'lib/gds_api/content_api.rb', line 82

def local_authorities_by_name(name)
  get_json("#{base_url}/local_authorities.json?name=#{CGI.escape(name)}")
end

#local_authorities_by_snac_code(snac_code) ⇒ Object



86
87
88
# File 'lib/gds_api/content_api.rb', line 86

def local_authorities_by_snac_code(snac_code)
  get_json("#{base_url}/local_authorities.json?snac_code=#{CGI.escape(snac_code)}")
end

#local_authority(snac_code) ⇒ Object



78
79
80
# File 'lib/gds_api/content_api.rb', line 78

def local_authority(snac_code)
  get_json("#{base_url}/local_authorities/#{CGI.escape(snac_code)}.json")
end

#root_sectionsObject



20
21
22
# File 'lib/gds_api/content_api.rb', line 20

def root_sections
  root_tags("section")
end

#root_tags(tag_type) ⇒ Object



39
40
41
# File 'lib/gds_api/content_api.rb', line 39

def root_tags(tag_type)
  get_list("#{base_url}/tags.json?type=#{CGI.escape(tag_type)}&root_sections=true")
end

#sectionsObject



16
17
18
# File 'lib/gds_api/content_api.rb', line 16

def sections
  tags("section")
end

#sub_sections(parent_tag) ⇒ Object



24
25
26
# File 'lib/gds_api/content_api.rb', line 24

def sub_sections(parent_tag)
  child_tags("section", parent_tag)
end

#tag(tag, tag_type = nil) ⇒ Object



53
54
55
56
57
58
59
60
# File 'lib/gds_api/content_api.rb', line 53

def tag(tag, tag_type = nil)
  if tag_type.nil?
    raise "Requests for tags without a tag_type are no longer supported. You probably want a tag_type of 'section'. See https://github.com/alphagov/govuk_content_api/blob/f4c0102a1ae4970be6a440707b89798442f768b9/govuk_content_api.rb#L241-L250"
  end

  url = [base_url, "tags", CGI.escape(tag_type), CGI.escape(tag)].join("/") + ".json"
  get_json(url)
end

#tags(tag_type, options = {}) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'lib/gds_api/content_api.rb', line 28

def tags(tag_type, options = {})
  params = [
    "type=#{CGI.escape(tag_type)}"
  ]
  params << "sort=#{options[:sort]}" if options.has_key?(:sort)
  params << "draft=true" if options[:draft]
  params << "cachebust=#{Time.now.utc.to_i}#{rand(1000)}" if options[:bust_cache]

  get_list("#{base_url}/tags.json?#{params.join('&')}")
end