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, user_credentials: nil, auth_key: nil, 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, user_credentials: nil, auth_key: nil, logger: nil)
  @company_id = company_id
  @user_credentials = user_credentials
  @auth_key = auth_key
  @logger = logger

  @bin_tickets = {}
end

Instance Method Details

#add_attachment(ticket_id, file_name, file_content, request_options = {}) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
# File 'lib/vimaly/client.rb', line 66

def add_attachment(ticket_id, file_name, file_content, request_options={})
  response = post("/tickets/#{ticket_id}/attachments?name=#{CGI.escape(file_name)}", file_content, request_options)
  case response.status
  when 200..299
    true
  else
    log_warn "status: #{response.status}"
    log_warn "        #{response.inspect}"
    false
  end
end

#bin(name_or_regex) ⇒ Object



78
79
80
81
82
83
84
85
# File 'lib/vimaly/client.rb', line 78

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



87
88
89
90
91
92
93
# File 'lib/vimaly/client.rb', line 87

def bins
  @bins ||= begin
    get('/bins?max-results=500').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
41
# 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_id = next_ticket_id
  ticket_to_json = ticket.to_json(custom_field_name_map)
  response = post("/tickets/#{ticket_id}", ticket_to_json)
  case response.status
  when 200..299
    ticket_id
  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



144
145
146
147
148
149
150
# File 'lib/vimaly/client.rb', line 144

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



133
134
135
136
137
138
139
140
141
# File 'lib/vimaly/client.rb', line 133

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



127
128
129
130
131
# File 'lib/vimaly/client.rb', line 127

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

#search_tickets(query_string, state: 'active') ⇒ Object



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

def search_tickets(query_string, state: 'active')
  ticket_type_map = Hash[ticket_types.map { |t| [t.id, t] }]
  bin_map = Hash[bins.map { |b| [b.id, b] }]

  get("/ticket-search?text=#{query_string}&state=#{state}").map do |ticket_data|
    Ticket.from_vimaly(ticket_data, ticket_type_map, bin_map, custom_fields)
  end
end

#ticket_type(name_or_regex) ⇒ Object



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

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



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

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



118
119
120
121
122
123
124
125
# File 'lib/vimaly/client.rb', line 118

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



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

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



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

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
    id
  else
    log_warn "status: #{response.status}"
    log_warn "        #{response.inspect}"
    false
  end
end