Class: Vimaly::Ticket
- Inherits:
-
Object
- Object
- Vimaly::Ticket
- Defined in:
- lib/vimaly/ticket.rb
Class Method Summary collapse
Instance Method Summary collapse
- #[](name) ⇒ Object
- #add_id(id) ⇒ Object
- #cast_to_vimaly(value, type) ⇒ Object
-
#initialize(*params) ⇒ Ticket
constructor
title, description, format, ticket_type, bin, id=nil.
- #method_missing(name) ⇒ Object
- #to_json(custom_field_name_map, for_update = false) ⇒ Object
- #to_vimaly(custom_field_name_map, for_update = false) ⇒ Object
- #update_fields(fields) ⇒ Object
Constructor Details
#initialize(*params) ⇒ Ticket
title, description, format, ticket_type, bin, id=nil
7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/vimaly/ticket.rb', line 7 def initialize(*params) if params.first.is_a?(Hash) @ticket_fields = HashWithIndifferentAccess.new(params.first) else @ticket_fields = HashWithIndifferentAccess.new [:title, :description, :format, :ticket_type, :bin, :id].each_with_index do |param_key, idx| break if idx>=params.size @ticket_fields[param_key] = params[idx] end end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name) ⇒ Object
115 116 117 118 119 |
# File 'lib/vimaly/ticket.rb', line 115 def method_missing(name) return @ticket_fields[name] if @ticket_fields.key?(name) super end |
Class Method Details
.from_vimaly(src_hash, ticket_types, bins, custom_fields) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/vimaly/ticket.rb', line 19 def self.from_vimaly(src_hash, ticket_types, bins, custom_fields) ticket_fields = Hash[ src_hash.map do |vimaly_name, field_value| case(vimaly_name) when 'rtformat' [:format, field_value] when 'name' [:title, field_value] when 'ticketType_id' [:ticket_type, ticket_types[field_value.to_i]] when 'bin_id' [:bin, bins[field_value.to_i]] when 'customFields' nil else [vimaly_name.to_sym, field_value] end end.compact ] (src_hash['customFields'] || []).each do |custom_field_id, value| ticket_fields[custom_fields[custom_field_id][:name]] = value end Vimaly::Ticket::new(ticket_fields) end |
Instance Method Details
#[](name) ⇒ Object
111 112 113 |
# File 'lib/vimaly/ticket.rb', line 111 def [](name) @ticket_fields[name] end |
#add_id(id) ⇒ Object
106 107 108 109 |
# File 'lib/vimaly/ticket.rb', line 106 def add_id(id) raise "Ticket already has id #{@ticket_fields[:'_id']} and we try and add #{id}" if @ticket_fields[:'_id'] @ticket_fields['_id'] = id end |
#cast_to_vimaly(value, type) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/vimaly/ticket.rb', line 87 def cast_to_vimaly(value, type) case type.to_s when 'String' value.to_s when 'Float' value.to_f when 'Integer' value.to_i when 'Array' value when 'Date' Date.parse(value.to_s).strftime('%Y-%m-%dT%H:%M:%S.%LZ') end end |
#to_json(custom_field_name_map, for_update = false) ⇒ Object
102 103 104 |
# File 'lib/vimaly/ticket.rb', line 102 def to_json(custom_field_name_map, for_update=false) to_vimaly(custom_field_name_map, for_update).to_json end |
#to_vimaly(custom_field_name_map, for_update = false) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/vimaly/ticket.rb', line 46 def to_vimaly(custom_field_name_map, for_update=false) vimaly_fields = Hash[@ticket_fields.map do |field_name, field_value| case(field_name) when 'id' nil # id should not be in ticket body when 'format' ['rtformat', field_value] when 'title' ['name', field_value] when 'description' ['description', normalise(field_value)] when 'ticket_type' ['ticketType_id', field_value.id] when 'bin' ['bin_id', field_value.id] else if for_update fdef = custom_field_name_map[field_name] raise "Field type #{field_name} is not a defined custom or standard field in Vimaly" unless fdef ["customFields.#{fdef[:id]}", cast_to_vimaly(field_value, fdef[:type])] else nil end end end.compact ] unless for_update custom_fields = @ticket_fields.select {|fn, _v| ![:format, :title, :description, :ticket_type, :bin].include?(fn.to_sym) } if custom_fields && !custom_fields.empty? vimaly_fields['customFields'] = Hash[custom_fields.map do |field_name, field_value| fdef = custom_field_name_map[field_name.to_s] [fdef[:id], cast_to_vimaly(field_value, fdef[:type])] end] end end vimaly_fields end |
#update_fields(fields) ⇒ Object
121 122 123 |
# File 'lib/vimaly/ticket.rb', line 121 def update_fields(fields) @ticket_fields.merge!(fields) end |