Class: Iro::Iro

Inherits:
Object
  • Object
show all
Defined in:
lib/iron_warbler.rb

Class Method Summary collapse

Class Method Details

.get_coinsObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/iron_warbler.rb', line 15

def self.get_coins
  out = HTTParty.get( "https://pro-api.coinmarketcap.com/v2/cryptocurrency/quotes/latest?slug=bitcoin,ethereum", {
    headers: { 'X-CMC_PRO_API_KEY' => COINMARKETCAP[:key] },
  })
  out = out.parsed_response.deep_symbolize_keys
  out[:data].each do |k, item|
    opi = Iro::Datapoint.new({
      date:     Time.now.to_date,
      kind:     Iro::Datapoint::KIND_CRYPTO,
      symbol:   item[:symbol],
      quote_at: item[:quote][:USD][:last_updated],
      value:    item[:quote][:USD][:price],
      volume:   item[:quote][:USD][:volume_24h],
    })
    opi.save!
  end
end

.get_currenciesObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/iron_warbler.rb', line 33

def self.get_currencies
  out = HTTParty.get "https://api.currencyfreaks.com/v2.0/rates/latest?apikey=#{CURRENCYFREAKS[:key]}&symbols=COP,EUR,JPY"
  out = out.parsed_response.deep_symbolize_keys!
  out[:rates].each do |currency, value|

    # opi = OPI.new({
    #   putCall:     'CURRENCY',
    #   symbol:      currency,
    #   ticker:      currency,
    #   exchangeName: 'currencyfreaks',
    #   mark:        value,
    #   lastPrice:   value,
    #   timestamp:   Time.now.to_date,
    # })
    # opi.save!

    datapoint = Iro::Datapoint.new({
      date: Time.now.to_date,
      quote_at: Time.now,
      kind: Iro::Datapoint::KIND_CURRENCY,
      symbol: currency,
      value: value,
    })
    datapoint.save!

    print '^'
  end
end

.get_treasuriesObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/iron_warbler.rb', line 62

def self.get_treasuries
  response = HTTParty.get( "https://home.treasury.gov/resource-center/data-chart-center/interest-rates/daily-treasury-rates.csv/all/#{Time.now.strftime('%Y%m')}?type=daily_treasury_yield_curve&field_tdr_date_value_month=#{Time.now.strftime('%Y%m')}&page&_format=csv")
  outs = CSV.parse( response.body, { headers: true })
  out = outs[0]
  date = Date.strptime(out['Date'], '%m/%d/%Y')
  {
    '1 Mo'  => Iro::Datapoint::SYMBOL_T1MO,
    '2 Mo'  => Iro::Datapoint::SYMBOL_T2MO,
    '3 Mo'  => Iro::Datapoint::SYMBOL_T3MO,
    '4 Mo'  => Iro::Datapoint::SYMBOL_T4MO,
    '6 Mo'  => Iro::Datapoint::SYMBOL_T6MO,
    '1 Yr'  => Iro::Datapoint::SYMBOL_T1YR,
    '2 Yr'  => Iro::Datapoint::SYMBOL_T2YR,
    '3 Yr'  => Iro::Datapoint::SYMBOL_T3YR,
    '5 Yr'  => Iro::Datapoint::SYMBOL_T5YR,
    '7 Yr'  => Iro::Datapoint::SYMBOL_T7YR,
    '10 Yr' => Iro::Datapoint::SYMBOL_T10YR,
    '20 Yr' => Iro::Datapoint::SYMBOL_T20YR,
    '30 Yr' => Iro::Datapoint::SYMBOL_T30YR,
  }.each do |k, v|
    opi = Iro::Datapoint.new({
      date:     date,
      quote_at: date,
      kind:     Iro::Datapoint::KIND_TREASURY,
      symbol:   v,
      value:    out[k],
    })
    opi.save!
    print '^'
  end
end

.import_1990_2023_treasuriesObject



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/iron_warbler.rb', line 94

