class ReadmeScore::Document::Loader

Constants

MARKDOWN_EXTENSIONS

Attributes

markdown[RW]
request[RW]
response[RW]

Public Class Methods

github_repo_name(url) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/readme-score/document/loader.rb, line 25
def self.is_url?(possible_url)
  !!(/https?:\/\//.match(possible_url))
end
markdown_url?(url) click to toggle source
# 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
new(url) click to toggle source
# File lib/readme-score/document/loader.rb, line 35
def initialize(url)
  @url = url
end

Public Instance Methods

github_repo_name() click to toggle source
# File lib/readme-score/document/loader.rb, line 39
def github_repo_name
  Loader.github_repo_name(@url)
end
html() click to toggle source
# File lib/readme-score/document/loader.rb, line 71
def html
  if markdown?
    parse_markdown(@response.body)
  else
    @response.body
  end
end
load!() click to toggle source
# 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
      # take a guess at the raw file name
      load_via_github_approximation!
    end
  else
    @markdown = Loader.markdown_url?(@url)
    @response ||= Unirest.get @url
  end
end
load_via_github_api!() click to toggle source
# 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!() click to toggle source
# 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?() click to toggle source
# File lib/readme-score/document/loader.rb, line 83
def markdown?
  @markdown == true
end
parse_markdown(markdown) click to toggle source
# File lib/readme-score/document/loader.rb, line 79
def parse_markdown(markdown)
  Parser.new(@response.body).to_html
end