Skip to content

Commit

Permalink
Early Functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
ChuckBTaylor committed Jan 21, 2018
1 parent 320e4e9 commit cd28046
Show file tree
Hide file tree
Showing 13 changed files with 113,804 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
./db/seeds.rb
./secrets.rb
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ gem 'puma', '~> 3.7'
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'

gem 'twilio-ruby'

gem 'rest-client'
# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development
Expand Down
9 changes: 9 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,16 @@ GEM
domain_name (0.5.20170404)
unf (>= 0.0.5, < 1.0.0)
erubi (1.7.0)
faraday (0.14.0)
multipart-post (>= 1.2, < 3)
ffi (1.9.18)
globalid (0.4.1)
activesupport (>= 4.2.0)
http-cookie (1.0.3)
domain_name (~> 0.5)
i18n (0.9.1)
concurrent-ruby (~> 1.0)
jwt (2.1.0)
listen (3.1.5)
rb-fsevent (~> 0.9, >= 0.9.4)
rb-inotify (~> 0.9, >= 0.9.7)
Expand All @@ -69,6 +72,7 @@ GEM
mini_mime (1.0.0)
mini_portile2 (2.3.0)
minitest (5.11.1)
multipart-post (2.0.0)
netrc (0.11.0)
nio4r (2.2.0)
nokogiri (1.8.1)
Expand Down Expand Up @@ -125,6 +129,10 @@ GEM
sprockets (>= 3.0.0)
thor (0.20.0)
thread_safe (0.3.6)
twilio-ruby (5.6.0)
faraday (~> 0.9)
jwt (>= 1.5, <= 2.5)
nokogiri (>= 1.6, < 2.0)
tzinfo (1.2.4)
thread_safe (~> 0.1)
unf (0.1.4)
Expand All @@ -147,6 +155,7 @@ DEPENDENCIES
rest-client
spring
spring-watcher-listen (~> 2.0.0)
twilio-ruby
tzinfo-data

BUNDLED WITH
Expand Down
5 changes: 5 additions & 0 deletions app/controllers/api/v1/rolls_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Api::V1::RollsController < ApplicationController



end
87 changes: 87 additions & 0 deletions app/models/roll.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
require 'rest-client'
require 'JSON'
require_relative '../../secrets'
require 'twilio-ruby'

class Roll < ApplicationRecord

validates :cuid, uniqueness: true
after_create :get_specifics

def self.fetch_recent_votes
JSON.parse(RestClient.get("#{API_BASE}/both/votes/recent.json", headers={"X-API-Key": API_KEY}))
end

def self.get_recent_votes
votes = Roll.fetch_recent_votes
votes['results']['votes'][0...2].each do |vote|
roll = Roll.new(cuid: Roll.generate_cuid(vote))
if roll.save
puts "Success"
else
puts "Fail"
end
end
end

def self.generate_cuid(sushi)
"#{sushi['congress']}-#{sushi['chamber']}-#{sushi['session']}-#{sushi['roll_call']}"
end

def fetch_specifics
congress, chamber, session, roll_call = self.cuid.split('-')
raw_vote_data = JSON.parse(RestClient.get(
"#{API_BASE}/#{congress}/#{chamber}/sessions/#{session}/votes/#{roll_call}.json",
headers={"X-API-Key": API_KEY})
)
raw_vote_data['results']['votes']['vote']
end

def text_body(voter, bill_id, description)
"This is a text from LAA regarding #{voter['name']}(#{voter['party']}) who voted #{voter['vote_position']} on bill #{bill_id} which is in reference to #{description}."
end

def message_user(user, body)
client = Twilio::REST::Client.new(TWILIO_SID, TWILIO_TOKEN)

message = client.messages.create(
'body': body,
'to': user.phone_number,
'from': '+15013024280'
)

puts message.sid
end

def get_specifics
vote_data = self.fetch_specifics
vote_data['positions'].each do |voter|

body = text_body(voter, vote_data['bill']['bill_id'], vote_data['description'])

