Class: Accern::Alpha

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token: nil, ticker: nil, index: nil, format: :json) ⇒ Alpha

Returns a new instance of Alpha.



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/accern/alpha.rb', line 8

def initialize(token: nil, ticker: nil, index: nil, format: :json)
  @token = token
  @ticker = ticker
  @index = index
  @format = format
  @params = params
  @base_url = 'https://feed.accern.com/v3/alphas'
  @uri = URI(base_url)
  read_last_id
  @flat = Flatner.new
end

Instance Attribute Details

#base_urlObject (readonly)

Returns the value of attribute base_url.



5
6
7
# File 'lib/accern/alpha.rb', line 5

def base_url
  @base_url
end

#docsObject (readonly)

Returns the value of attribute docs.



5
6
7
# File 'lib/accern/alpha.rb', line 5

def docs
  @docs
end

#flatObject (readonly)

Returns the value of attribute flat.



5
6
7
# File 'lib/accern/alpha.rb', line 5

def flat
  @flat
end

#formatObject (readonly)

Returns the value of attribute format.



5
6
7
# File 'lib/accern/alpha.rb', line 5

def format
  @format
end

#indexObject (readonly)

Returns the value of attribute index.



5
6
7
# File 'lib/accern/alpha.rb', line 5

def index
  @index
end

#last_idObject (readonly)

Returns the value of attribute last_id.



5
6
7
# File 'lib/accern/alpha.rb', line 5

def last_id
  @last_id
end

#new_idObject (readonly)

Returns the value of attribute new_id.



5
6
7
# File 'lib/accern/alpha.rb', line 5

def new_id
  @new_id
end

#paramsObject (readonly)

Returns the value of attribute params.



5
6
7
# File 'lib/accern/alpha.rb', line 5

def params
  @params
end

#tickerObject (readonly)

Returns the value of attribute ticker.



5
6
7
# File 'lib/accern/alpha.rb', line 5

def ticker
  @ticker
end

#tokenObject (readonly)

Returns the value of attribute token.



5
6
7
# File 'lib/accern/alpha.rb', line 5

def token
  @token
end

#uriObject (readonly)

Returns the value of attribute uri.



5
6
7
# File 'lib/accern/alpha.rb', line 5

def uri
  @uri
end

Instance Method Details

#create_uriObject



55
56
57
58
# File 'lib/accern/alpha.rb', line 55

def create_uri
  uri.query = URI.encode_www_form(combine_query)
  puts uri
end

#download(path) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/accern/alpha.rb', line 20

def download(path)
  create_uri
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  format_response(http.request_get(uri, header))

  return if new_id == last_id

  save_last_id

  generate_csv_header(path) if format == :csv

  File.open(path, 'a') do |f|
    if format == :json
      docs.reverse_each { |d| f.puts d.to_json }
    end

    if format == :csv
      docs.reverse_each { |d| f.puts flat.process(d) }
    end
  end

rescue => e
  puts e.message
  puts e.backtrace
end

#download_loop(path: nil) ⇒ Object



47
48
49
50
51
52
# File 'lib/accern/alpha.rb', line 47

def download_loop(path: nil)
  loop do
    download(path)
    sleep 8
  end
end