Class: GdsApi::ContentApi

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

Instance Attribute Summary

Attributes inherited from Base

#options

Instance Method Summary collapse

Methods included from ExceptionHandling

#ignoring

Methods inherited from Base

#client, #create_client, #url_for_slug

Constructor Details

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

Returns a new instance of ContentApi.



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

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



77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/gds_api/content_api.rb', line 77

def artefact(slug, params={})
  url = "#{base_url}/#{CGI.escape(slug)}.json"
  query = params.map { |k,v| "#{k}=#{v}" }
  if query.any?
    url += "?#{query.join("&")}"
  end

  if params[:edition] && ! options.include?(:bearer_token)
    raise GdsApi::NoBearerToken
  end
  get_json(url)
end

#artefactsObject



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

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

#business_support_schemes(identifiers) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/gds_api/content_api.rb', line 111

def business_support_schemes(identifiers)
  identifiers = identifiers.map {|i| CGI.escape(i) }
  url_template = "#{base_url}/business_support_schemes.json?identifiers="
  response = nil # assignment necessary for variable scoping

  start_url = "#{url_template}#{identifiers.shift}"
  last_batch_url = identifiers.inject(start_url) do |url, id|
    new_url = [url, id].join(',')
    if new_url.length >= 2000
      # fetch a batch using the previous url, then return a new start URL with this id
      response = get_batch(url, response)
      "#{url_template}#{id}"
    else
      new_url
    end
  end
  get_batch(last_batch_url, response)
end

#child_tags(tag_type, parent_tag) ⇒ Object



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

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



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

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



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

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



142
143
144
145
146
147
# File 'lib/gds_api/content_api.rb', line 142

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



149
150
151
152
153
154
# File 'lib/gds_api/content_api.rb', line 149

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



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

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



130
131
132
133
134
# File 'lib/gds_api/content_api.rb', line 130

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



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

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



98
99
100
# File 'lib/gds_api/content_api.rb', line 98

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



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

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



94
95
96
# File 'lib/gds_api/content_api.rb', line 94

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

#root_sectionsObject



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

def root_sections
  root_tags("section")
end

#root_tags(tag_type) ⇒ Object



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

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

#sectionsObject



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

def sections
  tags("section")
end

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



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

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



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

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

#tag(tag, tag_type = nil) ⇒ Object



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

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



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

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

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



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

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