Skip to content

Instantly share code, notes, and snippets.

@roymax
Created August 18, 2013 12:09
Show Gist options
  • Save roymax/6261332 to your computer and use it in GitHub Desktop.
Save roymax/6261332 to your computer and use it in GitHub Desktop.
持有基金查询,需要到http://juhe.cn 申请一个API KEY
funds:
-
code: "375010"
capital: 10000
units: 3353
-
code: "270002"
capital: 5000
units: 2544.47
# and more
# encoding: utf-8
require "httparty"
require "json"
require "settingslogic"
#require 'terminal-table'
require 'text-table'
require 'colorize'
# 基金查询小工具
class Settings < Settingslogic
source "data.yml"
end
key = ""
response = HTTParty.get("http://web.juhe.cn:8080/fund/netdata/all?key=#{key}")
fund_data = JSON.parse(response.body)
myfunds = Settings[:funds]
table = Text::Table.new
table.head = ['名称', '余额', '收益']
# table = Terminal::Table.new :headings => ['名称', '余额' , '收益'] do |t|
fund_data["result"][0].each do |key, fund|
myfunds.each do |my|
if my["code"] == fund["code"]
balance = fund["newnet"].to_f * my["units"].to_f
mynet = (balance - my["capital"] ).round(2)
# t.add_row ["#{fund["name"]}(#{fund["code"]})", "#{balance.round(2)}", "#{mynet > 0 ? mynet.to_s.red : mynet.to_s.green}"]
table.rows << ["#{fund["name"]}(#{fund["code"]})", "#{balance.round(2)}", "#{mynet > 0 ? mynet.to_s.red : mynet.to_s.green}"]
end
end
end
# end
p table
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment