Method: CF::Badge#initialize

Defined in:
lib/cf/badge.rb

#initialize(options = {}) ⇒ Badge

Returns a new instance of Badge.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/cf/badge.rb', line 23

def initialize(options={})
  options.symbolize_keys!
  #if the form is provided as a file then explicitly add title and raw_html for the form
  if options[:form] && options[:form].class != Hash
    if File.exist?(options[:form])
      file_type = IO.popen(["file", "--brief", "--mime-type", options[:form]], in: :close, err: :close).read.chomp
      if file_type == "text/html"
        options[:form] = {:title => "#{options[:name]}_form",:_type => "CustomTaskForm", :raw_html => File.read(options[:form])}
      else
        @errors = ["Wrong type of file for the badge form."]
        return @errors
        exit(1)
      end
    else
      @errors = ["Badge form path is not valid."]
    end
  end

  #known_answers of the badge get customize because to if unique identifier doesn't present then array of hashs returns the last hash for Httparty and get all key value merge into one in rest client/ rack
  options[:known_answers] = self.class.customize_known_answers(options[:known_answers], options[:name]) if options[:known_answers].present?

  @settings = options
  @name = options[:name]
  @description = options[:description]
  @allow_retake_after = options[:allow_retake_after]

  request =
  {
    :body =>
    {
      :api_key => CF.api_key,
      :badge => options
    }
  }

  resp = HTTParty.post("#{CF.api_url}#{CF.api_version}/accounts/#{CF.}/badges.json",request)
  begin
  self.errors = resp["error"]["message"] if resp.code != 200
  return resp
  rescue
    self.errors = ["Unexpected error found. Confirm that you are in correct path and form for the badge is provided."]
  end
end