Class: GdsApi::ContentApi

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

Defined Under Namespace

Classes: ListResponse

Instance Attribute Summary

Attributes inherited from Base

#options

Instance Method Summary collapse

Methods included from ExceptionHandling

#ignoring

Methods inherited from Base

#adapter_name, #client, #create_client, #initialize, #url_for_slug

Constructor Details

This class inherits a constructor from GdsApi::Base

Instance Method Details

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



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/gds_api/content_api.rb', line 36

def artefact(slug, params={})
  edition = params[:edition]
  snac = params[:snac]

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

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

#business_support_schemes(identifiers) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/gds_api/content_api.rb', line 69

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

#curated_list(tag) ⇒ Object



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

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

#get_list(url) ⇒ Object



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

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

#get_list!(url) ⇒ Object



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

def get_list!(url)
  get_json!(url) { |r| ListResponse.new(r, self) }
end

#licences_for_ids(ids) ⇒ Object



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

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



56
57
58
# File 'lib/gds_api/content_api.rb', line 56

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



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

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



52
53
54
# File 'lib/gds_api/content_api.rb', line 52

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

#root_sectionsObject



12
13
14
# File 'lib/gds_api/content_api.rb', line 12

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

#sectionsObject



8
9
10
# File 'lib/gds_api/content_api.rb', line 8

def sections
  get_list!("#{base_url}/tags.json?type=section")
end

#sorted_by(tag, sort_by) ⇒ Object



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

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

#sub_sections(parent_tag) ⇒ Object



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

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

#tag(tag) ⇒ Object



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

def tag(tag)
  get_json("#{base_url}/tags/#{CGI.escape(tag)}.json")
end

#with_tag(tag) ⇒ Object



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

def with_tag(tag)
  get_list!("#{base_url}/with_tag.json?tag=#{CGI.escape(tag)}&include_children=1")
end