Class: CertificateFactory::Certificate

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/certificate-factory/certificate.rb

Instance Method Summary collapse

Constructor Details

#initialize(url, options = {}) ⇒ Certificate

Returns a new instance of Certificate.



11
12
13
14
15
# File 'lib/certificate-factory/certificate.rb', line 11

def initialize(url, options = {})
  @url = url
  @dataset_url = options[:dataset_url]
  @campaign = options[:campaign]
end

Instance Method Details

#generateObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# 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

#postObject



73
74
75
# File 'lib/certificate-factory/certificate.rb', line 73

def post
  self.class.post("/datasets", body: body)
end

#resultObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# 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

#updateObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# 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