Class: CF::Badge

Inherits:
Object
  • Object
show all
Includes:
Client
Defined in:
lib/cf/badge.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#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

Instance Attribute Details

#allow_retake_afterObject

Returns the value of attribute allow_retake_after.



18
19
20
# File 'lib/cf/badge.rb', line 18

def allow_retake_after
  @allow_retake_after
end

#descriptionObject

the description for the badge



13
14
15
# File 'lib/cf/badge.rb', line 13

def description
  @description
end

#errorsObject

Contains error message



21
22
23
# File 'lib/cf/badge.rb', line 21

def errors
  @errors
end

#max_badge_assignmentsObject

number of badge provided to the worker



16
17
18
# File 'lib/cf/badge.rb', line 16

def max_badge_assignments
  @max_badge_assignments
end

#nameObject

Badge name



10
11
12
# File 'lib/cf/badge.rb', line 10

def name
  @name
end

#settingsObject

goldstandard settings



7
8
9
# File 'lib/cf/badge.rb', line 7

def settings
  @settings
end

Class Method Details

.create(*args) ⇒ Object



67
68
69
# File 'lib/cf/badge.rb', line 67

def self.create(*args)
  Badge.new(args.first)
end

.destroy(*args) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/cf/badge.rb', line 97

def self.destroy(*args)
  name = args[0].parameterize
  options = args[1].nil? ? {} : args[1]
  decendents = check_for_decendents(name)
  if decendents
    response = delete("/accounts/#{CF.}/badges/#{name}.json") if options.force
  else
    response = delete("/accounts/#{CF.}/badges/#{name}.json")
  end
  @errors = response['error']['message'] if response['code'] != 200
  return response
end

.list(name = nil) ⇒ Object

list all badges with in the account if name is not provided else it will return list all values of the given badge



111
112
113
114
115
# File 'lib/cf/badge.rb', line 111

def self.list(name = nil)
  response =  get("/accounts/#{CF.}/badges.json", :name => name)
  @errors = response['error']['message'] if response['code'] != 200
  return response
end

.show(name) ⇒ Object



117
118
119
120
121
# File 'lib/cf/badge.rb', line 117

def self.show(name)
  response =  get("/accounts/#{CF.}/badges/#{name.parameterize}.json")
  @errors = response['error']['message'] if response['code'] != 200
  return response
end

.update(badge_name, update_params) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/cf/badge.rb', line 71

def self.update(badge_name, update_params)
  badge_name = badge_name.parameterize
  update_params.symbolize_keys!
  if update_params[:form] && update_params[:form].class != Hash && File.exist?(update_params[:form])
    file_type = IO.popen(["file", "--brief", "--mime-type", update_params[:form]], in: :close, err: :close).read.chomp
    if file_type == "text/html"
      update_params[:form] = {:title => "#{update_params[:name]}_form",:_type => "CustomTaskForm", :raw_html => File.read(update_params[:form])}
    else
      return
    end
  end

  update_params[:known_answers] = customize_known_answers(update_params[:known_answers], update_params[:name]) if update_params[:known_answers].present?

  request =
  {
    :body =>
    {
      :api_key => CF.api_key,
      :badge => update_params
    }
  }
  resp = HTTParty.put("#{CF.api_url}#{CF.api_version}/accounts/#{CF.}/badges/#{badge_name}.json",request)
  return resp
end