Skip to content

Commit 2c47375

Browse files
committed
added call to web service for dissassembly
1 parent 8652a13 commit 2c47375

File tree

10 files changed

+96
-28
lines changed

10 files changed

+96
-28
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@
44
.ruby-version
55
.ruby-gemset
66
coverage
7+
.byebug_history
8+
.env

Gemfile

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,14 @@ gem 'puma'
55
gem 'json'
66
gem 'rake'
77
gem 'coveralls', require: false
8+
gem 'faraday'
89

910
group :test do
1011
gem 'rack-test'
11-
gem 'minitest'
12+
gem 'minitest'
13+
end
14+
15+
group :development, :test do
16+
gem 'dotenv'
17+
gem 'pry'
1218
end

Gemfile.lock

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,25 @@
11
GEM
22
remote: https://rubygems.org/
33
specs:
4+
coderay (1.1.1)
45
coveralls (0.8.13)
56
json (~> 1.8)
67
simplecov (~> 0.11.0)
78
term-ansicolor (~> 1.3)
89
thor (~> 0.19.1)
910
tins (~> 1.6.0)
1011
docile (1.1.5)
12+
dotenv (2.1.1)
13+
faraday (0.10.0)
14+
multipart-post (>= 1.2, < 3)
1115
json (1.8.3)
16+
method_source (0.8.2)
1217
minitest (5.9.0)
18+
multipart-post (2.0.0)
19+
pry (0.10.4)
20+
coderay (~> 1.1.0)
21+
method_source (~> 0.8.1)
22+
slop (~> 3.4)
1323
puma (3.4.0)
1424
rack (1.6.4)
1525
rack-protection (1.5.3)
@@ -26,6 +36,7 @@ GEM
2636
rack (~> 1.5)
2737
rack-protection (~> 1.4)
2838
tilt (>= 1.3, < 3)
39+
slop (3.6.0)
2940
term-ansicolor (1.3.2)
3041
tins (~> 1.0)
3142
thor (0.19.1)
@@ -37,8 +48,11 @@ PLATFORMS
3748

3849
DEPENDENCIES
3950
coveralls
51+
dotenv
52+
faraday
4053
json
4154
minitest
55+
pry
4256
puma
4357
rack-test
4458
rake

models/disassembler.rb

Lines changed: 0 additions & 14 deletions
This file was deleted.

models/disassembler_service.rb

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
require 'faraday'
2+
require 'json'
3+
4+
class DisassemblerService
5+
6+
def self.disassemble(version, src)
7+
case version
8+
when 'ruby_21'
9+
url = ENV['RUBY21_API']
10+
when 'ruby_22'
11+
url = ENV['RUBY22_API']
12+
when 'ruby_23'
13+
url = ENV['RUBY23_API']
14+
else
15+
url = nil
16+
end
17+
18+
if url.nil?
19+
puts "Unknown ruby target: #{version}"
20+
return "Unknown ruby target"
21+
else
22+
puts "Hitting #{url} for #{version}"
23+
end
24+
25+
begin
26+
conn = Faraday.new(url: url)
27+
resp = JSON.parse(conn.post('/', { code: src }).body)
28+
return resp['result'] if resp['result']
29+
return resp['errors'] if resp['errors']
30+
rescue StandardError => e
31+
puts e.message
32+
"Unknown error occurred"
33+
end
34+
end
35+
36+
end

public/javascripts/main.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,13 @@ function Codebytes() {
77
outputMirror.setValue("Running...");
88

99
var rawText = inputMirror.getValue();
10-
if (!rawText) return;
11-
var jsonText = JSON.stringify({"code":rawText});
10+
var version = $('#rubyVersion').val();
11+
if (!rawText || !version) {
12+
outputMirror.setValue("Please make sure code and version are not blank.");
13+
return;
14+
}
15+
16+
var jsonText = JSON.stringify({"code":rawText, "version":version});
1217
$.ajax({
1318
type: 'POST',
1419
url: '/',
@@ -27,7 +32,7 @@ function Codebytes() {
2732
}
2833
});
2934
};
30-
35+
3136
this.onReady= function() {
3237
inputMirror = CodeMirror.fromTextArea($("#inputTextArea").get(0), {lineNumbers:true});
3338
outputMirror = CodeMirror.fromTextArea($("#outputTextArea").get(0), {lineNumbers:true, readOnly :true});

public/stylesheets/main.css

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,23 @@ html {
1616
}
1717

1818
#container {
19-
padding:10px;
19+
padding: 10px;
2020
}
2121

2222
.page-header {
23-
margin: 5px 0 50px;
23+
margin: 5px 0 20px;
2424
}
2525

2626
.console-container {
2727
width: 45%;
2828
float: left;
29-
margin: 0 2%;
29+
margin: 0 2%;
30+
padding-bottom: 2em;
31+
}
32+
33+
.console-container-full {
34+
margin: 0 2%;
35+
width: 45%;
3036
}
3137

3238
.console {

spec/main_spec.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,16 @@
44
require 'coveralls'
55
Coveralls.wear!
66

7-
describe "my example spec" do
7+
describe "my example spec" do
88
it "should successfully return the main index page" do
99
get '/'
1010
last_response.body.must_include 'Ruby Dissassembler'
1111
end
1212

1313
it "should be able to post some code to disassemble" do
1414
code = "puts 'hi'"
15-
post('/', "{\"code\": \"#{code}\"}", { "CONTENT_TYPE" => "application/json" })
15+
version = "ruby_21"
16+
post('/', "{\"code\": \"#{code}\", \"version\": \"#{version}\"}", { "CONTENT_TYPE" => "application/json" })
1617
last_response.body.must_include 'disasm'
1718
end
18-
end
19+
end

views/index.erb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,16 @@
1212
</div>
1313

1414
<div id="main">
15+
<div class="console-container-full">
16+
<div class="form-group">
17+
<label for="rubyVersion" class="control-label">Ruby Version</label>
18+
<select class="form-control" id="rubyVersion">
19+
<option value="ruby_21">2.1</option>
20+
<option value="ruby_22">2.2</option>
21+
<option value="ruby_23">2.3</option>
22+
</select>
23+
</div>
24+
</div>
1525
<div class="console-container">
1626
Ruby Code
1727
<div class="console">

web.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
1+
require 'dotenv'
2+
Dotenv.load
3+
14
require 'sinatra'
25
require 'json'
3-
require_relative 'models/disassembler'
6+
require_relative 'models/disassembler_service'
47

58
get '/' do
69
erb :index
710
end
811

912
post '/?' do
1013
begin
11-
json = JSON.parse request.body.read
12-
code = json['code']
13-
@diss = Disassembler.disassemble code
14+
json = JSON.parse request.body.read
15+
@diss = DisassemblerService.disassemble json['version'], json['code']
1416
rescue => e
1517
logger.error "Error encountered while trying to dissassemble code"
1618
logger.error e

0 commit comments

Comments
 (0)