1. At the very beginning, install maxcdn library:

            gem install maxcdn
            
  2. First logical step would be to fetch the account info so, we will create a .rb script that hold API call for this:

            nano account-info.rb
            
  3. and define API key/secret parameters along with including some libraries we might need later on:

            require 'maxcdn'
            $api = MaxCDN::Client.new("alias", "key", "secret")
            
  4. To actually call API and get account info in JSON format we’ll use the GET method against /account.json:
    Methods: GET.

            puts $api.get('/account.json')
            
  5. Now, this will return raw json which we want to parse so, let’s try this:

            data = api.get('/account.json')
            puts "Created: ",(data['data']['account']['date_created']), "\n"
            puts "Updated: ",(data['data']['account']['date_updated']), "\n"
            puts "Edge Rules Status: ",(data['data']['account']['enable_rules']), "\n"
            puts "Edge Rules Credits: ",(data['data']['account']['edgerules_credits']), "\n"
            puts "Dev Tools Status: ",(data['data']['account']['enable_devtools']), "\n"
            puts "Company Name: ",(data['data']['account']['name']), "\n"
            puts "SSL Credits: ",(data['data']['account']['ssl_credits']), "\n"
            puts "Zone Credits: ",(data['data']['account']['zone_credits']), "\n"
            puts "Company Alias: ",(data['data']['account']['alias']), "\n"
            puts "Flex Credits: ",(data['data']['account']['flex_credits']), "\n"
            
  6. Output:

            Created:  2012-07-11 19:53:43
            Updated:  2014-02-09 15:13:21
            Edge Rules Status 1
            Edge Rules Credits 1
            Company Name:  TNT
            SSL Credits:  1
            Zone Credits:  3
            Company Alias:  ALIAS
            Flex Credits: 1
            

