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(={}) .symbolize_keys! #if the form is provided as a file then explicitly add title and raw_html for the form if [:form] && [:form].class != Hash if File.exist?([:form]) file_type = IO.popen(["file", "--brief", "--mime-type", [:form]], in: :close, err: :close).read.chomp if file_type == "text/html" [:form] = {:title => "#{[:name]}_form",:_type => "CustomTaskForm", :raw_html => File.read([: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 [:known_answers] = self.class.customize_known_answers([:known_answers], [:name]) if [:known_answers].present? @settings = @name = [:name] @description = [:description] @allow_retake_after = [:allow_retake_after] request = { :body => { :api_key => CF.api_key, :badge => } } resp = HTTParty.post("#{CF.api_url}#{CF.api_version}/accounts/#{CF.account_name}/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 |