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(options) ⇒ Alpha

Returns a new instance of Alpha.



6
7
8
9
10
11
12
13
14
# File 'lib/accern/alpha.rb', line 6

def initialize(options)
  @token = options.fetch(:token)
  @format = options.fetch(:format, :json)
  @params = options.fetch(:params, {})
  @base_url = 'http://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.



3
4
5
# File 'lib/accern/alpha.rb', line 3

def base_url
  @base_url
end

#docsObject (readonly)

Returns the value of attribute docs.



3
4
5
# File 'lib/accern/alpha.rb', line 3

def docs
  @docs
end

#flatObject (readonly)

Returns the value of attribute flat.



3
4
5
# File 'lib/accern/alpha.rb', line 3

def flat
  @flat
end

#formatObject (readonly)

Returns the value of attribute format.



3
4
5
# File 'lib/accern/alpha.rb', line 3

def format
  @format
end

#last_idObject (readonly)

Returns the value of attribute last_id.



3
4
5
# File 'lib/accern/alpha.rb', line 3

def last_id
  @last_id
end

#new_idObject (readonly)

Returns the value of attribute new_id.



3
4
5
# File 'lib/accern/alpha.rb', line 3

def new_id
  @new_id
end

#paramsObject (readonly)

Returns the value of attribute params.



3
4
5
# File 'lib/accern/alpha.rb', line 3

def params
  @params
end

#tokenObject (readonly)

Returns the value of attribute token.



3
4
5
# File 'lib/accern/alpha.rb', line 3

def token
  @token
end

#uriObject (readonly)

Returns the value of attribute uri.



3
4
5
# File 'lib/accern/alpha.rb', line 3

def uri
  @uri
end

Instance Method Details

#download(path) ⇒ Object



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

def download(path)
  uri.query = URI.encode_www_form(last_id: last_id) if last_id
  puts uri

  format_response(
    Net::HTTP.new(uri.host, uri.port).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) ⇒ Object



45
46
47
48
49
50
# File 'lib/accern/alpha.rb', line 45

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