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



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# 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
  debug   ||= false
  staging ||= false

  # Set the base_url to the staging or production endpoint
  case staging
  when 'production', false
    @base_url = "https://learn.puppet.com/api/rest/v2"
  when 'staging', true
    @base_url = "https://puppetlabs-staging.trainingrocket.com/api/rest/v2"
  when 'sandbox'
    @base_url = "https://puppetlabs-sandbox.trainingrocket.com/api/rest/v2"
  end

  @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



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

def logger=(value)
  @logger = value
end

Instance Method Details

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



52
53
54
55
56
57
# File 'lib/learndot/api.rb', line 52

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



65
66
67
68
# File 'lib/learndot/api.rb', line 65

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

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



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/learndot/api.rb', line 37

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



60
61
62
63
# File 'lib/learndot/api.rb', line 60

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