-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
91 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,6 +48,4 @@ end | |
|
||
gem 'httparty' | ||
gem 'httmultiparty' | ||
|
||
|
||
|
||
gem 'nokogiri' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
module CompanyHelper | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
require 'test_helper' | ||
|
||
class CompanyHelperTest < ActionView::TestCase | ||
end |