state = State.find_by(abbreviation: voter['state'])

if voter['district']
district = District.find_by(number: voter['district'], state: state)
district.users.each do |user|
message_user(user, body)
end
else
state.users.each do |user|
message_user(user, body)
end
end
end
end

def self.funny
client = Twilio::REST::Client.new(TWILIO_SID, TWILIO_TOKEN)
message = client.message.create(
'body': "How's life over there?",
'to': '+15018279722',
'from': '+15013024280'
)
message.sid
end

end
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
Rails.application.routes.draw do


namespace :api do
namespace :v1 do
resources :states
resources :districts
resources :reps
resources :users
resources :senators
resources :rolls
end
end
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
Expand Down
9 changes: 9 additions & 0 deletions db/migrate/20180120211637_create_rolls.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class CreateRolls < ActiveRecord::Migration[5.1]
def change
create_table :rolls do |t|
t.string :cuid

t.timestamps
end
end
end
5 changes: 5 additions & 0 deletions db/migrate/20180120223338_add_member_id_to_senator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddMemberIdToSenator < ActiveRecord::Migration[5.1]
def change
add_column :senators, :member_id, :string
end
end
5 changes: 5 additions & 0 deletions db/migrate/20180120223345_add_member_id_to_rep.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddMemberIdToRep < ActiveRecord::Migration[5.1]
def change
add_column :reps, :member_id, :string
end
end
10 changes: 9 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20180120200634) do
ActiveRecord::Schema.define(version: 20180120223345) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand All @@ -29,15 +29,23 @@
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "party"
t.string "member_id"
t.index ["district_id"], name: "index_reps_on_district_id"
end

create_table "rolls", force: :cascade do |t|
t.string "cuid"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

create_table "senators", force: :cascade do |t|
t.bigint "state_id"
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "party"
t.string "member_id"
t.index ["state_id"], name: "index_senators_on_state_id"
end

Expand Down
17 changes: 12 additions & 5 deletions db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
require 'rest-client'
require 'JSON'

API_KEY = "hOsiWTOsFAa4UVwNKSWxME1RfXigd1mOs9owVI5g"
require_relative '../secrets'

al = State.create(name: "Alabama", abbreviation: "AL")
counter = 1
Expand Down Expand Up @@ -361,19 +361,21 @@
counter += 1
end

senator_string = RestClient.get('https://api.propublica.org/congress/v1/115/senate/members.json', headers={"X-API-Key": API_KEY})
senator_string = RestClient.get("#{API_BASE}/115/senate/members.json", headers={"X-API-Key": API_KEY})

senator_data = JSON.parse(senator_string)
senator_data['results'][0]['members'].each do |member|
if member['in_office']
Senator.create(
name: "#{member['first_name']} #{member['last_name']}",
party: member['party'],
state: State.find_by(abbreviation: member['state']))
state: State.find_by(abbreviation: member['state']),
member_id: member['id']
)
end
end

rep_string = RestClient.get('https://api.propublica.org/congress/v1/115/house/members.json', headers={"X-API-Key": API_KEY})
rep_string = RestClient.get("#{API_BASE}/115/house/members.json", headers={"X-API-Key": API_KEY})

rep_data = JSON.parse(rep_string)
counter = 0
Expand All @@ -385,7 +387,8 @@
Rep.create(
name: "#{member['first_name']} #{member['last_name']}",
party: member['party'],
district: state.getUserDistrict(district)
district: state.getUserDistrict(district),
member_id: member['id']
)
else
counter +=1
Expand All @@ -398,3 +401,7 @@
#OH 12th
#MI 13th
#AZ 8th

User.create(phone_number: "+15018279722", state: ar, district: ar.getUserDistrict('2'))
# User.create(phone_number: "+19164793073", state: ca, district: ca.getUserDistrict('9'))
User.create(phone_number: "+16033031118", state: ny, district: ny.getUserDistrict('12'))
Loading

0 comments on commit cd28046

Please # to comment.