Skip to content

Instantly share code, notes, and snippets.

@jmoe
Last active August 29, 2015 14:06
Show Gist options
  • Select an option

  • Save jmoe/2c1ac03d0f5cc02ee2be to your computer and use it in GitHub Desktop.

Select an option

Save jmoe/2c1ac03d0f5cc02ee2be to your computer and use it in GitHub Desktop.

Revisions

  1. jmoe revised this gist Sep 8, 2014. 1 changed file with 25 additions and 13 deletions.
    38 changes: 25 additions & 13 deletions fastly.rake
    Original file line number Diff line number Diff line change
    @@ -1,31 +1,43 @@
    namespace :fastly do

    desc "set fastly cors headers to fix chrome/firefox font loading issues"
    task apply_cors_header: :environment do
    task allow_cors: :environment do

    # check for the Cors Allow header

    versions = Yajl.load(Excon.get("https://api.fastly.com/service/#{ENV['FASTLY_SERVICE_ID']}/version",
    :headers => {'Fastly-Key'=>ENV['FASTLY_API_KEY']}).body)
    last_version = versions.last['number']

    puts versions
    exists = Excon.get("https://api.fastly.com/service/#{ENV['FASTLY_SERVICE_ID']}/version/#{last_version}/header/Cors%20Allow",
    :headers => {'Fastly-Key'=>ENV['FASTLY_API_KEY']}).status

    if(exists == 200)
    puts "Allow Cors already set. Done."
    else

    cloned = Yajl.load(Excon.put("https://api.fastly.com/service/#{ENV['FASTLY_SERVICE_ID']}/version/#{last_version}/clone",
    :headers => {'Fastly-Key'=>ENV['FASTLY_API_KEY']}).body)
    cloned = Yajl.load(Excon.put("https://api.fastly.com/service/#{ENV['FASTLY_SERVICE_ID']}/version/#{last_version}/clone",
    :headers => {'Fastly-Key'=>ENV['FASTLY_API_KEY']}).body)

    cloned_version = cloned['number']
    cloned_version = cloned['number']

    puts cloned
    puts cloned

    response = Yajl.load(Excon.post("https://api.fastly.com/service/#{ENV['FASTLY_SERVICE_ID']}/version/#{cloned_version}/header",
    :headers => {'Fastly-Key'=>ENV['FASTLY_API_KEY']},
    :body => 'name=Cors Allow&type=response&action=set&dst=http.Access-Control-Allow-Origin&src="*"&ignore_if_set=0&priority=10').body)
    response = Yajl.load(Excon.post("https://api.fastly.com/service/#{ENV['FASTLY_SERVICE_ID']}/version/#{cloned_version}/header",
    :headers => {'Fastly-Key'=>ENV['FASTLY_API_KEY']},
    :body => 'name=Cors Allow&type=response&action=set&dst=http.Access-Control-Allow-Origin&src="*"&ignore_if_set=0&priority=10').body)

    puts response
    puts response

    activated = Yajl.load(Excon.put("https://api.fastly.com/service/#{ENV['FASTLY_SERVICE_ID']}/version/#{cloned_version}/activate",
    :headers => {'Fastly-Key'=>ENV['FASTLY_API_KEY']}).body)
    activated = Excon.put("https://api.fastly.com/service/#{ENV['FASTLY_SERVICE_ID']}/version/#{cloned_version}/activate",
    :headers => {'Fastly-Key'=>ENV['FASTLY_API_KEY']}).status

    if(activated == 200)
    puts "Allow Cors header set. Done."
    end

    end

    puts activated
    end

    end
  2. jmoe created this gist Sep 8, 2014.
    31 changes: 31 additions & 0 deletions fastly.rake
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,31 @@
    namespace :fastly do

    desc "set fastly cors headers to fix chrome/firefox font loading issues"
    task apply_cors_header: :environment do

    versions = Yajl.load(Excon.get("https://api.fastly.com/service/#{ENV['FASTLY_SERVICE_ID']}/version",
    :headers => {'Fastly-Key'=>ENV['FASTLY_API_KEY']}).body)
    last_version = versions.last['number']

    puts versions

    cloned = Yajl.load(Excon.put("https://api.fastly.com/service/#{ENV['FASTLY_SERVICE_ID']}/version/#{last_version}/clone",
    :headers => {'Fastly-Key'=>ENV['FASTLY_API_KEY']}).body)

    cloned_version = cloned['number']

    puts cloned

    response = Yajl.load(Excon.post("https://api.fastly.com/service/#{ENV['FASTLY_SERVICE_ID']}/version/#{cloned_version}/header",
    :headers => {'Fastly-Key'=>ENV['FASTLY_API_KEY']},
    :body => 'name=Cors Allow&type=response&action=set&dst=http.Access-Control-Allow-Origin&src="*"&ignore_if_set=0&priority=10').body)

    puts response

    activated = Yajl.load(Excon.put("https://api.fastly.com/service/#{ENV['FASTLY_SERVICE_ID']}/version/#{cloned_version}/activate",
    :headers => {'Fastly-Key'=>ENV['FASTLY_API_KEY']}).body)

    puts activated
    end

    end