Permalink
Browse files

You can't make jsonp POST requests cross-domains (doug). So making an…

… ajax request to the server and opening the created gist in a new tab/window.
  • Loading branch information...
hamin committed Mar 16, 2012
1 parent 07f5be1 commit a4b2506ee1598ef7e569c25c6775c13272f96339
Showing with 27 additions and 18 deletions.
  1. +1 −0 Gemfile
  2. +7 −0 Gemfile.lock
  3. +5 −17 app/assets/javascripts/riddle.js.coffee
  4. +11 −1 app/controllers/riddles_controller.rb
  5. +3 −0 config/routes.rb
View
@@ -10,6 +10,7 @@ gem "compass-rails", ">=0.12"
gem "paper_trail", '~> 2'
gem "has_tokens", "1.0.1"
gem "airbrake"
+gem "activegist"
group :assets do
gem 'sass-rails', '~> 3.2.3'
View
@@ -23,6 +23,10 @@ GEM
rack-cache (~> 1.1)
rack-test (~> 0.6.1)
sprockets (~> 2.1.2)
+ activegist (0.6.3)
+ activemodel (~> 3.1)
+ activesupport (~> 3.1)
+ rest-client (~> 1.6.7)
activemodel (3.2.2)
activesupport (= 3.2.2)
builder (~> 3.0.0)
@@ -112,6 +116,8 @@ GEM
rake (0.9.2.2)
rdoc (3.12)
json (~> 1.4)
+ rest-client (1.6.7)
+ mime-types (>= 1.16)
rspec (2.8.0)
rspec-core (~> 2.8.0)
rspec-expectations (~> 2.8.0)
@@ -156,6 +162,7 @@ PLATFORMS
ruby
DEPENDENCIES
+ activegist
airbrake
coffee-rails (~> 3.2.1)
compass-rails (>= 0.12)
@@ -15,30 +15,18 @@ $ ->
$("#create_gist").on "click", ->
- gistData = {
- "public": true,
- "files": {
- "rubyfiddle_riddle.rb": {
- "content": editor.getValue()
- }
- }
- }
-
+ gistData = {"gist_content": editor.getValue() }
$.ajax
type: 'POST',
- url: "https://api.github.com/gists",
- crossDomain: true,
- username: '',
- password: '',
- data: gistData,
+ url: "/riddles/gistify",
+ data: JSON.stringify(gistData),
contentType: "application/json",
- dataType: "jsonp",
+ dataType: "json",
success: (data)->
- console.log "CAME TO SUCCESS"
console.log data
+ window.open data.gist_url
error: (data)->
- console.log "CAME TO ERROR"
console.log data
$("#save_riddle").on "click", ->
@@ -1,6 +1,6 @@
class RiddlesController < ApplicationController
respond_to :html, :js
- rescue_from ActionController::RoutingError, with: :not_found
+ rescue_from ActionController::RoutingError, :with => :not_found
def index
@riddle = Riddle.new
@@ -30,6 +30,16 @@ def show
render :index
end
end
+
+ def gistify
+ gist = ActiveGist.new(:public => true, :description => "testing activegist", :files => {'foo.rb' =>{:content => params[:gist_content] }})
+
+ if gist.save
+ render :json => {:gist_url => gist.html_url}, :status => :ok
+ else
+ render :json => {:error => "Unable to create"}, :status => :error
+ end
+ end
def not_found
render :file => "#{Rails.root}/public/404.html", :status => :not_found
View
@@ -1,6 +1,9 @@
Rubyfiddle::Application.routes.draw do
root to: "riddles#index"
resources :riddles do
+ collection do
+ post :gistify
+ end
member do
put :fork
end

0 comments on commit a4b2506

Please sign in to comment.