Class: ReadmeScore::Document::Loader
- Inherits:
-
Object
- Object
- ReadmeScore::Document::Loader
show all
- Defined in:
- lib/readme-score/document/loader.rb,
lib/readme-score/document/loader/github_readme_finder.rb
Defined Under Namespace
Classes: GithubReadmeFinder
Constant Summary
collapse
- MARKDOWN_EXTENSIONS =
%w{md mdown markdown}
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(url) ⇒ Loader
Returns a new instance of Loader.
35
36
37
|
# File 'lib/readme-score/document/loader.rb', line 35
def initialize(url)
@url = url
end
|
Instance Attribute Details
#markdown ⇒ Object
Returns the value of attribute markdown.
11
12
13
|
# File 'lib/readme-score/document/loader.rb', line 11
def markdown
@markdown
end
|
#request ⇒ Object
Returns the value of attribute request.
11
12
13
|
# File 'lib/readme-score/document/loader.rb', line 11
def request
@request
end
|
#response ⇒ Object
Returns the value of attribute response.
33
34
35
|
# File 'lib/readme-score/document/loader.rb', line 33
def response
@response
end
|
Class Method Details
.github_repo_name(url) ⇒ Object
13
14
15
16
17
18
19
|
# File 'lib/readme-score/document/loader.rb', line 13
def self.github_repo_name(url)
uri = URI.parse(url)
return nil unless ["github.com", "www.github.com"].include?(uri.host)
path_components = uri.path.split("/")
return nil if path_components.reject(&:empty?).count != 2
path_components[-2..-1].join("/")
end
|
.is_github_repo_slug?(possible_repo) ⇒ Boolean
21
22
23
|
# File 'lib/readme-score/document/loader.rb', line 21
def self.is_github_repo_slug?(possible_repo)
!!(/^(\w|-)+\/(\w|-|\.)+$/.match(possible_repo))
end
|
.is_url?(possible_url) ⇒ Boolean
25
26
27
|
# File 'lib/readme-score/document/loader.rb', line 25
def self.is_url?(possible_url)
!!(/https?:\/\//.match(possible_url))
end
|
.markdown_url?(url) ⇒ Boolean
29
30
31
|
# File 'lib/readme-score/document/loader.rb', line 29
def self.markdown_url?(url)
MARKDOWN_EXTENSIONS.select {|ext| url.downcase.end_with?(".#{ext}")}.any?
end
|
Instance Method Details
#github_repo_name ⇒ Object
39
40
41
|
# File 'lib/readme-score/document/loader.rb', line 39
def github_repo_name
Loader.github_repo_name(@url)
end
|
#html ⇒ Object
71
72
73
74
75
76
77
|
# File 'lib/readme-score/document/loader.rb', line 71
def html
if markdown?
parse_markdown(@response.body)
else
@response.body
end
end
|
#load! ⇒ Object
43
44
45
46
47
48
49
50
51
52
53
54
55
|
# File 'lib/readme-score/document/loader.rb', line 43
def load!
if github_repo_name
if ReadmeScore.use_github_api?
load_via_github_api!
else
load_via_github_approximation!
end
else
@markdown = Loader.markdown_url?(@url)
@response ||= Unirest.get @url
end
end
|
#load_via_github_api! ⇒ Object
57
58
59
60
61
62
63
|
# File 'lib/readme-score/document/loader.rb', line 57
def load_via_github_api!
@markdown = false
@response ||= OpenStruct.new.tap {|o|
@@client ||= Octokit::Client.new(access_token: ReadmeScore.github_api_token)
o.body = @@client.readme(github_repo_name, :accept => 'application/vnd.github.html').force_encoding("UTF-8")
}
end
|
#load_via_github_approximation! ⇒ Object
65
66
67
68
69
|
# File 'lib/readme-score/document/loader.rb', line 65
def load_via_github_approximation!
@github_approximation_url ||= GithubReadmeFinder.new(url).find_url
@markdown = Loader.markdown_url?(@github_approximation_url)
@response ||= Unirest.get @github_approximation_url
end
|
#markdown? ⇒ Boolean
83
84
85
|
# File 'lib/readme-score/document/loader.rb', line 83
def markdown?
@markdown == true
end
|
#parse_markdown(markdown) ⇒ Object
79
80
81
|
# File 'lib/readme-score/document/loader.rb', line 79
def parse_markdown(markdown)
Parser.new(@response.body).to_html
end
|