def self.import_1990_2023_treasuries
  outs = CSV.parse( File.read( Rails.root.join('data', 'treasuries', '1990..23 daily-treasury-rates.csv') ), { headers: true })
  outs.each do |out|
    date = Date.strptime(out['Date'], '%m/%d/%y')
    {
      '1 Mo'  => Iro::Datapoint::SYMBOL_T1MO,
      '2 Mo'  => Iro::Datapoint::SYMBOL_T2MO,
      '3 Mo'  => Iro::Datapoint::SYMBOL_T3MO,
      '4 Mo'  => Iro::Datapoint::SYMBOL_T4MO,
      '6 Mo'  => Iro::Datapoint::SYMBOL_T6MO,
      '1 Yr'  => Iro::Datapoint::SYMBOL_T1YR,
      '2 Yr'  => Iro::Datapoint::SYMBOL_T2YR,
      '3 Yr'  => Iro::Datapoint::SYMBOL_T3YR,
      '5 Yr'  => Iro::Datapoint::SYMBOL_T5YR,
      '7 Yr'  => Iro::Datapoint::SYMBOL_T7YR,
      '10 Yr' => Iro::Datapoint::SYMBOL_T10YR,
      '20 Yr' => Iro::Datapoint::SYMBOL_T20YR,
      '30 Yr' => Iro::Datapoint::SYMBOL_T30YR,
    }.each do |k, v|
      if out[k]
        opi = Iro::Datapoint.new({
          date:     date,
          quote_at: date,
          kind:     Iro::Datapoint::KIND_TREASURY,
          symbol:   v,
          value:    out[k],
        })
        begin
          opi.save!
        rescue Mongoid::Errors::Validations => err
          puts! err, 'err'
        end
        print '^'
      end
    end
  end
end

.import_2024_treasuriesObject



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/iron_warbler.rb', line 132

def self.import_2024_treasuries
  outs = CSV.parse( File.read( Rails.root.join('data', 'treasuries', '2024 daily-treasury-rates.csv') ), { headers: true })
  outs.each do |out|
    date = Date.strptime(out['Date'], '%m/%d/%Y')
    {
      '1 Mo'  => Iro::Datapoint::SYMBOL_T1MO,
      '2 Mo'  => Iro::Datapoint::SYMBOL_T2MO,
      '3 Mo'  => Iro::Datapoint::SYMBOL_T3MO,
      '4 Mo'  => Iro::Datapoint::SYMBOL_T4MO,
      '6 Mo'  => Iro::Datapoint::SYMBOL_T6MO,
      '1 Yr'  => Iro::Datapoint::SYMBOL_T1YR,
      '2 Yr'  => Iro::Datapoint::SYMBOL_T2YR,
      '3 Yr'  => Iro::Datapoint::SYMBOL_T3YR,
      '5 Yr'  => Iro::Datapoint::SYMBOL_T5YR,
      '7 Yr'  => Iro::Datapoint::SYMBOL_T7YR,
      '10 Yr' => Iro::Datapoint::SYMBOL_T10YR,
      '20 Yr' => Iro::Datapoint::SYMBOL_T20YR,
      '30 Yr' => Iro::Datapoint::SYMBOL_T30YR,
    }.each do |k, v|
      if out[k]
        opi = Iro::Datapoint.new({
          date:     date,
          quote_at: date,
          kind:     Iro::Datapoint::KIND_TREASURY,
          symbol:   v,
          value:    out[k],
        })
        begin
          opi.save!
        rescue Mongoid::Errors::Validations => err
          puts! err, 'err'
        end
        print '^'
      end
    end
  end
end

.schwab_syncObject



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/iron_warbler.rb', line 170

def self.schwab_sync
  profile = Wco::Profile.find_by email: 'piousbox@gmail.com'
  out = Schwab.post( "https://api.schwabapi.com/v1/oauth/token", {
    headers: { "Content-Type": "application/x-www-form-urlencoded" },
    basic_auth: { username: SCHWAB_DATA[:key], password: SCHWAB_DATA[:secret] },
    body: {
      grant_type: 'refresh_token',
      refresh_token: profile.schwab_refresh_token
    },
  })
  out = out.parsed_response
  puts! out, 'out'

  attrs = {
    schwab_access_token:  out['access_token'],
    schwab_refresh_token: out['refresh_token'],
    schwab_id_token:      out['id_token'],
  }
  # puts! attrs, 'attrs'

  if attrs[:schwab_refresh_token]
    profile.update(attrs)
    profile.save!
    return attrs
  else
    return false
  end
end