class Accern::Alpha

Attributes

base_url[R]
docs[R]
flat[R]
format[R]
index[R]
last_id[R]
new_id[R]
params[R]
ticker[R]
token[R]
uri[R]

Public Class Methods

new(token: nil, ticker: nil, index: nil, format: :json) click to toggle source
# 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

Public Instance Methods

create_uri() click to toggle source
# File lib/accern/alpha.rb, line 55
def create_uri
  uri.query = URI.encode_www_form(combine_query)
  puts uri
end
download(path) click to toggle source
# 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) click to toggle source
# File lib/accern/alpha.rb, line 47
def download_loop(path: nil)
  loop do
    download(path)
    sleep 8
  end
end

Private Instance Methods

combine_query() click to toggle source
# File lib/accern/alpha.rb, line 62
def combine_query
  filters = Hash.new
  filters[:ticker] = ticker unless ticker.empty?
  filters[:last_id] = last_id if last_id
  filters[:index] = index unless index.empty?
  filters
end
csv_header() click to toggle source
# File lib/accern/alpha.rb, line 96
def csv_header
  %w(
    article_id
    story_id
    harvested_at
    entities_name_1
    entities_ticker_1
    entities_global_id_1
    entities_entity_id_1
    entities_type_1
    entities_exchange_1
    entities_sector_1
    entities_industry_1
    entities_country_1
    entities_region_1
    entities_index_1
    entities_competitors_1
    entities_name_2
    entities_ticker_2
    entities_global_id_2
    entities_entity_id_2
    entities_type_2
    entities_exchange_2
    entities_sector_2
    entities_industry_2
    entities_country_2
    entities_region_2
    entities_index_2
    entities_competitors_2
    event_groups_group_1
    event_groups_type_1
    event_groups_group_2
    event_groups_type_2
    story_sentiment
    story_saturation
    story_volume
    story_traffic
    story_shares
    first_mention
    article_type
    article_sentiment
    article_traffic
    source_id
    overall_source_rank
    event_source_rank_1
    event_source_rank_2
    author_id
    overall_author_rank
    event_author_rank_1
    event_author_rank_2
    event_impact_score_overall
    event_impact_score_entity_1
    event_impact_score_entity_2
    avg_day_sentiment_1
    avg_day_sentiment_2
    correlations_max_positive_ticker_1
    correlations_max_positive_value_1
    correlations_max_negative_ticker_1
    correlations_max_negative_value_1
    correlations_max_positive_ticker_2
    correlations_max_positive_value_2
    correlations_max_negative_ticker_2
    correlations_max_negative_value_2
    article_url
  ).join(',')
end
format_response(response) click to toggle source
# File lib/accern/alpha.rb, line 79
def format_response(response)
  @docs = JSON.parse(response.body)
  @new_id = docs.first.to_h.fetch('id', last_id)
end
generate_csv_header(path) click to toggle source
# File lib/accern/alpha.rb, line 90
def generate_csv_header(path)
  unless File.exists?(path)
    File.open(path, 'a') { |f| f.puts csv_header }
  end
end
header() click to toggle source
# File lib/accern/alpha.rb, line 70
def header
  { 'Authorization' => %(Token token="#{token}"),  'User-Agent' => "Accern #{VERSION} (#{RUBY_PLATFORM})" }
end
read_last_id() click to toggle source
# File lib/accern/alpha.rb, line 84
def read_last_id
  if File.exists?('./alpha_last_id.yml')
    @last_id = YAML.load_file('./alpha_last_id.yml')
  end
end
save_last_id() click to toggle source
# File lib/accern/alpha.rb, line 74
def save_last_id
  @last_id = docs.first['id']
  File.write('./alpha_last_id.yml', last_id.to_yaml)
end