Skip to content

Commit 8a97dd1

Browse files
committed
Revert "remove existing changeLog'd API integration - Bounty #488"
1 parent e3779ac commit 8a97dd1

File tree

3 files changed

+105
-2
lines changed

3 files changed

+105
-2
lines changed

app/models/badges/changelogd.rb

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
class Changelogd < BadgeBase
1+
# TODO: broken
2+
!class Changelogd < BadgeBase
23
describe "Changelog'd",
34
skill: 'Open Source',
45
description: "Have an original repo featured on the Changelog show",
@@ -7,4 +8,72 @@ class Changelogd < BadgeBase
78
weight: 2,
89
providers: :github
910

11+
API_URI = "http://thechangelog.com/api/read" # tagged=episode & tagged=github
12+
REPO = /([http|https]*:\/\/github\.com\/[\w | \-]*\/[\w | \-]*)/i
13+
USERNAME = /github\.com\/([\w | \-]*)\/[\w | \-]*/i
14+
REPO_NAME = /github\.com\/[\S|\D]*\/([\S|\D]*)/i
15+
16+
def reasons
17+
@reasons ||= begin
18+
links = user.facts.select do |fact|
19+
fact.tagged?('changedlog')
20+
end.collect do |fact|
21+
begin
22+
match = fact.url.match(REPO_NAME)
23+
{ match[1] => fact.url }
24+
rescue
25+
{ fact.url => fact.url }
26+
end
27+
end
28+
{ links: links }
29+
end
30+
end
31+
32+
def award?
33+
!reasons[:links].empty?
34+
end
35+
36+
class << self
37+
def perform
38+
create_assignments! all_repos
39+
end
40+
41+
def quick_refresh
42+
create_assignments! latest_repos
43+
end
44+
45+
def refresh
46+
perform
47+
end
48+
49+
def create_assignments!(repos)
50+
repos.each do |repo_url|
51+
match = repo_url.match(USERNAME)
52+
break if match.nil?
53+
github_username = match[1]
54+
Fact.append!("#{repo_url}:changedlogd", "github:#{github_username}", "Repo featured on Changelogd", Time.now, repo_url, ['repo', 'changedlog'])
55+
end
56+
end
57+
58+
def latest_repos
59+
repos_in(API_URI).flatten.uniq
60+
end
61+
62+
def all_repos
63+
repos = []
64+
(1...20).each do |time|
65+
start = ((time * 50) + 1) - 50
66+
repos << repos_in(API_URI + "?start=#{start}&num=50")
67+
end
68+
repos.flatten.uniq
69+
end
70+
71+
def repos_in(url)
72+
res = Servant.get(url)
73+
doc = Nokogiri::HTML(res.to_s)
74+
doc.xpath('//post/link-description').collect do |element|
75+
element.content.scan(REPO)
76+
end
77+
end
78+
end
1079
end

lib/tasks/facts.rake

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
namespace :facts do
22
# PRODUCTION: RUNS DAILY
33
task system: :environment do
4+
puts "Changelogd"
5+
Changelogd.refresh
46
puts "Ashcat"
57
Ashcat.perform
68
end

spec/models/badges/changelogd_spec.rb

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,40 @@
11
require 'spec_helper'
22

3-
RSpec.describe Changelogd, type: :model do
3+
RSpec.describe Changelogd, type: :model, skip: true do
4+
it 'should award a user if there is a tag' do
5+
stub_request(:get, Changelogd::API_URI).to_return(body: File.read(File.join(Rails.root, 'spec', 'fixtures', 'changelogd_feed.xml')))
6+
Changelogd.quick_refresh
7+
8+
user = Fabricate(:user, github: 'CloudMade')
9+
10+
changelogd = Changelogd.new(user)
11+
expect(changelogd.award?).to eq(true)
12+
expect(changelogd.reasons[:links].first['Leaflet']).to eq('http://github.com/CloudMade/Leaflet')
13+
end
14+
415
it 'should have a name and description' do
516
expect(Changelogd.name).not_to be_blank
617
expect(Changelogd.description).not_to be_blank
718
end
19+
20+
it 'should should find github projects' do
21+
stub_request(:get, Changelogd::API_URI).to_return(body: File.read(File.join(Rails.root, 'spec', 'fixtures', 'changelogd_feed.xml')))
22+
expect(Changelogd.latest_repos.first).to eq('http://github.com/CloudMade/Leaflet')
23+
end
24+
25+
it 'should create a fact' do
26+
stub_request(:get, Changelogd::API_URI).to_return(body: File.read(File.join(Rails.root, 'spec', 'fixtures', 'changelogd_feed.xml')))
27+
Changelogd.quick_refresh
28+
fact = Fact.where(identity: 'http://github.com/CloudMade/Leaflet:changedlogd').first
29+
expect(fact).not_to be_nil
30+
end
31+
32+
it 'should find the first and last project', functional: true, slow: true, skip: 'resource not found' do
33+
expect(Changelogd.all_repos).to include('http://github.com/kennethreitz/tablib')
34+
expect(Changelogd.all_repos).to include('http://github.com/johnsheehan/RestSharp')
35+
end
36+
37+
it 'should find repos in episodes too', functional: true, skip: 'resource not found' do
38+
expect(Changelogd.all_repos).to include('https://github.com/geemus/excon')
39+
end
840
end

0 commit comments

Comments
 (0)