class CertificateFactory::Certificate

Public Class Methods

new(url, options = {}) click to toggle source
# File lib/certificate-factory/certificate.rb, line 11
def initialize(url, options = {})
  @url = url
  @dataset_url = options[:dataset_url]
  @campaign = options[:campaign]
end

Public Instance Methods

generate() click to toggle source
# File lib/certificate-factory/certificate.rb, line 17
def generate
  response = post
  if response.code == 202
    @dataset_url = response["dataset_url"]
    {
      success: "pending",
      documentation_url: @url,
      dataset_url: @dataset_url
    }
  else
    {
      success: "false",
      published: "false",
      documentation_url: @url,
      dataset_url: response["dataset_url"],
      error: get_error(response)
    }
  end
end
post() click to toggle source
# File lib/certificate-factory/certificate.rb, line 73
def post
  self.class.post("/datasets", body: body)
end
result() click to toggle source
# File lib/certificate-factory/certificate.rb, line 53
def result
  if @dataset_url
    result = get_result(@dataset_url)
    {
      success: result["success"],
      published: result["published"],
      documentation_url: @url,
      certificate_url: result["certificate_url"],
      user: result["owner_email"]
    }
  else
    response = generate
    if @dataset_url
      self.result
    else
      response
    end
  end
end
update() click to toggle source
# File lib/certificate-factory/certificate.rb, line 37
def update
  response = generate
  if response[:success] != "pending" && response[:error] == "Dataset already exists"
    dataset_id = response['dataset_id']
    response = self.class.post("/datasets/#{dataset_id}/certificates", body: body)
    {
      success: response['success'],
      documentation_url: @url,
      dataset_url: @dataset_url,
      error: get_error(response)
    }
  else
    return response
  end
end

Private Instance Methods

body() click to toggle source
# File lib/certificate-factory/certificate.rb, line 79
def body
  hash = {
    "jurisdiction" => "gb",
    "create_user" => true,
    "dataset" => {
      "documentationUrl" => @url
    }
  }
  hash['campaign'] = @campaign if @campaign
  hash.to_json
end
get_error(response) click to toggle source
# File lib/certificate-factory/certificate.rb, line 100
def get_error(response)
  if response.code == 422
    response["errors"].first
  elsif response.code == 401
    "Username and / or API key not recognised"
  else
    "Unknown error"
  end
end
get_result(url) click to toggle source
# File lib/certificate-factory/certificate.rb, line 91
def get_result(url)
  loop do
    result = self.class.get(url)
    return result if result["success"] != "pending"
    url = result["dataset_url"]
    sleep 5
  end
end