<commit_before>require "nbs/version"
require "nbs/configuration"
require "httparty"
require 'active_support/all'

class Nbs
  include HTTParty
  extend Configuration

  # define_setting :access_key
  # define_setting :least_favorite_liquid, "seltzer water"

  def initialize(_nbs_id=356, _options = {}) # kanye west
    @nbs_id = "#{_nbs_id}.json"
    @options = {from: (Time.now - 2.day).to_i, to: Time.now.to_i}.merge(_options)
  end

  def all_metrics
    @all_metrics_raw ||= self.class.post("/metrics/artist/#{@nbs_id}", @options).parsed_response
    if @tidied.blank?
      @tidied = {}
      @all_metrics_raw.each do |am|
        @tidied[am["Service"]["name"].downcase] = am["Metric"] if am["Metric"].present?
      end
    end
    @tidied
  end

  def metrics
    if @metrics.blank?
      @metrics = {}
      @metrics[:facebook]  = tidy_metrics(all_metrics['facebook']['fans'])  if all_metrics['facebook']['fans'].present?
      @metrics[:twitter]   = tidy_metrics(all_metrics['twitter']['fans'])   if all_metrics['twitter']['fans'].present?
      @metrics[:youtube]   = tidy_metrics(all_metrics['youtube']['fans'])   if all_metrics['youtube']['fans'].present?
      @metrics[:instagram] = tidy_metrics(all_metrics['instagram']['fans']) if all_metrics['instagram']['fans'].present?
    end
    @metrics
  end

  def self.artist_search(_name)
    if @name != _name
      @name = _name
      result = get("/artists/search.json", { query: {q: "#{_name}"}}).parsed_response
      @artist_search = []
      result.each do |id, values|
        @artist_search << {id: id, name: values["name"], music_brainz_id: values["music_brainz_id"]}
      end
    end
    @artist_search
  end

  def self.setup(_key)
    base_uri "#{_key}.api3.nextbigsound.com"
  end

  private

  def tidy_metrics(_arry)
    _arry.map do |day, value|
      {time: Time.at(day.to_i.days.to_i).utc, volume: value}
    end
  end

end<commit_msg>artist_search no longer stores results<commit_after>require "nbs/version"
require "nbs/configuration"
require "httparty"
require 'active_support/all'

class Nbs
  include HTTParty
  extend Configuration

  # define_setting :access_key
  # define_setting :least_favorite_liquid, "seltzer water"

  def initialize(_nbs_id=356, _options = {}) # kanye west
    @nbs_id = "#{_nbs_id}.json"
    @options = {from: (Time.now - 2.day).to_i, to: Time.now.to_i}.merge(_options)
  end

  def all_metrics
    @all_metrics_raw ||= self.class.post("/metrics/artist/#{@nbs_id}", @options).parsed_response
    if @tidied.blank?
      @tidied = {}
      @all_metrics_raw.each do |am|
        @tidied[am["Service"]["name"].downcase] = am["Metric"] if am["Metric"].present?
      end
    end
    @tidied
  end

  def metrics
    if @metrics.blank?
      @metrics = {}
      @metrics[:facebook]  = tidy_metrics(all_metrics['facebook']['fans'])  if all_metrics['facebook']['fans'].present?
      @metrics[:twitter]   = tidy_metrics(all_metrics['twitter']['fans'])   if all_metrics['twitter']['fans'].present?
      @metrics[:youtube]   = tidy_metrics(all_metrics['youtube']['fans'])   if all_metrics['youtube']['fans'].present?
      @metrics[:instagram] = tidy_metrics(all_metrics['instagram']['fans']) if all_metrics['instagram']['fans'].present?
    end
    @metrics
  end

  def self.artist_search(_name)
    result = get("/artists/search.json", { query: {q: "#{_name}"}}).parsed_response
    @artist_search = []
    result.each do |id, values|
      @artist_search << {nbs_id: id, name: values["name"], music_brainz_id: values["music_brainz_id"]}
    end
  rescue
    []
  end

  def self.setup(_key)
    base_uri "#{_key}.api3.nextbigsound.com"
  end

  private

  def tidy_metrics(_arry)
    _arry.map do |day, value|
      {time: Time.at(day.to_i.days.to_i).utc, volume: value}
    end
  end

end
{'fraction_non_alphanumeric': 0.15850447604002108, 'fraction_numerical': 0.0026329647182727752, 'mean_word_length': 3.8456632653061225, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3332585', 'n_tokens_mistral': 1356, 'n_tokens_neox': 1326, 'n_words': 245}