class Gh::Trending::Parsers::ReposParser

Parses the trending repositories page

Constants

DIV_WITH_DESCRIPTION
LIST_WITH_REPO_NODES_HTML_CLASS
SPAN_WITH_LANGUAGE
SPAN_WITH_STARS

Public Instance Methods

parse(parser_args) click to toggle source
# File lib/gh_trending/parsers/repos_parser.rb, line 11
def parse(parser_args)
  url = build_request_url_with(parser_args)
  page = agent.get(url)
  parsed_page = page.at(LIST_WITH_REPO_NODES_HTML_CLASS)

  if parsed_page.nil?
    changed_page = :front_page_with_repos
    error = Gh::Trending::Parsers::HtmlChangedError.new(changed_page)
    raise error
  end

  repositories = parsed_page.search('li')
  build_repositories_from_node(repositories)
end
resource_url() click to toggle source
# File lib/gh_trending/parsers/repos_parser.rb, line 26
def resource_url
  'https://github.com/trending'.freeze
end

Private Instance Methods

build_repositories_from_node(repositories_node) click to toggle source
# File lib/gh_trending/parsers/repos_parser.rb, line 32
def build_repositories_from_node(repositories_node)
  repositories_node.map do |repo_node|
    Repository.new extract_repo_data repo_node
  end
end
extract_repo_data(repo_node) click to toggle source
# File lib/gh_trending/parsers/repos_parser.rb, line 38
def extract_repo_data(repo_node)
  {
    name:         get_name(repo_node),
    description:  get_description(repo_node),
    language:     get_language(repo_node),
    period_stars: get_stars(repo_node),
    url:          get_repo_url(repo_node)
  }
end
get_description(repo_node) click to toggle source
# File lib/gh_trending/parsers/repos_parser.rb, line 52
def get_description(repo_node)
  repo_node.search(DIV_WITH_DESCRIPTION).text.strip
end
get_language(repo_node) click to toggle source
# File lib/gh_trending/parsers/repos_parser.rb, line 56
def get_language(repo_node)
  repo_node.search(SPAN_WITH_LANGUAGE)[0].text.strip
end
get_name(repo_node) click to toggle source
# File lib/gh_trending/parsers/repos_parser.rb, line 48
def get_name(repo_node)
  repo_node.search('h3')[0].children[1].attributes['href'].value
end
get_repo_url(repo_node) click to toggle source
# File lib/gh_trending/parsers/repos_parser.rb, line 74
def get_repo_url(repo_node)
  parsed_url = repo_node.search('h3')
                        .children
                        .search('a')[0]
                        .attributes['href']
                        .value
  build_github_url(parsed_url)
end
get_stars(repo_node) click to toggle source
# File lib/gh_trending/parsers/repos_parser.rb, line 60
def get_stars(repo_node)
  string_with_stars = repo_node.search("div.f6")[0].search("a.muted-link")[0].text.strip
  # Github shows the string "1,263 stars this week"
  # Lets strip only the number.
  string_with_stars.gsub!(',', '')

  digits_array = string_with_stars.match(/\d+/)

  # Since, after replacing the commans with '', there can only possibly
  # be one number in the matched data, so we return the first one
  # and convert it to integer.
  digits_array.to_a[0].to_i
end