Class: KeytechKit::NoteHandler

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/keytechKit/elements/notes/note_handler.rb

Overview

Notes

Instance Method Summary collapse

Constructor Details

#initialize(base_url, username, password) ⇒ NoteHandler

Returns a new instance of NoteHandler.



10
11
12
13
# File 'lib/keytechKit/elements/notes/note_handler.rb', line 10

def initialize(base_url, username, password)
  self.class.base_uri(base_url)
  @auth = { username: username, password: password }
end

Instance Method Details

#create(note_type, element_key) ⇒ Object



38
39
40
41
42
43
# File 'lib/keytechKit/elements/notes/note_handler.rb', line 38

def create(note_type, element_key)
  note = Note.new(NoteType: note_type)
  note.note_type = note_type
  note.element_key = element_key
  note
end

#load(element_key) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/keytechKit/elements/notes/note_handler.rb', line 15

def load(element_key)
  parameter = { basic_auth: @auth }

  response = self.class.get("/elements/#{element_key}/notes", parameter)
  if response.success?
    parse_notes(response['NotesList'], element_key)
  else
    # TODO: return an error object, dont raise exception
    raise response.response
  end
end

#load_note_typesObject



27
28
29
30
31
32
33
34
35
36
# File 'lib/keytechKit/elements/notes/note_handler.rb', line 27

def load_note_types
  parameter = { basic_auth: @auth }
  response = self.class.get('/notetypes', parameter)
  if response.success?
    parse_note_types(response['Notetypes'])
  else
    # TODO: return an error object, dont raise exception
    raise response.response
  end
end

#remove(note) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/keytechKit/elements/notes/note_handler.rb', line 60

def remove(note)
  parameter = { basic_auth: @auth }
  element_key = note.element_key
  note_id = note.id
  response = self.class.delete("/elements/#{element_key}/notes/#{note_id}", parameter)
  ResponseHelper.success(response)
end

#save(note) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/keytechKit/elements/notes/note_handler.rb', line 45

def save(note)
  element_key = note.element_key
  note_hash = note.to_hash
  parameter = { basic_auth: @auth,
                body: note_hash.to_json,
                headers: { 'Content-Type': 'application/json; charset=utf-8
' } }

  response = if note.id.zero?
               self.class.post("/elements/#{element_key}/notes", parameter)
             else
               self.class.put("/elements/#{element_key}/notes", parameter)
             end
  ResponseHelper.created_with_location(response)
end