Class: JsonResume::Reader

Inherits:
Object show all
Defined in:
lib/json_resume/reader.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(json_input, options) ⇒ Reader

Returns a new instance of Reader.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/json_resume/reader.rb', line 12

def initialize(json_input, options)
  output_type = options[:output_type] || 'html' # default html, others latex, md
  @json_string = case json_input
                 when /^(http|https|www)/ then RestClient.get(json_input)
                 when /\.json$/i then File.read(json_input)
                 when /\.ya?ml$/i then JSON.dump(YAML.load_file(json_input))
                 else json_input
       end
  @output_type = output_type
  begin
    @hash = JSON.parse(@json_string)
  rescue JSON::ParserError => e
    raise Exception, 'Either you entered a file without .json extension or JSON string is wrong: ' + e.message
  end
end

Instance Attribute Details

#hashObject

Returns the value of attribute hash.



10
11
12
# File 'lib/json_resume/reader.rb', line 10

def hash
  @hash
end

Instance Method Details

#format!Object



28
29
30
31
32
33
34
35
36
# File 'lib/json_resume/reader.rb', line 28

def format!
  formatters = {
    latex: JsonResume::FormatterLatex,
    html: JsonResume::FormatterHtml,
    markdown: JsonResume::FormatterMd
  }
  type = @output_type.to_sym
  @hash = formatters[type].new(@hash).format.hash
end