Skip to content

Instantly share code, notes, and snippets.

@myun2
Last active May 30, 2017 08:28
Show Gist options
  • Save myun2/7070cd7dda2a5b2ecf523ce73b2872b0 to your computer and use it in GitHub Desktop.
Save myun2/7070cd7dda2a5b2ecf523ce73b2872b0 to your computer and use it in GitHub Desktop.
#! /usr/bin/env ruby
require 'rest-client'
require 'json'
require 'active_record'
require 'sqlite3'
require 'sinatra'
URL = 'https://www.gaitameonline.com/rateaj/getrate'
@rates = JSON.parse(RestClient.get(URL))["quotes"]
ActiveRecord::Base.establish_connection(adapter: :sqlite3, database: "fx.sqlite3")
def create_positions_tbl
ActiveRecord::Base.connection.execute(
"create table positions(currency string, price float, final float, created_at datetime);"
)
end
def create_table(tbl)
ActiveRecord::Base.connection.execute "create table #{tbl}(price float, created_at datetime);"
end
#create_positions_tbl
#create_table "usd_jpies"
class Position < ActiveRecord::Base
scope :price, -> (v) { where(currency: v) }
def self.profit(cur)
price(cur).all.inject(0) {|sum, pos|
if pos.final
sum + pos.final - pos.price
else
sum
end
}
end
end
class UsdJpy < ActiveRecord::Base; end
def price(cur)
@rates.select { |r|
r["currencyPairCode"] == cur
}.first["ask"]
end
def buy(cur, price)
Position.create(cur: cur, price: price)
end
p Position.profit("USDJPY")
v = UsdJpy.create(created_at: Time.now, price: price("USDJPY"))
p v.created_at
p v.price
# frozen_string_literal: true
source "https://rubygems.org"
gem "rest-client"
gem "sqlite3"
gem "activerecord"
gem "sinatra"
GEM
remote: https://rubygems.org/
specs:
activemodel (5.1.1)
activesupport (= 5.1.1)
activerecord (5.1.1)
activemodel (= 5.1.1)
activesupport (= 5.1.1)
arel (~> 8.0)
activesupport (5.1.1)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (~> 0.7)
minitest (~> 5.1)
tzinfo (~> 1.1)
arel (8.0.0)
concurrent-ruby (1.0.5)
domain_name (0.5.20170404)
unf (>= 0.0.5, < 1.0.0)
http-cookie (1.0.3)
domain_name (~> 0.5)
i18n (0.8.1)
mime-types (3.1)
mime-types-data (~> 3.2015)
mime-types-data (3.2016.0521)
minitest (5.10.2)
mustermann (1.0.0)
netrc (0.11.0)
rack (2.0.3)
rack-protection (2.0.0)
rack
rest-client (2.0.2)
http-cookie (>= 1.0.2, < 2.0)
mime-types (>= 1.16, < 4.0)
netrc (~> 0.8)
sinatra (2.0.0)
mustermann (~> 1.0)
rack (~> 2.0)
rack-protection (= 2.0.0)
tilt (~> 2.0)
sqlite3 (1.3.13)
thread_safe (0.3.6)
tilt (2.0.7)
tzinfo (1.2.3)
thread_safe (~> 0.1)
unf (0.1.4)
unf_ext
unf_ext (0.0.7.4)
PLATFORMS
ruby
DEPENDENCIES
activerecord
rest-client
sinatra
sqlite3
BUNDLED WITH
1.14.6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment