Class: Shiba::Output

Inherits:
Object
  • Object
show all
Defined in:
lib/shiba/output.rb

Constant Summary collapse

WEB_PATH =
File.join(File.dirname(__FILE__), "..", "..", "web")

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(queries, options = {}) ⇒ Output

Returns a new instance of Output.



14
15
16
17
# File 'lib/shiba/output.rb', line 14

def initialize(queries, options = {})
  @queries = queries
  @options = options
end

Class Method Details

.tagsObject



10
11
12
# File 'lib/shiba/output.rb', line 10

def self.tags
  @tags ||= YAML.load_file(File.join(File.dirname(__FILE__), "output", "tags.yaml"))
end

Instance Method Details

#as_jsonObject



53
54
55
56
57
58
59
# File 'lib/shiba/output.rb', line 53

def as_json
  {
    queries: @queries,
    tags: self.class.tags,
    url: remote_url
  }
end

#default_filenameObject



19
20
21
# File 'lib/shiba/output.rb', line 19

def default_filename
  @default_filename ||= "shiba_results-#{Time.now.to_i}.html"
end

#js_pathObject



37
38
39
# File 'lib/shiba/output.rb', line 37

def js_path
  File.join(output_path, "js")
end

#logdirObject



23
24
25
# File 'lib/shiba/output.rb', line 23

def logdir
  File.join(Dir.pwd, "log")
end

#make_web!Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/shiba/output.rb', line 61

def make_web!
  data = as_json

  index_path = File.join(WEB_PATH, "dist", "index.html")
  if !File.exist?(index_path)
    raise Shiba::Error.new("dist/index.html not found. Try running 'rake build_web'")
  end
  index = File.read(index_path)
  data_block = "var shibaData = #{JSON.dump(as_json)};\n"

  index.sub!(%r{<script src=(.*?)>}) do |match|
    "<script>" + data_block + File.read(File.join(WEB_PATH, "dist", $1))
  end

  File.open(output_path, "w+") do |f|
    f.write(index)
  end

  output_path
end

#output_pathObject



27
28
29
30
31
32
33
34
35
# File 'lib/shiba/output.rb', line 27

def output_path
  return @options['output'] if @options['output']
  if File.exist?(logdir)
    FileUtils.mkdir_p(File.join(logdir, "shiba_results"))
    File.join(Dir.pwd, "log", "shiba_results", default_filename)
  else
    File.join(Shiba.path, default_filename)
  end
end

#remote_urlObject



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/shiba/output.rb', line 41

def remote_url
  url = `git config --get remote.origin.url` rescue nil
  return nil unless url
  return nil if url =~ %r{burrito-brothers/shiba}
  url.chomp!
  url.gsub!('[email protected]:', 'https://github.com/')
  url.gsub!(/\.git$/, '')

  branch = `git symbolic-ref HEAD`.strip.split('/').last
  url + "/blob/#{branch}"
end