Class: Learndot::API

Inherits:
Object
  • Object
show all
Defined in:
lib/learndot/api.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token = nil, debug = false, staging = false) ⇒ API

Returns a new instance of API.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/learndot/api.rb', line 8

def initialize(token = nil, debug = false, staging = false)
  @logger           = Logger.new(STDOUT)
  @logger.level     = debug ? Logger::DEBUG : Logger::WARN
  @logger.formatter = proc { |level, date, name, msg|
    "#{level}: #{msg}\n"
  }

  token ||= get_token

  # Set the base_url to the staging or production endpoint
  @base_url = staging ? "https://puppetlabs-staging.trainingrocket.com/api/rest/v2" : "https://learn.puppet.com/api/rest/v2"

  @headers  = {
    "TrainingRocket-Authorization"      => token,
    "Learndot Enterprise-Authorization" => token,
    "Content-Type" => "application/json",
    "Accept"       => "application/json; charset=utf-8"
  }
end

Instance Attribute Details

#logger=(value) ⇒ Object (writeonly)

Sets the attribute logger

Parameters:

  • value

    the value to set the attribute logger to.



6
7
8
# File 'lib/learndot/api.rb', line 6

def logger=(value)
  @logger = value
end

Instance Method Details

#count(entity, conditions = {}) ⇒ Object



43
44
45
46
47
48
# File 'lib/learndot/api.rb', line 43

def count(entity, conditions = {})
  endpoint = "/manage/#{entity}/search"

  num_records = api_post(endpoint, conditions)['size']
  num_records.is_a?(Integer) ? num_records : 0
end

#create(entity, conditions) ⇒ Object



56
57
58
59
# File 'lib/learndot/api.rb', line 56

def create(entity, conditions)
  endpoint = "/manage/#{entity}"
  api_post(endpoint, conditions)
end

#search(entity, conditions = {}, query = {}) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/learndot/api.rb', line 28

def search(entity, conditions = {}, query = {})
  endpoint       = "/manage/#{entity}/search"
  query[:asc]  ||= false
  query[:or]   ||= false

  if query.include? 'page'
    api_post(endpoint, conditions, query)
  else
    page do |count|
      query[:page] = count
      api_post(endpoint, conditions, query)
    end
  end
end

#update(entity, conditions, id) ⇒ Object

keep seperate from create to avoid accidental record creation



51
52
53
54
# File 'lib/learndot/api.rb', line 51

def update(entity, conditions, id)
  endpoint = "/manage/#{entity}/#{id}"
  api_post(endpoint, conditions)
end