Skip to content

Commit

Permalink
🎂 🐛 New version 1.2.0.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
elifoster committed Mar 22, 2016
1 parent e169c4d commit 4b66329
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 21 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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'
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -10,6 +11,7 @@ PLATFORMS
ruby

DEPENDENCIES
curb (>= 0.9.1)
nokogiri (>= 1.6.7.2)
string-utility (>= 2.7.1)

Expand Down
3 changes: 2 additions & 1 deletion github-calendar.gemspec
Original file line number Diff line number Diff line change
@@ -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. ' \
Expand All @@ -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
39 changes: 19 additions & 20 deletions lib/github/calendar.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require 'open-uri'
require 'curb'
require 'nokogiri'
require 'string-utility'
require_relative 'exceptions'
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -133,19 +132,19 @@ 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
end

# 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.
Expand Down

0 comments on commit 4b66329

Please sign in to comment.