Skip to main content

Retrieve and display GOV.UK content

The example below illustrates utilising Ruby on Rails with Rest Client to make use of GOV.UK in your application.

require "rest-client"

tax_help = Rails.cache.fetch("govuk/tax-help", expires_in: 1.hour) do
  response = RestClient.get("https://www.gov.uk/api/content/tax-help", { content_type: "json" })
  JSON.parse(response.body).dig("details", "body")
end

content = "<h1>GOV.UK Tax Help</h1><div>#{tax_help}</div>"

In this example we utilise the Rails cache layer so that we can infrequently access the content without concerns of hitting the rate limit.

We then use the API to access the content for Tax Help. In the response we access the body field from within the details object. We store this to a variable tax_help.

Finally we embed this in our own HTML and are ready to output to users.