diff --git a/app/controllers/home_controller.rb b/app/controllers/home_controller.rb index 95f2992..67274fb 100644 --- a/app/controllers/home_controller.rb +++ b/app/controllers/home_controller.rb @@ -1,4 +1,21 @@ class HomeController < ApplicationController def index + + end + + def seed + market = Market.where(name: 'BitBay').first + if market.blank? + market = Market.new(name: 'BitBay', url: 'https://bitbay.net') + market.save + end + bit_bay_currencies = [['BTC', 'Bitcoin'],['BCC', 'Bitcoin Cash'], ['ETH','Ethereum'], ['LSK', 'Lisk'], ['LTC', 'Litecoin'], ['DASH', 'Dash'], ['GAME', 'GameCredits']] + for bbc in bit_bay_currencies + curr = Currency.where('name = ?', bbc[0]).first + if curr.blank? + curr = Currency.new(name: bbc[0], description: bbc[1], market_id: market.id) + curr.save + end + end end end diff --git a/app/models/bit_bay_trade.rb b/app/models/bit_bay_trade.rb new file mode 100644 index 0000000..b3627e2 --- /dev/null +++ b/app/models/bit_bay_trade.rb @@ -0,0 +1,3 @@ +class BitBayTrade < ApplicationRecord + belongs_to :currency +end diff --git a/app/models/currency.rb b/app/models/currency.rb new file mode 100644 index 0000000..7dfdd4d --- /dev/null +++ b/app/models/currency.rb @@ -0,0 +1,3 @@ +class Currency < ApplicationRecord + has_many :bit_bay_trades +end diff --git a/app/models/market.rb b/app/models/market.rb new file mode 100644 index 0000000..aa7cfed --- /dev/null +++ b/app/models/market.rb @@ -0,0 +1,2 @@ +class Market < ApplicationRecord +end diff --git a/app/scripts/get_bit_bay_trades.rb b/app/scripts/get_bit_bay_trades.rb new file mode 100644 index 0000000..b5dbfb2 --- /dev/null +++ b/app/scripts/get_bit_bay_trades.rb @@ -0,0 +1,44 @@ +#!/usr/bin/env ruby +# encoding: UTF-8 + +ENV["RAILS_ENV"] ||= "development" +require File.dirname(__FILE__) + "/../../config/environment" +require 'uri' +require 'net/http' +require 'json' +pause_interval = 60 + +start_index = '-1' +currency = Currency.where(name: 'GAME').first +while true + start_time = Time.now.to_i + bbt_index = BitBayTrade.where('currency_id = ?', currency.id).order('cast(tid as unsigned) DESC').first + unless bbt_index.blank? + start_index = bbt_index.tid + end + uri = "https://bitbay.net/API/Public/#{currency.name}PLN/trades.json?since=#{start_index}" + puts "start at #{start_index} index" + url = URI.parse(uri) + resp = Net::HTTP.get_response(url) + my_hash = JSON.parse(resp.body) + for mh in my_hash + bbt = BitBayTrade.where('currency_id = ? AND tid = ?', currency.id, mh['tid']).first + if bbt.blank? + BitBayTrade.create(currency_id: currency.id, tid: mh['tid'], date: Time.at(mh['date'].to_i), transaction_type: mh['type'], price: mh['price'], amount: mh['amount']) + #puts "#{mh['tid']} -#{mh['date']} - #{mh['price']} - #{mh['type']} - #{mh['amount']}" + end + + end + end_time = Time.now.to_i +=begin + if end_time > start_time + diff_time = end_time - start_time + if diff_time < 60 + puts "Sleep #{pause_interval - diff_time + 1} dla #{diff_time}" + sleep(pause_interval - diff_time + 1) + end + else + sleep(pause_interval) + end +=end +end \ No newline at end of file diff --git a/app/scripts/get_currencies.rb b/app/scripts/get_currencies.rb new file mode 100644 index 0000000..0e2b1ef --- /dev/null +++ b/app/scripts/get_currencies.rb @@ -0,0 +1,13 @@ +market = Market.where(name: 'BitBay').first +if market.blank? + market = Market.new(name: 'BitBay', url: 'https://bitbay.net') + market.save +end +bit_bay_currencies = [['BTC', 'Bitcoin'],['BCC', 'Bitcoin Cash'], ['ETH','Ethereum'], ['LSK', 'Lisk'], ['LTC', 'Litecoin'], ['DASH', 'Dash'], ['GAME', 'GameCredits']] +for bbc in bit_bay_currencies + curr = Currency.where('name = ?', bbc[0]).first + if curr.blank? + curr = Currency.new(name: bcc[0], description: bcc[1], market: market) + curr.save + end +end \ No newline at end of file diff --git a/config/database.yml b/config/database.yml index 42cd5da..b10db3b 100644 --- a/config/database.yml +++ b/config/database.yml @@ -15,7 +15,7 @@ default: &default pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> username: crypto password: yX6txaHN8WenxrK2 - host: localhost + host: 51.254.143.84 development: <<: *default diff --git a/config/routes.rb b/config/routes.rb index 80a21f6..25be439 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,5 +1,6 @@ Rails.application.routes.draw do get 'home/index' + get 'home/seed' root 'home#index' # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html end diff --git a/db/migrate/20171129203113_create_currencies.rb b/db/migrate/20171129203113_create_currencies.rb new file mode 100644 index 0000000..2fa0cbe --- /dev/null +++ b/db/migrate/20171129203113_create_currencies.rb @@ -0,0 +1,10 @@ +class CreateCurrencies < ActiveRecord::Migration[5.1] + def change + create_table :currencies do |t| + t.string :name + t.string :description + + t.timestamps + end + end +end diff --git a/db/migrate/20171129204104_create_bit_bay_trades.rb b/db/migrate/20171129204104_create_bit_bay_trades.rb new file mode 100644 index 0000000..1f99365 --- /dev/null +++ b/db/migrate/20171129204104_create_bit_bay_trades.rb @@ -0,0 +1,15 @@ +class CreateBitBayTrades < ActiveRecord::Migration[5.1] + def change + create_table :bit_bay_trades do |t| + t.references :currency, foreign_key: true + t.string :tid + t.timestamp :date + t.string :type + t.float :price + t.float :amount + + t.timestamps + end + add_index :bit_bay_trades, :tid + end +end diff --git a/db/migrate/20171129210142_add_is_active_to_currencies.rb b/db/migrate/20171129210142_add_is_active_to_currencies.rb new file mode 100644 index 0000000..4eed97a --- /dev/null +++ b/db/migrate/20171129210142_add_is_active_to_currencies.rb @@ -0,0 +1,6 @@ +class AddIsActiveToCurrencies < ActiveRecord::Migration[5.1] + def change + add_column :currencies, :is_active, :boolean, default: true + add_index :currencies, :is_active + end +end diff --git a/db/migrate/20171129210307_create_markets.rb b/db/migrate/20171129210307_create_markets.rb new file mode 100644 index 0000000..3de1a09 --- /dev/null +++ b/db/migrate/20171129210307_create_markets.rb @@ -0,0 +1,12 @@ +class CreateMarkets < ActiveRecord::Migration[5.1] + def change + create_table :markets do |t| + t.string :name + t.string :url + t.boolean :is_active, default: true + + t.timestamps + end + add_index :markets, :is_active + end +end diff --git a/db/migrate/20171129210447_add_market_to_currencies.rb b/db/migrate/20171129210447_add_market_to_currencies.rb new file mode 100644 index 0000000..6bc1dc8 --- /dev/null +++ b/db/migrate/20171129210447_add_market_to_currencies.rb @@ -0,0 +1,5 @@ +class AddMarketToCurrencies < ActiveRecord::Migration[5.1] + def change + add_reference :currencies, :market, foreign_key: true + end +end diff --git a/db/schema.rb b/db/schema.rb new file mode 100644 index 0000000..8c47837 --- /dev/null +++ b/db/schema.rb @@ -0,0 +1,50 @@ +# This file is auto-generated from the current state of the database. Instead +# of editing this file, please use the migrations feature of Active Record to +# incrementally modify your database, and then regenerate this schema definition. +# +# Note that this schema.rb definition is the authoritative source for your +# database schema. If you need to create the application database on another +# system, you should be using db:schema:load, not running all the migrations +# from scratch. The latter is a flawed and unsustainable approach (the more migrations +# you'll amass, the slower it'll run and the greater likelihood for issues). +# +# It's strongly recommended that you check this file into your version control system. + +ActiveRecord::Schema.define(version: 20171129210447) do + + create_table "bit_bay_trades", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t| + t.bigint "currency_id" + t.string "tid" + t.timestamp "date" + t.string "type" + t.float "price", limit: 24 + t.float "amount", limit: 24 + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["currency_id"], name: "index_bit_bay_trades_on_currency_id" + t.index ["tid"], name: "index_bit_bay_trades_on_tid" + end + + create_table "currencies", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t| + t.string "name" + t.string "description" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.boolean "is_active", default: true + t.bigint "market_id" + t.index ["is_active"], name: "index_currencies_on_is_active" + t.index ["market_id"], name: "index_currencies_on_market_id" + end + + create_table "markets", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t| + t.string "name" + t.string "url" + t.boolean "is_active", default: true + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["is_active"], name: "index_markets_on_is_active" + end + + add_foreign_key "bit_bay_trades", "currencies" + add_foreign_key "currencies", "markets" +end diff --git a/test/fixtures/bit_bay_trades.yml b/test/fixtures/bit_bay_trades.yml new file mode 100644 index 0000000..ae0fdb2 --- /dev/null +++ b/test/fixtures/bit_bay_trades.yml @@ -0,0 +1,17 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + currency: one + tid: MyString + date: 2017-11-29 21:41:04 + type: + price: 1.5 + amount: 1.5 + +two: + currency: two + tid: MyString + date: 2017-11-29 21:41:04 + type: + price: 1.5 + amount: 1.5 diff --git a/test/fixtures/currencies.yml b/test/fixtures/currencies.yml new file mode 100644 index 0000000..1a15e34 --- /dev/null +++ b/test/fixtures/currencies.yml @@ -0,0 +1,9 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + name: MyString + description: MyString + +two: + name: MyString + description: MyString diff --git a/test/fixtures/markets.yml b/test/fixtures/markets.yml new file mode 100644 index 0000000..03d32c1 --- /dev/null +++ b/test/fixtures/markets.yml @@ -0,0 +1,11 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + name: MyString + url: MyString + is_active: false + +two: + name: MyString + url: MyString + is_active: false diff --git a/test/models/bit_bay_trade_test.rb b/test/models/bit_bay_trade_test.rb new file mode 100644 index 0000000..13ebbb3 --- /dev/null +++ b/test/models/bit_bay_trade_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class BitBayTradeTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/test/models/currency_test.rb b/test/models/currency_test.rb new file mode 100644 index 0000000..c99429d --- /dev/null +++ b/test/models/currency_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class CurrencyTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/test/models/market_test.rb b/test/models/market_test.rb new file mode 100644 index 0000000..62e1601 --- /dev/null +++ b/test/models/market_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class MarketTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end