Module: MrEko

Defined in:
lib/mr_eko.rb,
lib/mr_eko/core.rb,
lib/mr_eko/exceptions.rb

Defined Under Namespace

Modules: Core, Presets Classes: EnmfpError, InvalidAttributes, NoSongsError, Playlist, PlaylistEntry, Song, TagParser, TimedPlaylist

Constant Summary collapse

VERSION =
'0.7.0'
USER_DIR =
File.join(ENV['HOME'], ".mreko")
FINGERPRINTS_DIR =
File.join(USER_DIR, 'fingerprints')
LOG_DIR =
File.join(USER_DIR, 'logs')
HOME_DIR =
File.join(File.dirname(__FILE__), '..')
MODES =
%w(minor major).freeze
CHROMATIC_SCALE =
%w(C C# D D# E F F# G G# A A# B).freeze

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.loggerObject

Returns the value of attribute logger.



35
36
37
# File 'lib/mr_eko.rb', line 35

def logger
  @logger
end

Class Method Details

.api_keyObject



105
106
107
108
109
110
# File 'lib/mr_eko.rb', line 105

def api_key
  [File.join(USER_DIR, 'echonest_api.key'), File.join(HOME_DIR, 'echonest_api.key')].each do |file|
    return file if File.exists?(file)
  end
  raise "You need to create an echonest_api.key file in #{USER_DIR}"
end

.connectionObject



41
42
43
# File 'lib/mr_eko.rb', line 41

def connection
  @connection
end

.db_nameObject



101
102
103
# File 'lib/mr_eko.rb', line 101

def db_name
  env == 'test' ? File.join('db', 'eko_test.db') : File.join(USER_DIR, 'eko.db')
end

.enmfp_binaryObject

Use the platform-specific binary.



128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/mr_eko.rb', line 128

def enmfp_binary
  bin = case ruby_platform
  when /darwin/
    'codegen.Darwin'
  when /686/
    'codegen.Linux-i686'
  when /x86/
    'codegen.Linux-x86_64'
  else
    'codegen.windows.exe'
  end

  File.join(HOME_DIR, 'ext', 'enmfp', bin)
end

.envObject



37
38
39
# File 'lib/mr_eko.rb', line 37

def env
  EKO_ENV
end

.handle_rate_limitObject

A bit ghetto since we can’t easily access the HTTP headers Echonest returns detailing the rate limit. @TODO look at fixing this later.



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/mr_eko.rb', line 53

def handle_rate_limit

  requests_in_window = @connection[:api_logs].where("created_on >= ?", Time.now - 60).count

  if requests_in_window >= 120
    puts "Throttling you..."
    sleep 1
  end

  @connection[:api_logs].insert(:created_on => Time.now)
end

.key_letter(key) ⇒ Object

Takes an integer and returns its standard (chromatic) representation.



123
124
125
# File 'lib/mr_eko.rb', line 123

def key_letter(key)
  CHROMATIC_SCALE[key]
end

.key_lookup(key_letter) ⇒ Object

Takes a chromatic key (eg: G#) and returns its integer representation.



118
119
120
# File 'lib/mr_eko.rb', line 118

def key_lookup(key_letter)
  CHROMATIC_SCALE.index(key_letter.upcase)
end

.md5(filename) ⇒ Object



65
66
67
# File 'lib/mr_eko.rb', line 65

def md5(filename)
  Digest::MD5.hexdigest(open(filename).read)
end

.mode_lookup(mode) ⇒ Object

Takes ‘minor’ or ‘major’ and returns its integer representation.



113
114
115
# File 'lib/mr_eko.rb', line 113

def mode_lookup(mode)
  MODES.index(mode.to_s.downcase)
end

.nestObject



45
46
47
48
# File 'lib/mr_eko.rb', line 45

def nest
  handle_rate_limit
  @nest
end

.ruby_platformObject



143
144
145
# File 'lib/mr_eko.rb', line 143

def ruby_platform
  RUBY_PLATFORM
end

.setup!Object



69
70
71
72
73
74
# File 'lib/mr_eko.rb', line 69

def setup!
  setup_directories!
  setup_logger!
  setup_db!
  setup_echonest!
end

.setup_db!Object



88
89
90
91
92
93
94
95
# File 'lib/mr_eko.rb', line 88

def setup_db!
  return @connection if @connection
  @connection = Sequel.sqlite(db_name)
  @connection.loggers << @logger

  File.unlink(MrEko.db_name) if File.exist?(MrEko.db_name) && env == 'test'
  Sequel::Migrator.apply(MrEko.connection, File.join(File.dirname(__FILE__), "..", "db", "migrate"))
end

.setup_directories!Object



82
83
84
85
86
# File 'lib/mr_eko.rb', line 82

def setup_directories!
  Dir.mkdir(USER_DIR) unless File.directory?(USER_DIR)
  Dir.mkdir(FINGERPRINTS_DIR) unless File.directory?(FINGERPRINTS_DIR)
  Dir.mkdir(LOG_DIR) unless File.directory?(LOG_DIR)
end

.setup_echonest!Object



97
98
99
# File 'lib/mr_eko.rb', line 97

def setup_echonest!
  @nest ||= Echonest(File.read(api_key))
end

.setup_logger!Object

Output to STDOUT in debug, otherwise, save to logfile



77
78
79
80
# File 'lib/mr_eko.rb', line 77

def setup_logger!
  out = ENV['DEBUG'] ? STDOUT : File.join(LOG_DIR, "#{env}.log")
  @logger ||= Logger.new(out)
end