<repo_name>coreyleveen/moneybird<file_sep>/db/migrate/20140519200256_drop_searchdate_column_from_charts.rb
class DropSearchdateColumnFromCharts < ActiveRecord::Migration
  def change
    remove_column :charts, :search_date
  end
end
<file_sep>/app/models/user.rb
class User < ActiveRecord::Base
  has_secure_password
  validates :email,  presence: true, confirmation: true, uniqueness: true, email: true
  validates :terms_of_service, acceptance: true
  validates :password, length: {within: 6..20, too_short: "Passwords must be 6 characters or more.", too_long: "That password is too long. Please try one under 20 characters."}
  has_many :charts
end
<file_sep>/db/migrate/20140519195629_drop_stock_price_and_tweet_count_columns_from_charts.rb
class DropStockPriceAndTweetCountColumnsFromCharts < ActiveRecord::Migration
  def change
    remove_column :charts, :stock_price
    remove_column :charts, :tweet_count
  end
end
<file_sep>/lib/normalizer.rb
module Normalizer
  def normalize_tweets(chart)
    price_arr = []
    chart.share_prices.each {|x| price_arr << x.value }
    @price_min = price_arr.min
    @price_max = price_arr.max
    price_range = @price_max - @price_min

    # Proportional array mapping
    tweet_count_arr = []
    chart.tweet_counts.each {|x| tweet_count_arr << x.value}
    tweet_min = tweet_count_arr.min
    tweet_max = tweet_count_arr.max
    tweet_range = tweet_max - tweet_min
    atc_arr = tweet_count_arr.map { |x| x - tweet_min }
    tweet_prop_arr = atc_arr.map { |x| x / (tweet_range.to_f) }
    tp_to_price = tweet_prop_arr.map { |x| x * price_range }
    transformed_tweet_arr = tp_to_price.map { |x| x + @price_min }
    chart.tweet_counts.each_with_index { |tc, i| tc.value = transformed_tweet_arr[i] }
  end
end
<file_sep>/app/models/tweet_count.rb
class TweetCount < ActiveRecord::Base
  belongs_to :charts
end
<file_sep>/Project_plan_deliverables.md
Scope:

  I'm going to build Moneybird — a web app that will display a graph of the
past week's daily share price for a particular company overlaid with the daily
number of tweets for the past week containing or hashtagged with the company's
name, or other keyword, with the goal of viewing correlations the company's
share price and number of tweets that week for each day.

Features: Moneybird will feature a search bar in which a company name or ticker
symbol is to be entered. By default, the graph created as a result will feature
the past week of price information for that company, along with the number of
daily tweets for the past week containing or hashtagged with the company's name.
The user will then be able to add and remove keywords from the graph using a
text input and add button. Each added keyword will appear next to the graph and
will have a delete button which will remove it from the graph. Users can also
add more graphs, and will be prompted for a company name/ticker symbol for
each. Users will be able to have multiple graphs at once on a page, each with
its own company name and keywords for Twitter activity. Moneybird will use the
Yahoo Finance and Twitter APIs for data retrieval.

Object models (not sure about this yet):

Human - the person using the application, a user
Company - The company chosen by the user entering a search query
Keyword - The keyword entered to be sent to Twitter to display a Tweet activity
overlay on the company's share price graph.
Human has many companies, company has many keywords.

Resources:

Yahoo Finance API / gem
Twitter API
Rails guides
Instructors
more to come..

Milestones:
* Successfully connect to both Twitter and Yahoo Finance APIs, retrieving
relevant data from both
* Overlay of divs whose height responds correctly to API responses
* Users can create/delete accounts, sign in,
  sign out, and edit their account info
* Users can enter company name query into a search bar and get its 7 day
  trailing share price info.
* User can enter company name query into a search bar and get its 7 day trailing
  share price info on a graph
* User's share price graph is now overlaid with that company's twitter activity
* Users can add multiple graphs for different companies.
* Users can add keywords to a company's graph
* Users can delete graphs

