Class: DashboardAPI

Inherits:
Object
  • Object
show all
Includes:
Clients, DashboardAPIVersion, Devices, HTTParty, Networks, Organizations, SSIDs
Defined in:
lib/dashboard-api.rb

Overview

Author:

  • Joe Letizia

Constant Summary

Constants included from DashboardAPIVersion

DashboardAPIVersion::VERSION

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from SSIDs

#get_single_ssid, #list_ssids_in_network, #update_single_ssid

Methods included from Devices

#claim_device_into_network, #get_device_uplink_stats, #get_single_device, #list_devices_in_network, #remove_device_from_network, #update_device_attributes

Methods included from Clients

#get_client_info_for_device

Methods included from Networks

#create_network, #delete_network, #get_auto_vpn_settings, #get_ms_access_policies, #get_networks, #get_single_network, #update_auto_vpn_settings, #update_network

Methods included from Organizations

#get_inventory, #get_license_state, #get_organization, #get_snmp_status, #get_third_party_peers

Constructor Details

#initialize(key) ⇒ DashboardAPI

Returns a new instance of DashboardAPI.



23
24
25
# File 'lib/dashboard-api.rb', line 23

def initialize(key)
  @key = key
end

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key.



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

def key
  @key
end

Instance Method Details

#make_api_call(endpoint_url, http_method, options_hash = {}) ⇒ Object

TODO:

Eventually this will need to support POST, PUT and DELETE. It also needs to be a bit more resillient, instead of relying on HTTParty for exception handling

Inner function, not to be called directly



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/dashboard-api.rb', line 32

def make_api_call(endpoint_url, http_method, options_hash={})
  headers = {"X-Cisco-Meraki-API-Key" => @key, 'Content-Type' => 'application/json'}
  headers.merge!(options_hash[:headers]) if options_hash[:headers]

  options = {:headers => headers, :body => options_hash[:body].to_json}
  case http_method
  when 'GET'
    res = HTTParty.get("#{self.class.base_uri}/#{endpoint_url}", options)
    raise "404 returned. Are you sure you are using the proper IDs?" if res.code == 404
    return JSON.parse(res.body)
  when 'POST'
    res = HTTParty.post("#{self.class.base_uri}/#{endpoint_url}", options)
    raise "Bad Request due to the following error(s): #{res['errors']}" if res['errors']
    raise "404 returned. Are you sure you are using the proper IDs?" if res.code == 404
    begin
      return JSON.parse(res.body)
    rescue JSON::ParserError => e
      return res.code 
    rescue TypeError => e
      return res.code
    end
  when 'PUT'
    res = HTTParty.put("#{self.class.base_uri}/#{endpoint_url}", options)
    raise "Bad Request due to the following error(s): #{res['errors']}" if res['errors']
    raise "404 returned. Are you sure you are using the proper IDs?" if res.code == 404
    return JSON.parse(res.body)
  when 'DELETE'
    res = HTTParty.delete("#{self.class.base_uri}/#{endpoint_url}", options)
    raise "Bad Request due to the following error(s): #{res['errors']}" if res['errors']
    raise "404 returned. Are you sure you are using the proper IDs?" if res.code == 404
    return res
  else
    raise 'Invalid HTTP Method. Only GET, POST, PUT and DELETE are supported.'
  end
end