Managing Your Account

  1. Purge
    Methods: PURGE, DELETE.

            #function to purge single file:
            def file()
                puts "Zone Id: \n"
                zoneid = gets
                puts "File path to purge (starting with slash '/file.ext'): \n"
                file = gets
                puts $api.purge(zoneid.chomp, file.chomp)
                puts "Hit [ENTER] to get in or type 'exit' to leave\n"
            end
    
            #function to purge all
            def all()
                puts "Zone Id: \n"
                    zoneid = gets
                puts $api.delete('/zones/pull.json/' + zoneid.chomp + '/cache')
                print "Hit [ENTER] to get in or type 'exit' to leave \n"
            end
    
            #function that takes a path to txt file with list of all files you want to purge
            def list()
                ar = Array.new
                puts "Zone Id: \n"
                zoneid = gets
                puts "File (/list-with-files-to-purge.txt): \n"
                localfile = gets
                f = File.open(localfile.chomp, "r")
                f.each_line do |line|
                puts line
                ar.push line
                end
                f.close
                $api.purge(zoneid.chomp, ar)
                puts "Hit [ENTER] to get in or type 'exit' to leave\n"
            end
            puts "Purge method: \n"
                    puts "all/file/list ?\n"
                    method = gets
                    if method == "all\n"
                            all()
                    end
                    if method == "file\n"
                            file()
                    end
                    if method == "list\n"
                            list()
                    end
            
  2. List zones
    Methods: GET.

            #define function
            def list(ztype)
                zonen = $data['data']['summary'][ztype]
                    zones = $api.get('/zones/' + ztype + '.json')
                    zonedec = (zones['data'][ztype + 'zones'])
                    for i in 0..zonen
                        puts "------| Zone |------\n"
                        puts "Zone ID: ",(zonedec[i]['id']), "\n"
                        puts "Zone Name: ",(zonedec[i]['name']), "\n"
                        if ztype == "pull"
                            puts "Origin URL: ",(zonedec[i]['url']), "\n"
                    end
                        puts "Zone Label: ",(zonedec[i]['label']), "\n"
                end
            end
            puts "Zone type (pull, push,...): \n"
            ztype = gets
            list(ztype.chomp)
            
  3. Create zones
    Methods: POST.

            puts "Zone Name: \n"
            name = gets
            puts "Origin URL (Starting with http://): \n"
            origin = gets
            puts "Label: \n"
            label = gets
            puts "Use GZIP ? (1 or 0): \n"
            gzip = gets
            data = api.post("/zones/pull.json", {'name'=> name.chomp, 'url'=> origin.chomp, 'compress'=> gzip.chomp})
            puts "Created (property name 'creation_date'): ",(data['data']['pullzone']['creation_date'].to_s), "\n"
            puts "Zone Name (property name 'name'): ",(data['data']['pullzone']['name']), "\n"
            puts "CDN URL (propery name 'tmp_url'): ",(data['data']['pullzone']['tmp_url']), "\n"
            puts "Done!\n"
            
  4. Edit zones
    Methods: PUT.

            while gets != "exit"
                def mng(zone_id,zone_type)
                            puts "Type in property name (from above list) you want to change or 'exit' to leave: \n"
                            pname = gets
                    if pname == "exit\n"
                        exit()
                    end
                            puts "New value for " + pname.chomp + ": \n"
                            value = gets
                            $api.put('/zones/' + zone_type.chomp + '.json/' +  zone_id.chomp, {pname.chomp => value.chomp})
                    view(zone_id.chomp,zone_type.chomp)
                end
                def view(zone_id,zone_type)
                    data = $api.get('/zones/' + zone_type.chomp + '.json/' + zone_id.chomp)
                    puts "------| Zone Details |------\n"
                    type = " "
                    if zone_type == "pull"
                        type = "pullzone"
                    end
                    if zone_type == "push"
                        type = "pushzone"
                    end
                    puts "Created (property name 'creation_date'): ",(data['data'][type]['creation_date'].to_s), "\n"
                    puts "Zone Name (property name 'name'): ",(data['data'][type]['name']), "\n"
                    puts "CDN URL (propery name 'tmp_url'): ",(data['data'][type]['tmp_url']), "\n"
                    puts "Alias: ",(data['data'][type]['username']), "\n"
                    if zone_type != "live" or zone_type != "vod"
                        puts "GZIP Status (property name 'compress'): ",(data['data'][type]['compress']), "\n"
                        puts "Shared SSL Status (property name 'sslshared'): ",(data['data'][type]['sslshared']), "\n"
                        puts "Force Files to Download (property name 'content_disposition'): ",(data['data'][type]['content_disposition']), "\n"
                    end
                    if zone_type == "pull"
                        puts "Strip Cookies Status (property name 'ignore_setcookie_header'): ",(data['data'][type]['ignore_setcookie_header']), "\n"
                        puts "Default Cache Time (property name 'cache_valid'): ",(data['data'][type]['cache_valid']), "\n"
                        puts "Ignore CCH Status (property name 'ignore_cache_control'): ",(data['data'][type]['ignore_cache_control']), "\n"
                        puts "Origin (property name 'url'): ",(data['data'][type]['url']), "\n"
                    end
                    mng(zone_id,zone_type)
                end
                puts "Zone type (pull, push,...): \n"
                    zone_type = gets
                    puts "Zone ID: \n"
                    zone_id = gets
                view(zone_id.chomp,zone_type.chomp)
    
            end
            
  5. Manage Users
    Methods: DELETE, POST, GET.

            while gets != "exit"
                def mng(action)
                    if action == "view"
                        view()
                    end
                    if action == "delete"
                        puts "User Id: \n"
                                userid = gets
                        $api.delete('/users.json/' + userid.chomp)
                        view()
                    end
                    if action == "add"
                        puts "Email: \n"
                                email = gets
                                puts "Password: \n"
                                password = gets
                                puts "First Name: \n"
                                fname = gets
                                puts "Last Name: \n"
                                lname = gets
                                userparams = {'email'=> email.chomp,'password'=> password.chomp,'firstname'=> fname.chomp,'lastname'=> lname.chomp}
                                $api.post('/users.json', userparams)
                        view()
                    end
                end
                def view()
                    data = $api.get('/users.json')
                    usersn = data['data']['users'].length
                    data = data['data']['users']
                    for i in 0..usersn
                        puts "------| User |------"
                        puts "Created: ",(data[i]['date_created'])
                        puts "First Name: ",(data[i]['firstname'])
                        puts "Last Name: ",(data[i]['lastname'])
                        puts "User Id: ",(data[i]['id'])
                        puts "Email: ",(data[i]['email'])
                        puts "Last Login: ",(data[i]['date_last_login'])
                        puts "Last Login from IP: ",(data[i]['ip_last_login'])
                    end
                    puts "Type 'exit' to enter main menu or hit [enter] to return to options\n"
                end
                puts "view/add/delete user(s) ?\n"
                    action = gets
                mng(action.chomp)
            end
            
  6. Manage Custom Domains
    Methods: GET, POST, DELETE.

            while gets != "exit" do
                def mng(action)
                    if action == "view"
                        view("","")
                    end
                    if action == "delete"
                        puts "Zone Id\n: "
                        zoneid = gets
                        puts "Zone Type (pull, push,...): \n"
                        type = gets
                        puts "Custom Domain Id: \n"
                        cdid = gets
                        puts $api.delete('/zones/' + type.chomp + '/' + zoneid.chomp + '/customdomains.json/' + cdid.chomp)
                        view(zoneid,type)
                    end
                    if action == "add"
                        puts "Zone Id: \n"
                                zoneid = gets
                                puts "Zone Type (pull, push,...): \n"
                                type = gets
                                puts "Custom Domain To Add: \n"
                        cdom = gets
                                params = {"custom_domain"=> cdom.chomp}
                                puts $api.post('/zones/' + type.chomp + '/' + zoneid.chomp + '/customdomains.json', params)
                        view(zoneid,type)
                    end
                end
                def view(zoneid,type)
                    if zoneid == "" or type == ""
                        puts "Zone Id: \n"
                        zoneid = gets
                        puts "Zone Type (pull, push,...): \n"
                        type = gets
                    end
                    data = $api.get('/zones/' + type.chomp + '/' + zoneid.chomp + '/customdomains.json')
                    puts data
                    cdomn = data['data']['customdomains'].length
                    data = (data['data']['customdomains'])
                    for i in (0..cdomn)
                        puts "------| Custom Domain |------\n"
                        puts "Custom Domain Id: ",(data[i]['id']), "\n"
                        puts "Custom Domain Name: ",(data[i]['custom_domain']), "\n"
                    end
                    puts "Type 'exit' to enter main menu or hit [enter] to return to options\n"
                end
                puts "view/add/delete custom domains ?\n"
                action = gets
                    mng(action.chomp)
            end
            
  7. Reporting
    Methods: GET.

            while gets != "exit\n"
                def popular(zoneid,ztype)
                    if zoneid != ""
                        zoneid = "/" + zoneid
                    end
                    if ztype != ""
                        ztype = "/" + ztype
                    end
                    puts $api.get('/reports' + ztype + zoneid + '/popularfiles.json')
                    data = $api.get('/reports' + ztype + zoneid + '/popularfiles.json')
                    text_file = open("reports/popular.json", "w")
                    text_file.write(data.to_s)
                    text_file.close()
                    puts "Report saved in reports/popular.json"
                end
                def filetypes(zoneid,ztype)
                    puts "Report type (hourly/daily/monthly - hit [ENTER] to show total usage): "
                    rtype = gets
                    if rtype != "\n"
                        rtype = "/" + rtype.chomp
                    end
                    if zoneid != "\n"
                        zoneid = "/" + zoneid.chomp
                    end
                    if ztype != "\n"
                        ztype = "/" + ztype.chomp
                    end
                    puts $api.get('/reports' + ztype + zoneid + '/filetypes.json' + rtype.chomp)
                    data = $api.get('/reports' + ztype + zoneid + '/filetypes.json' + rtype.chomp)
                    text_file = open("reports/filetypes.json", "w")
                            text_file.write(data.to_s)
                            text_file.close()
                            puts "Report saved in reports/filetypes.json\n"
                end
                def statuscodes(zoneid,ztype)
                    puts "Report type (hourly/daily/monthly - hit [ENTER] to show total usage): \n"
                    rtype = gets
                    if rtype != "\n"
                        rtype = "/" + rtype.chomp
                    end
                    if zoneid != "\n"
                        zoneid = "/" + zoneid.chomp
                    end
                    if ztype != "\n"
                        ztype = "/" + ztype.chomp
                    end
                    puts $api.get('/reports' + ztype + zoneid + '/statuscodes.json' + rtype.chomp)
                    data = $api.get('/reports' + ztype + zoneid + '/statuscodes.json' + rtype.chomp)
                    text_file = open("reports/statuscodes.json", "w")
                            text_file.write(data.to_s)
                            text_file.close()
                            puts "Report saved in reports/statuscodes.json\n"
                end
                def nodes(zoneid,nodeid)
                    puts "Report type (hourly/daily/monthly - hit [ENTER] to show total usage): \n"
                    rtype = gets
                    if rtype != "\n"
                        rtype = "/stats/" + rtype.chomp
                    end
                    if zoneid != ""
                        zoneid = zoneid.chomp + "/"
                    end
                    if nodeid != ""
                        nodeid = "/" + nodeid.chomp
                    end
                    puts $api.get('/reports/' + zoneid.chomp + 'nodes.json' + rtype.chomp)
                    data = $api.get('/reports/' + zoneid.chomp + 'nodes.json' + rtype.chomp)
                    text_file = open("reports/nodes.json", "w")
                            text_file.write(data.to_s)
                            text_file.close()
                            puts "Report saved in reports/nodes.json\n"
                end
                def summary(param,type)
                    puts "Report type (hourly/daily/monthly - hit [ENTER] to show total usage): \n"
                    rtype = gets
                    sep = "/"
                    zoneid = ""
                    if param != ""
                        zoneid = param + "/"
                    end
                    if rtype == "\n"
                        sep = ""
                    end
                    if type == ""
                        type = "stats"
                    end
                    puts $api.get('/reports/' + zoneid + 'stats.json' + sep + rtype.chomp)
                    data = $api.get('/reports/'+ zoneid + 'stats.json'+ sep + rtype.chomp)
                    text_file = open("reports/summary.json", "w")
                            text_file.write(data.to_s)
                            text_file.close()
                            puts "Report saved in reports/summary.json\n"
                    newdata = data['data']['stats']
                            lines = data['data']['stats'].length
                            puts lines
                            sum1 = ""
                            sum2 = ""
                            for i in 0..lines
                                    time = newdata[i]['timestamp']
                                    hits = newdata[i]['cache_hit']
                                    misses = newdata[i]['noncache_hit']
                                    reqs = newdata[i]['hit']
                                    traffic = newdata[i]['size']
                                    sum1 += "[\'" + time + "\'," + traffic + "],"
                                    sum2 += "[\'" + time + "\'," + hits + ","+ misses +","+ reqs +"],"
                    end
                            puts sum1
                            puts sum2
                            hitsum = data['data']['summary']['cache_hit']
                            misssum = data['data']['summary']['noncache_hit']
                            hitmissrate = "['Hits'," +  hitsum + "],['Misses'," + misssum + "]"
                end
                puts "--------------------------\nReports options\n--------------------------\n1. summary\n2. stats per zone\n3. node distribution\n4. status codes\n5. file types\n6. popular files\n"
                puts "Type in the report type you need: \n"
                type = gets
                if type == "summary\n"
                    summary("","")
                end
                if type == "stats per zone\n"
                    puts "Zone Id: \n"
                    zoneid = gets
                    summary(zoneid.chomp,"")
                end
                if type == "node distribution\n"
                    puts "Zone Id (Hit [enter] for total usage): \n"
                    zoneid = gets
                    puts "Node Id (Hit [enter] for all): \n"
                    nodeid = gets
                    nodes(zoneid.chomp,nodeid.chomp)
                end
                if type == "status codes\n"
                    puts "Zone Id (Hit [enter] for total usage): \n"
                    zoneid = gets
                    puts "Zone type (Hit [enter] for all types): "
                    ztype = gets
                    statuscodes(zoneid.chomp,ztype.chomp)
                end
                if type == "file types\n"
                    puts "Zone Id (Hit [enter] for total usage): \n"
                    zoneid = gets
                    puts "Zone type (Hit [enter] for all types): \n"
                    ztype = gets
                    filetypes(zoneid.chomp,ztype.chomp)
                end
                if type == "popular files\n"
                    puts "Zone Id (Hit [enter] for all zones): \n"
                    zoneid = gets
                    puts "Zone type (Hit [enter] for all types): \n"
                    ztype = gets
                    popular(zoneid.chomp,ztype.chomp)
                end
                puts "Hit [enter] to return to menu or type 'exit' to go back\n"
            end
            
  8. Raw Logs
    Methods: GET.

    puts "Hit [ENTER] to get it"
    while gets != "exit\n"
            def fetch(dfrom, dto, option, value, zid)
                    if option != ""
                            option = "&" + option + "=" + value
                    end
                    if zid != ""
                            zid = "&zone_id=" + zid
                    end
                    puts option
                    puts zid
                    puts value
                    data = $api.get('/v3/reporting/logs.json?start=' + dfrom + '&end=' + dto + option)
                    records = data['records']
                    lines = records.length
                    for i in 0..lines
                            puts "\nZone ID: "
                            puts records[i]['zone_id']
                            puts "Source IP: "
                            puts records[i]['client_ip']
                            puts "Uri: "
                            puts records[i]['uri']
                            puts "Referrer: "
                            puts records[i]['referer']
                    end
            end
            puts "--------------------------\nFilter Options - type the number in front of an option:\n--------------------------\n1. summary\n2. status codes\n3. referrers\n4. host names\n5. URi\n6. source ip\n"
            puts "Type in the report type you need: "
            type = gets
            if type == "1\n"
                    puts "FROM: (yyyy-mm-dd)\n"
                    dfrom = gets
                    puts "TO: (yyyy-mm-dd)\n"
                    dto = gets
                    puts "Zone ID: (Empty for all)\n"
                    zid = gets
                    fetch(dfrom.chomp, dto.chomp, "", "", zid.chomp)
            end
            if type == "2\n"
                    puts "FROM: (yyyy-mm-dd)\n"
                    dfrom = gets
                    puts "TO: (yyyy-mm-dd)\n"
                    dto = gets
                    puts "Zone ID: (Empty for all)\n"
                    zid = gets
                    puts "Status Code: \n"
                    scode = gets
                    fetch(dfrom.chomp, dto.chomp, "status", scode.chomp, zid.chomp)
            end
            if type == "3\n"
                    puts "FROM: (yyyy-mm-dd)\n"
                    dfrom = gets
                    puts "TO: (yyyy-mm-dd)\n"
                    dto = gets
                    puts "Zone ID: (Empty for all)\n"
                    zid = gets
                    puts "Referrer: \n"
                    ref = gets
                    fetch(dfrom.chomp, dto.chomp, "referer", ref.chomp, zid.chomp)
            end
            if type == "4\n"
                    puts "FROM: (yyyy-mm-dd)\n"
                    dfrom = gets
                    puts "TO: (yyyy-mm-dd)\n"
                    dto = gets
                    puts "Zone ID: (Empty for all)\n"
                    zid = gets
                    puts "Host Name: \n"
                    hname = gets
                    fetch(dfrom.chomp, dto.chomp, "hostname", hname.chomp, zid.chomp)
            end
            if type == "5\n"
                    puts "FROM: (yyyy-mm-dd)\n"
                    dfrom = gets
                    puts "TO: (yyyy-mm-dd)\n"
                    dto = gets
                    puts "Zone ID: (Empty for all)\n"
                    zid = gets
                    puts "URi: \n"
                    uri = gets
                    fetch(dfrom.chomp, dto.chomp, "uri", uri.chomp, zid.chomp)
            end
            if type == "6\n"
                    puts "FROM: (yyyy-mm-dd)\n"
                    dfrom = gets
                    puts "TO: (yyyy-mm-dd)\n"
                    dto = gets
                    puts "Zone ID: (Empty for all)\n"
                    zid = gets
                    puts "Source IP: \n"
                    cip = gets
                    fetch(dfrom.chomp, dto.chomp, "client_ip", cip.chomp, zid.chomp)
            end
            puts "Hit [enter] to return to menu or type 'exit' to go back"
    end
    

Important Notes

  • If your script has defined function you will need to define any variable as a global variable in order to pass the value from outside to within function thread.
  • When accepting input from console, along with user input new line “\n” is passed as well so, to remove it you will use chomp along with accepted variable.
  • Guest

    Test