Class: DashboardAPI

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

Overview

Ruby Implementation of the Meraki Dashboard api

Author:

  • Joe Letizia

Constant Summary

Constants included from DashboardAPIVersion

DashboardAPIVersion::VERSION

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Switchports

#get_single_switch_port, #get_switch_ports, #update_switchport

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



26
27
28
# File 'lib/dashboard-api.rb', line 26

def initialize(key)
  @key = key
end

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key.



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

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



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
67
68
69
# File 'lib/dashboard-api.rb', line 35

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