Class: Curldown

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

Constant Summary collapse

@@lexers =
[]

Instance Method Summary collapse

Constructor Details

#initializeCurldown

Returns a new instance of Curldown.



8
9
10
# File 'lib/curldown.rb', line 8

def initialize
	@options = {:user => nil, :repo => nil, :branch => "master", :file => "readme.md", :url => nil}
end

Instance Method Details

#check_response_code(url) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/curldown.rb', line 27

def check_response_code(url)
  if Curl.get(url).response_code == 200
    true
  else
    false
  end
end

#fetch(url) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/curldown.rb', line 34

def fetch(url)
  if check_response_code(url)
    html = GitHub::Markdown.render_gfm(Curl.get(url).body_str)
    doc = Nokogiri::HTML(html)
    doc.css("pre").each do |pre|
      if @@lexers.include?(pre.attributes["lang"].value)
        lang = pre.attributes["lang"].value
      else
        lang = "sh"
      end
      highlight = Pygments.highlight(pre.content, :lexer => lang)
      parsed = Nokogiri::HTML(highlight).css(".highlight")
      pre.add_next_sibling(parsed)
      pre.remove
    end
    doc.css("body").children.to_html
  else
    raise "#{url} is responding with a 404 error"
  end
end

#get(*options) ⇒ Object



17
18
19
20
21
22
23
24
25
26
# File 'lib/curldown.rb', line 17

def get(*options)
	if !options.empty?
		@options.merge!(options[0])
	end
	if @options[:url]
     fetch(@options[:url])
	else
     fetch("https://raw.github.com/#{@options[:user]}/#{@options[:repo]}/#{@options[:branch]}/#{@options[:file]}")
	end
end

#optionsObject



11
12
13
# File 'lib/curldown.rb', line 11

def options
	@options	
end

#options=(options) ⇒ Object



14
15
16
# File 'lib/curldown.rb', line 14

def options=(options)
	@options.merge!(options)
end

#url_pathObject



54
55
56
57
58
59
60
# File 'lib/curldown.rb', line 54

def url_path
  if @options[:url]
    @options[:url]
  else
    "https://raw.github.com/#{@options[:user]}/#{@options[:repo]}/#{@options[:branch]}/#{@options[:file]}"
  end
end