Class: GdsApi::ContentApi

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

Direct Known Subclasses

ContentStore

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.



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

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



76
77
78
# File 'lib/gds_api/content_api.rb', line 76

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

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



80
81
82
# File 'lib/gds_api/content_api.rb', line 80

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

#artefactsObject



84
85
86
# File 'lib/gds_api/content_api.rb', line 84

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

#business_support_schemes(facets) ⇒ Object



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

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) ⇒ Object



37
38
39
# File 'lib/gds_api/content_api.rb', line 37

def child_tags(tag_type, parent_tag)
  get_list!("#{base_url}/tags.json?type=#{CGI.escape(tag_type)}&parent_id=#{CGI.escape(parent_tag)}")
end

#curated_list(tag, tag_type = nil) ⇒ Object



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

def curated_list(tag, tag_type=nil)
  tag_key = key_for_tag_type(tag_type)

  get_list("#{base_url}/with_tag.json?#{tag_key}=#{CGI.escape(tag)}&sort=curated")
end

#for_need(need_id) ⇒ Object



72
73
74
# File 'lib/gds_api/content_api.rb', line 72

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



127
128
129
130
131
132
# File 'lib/gds_api/content_api.rb', line 127

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_json!(url, &create_response) ⇒ Object



134
135
136
137
138
139
# File 'lib/gds_api/content_api.rb', line 134

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



121
122
123
124
125
# File 'lib/gds_api/content_api.rb', line 121

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

#get_list!(url) ⇒ Object



115
116
117
118
119
# File 'lib/gds_api/content_api.rb', line 115

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



100
101
102
103
# File 'lib/gds_api/content_api.rb', line 100

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



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

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



96
97
98
# File 'lib/gds_api/content_api.rb', line 96

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



88
89
90
# File 'lib/gds_api/content_api.rb', line 88

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

#root_sectionsObject



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

def root_sections
  root_tags("section")
end

#root_tags(tag_type) ⇒ Object



33
34
35
# File 'lib/gds_api/content_api.rb', line 33

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

#sectionsObject



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

def sections
  tags("section")
end

#sorted_by(tag, sort_by, tag_type = nil) ⇒ Object



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

def sorted_by(tag, sort_by, tag_type=nil)
  tag_key = key_for_tag_type(tag_type)

  get_list!("#{base_url}/with_tag.json?#{tag_key}=#{CGI.escape(tag)}&sort=#{sort_by}")
end

#sub_sections(parent_tag) ⇒ Object



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

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

#tag(tag, tag_type = nil) ⇒ Object



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

def tag(tag, tag_type=nil)
  url = "#{base_url}/tags"

  if tag_type
    url << "/#{CGI.escape(tag_type)}"
  end

  get_json("#{url}/#{CGI.escape(tag)}.json")
end

#tags(tag_type) ⇒ Object



29
30
31
# File 'lib/gds_api/content_api.rb', line 29

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

#with_tag(tag, tag_type = nil, options = {}) ⇒ Object



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

def with_tag(tag, tag_type=nil, options={})
  tag_key = key_for_tag_type(tag_type)

  url = "#{base_url}/with_tag.json?#{tag_key}=#{CGI.escape(tag)}"
  url << "&group_by=#{CGI.escape(options[:group_by])}" if options.has_key?(:group_by)

  get_list!(url)
end