Class: CodeSync::Server::SourceProvider
- Inherits:
-
Object
- Object
- CodeSync::Server::SourceProvider
- Defined in:
- lib/code_sync/server.rb
Instance Attribute Summary collapse
-
#options ⇒ Object
Returns the value of attribute options.
-
#root ⇒ Object
Returns the value of attribute root.
-
#sprockets ⇒ Object
Returns the value of attribute sprockets.
Instance Method Summary collapse
- #allow_saving? ⇒ Boolean
-
#call(env) ⇒ Object
Why am I not just using Sinatra here?.
- #handle_adhoc_compilation(params = {}, response = {}) ⇒ Object
- #handle_file_write(params = {}, response) ⇒ Object
- #handle_get(env) ⇒ Object
- #handle_post(env) ⇒ Object
-
#initialize(app, options = {}) ⇒ SourceProvider
constructor
A new instance of SourceProvider.
- #process_compiled_asset(contents, filename, extension) ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(app, options = {}) ⇒ SourceProvider
Returns a new instance of SourceProvider.
70 71 72 73 74 |
# File 'lib/code_sync/server.rb', line 70 def initialize app, ={} @options = @sprockets = [:sprockets] @root = [:root] || Dir.pwd() end |
Instance Attribute Details
#options ⇒ Object
Returns the value of attribute options.
68 69 70 |
# File 'lib/code_sync/server.rb', line 68 def @options end |
#root ⇒ Object
Returns the value of attribute root.
68 69 70 |
# File 'lib/code_sync/server.rb', line 68 def root @root end |
#sprockets ⇒ Object
Returns the value of attribute sprockets.
68 69 70 |
# File 'lib/code_sync/server.rb', line 68 def sprockets @sprockets end |
Instance Method Details
#allow_saving? ⇒ Boolean
80 81 82 |
# File 'lib/code_sync/server.rb', line 80 def allow_saving? [:allow_saving] == true end |
#call(env) ⇒ Object
Why am I not just using Sinatra here?
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/code_sync/server.rb', line 173 def call(env) response = begin if env['REQUEST_METHOD'] == "POST" handle_post(env) else handle_get(env) end rescue response[:success] = false response[:error] = "Fatal Error: #{ $! }" end if response[:success] [200, {"Access-Control-Allow-Origin"=>"*","Content-Type" => "application/json"}, [JSON.generate(response)] ] else [200, {"Access-Control-Allow-Origin"=>"*","Content-Type" => "application/json"}, [JSON.generate(response)] ] end end |
#handle_adhoc_compilation(params = {}, response = {}) ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/code_sync/server.rb', line 106 def handle_adhoc_compilation(params={}, response={}) contents, filename, extension = params.values_at("contents", "name", "extension") begin asset = TempAsset.create_from(contents, env: sprockets, filename: filename, extension: extension) response[:contents] = contents response[:compiled] = process_compiled_asset(asset.to_s,filename,extension) rescue response[:error] = $! response[:success] = false end response end |
#handle_file_write(params = {}, response) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/code_sync/server.rb', line 84 def handle_file_write(params={}, response) path, contents = params.values_at "path", "contents" unless path.match(/^\//) path = File.join(root,path) end begin raise 'Saving is disabled' unless allow_saving? if File.exists?(path) File.open(path,"w+") {|fh| fh.puts(contents)} else response[:success] = false response[:error] = "File not found: #{ path } :: #{ $! }" end rescue response[:error] = "#{ $! }" response[:success] = false end end |
#handle_get(env) ⇒ Object
158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/code_sync/server.rb', line 158 def handle_get(env) response = {success: true} query = Rack::Utils.parse_query env['QUERY_STRING'] if query['path'] response.merge! success: true, contents: IO.read(query["path"]) else response.merge! success: false, error: "Must specify an asset path" end response end |
#handle_post(env) ⇒ Object
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/code_sync/server.rb', line 130 def handle_post(env) response = {success: true} env['rack.input'].rewind body = env['rack.input'].read params = begin JSON.parse(body) rescue response[:success] = false response[:error] = $! response[:body] = body return response end if params["path"] && params["contents"] && params["path"].length > 0 handle_file_write(params, response) return response end if params["name"] && params["extension"] && params["contents"] handle_adhoc_compilation(params, response) return response end response end |
#process_compiled_asset(contents, filename, extension) ⇒ Object
121 122 123 124 125 126 127 128 |
# File 'lib/code_sync/server.rb', line 121 def process_compiled_asset contents, filename, extension begin processor = CodeSync.lookup_processor_for_extension(extension) processor.process(contents,filename,extension) rescue contents end end |
#to_s ⇒ Object
76 77 78 |
# File 'lib/code_sync/server.rb', line 76 def to_s "codesync source gateway" end |