From 4b663293766c33168fae0f52c08f39e7aa5fcb9f Mon Sep 17 00:00:00 2001 From: elifoster Date: Mon, 21 Mar 2016 20:11:42 -0700 Subject: [PATCH] :birthday: :bug: New version 1.2.0. We now use Curb instead of OpenURI to prevent monkeypatch overlapping between it and Sinatra. Properly use Nokogiris methods instead of to_s and regular expressions. Improve get_calendar so that the initial g is not included. Not closing either #4 or ghuls-apps/ghuls-web#15 because it *might* not actually resolve the bug. --- CHANGELOG.md | 5 +++++ Gemfile | 1 + Gemfile.lock | 2 ++ github-calendar.gemspec | 3 ++- lib/github/calendar.rb | 39 +++++++++++++++++++-------------------- 5 files changed, 29 insertions(+), 21 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 24f7957..11dd75f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Changelog ## Version 1 +### Version 1.2.0 +* Switch to Curb instead of OpenURI for the `#get_page_source` method, due to possible conflicted `Kernel#open` monkeypatches from Sinatra and OpenURI (#4, ghuls-web#15) +* Improve reliability of `#get_daily`, `#get_weekly`, and `#get_monthly` by properly using Nokogiri's Element methods. +* `#get_calendar` no longer gets the initial `g` (containing the entire calendar); just the individual weeks as a NodeSet. + ### Version 1.1.0 * Bump Ruby to 2.3.0, Nokogiri to 1.6.7.2, and StringUtility to 2.7.1. * Improve some redundant regex. diff --git a/Gemfile b/Gemfile index 6fb9a5e..ffd3db9 100644 --- a/Gemfile +++ b/Gemfile @@ -4,3 +4,4 @@ ruby '2.3.0' gem 'nokogiri', '>= 1.6.7.2' gem 'string-utility', '>= 2.7.1' +gem 'curb', '>= 0.9.1' \ No newline at end of file diff --git a/Gemfile.lock b/Gemfile.lock index 9d4a090..354988b 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,6 +1,7 @@ GEM remote: http://rubygems.org/ specs: + curb (0.9.1) mini_portile2 (2.0.0) nokogiri (1.6.7.2) mini_portile2 (~> 2.0.0.rc2) @@ -10,6 +11,7 @@ PLATFORMS ruby DEPENDENCIES + curb (>= 0.9.1) nokogiri (>= 1.6.7.2) string-utility (>= 2.7.1) diff --git a/github-calendar.gemspec b/github-calendar.gemspec index 53fd52b..7ad62db 100644 --- a/github-calendar.gemspec +++ b/github-calendar.gemspec @@ -1,6 +1,6 @@ Gem::Specification.new do |s| s.name = 'github-calendar' - s.version = '1.1.0' + s.version = '1.2.0' s.required_ruby_version = '>= 2.3.0' s.authors = ['Eli Foster'] s.description = 'A library that allows for quick HTML parsing of GitHub user profile contribution calendars. ' \ @@ -15,4 +15,5 @@ Gem::Specification.new do |s| s.summary = 'Getting GitHub user profile calendar data through HTML parsing.' s.add_runtime_dependency('nokogiri', '>= 1.6.7.2') s.add_runtime_dependency('string-utility', '>= 2.7.1') + s.add_runtime_dependency('curb', '>= 0.9.1') end diff --git a/lib/github/calendar.rb b/lib/github/calendar.rb index 48280de..4d47c7c 100644 --- a/lib/github/calendar.rb +++ b/lib/github/calendar.rb @@ -1,4 +1,4 @@ -require 'open-uri' +require 'curb' require 'nokogiri' require 'string-utility' require_relative 'exceptions' @@ -42,13 +42,12 @@ def get_weekly(user) weeks = get_calendar(user) ret = {} count = 0 - weeks[1..-1].each do |k| - data = 0 - capture = k.to_s.scan(/data-count="(.*?)"/) - capture[1..-1].each do |ints| - data += ints[0].to_i + weeks.each do |k| + week_data = 0 + k.children.each do |element| + week_data += element.attribute('data-count').value.to_i end - ret[count] = data + ret[count] = week_data count += 1 end ret @@ -61,10 +60,10 @@ def get_daily(user) weeks = get_calendar(user) ret = {} count = 0 - weeks[1..-1].each do |k| - capture = k.to_s.scan(/data-count="(.*?)"/) - capture[1..-1].each do |i| - ret[count] = i[0].to_i + weeks.each do |k| + k.children.each do |day| + val = day.attribute('data-count').value.to_i + ret[count] = val count += 1 end end @@ -91,11 +90,11 @@ def get_monthly(user) '11' => 0, '12' => 0 } - weeks[1..-1].each do |k| - date = k.to_s.scan(/data-date=".*-(.*?)-/) - capture = k.to_s.scan(/data-count="(.*?)"/) - capture[1..-1].each do |i| - ret[date[0][0]] += i[0].to_i + weeks.each do |k| + k.children.each do |day| + date = day.attribute('data-date').value.split('-')[1] + count = day.attribute('data-count').value + ret[date] += count.to_i end end ret @@ -133,7 +132,7 @@ def get_average_month(user) # @raise [UserNotFoundException] If the user is not found. def get_page_source(user) begin - Nokogiri::HTML(open("https://github.com/#{user}"), &:noblanks) + Nokogiri::HTML(Curl.get("https://github.com/#{user}").body_str, &:noblanks) rescue OpenURI::HTTPError raise GitHub::Exceptions::UserNotFoundException.new(user) end @@ -141,11 +140,11 @@ def get_page_source(user) # Gets the parsed calendar HTML source for the user profile. # @param user [String] See #get_total_year - # @return [Nokogiri::XML::NodeSet] The NodeSet for all the g's in the calendar. Consider this as an array of all - # the weeks. In iteration, you will probably want to skip the first, as it is the total yearly. + # @return [Nokogiri::XML::NodeSet] The NodeSet for all the days in the calendar. Consider this as an array of all + # the weeks. def get_calendar(user) source = get_page_source(user) - source.css('svg.js-calendar-graph-svg g') + source.css('svg.js-calendar-graph-svg g g') end # Gets an average for all the integer values in a hash.