Class: Vimaly::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/vimaly/client.rb

Constant Summary collapse

VIMALY_ROOT_URL =
"https://o1.vimaly.com"
CUSTOM_FIELD_TYPES =
[::NilClass, ::String, ::Float, ::Integer, ::Array, ::Date]

Instance Method Summary collapse

Constructor Details

#initialize(company_id, username, password, logger = nil) ⇒ Client

Returns a new instance of Client.



11
12
13
14
15
16
17
18
# File 'lib/vimaly/client.rb', line 11

def initialize(company_id, username, password, logger=nil)
  @company_id = company_id
  @username = username
  @password = password
  @logger = logger

  @bin_tickets = {}
end

Instance Method Details

#bin(name_or_regex) ⇒ Object



56
57
58
59
60
61
62
63
# File 'lib/vimaly/client.rb', line 56

def bin(name_or_regex)
  case name_or_regex
  when String
    bins.detect { |bin| bin.name == name_or_regex }
  when Regexp
    bins.detect { |bin| bin.name =~ name_or_regex }
  end
end

#binsObject



65
66
67
68
69
70
71
# File 'lib/vimaly/client.rb', line 65

def bins
  @bins ||= begin
    get('/bins').map do |bin_data|
      Bin.new(bin_data['_id'], bin_data['name'])
    end
  end
end

#create_ticket(title, description, format: 'text', ticket_type_name: 'bug', bin_name: /please panic/i, **others) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/vimaly/client.rb', line 20

def create_ticket(title, description, format: 'text', ticket_type_name: 'bug', bin_name: /please panic/i, **others)
  ticket = Ticket.new({
    title: title,
    description: description,
    format: format,
    ticket_type: ticket_type(ticket_type_name),
    bin: bin(bin_name) }.
    merge(others)
  )

  ticket_to_json = ticket.to_json(custom_field_name_map)
  response = post("/tickets/#{next_ticket_id}", ticket_to_json)
  case response.status
    when 200..299
      true
  else
    log_warn "status: #{response.status}"
    log_warn "        #{response.inspect}"
    false
  end
end

#custom_fieldsObject

Get custom fields as a hash of id => name, type



123
124
125
126
127
128
129
# File 'lib/vimaly/client.rb', line 123

def custom_fields
  @custom_fields ||= begin
    Hash[get('/custom-fields').map do |cf|
      [cf['_id'], { id: cf['_id'], name: cf['name'], type: CUSTOM_FIELD_TYPES[cf['type'].to_i] }]
    end]
  end
end

#matching_tickets_in_bin(bin_id, title_matcher) ⇒ Object



112
113
114
115
116
117
118
119
120
# File 'lib/vimaly/client.rb', line 112

def matching_tickets_in_bin(bin_id, title_matcher)
  tickets = tickets_in_bin(bin_id)
  case title_matcher
  when String
    tickets.select { |t| t.title == title_matcher }
  when Regexp
    tickets.select { |t| t.title =~ title_matcher }
  end
end

#matching_tickets_in_named_bin(bin_name, title_matcher) ⇒ Object



106
107
108
109
110
# File 'lib/vimaly/client.rb', line 106

def matching_tickets_in_named_bin(bin_name, title_matcher)
  named_bin = bin(bin_name)
  raise "Bin #{bin_name} not found" unless named_bin
  matching_tickets_in_bin(named_bin.id, title_matcher)
end

#ticket_type(name_or_regex) ⇒ Object



73
74
75
76
77
78
79
80
# File 'lib/vimaly/client.rb', line 73

def ticket_type(name_or_regex)
  case name_or_regex
  when String
    ticket_types.detect { |tt| tt.name == name_or_regex }
  when Regexp
    ticket_types.detect { |tt| tt.name =~ name_or_regex }
  end
end

#ticket_typesObject



82
83
84
85
86
87
88
# File 'lib/vimaly/client.rb', line 82

def ticket_types
  @ticket_types ||= begin
    get('/ticket-types').map do |bin_data|
      TicketType.new(bin_data['_id'], bin_data['name'])
    end
  end
end

#tickets_in_bin(bin_id) ⇒ Object



96
97
98
99
100
101
102
103
104
# File 'lib/vimaly/client.rb', line 96

def tickets_in_bin(bin_id)
  ticket_type_map = Hash[ticket_types.map { |t| [t.id, t] }]
  bin_map = Hash[bins.map { |b| [b.id, b] }]

  get("/tickets?bin_id=#{bin_id}").map do |ticket_data|
    Ticket.from_vimaly(ticket_data, ticket_type_map, bin_map, custom_fields)
  end

end

#tickets_in_named_bin(bin_name) ⇒ Object



90
91
92
93
94
# File 'lib/vimaly/client.rb', line 90

def tickets_in_named_bin(bin_name)
  named_bin = bin(bin_name)
  raise "Bin #{bin_name} not found" unless named_bin
  tickets_in_bin(named_bin.id)
end

#update_ticket(id, other_fields) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/vimaly/client.rb', line 42

def update_ticket(id, other_fields)
    ticket = Ticket.new(other_fields)

  response = put("/tickets/#{id}", ticket.to_json(custom_field_name_map, true))
  case response.status
    when 200..299
      true
    else
      log_warn "status: #{response.status}"
      log_warn "        #{response.inspect}"
      false
  end
end