Trello: https://trello.com/b/H3dx6col/moneybird

<file_sep>/config/routes.rb
Rails.application.routes.draw do
   # Users
   root "users#index"
   get 'users/new' => "users#new"
   post 'users' => "users#create"
   get 'users' => "users#index"

   # Charts
   get 'charts/new' => "charts#new"
   post 'charts' => "charts#create"
   post 'charts/:id/' => "charts#update", as: 'update'
   delete 'charts/:id' => "charts#destroy", as: 'delete'

   # Sessions
   get 'sessions/new' => 'sessions#new', as: 'log_in'
   post 'sessions' => 'sessions#create'
   delete 'sessions' => 'sessions#destroy', as: 'log_out'

   get 'profiles' => 'profiles#index'
end
<file_sep>/app/controllers/charts_controller.rb
class ChartsController < ApplicationController
  def index
  end

  def new
    @chart = Chart.new
  end

  def create
    chart = Chart.create(chart_params)
    current_user.charts << chart
    redirect_to root_path
  end

  def update
    Chart.find(params[:id]).tweet_search
    Chart.find(params[:id]).price_history
    redirect_to root_path
  end

  def destroy
    Chart.delete(params[:id])
    redirect_to root_path
  end

  private

  def chart_params
    params.require(:chart).permit(:ticker)
  end
end
<file_sep>/app/models/chart.rb
class Chart < ActiveRecord::Base
  belongs_to :users
  has_many :share_prices
  has_many :tweet_counts

  after_create :tweet_search, :price_history

  def tweet_search
    config = {
      :consumer_key    => ENV['TWITTER_KEY'],
      :consumer_secret => ENV['TWITTER_SECRET']
    }
    client = Twitter::REST::Client.new(config)

    i = 7
    @dates_arr = []
    loop do
      @dates_arr << Date.today.prev_day(n=i).to_s
      i -= 1
      break if i < 0
    end

    n = 0
    tweet_count_arr = []
    loop do
      tweet_count_arr << client.search(self.ticker + " since:#{@dates_arr[n]} until:#{@dates_arr[n+1]}").count
      n += 1
      break if n > 6
    end

    self.tweet_counts.delete_all

    i = 0
    tweet_count_arr.each do |x|
      tweet_count = TweetCount.create( { value: x, date: @dates_arr[i] })
      self.tweet_counts << tweet_count
      i += 1
    end
  end

  def price_history(num_days=7)
    num_days_search = num_days + 10
    obj_arr = YahooFinance.historical_quotes("#{self.ticker}", Time::now-(24*60*60*num_days_search), Time::now).take(num_days)
    price_arr = []
    obj_arr.each do |x|
      num = x.close.to_f
      price_arr << num
    end
    price_arr.reverse!

    self.share_prices.delete_all

    price_arr.each_with_index do |x,i|
      share_price = SharePrice.create( { value: x, date: @dates_arr[i] })
      self.share_prices << share_price
    end
  end
end
<file_sep>/README.md
# moneybird
__moneybird__ graphs companies' share price history with their Twitter activity for the past week. 

### Instructions

1. Sign up for an account
2. Login
3. Make a chart by entering in a stock's ticker symbol from the NYSE

Some sample charts: 

![alt-text](http://i.imgur.com/mJGadot.png)

This application began as a week-long project as part of my Web Development Immersive course at General Assembly. It's built on Rails and uses the Yahoo Finance and Twitter APIs. 
<file_sep>/db/migrate/20140519024951_create_charts.rb
class CreateCharts < ActiveRecord::Migration
  def change
    create_table :charts do |t|
      t.date :search_date
      t.string :ticker
      t.integer :stock_price
      t.integer :tweet_count

      t.timestamps
    end
  end
end
<file_sep>/app/models/share_price.rb
class SharePrice < ActiveRecord::Base
  belongs_to :charts
end
