Skip to content

Commit

Permalink
fetch companies meta data
Browse files Browse the repository at this point in the history
  • Loading branch information
ianchen committed Jan 29, 2014
1 parent 0c48a7d commit 2523f98
Show file tree
Hide file tree
Showing 10 changed files with 91 additions and 3 deletions.
4 changes: 1 addition & 3 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,4 @@ end

gem 'httparty'
gem 'httmultiparty'



gem 'nokogiri'
4 changes: 4 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,14 @@ GEM
mime-types (~> 1.16)
treetop (~> 1.4.8)
mime-types (1.25.1)
mini_portile (0.5.1)
minitest (4.7.5)
multi_json (1.8.4)
multi_xml (0.5.5)
multipart-post (2.0.0)
mysql2 (0.3.13)
nokogiri (1.6.0)
mini_portile (~> 0.5.0)
polyglot (0.3.3)
rack (1.5.2)
rack-test (0.6.2)
Expand Down Expand Up @@ -123,6 +126,7 @@ DEPENDENCIES
jbuilder (~> 1.2)
jquery-rails
mysql2
nokogiri
rails (= 4.0.0)
sass-rails (~> 4.0.0)
sdoc
Expand Down
3 changes: 3 additions & 0 deletions app/assets/javascripts/company.js.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/
3 changes: 3 additions & 0 deletions app/assets/stylesheets/company.css.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place all the styles related to the company controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
10 changes: 10 additions & 0 deletions app/controllers/company_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class CompanyController < ApplicationController

skip_before_filter :verify_authenticity_token

def refresh
Company.refresh
render :json => {:status => "success"}
end

end
2 changes: 2 additions & 0 deletions app/helpers/company_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module CompanyHelper
end
54 changes: 54 additions & 0 deletions app/models/company.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,56 @@
class Company < ActiveRecord::Base

include HTTMultiParty

def self.refresh
parse_html_to_db(Nokogiri::HTML(sii_companies_html))
parse_html_to_db(Nokogiri::HTML(otc_companies_html))
end

private

def self.parse_html_to_db(doc)
doc.css("tr").each do |tr|
next if tr.attr("class") != "odd" and tr.attr("class") != "even"

tds = tr.css('td')
symbol = tds[0].text[1..-1]
name = tds[1].text
company = where(:symbol => symbol).first

if company.nil?
create(:symbol => symbol, :name => name)
else
company.symbol = symbol
company.name = name
company.save
end
end
end

def self.sii_companies_html
result = self.post("http://mops.twse.com.tw/mops/web/ajax_t51sb01", :query => {
:encodeURIComponent => 1,
:step => 1,
:firstin => 1,
:TYPEK => "sii"
})

result.body

end

def self.otc_companies_html
result = self.post("http://mops.twse.com.tw/mops/web/ajax_t51sb01", :query => {
:encodeURIComponent => 1,
:step => 1,
:firstin => 1,
:TYPEK => "otc"
})

result.body

end


end
3 changes: 3 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
Kandage::Application.routes.draw do

post 'companies/refresh' => 'company#refresh'

# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".

Expand Down
7 changes: 7 additions & 0 deletions test/controllers/company_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'test_helper'

class CompanyControllerTest < ActionController::TestCase
# test "the truth" do
# assert true
# end
end
4 changes: 4 additions & 0 deletions test/helpers/company_helper_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
require 'test_helper'

class CompanyHelperTest < ActionView::TestCase
end

0 comments on commit 2523f98

Please sign in to comment.