class GdsApi::ContentApi
def artefact(slug, params={})
def artefact(slug, params={}) get_json(artefact_url(slug, params)) end
def artefact!(slug, params={})
def artefact!(slug, params={}) get_json!(artefact_url(slug, params)) end
def artefact_url(slug, params)
def artefact_url(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 url end
def artefacts
def artefacts get_list!("#{base_url}/artefacts.json") end
def base_url
def base_url endpoint end
def business_support_schemes(facets)
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
def child_tags(tag_type, parent_tag, options={})
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
def curated_list(tag, tag_type=nil)
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
def for_need(need_id)
def for_need(need_id) get_list("#{base_url}/for_need/#{CGI.escape(need_id.to_s)}.json") end
def get_json(url, &create_response)
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
def get_json!(url, &create_response)
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
def get_list(url)
def get_list(url) get_json(url) { |r| GdsApi::ListResponse.new(r, self, web_urls_relative_to: @web_urls_relative_to) } end
def get_list!(url)
def get_list!(url) get_json!(url) { |r| GdsApi::ListResponse.new(r, self, web_urls_relative_to: @web_urls_relative_to) } end
def initialize(endpoint_url, options = {})
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
def key_for_tag_type(tag_type)
def key_for_tag_type(tag_type) tag_type.nil? ? "tag" : CGI.escape(tag_type) end
def licences_for_ids(ids)
def licences_for_ids(ids) ids = ids.map(&:to_s).sort.join(',') get_json("#{@endpoint}/licences.json?ids=#{ids}") end
def local_authorities_by_name(name)
def local_authorities_by_name(name) get_json!("#{base_url}/local_authorities.json?name=#{CGI.escape(name)}") end
def local_authorities_by_snac_code(snac_code)
def local_authorities_by_snac_code(snac_code) get_json!("#{base_url}/local_authorities.json?snac_code=#{CGI.escape(snac_code)}") end
def local_authority(snac_code)
def local_authority(snac_code) get_json("#{base_url}/local_authorities/#{CGI.escape(snac_code)}.json") end
def root_sections
def root_sections root_tags("section") end
def root_tags(tag_type)
def root_tags(tag_type) get_list!("#{base_url}/tags.json?type=#{CGI.escape(tag_type)}&root_sections=true") end
def sections
def sections tags("section") end
def sorted_by(tag, sort_by, tag_type=nil)
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
def sub_sections(parent_tag)
def sub_sections(parent_tag) child_tags("section", parent_tag) end
def tag(tag, tag_type=nil)
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
def tags(tag_type, options={})
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] get_list!("#{base_url}/tags.json?#{params.join('&')}") end
def with_tag(tag, tag_type=nil, options={})
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