Class: TaskMaster::CustomRequest
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- TaskMaster::CustomRequest
- Defined in:
- app/models/task_master/custom_request.rb
Constant Summary collapse
- STATUSES =
{ 1 => "NEW", 5 => "OPEN", 7 => "AWAITING_RESPONSE", 20 => "CLOSED", 21 => "EXPIRED" }
Instance Method Summary collapse
- #_custom_fields_are_all_valid ⇒ Object
- #_custom_fields_is_not_empty ⇒ Object
- #_initialize_custom_fields ⇒ Object
- #_set_attributes_from_last_response ⇒ Object
- #_set_closed_without_answers ⇒ Object
- #_to_fancy_hands_data ⇒ Object
- #cancel ⇒ Object
-
#post_to_fancyhands ⇒ Object
Override this method if you’d like to move the post to Fancy Hands into the background.
- #post_to_fancyhands_now ⇒ Object
- #status ⇒ Object
- #trigger_callback ⇒ Object
Instance Method Details
#_custom_fields_are_all_valid ⇒ Object
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'app/models/task_master/custom_request.rb', line 148 def _custom_fields_are_all_valid = {} custom_fields.map do |f| CustomRequestField.new(f.to_hash) end.each_with_index do |field, index| next if field.valid? [index] = field.errors. end if .any? errors.add( :custom_fields, .map do |index, | "custom field at index #{index} has errors #{.join(", ")}." end.join(", ") ) end end |
#_custom_fields_is_not_empty ⇒ Object
142 143 144 145 146 |
# File 'app/models/task_master/custom_request.rb', line 142 def _custom_fields_is_not_empty if custom_fields.empty? errors.add :custom_fields, "can't be empty" end end |
#_initialize_custom_fields ⇒ Object
138 139 140 |
# File 'app/models/task_master/custom_request.rb', line 138 def _initialize_custom_fields self.custom_fields ||= [] end |
#_set_attributes_from_last_response ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'app/models/task_master/custom_request.rb', line 72 def _set_attributes_from_last_response sorted_responses = [] # add the responses that don't have date_updated present to the beginning # sorted responses sorted_responses += responses.select do |response| response["date_updated"].blank? end sorted_responses += responses.select do |response| response["date_updated"].present? end.sort_by do |response| DateTime.parse(response["date_updated"]) end self.responses = sorted_responses return unless last_response = responses.last if key.blank? raise StandardError, "The last response had a blank key. #{last_response.inspect}" if last_response["key"].blank? self.key = last_response["key"] end if last_response["numeric_status"].present? self.numeric_status = Integer(last_response["numeric_status"]) end if last_response["date_created"].present? && !fancyhands_created_at.present? self.fancyhands_created_at = DateTime.parse(last_response["date_created"]) end if last_response["date_updated"].present? self.fancyhands_updated_at = DateTime.parse(last_response["date_updated"]) end if self.numeric_status == 20 self.fancyhands_closed_at = fancyhands_updated_at end self.answers ||= {} Array(last_response["custom_fields"]).each do |custom_field| self.answers[custom_field["field_name"]] = custom_field["answer"] end self. = Array(last_response["messages"]) self.phone_calls = Array(last_response["phone_calls"]) Array(last_response["phone_calls"]).each do |phone_call| self.phone_call_seconds += Integer(phone_call["duration"]) end return true end |
#_set_closed_without_answers ⇒ Object
129 130 131 132 133 134 135 136 |
# File 'app/models/task_master/custom_request.rb', line 129 def _set_closed_without_answers return unless numeric_status == 20 self.closed_without_answers = answers.values.all?(&:blank?) # don't stop the save process return true end |
#_to_fancy_hands_data ⇒ Object
166 167 168 169 170 171 172 173 174 |
# File 'app/models/task_master/custom_request.rb', line 166 def _to_fancy_hands_data { title: title, description: description, bid: bid.to_f, expiration_date: expiration_date.strftime("%Y-%m-%dT%H:%M:%SZ"), custom_fields: custom_fields.map(&:to_hash).to_json } end |
#cancel ⇒ Object
54 55 56 57 58 |
# File 'app/models/task_master/custom_request.rb', line 54 def cancel return false unless key.present? Client.new.cancel_custom_request(key) end |
#post_to_fancyhands ⇒ Object
Override this method if you’d like to move the post to Fancy Hands into the background. Redefine #post_to_fancyhands to queue the job in whatever other way you’d prefer.
42 43 44 |
# File 'app/models/task_master/custom_request.rb', line 42 def post_to_fancyhands post_to_fancyhands_now end |
#post_to_fancyhands_now ⇒ Object
46 47 48 49 50 51 52 |
# File 'app/models/task_master/custom_request.rb', line 46 def post_to_fancyhands_now return if key.present? response = Client.new.create_custom_request(_to_fancy_hands_data) self.responses << response save end |
#status ⇒ Object
66 67 68 69 70 |
# File 'app/models/task_master/custom_request.rb', line 66 def status return unless numeric_status.present? STATUSES[numeric_status] end |
#trigger_callback ⇒ Object
60 61 62 63 64 |
# File 'app/models/task_master/custom_request.rb', line 60 def trigger_callback return false unless key.present? Client.new.trigger_callback(key) end |