Class: Dashbeautiful::API

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

Overview

description TODO

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, requestor: HTTParty) ⇒ API

Returns a new instance of API.



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/dashbeautiful/api.rb', line 8

def initialize(key, requestor: HTTParty)
  @key = key
  @base_url = 'https://api.meraki.com/api/v0'
  @headers = {
    'X-Cisco-Meraki-API-Key' => key,
    'Content-Type' => 'application/json'
  }
  @options = {
    headers: @headers
  }
  @requestor = requestor
end

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key.



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

def key
  @key
end

Instance Method Details

#devices(network_id) ⇒ Object



38
39
40
# File 'lib/dashbeautiful/api.rb', line 38

def devices(network_id)
  get("/networks/#{network_id}/devices").map { |h| symbolize_keys(h) }
end

#get(path, **options) ⇒ Object

Raises:



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

def get(path, **options)
  options = @options.merge(options)
  response = @requestor.get(@base_url + path, options)

  raise APIRequestError if response.code != 200

  response
end

#networks(organization_id) ⇒ Object



34
35
36
# File 'lib/dashbeautiful/api.rb', line 34

def networks(organization_id)
  get("/organizations/#{organization_id}/networks").map { |h| symbolize_keys(h) }
end

#organizationsObject



21
22
23
# File 'lib/dashbeautiful/api.rb', line 21

def organizations
  get('/organizations').map { |h| symbolize_keys(h) }
end

#put(path, valid_keys:, body:, **options) ⇒ Object

Raises:

  • (ArgumentError)


51
52
53
54
55
56
57
58
59
60
61
# File 'lib/dashbeautiful/api.rb', line 51

def put(path, valid_keys:, body:, **options)
  raise ArgumentError, "body key can only be #{valid_keys}" unless valid_keys.all? { |key| body.key? key }

  options = @options.merge(options)
  options = options.merge(body: body.to_json)
  response = @requestor.put(@base_url + path, options)

  raise APIRequestError if response.code != 200

  response
end

#update_organization(organization_id, body) ⇒ Object

Raises:

  • (ArgumentError)


25
26
27
28
29
30
31
32
# File 'lib/dashbeautiful/api.rb', line 25

def update_organization(organization_id, body)
  valid_keys = %i[name]

  raise ArgumentError, 'body must be a hash' unless body.is_a? Hash
  raise ArgumentError, 'body cannot be empty' if body.empty?

  put("/organizations/#{organization_id}", valid_keys: valid_keys, body: body)
end