Skip to content

Commit

Permalink
Use Councilmatic if available
Browse files Browse the repository at this point in the history
  • Loading branch information
pjsier committed Jul 25, 2019
1 parent e172882 commit 459ef0b
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 28 deletions.
3 changes: 2 additions & 1 deletion functions/update_bill/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ func UpdateBill(bill models.Bill, actions []models.LegistarAction, snsClient svc
return err
}
bill.Data = string(actionJson)
data := svc.TweetData{Text: bill.CreateTweet()}
billUrl := bill.GetTweetURL()
data := svc.TweetData{Text: bill.CreateTweet(billUrl)}
tweetJson, err := json.Marshal(data)
if err != nil {
return err
Expand Down
22 changes: 20 additions & 2 deletions pkg/models/bill.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,25 @@ func (b *Bill) FetchBillData() (string, string, []LegistarAction, error) {
return title, classification, actions, nil
}

func (b *Bill) CreateTweet() string {
func (b *Bill) GetTweetURL() string {
councilmaticUrl := fmt.Sprintf(
"https://chicago.councilmatic.org/legislation/%s/",
strings.ToLower(b.GetCleanBillID()),
)
response, err := http.Get(councilmaticUrl)
if err != nil {
return b.URL
}
defer response.Body.Close()

if response.StatusCode == 200 {
return councilmaticUrl
} else {
return b.URL
}
}

func (b *Bill) CreateTweet(billUrl string) string {
billId := b.GetCleanBillID()
billTitle := b.Title
actions := b.GetActions()
Expand Down Expand Up @@ -219,7 +237,7 @@ func (b *Bill) CreateTweet() string {
tweetContent = fmt.Sprintf("%s%s%s.", strings.TrimSpace(billTitle[0:len(billTitle)-tweetDiff]), ellipsis, actionText)
}

return fmt.Sprintf("%s See more at %s #%s", tweetContent, b.URL, b.BillID)
return fmt.Sprintf("%s See more at %s #%s", tweetContent, billUrl, b.BillID)
}

func (b *Bill) SetNextRun() {
Expand Down
55 changes: 30 additions & 25 deletions pkg/models/bill_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,54 +67,59 @@ func TestCreateTweet(t *testing.T) {
BillID: "O201011",
Data: `[]`,
}
tweetEnd := "See more at https://chicago.legistar.com #O201011"
if bill.CreateTweet() != fmt.Sprintf("O2010-11: Testing bill. %s", tweetEnd) {
t.Errorf("Tweet with no actions is incorrect: %s", bill.CreateTweet())
councilmaticUrl := "https://chicago.councilmatic.com/legislation/o2010-11/"
tweetEnd := fmt.Sprintf("See more at %s #O201011", councilmaticUrl)
if bill.CreateTweet(councilmaticUrl) != fmt.Sprintf("O2010-11: Testing bill. %s", tweetEnd) {
t.Errorf("Tweet with alternate URL is incorrect: %s", bill.CreateTweet(councilmaticUrl))
}
tweetEnd = "See more at https://chicago.legistar.com #O201011"
if bill.CreateTweet(bill.URL) != fmt.Sprintf("O2010-11: Testing bill. %s", tweetEnd) {
t.Errorf("Tweet with no actions is incorrect: %s", bill.CreateTweet(bill.URL))
}
bill.Data = `[{"action": "Add Co-Sponsor(s)"}]`
if bill.CreateTweet() != fmt.Sprintf("O2010-11: Testing bill add co-sponsor(s). %s", tweetEnd) {
t.Errorf("Tweet with alternate action is incorrect: %s", bill.CreateTweet())
if bill.CreateTweet(bill.URL) != fmt.Sprintf("O2010-11: Testing bill add co-sponsor(s). %s", tweetEnd) {
t.Errorf("Tweet with alternate action is incorrect: %s", bill.CreateTweet(bill.URL))
}
bill.Data = `[{"action": ""}]`
if bill.CreateTweet() != fmt.Sprintf("O2010-11: Testing bill. %s", tweetEnd) {
t.Errorf("Tweet with empty action is incorrect: %s", bill.CreateTweet())
if bill.CreateTweet(bill.URL) != fmt.Sprintf("O2010-11: Testing bill. %s", tweetEnd) {
t.Errorf("Tweet with empty action is incorrect: %s", bill.CreateTweet(bill.URL))
}
bill.Data = `[{"action": "Introduced", "actor": "Chicago City Council"}]`
if bill.CreateTweet() != fmt.Sprintf("O2010-11: Testing bill was introduced in Chicago City Council. %s", tweetEnd) {
t.Errorf("Tweet for introduction is incorrect: %s", bill.CreateTweet())
if bill.CreateTweet(bill.URL) != fmt.Sprintf("O2010-11: Testing bill was introduced in Chicago City Council. %s", tweetEnd) {
t.Errorf("Tweet for introduction is incorrect: %s", bill.CreateTweet(bill.URL))
}
bill.Data = `[{"action": "Referred"}]`
if bill.CreateTweet() != fmt.Sprintf("O2010-11: Testing bill was referred to committee. %s", tweetEnd) {
t.Errorf("Tweet for referral with no entity is incorrect: %s", bill.CreateTweet())
if bill.CreateTweet(bill.URL) != fmt.Sprintf("O2010-11: Testing bill was referred to committee. %s", tweetEnd) {
t.Errorf("Tweet for referral with no entity is incorrect: %s", bill.CreateTweet(bill.URL))
}
bill.Data = `[{"action": "Referred", "actor": "", "committee": "Test Committee"}]`
if bill.CreateTweet() != fmt.Sprintf("O2010-11: Testing bill was referred to the Test Committee. %s", tweetEnd) {
t.Errorf("Tweet for referral with committee is incorrect: %s", bill.CreateTweet())
if bill.CreateTweet(bill.URL) != fmt.Sprintf("O2010-11: Testing bill was referred to the Test Committee. %s", tweetEnd) {
t.Errorf("Tweet for referral with committee is incorrect: %s", bill.CreateTweet(bill.URL))
}
bill.Data = `[{"action": "Recommended for Passage", "actor": "Test Committee"}]`
if bill.CreateTweet() != fmt.Sprintf("O2010-11: Testing bill was recommended to pass by the Test Committee. %s", tweetEnd) {
t.Errorf("Tweet for committee passage is incorrect: %s", bill.CreateTweet())
if bill.CreateTweet(bill.URL) != fmt.Sprintf("O2010-11: Testing bill was recommended to pass by the Test Committee. %s", tweetEnd) {
t.Errorf("Tweet for committee passage is incorrect: %s", bill.CreateTweet(bill.URL))
}
bill.Data = `[{"action": "Passed"}]`
if bill.CreateTweet() != fmt.Sprintf("O2010-11: Testing bill passed. %s", tweetEnd) {
t.Errorf("Tweet for passage is incorrect: %s", bill.CreateTweet())
if bill.CreateTweet(bill.URL) != fmt.Sprintf("O2010-11: Testing bill passed. %s", tweetEnd) {
t.Errorf("Tweet for passage is incorrect: %s", bill.CreateTweet(bill.URL))
}
bill.Data = `[{"action": "Passed"}, {"action": "Referred"}]`
if bill.CreateTweet() != fmt.Sprintf("O2010-11: Testing bill passed. %s", tweetEnd) {
t.Errorf("Tweet for most recent action is incorrect: %s", bill.CreateTweet())
if bill.CreateTweet(bill.URL) != fmt.Sprintf("O2010-11: Testing bill passed. %s", tweetEnd) {
t.Errorf("Tweet for most recent action is incorrect: %s", bill.CreateTweet(bill.URL))
}
bill.Title = ""
if bill.CreateTweet() != fmt.Sprintf("O2010-11 passed. %s", tweetEnd) {
t.Errorf("Tweet for bill without title is incorrect: %s", bill.CreateTweet())
if bill.CreateTweet(bill.URL) != fmt.Sprintf("O2010-11 passed. %s", tweetEnd) {
t.Errorf("Tweet for bill without title is incorrect: %s", bill.CreateTweet(bill.URL))
}
bill.Data = `[{"action": "Recommended for Passage", "actor": "Committee on Ethics and Government Oversight"}]`
bill.Title = "Certification of city funding requirement for Laborers' and Retirement Board Employees Annuity and Benefit Fund of Chicago for tax year 2020, payment year 2021"
if bill.CreateTweet() != fmt.Sprintf("O2010-11: Certification of city funding requirement for Laborers' and Retirement Board Employees Annuity and Benefit Fund of Chicago for tax year 2020, pay... was recommended to pass by the Committee on Ethics and Government Oversight. %s", tweetEnd) {
t.Errorf("Clipped title tweet text with action is incorrect: %s", bill.CreateTweet())
if bill.CreateTweet(bill.URL) != fmt.Sprintf("O2010-11: Certification of city funding requirement for Laborers' and Retirement Board Employees Annuity and Benefit Fund of Chicago for tax year 2020, pay... was recommended to pass by the Committee on Ethics and Government Oversight. %s", tweetEnd) {
t.Errorf("Clipped title tweet text with action is incorrect: %s", bill.CreateTweet(bill.URL))
}
bill.Title = "Certification of city funding requirement for Laborers' and Retirement Board Employees Annuity and Benefit Fund of Chicago for tax year 2020, payment year 2021 "
bill.Data = `[]`
if bill.CreateTweet() != fmt.Sprintf("O2010-11: Certification of city funding requirement for Laborers' and Retirement Board Employees Annuity and Benefit Fund of Chicago for tax year 2020, payment year 2021... %s", tweetEnd) {
t.Errorf("Clipped title tweet text with no actions is incorrect: %s", bill.CreateTweet())
if bill.CreateTweet(bill.URL) != fmt.Sprintf("O2010-11: Certification of city funding requirement for Laborers' and Retirement Board Employees Annuity and Benefit Fund of Chicago for tax year 2020, payment year 2021... %s", tweetEnd) {
t.Errorf("Clipped title tweet text with no actions is incorrect: %s", bill.CreateTweet(bill.URL))
}
}

0 comments on commit 459ef0b

Please sign in to comment.