Skip to content
This repository has been archived by the owner on Jun 9, 2021. It is now read-only.

Add lastfm adapter #11

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions app/adapters/lastfm_adapter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
class LastfmAdapter < BaseAdapter
class << self
def required_keys
%i(username api_key)
end

def auth_type
:none
end

def website_link
"https://www.last.fm"
end

def title
"Last.fm"
end

def valid_credentials?(credentials)
username, api_key = credentials.values_at(:username, :api_key)
username.present? && api_key.present?
end
end

def scrobbles
username, api_key = credentials.values_at(:username, :api_key)
resp = Faraday.get("http://ws.audioscrobbler.com/2.0/?method=user.getinfo&user=#{username}&api_key=#{api_key}&format=json")

user_info = JSON.parse(resp.body)
if user_info
user_info.fetch("user.playcount").to_i
else
raise "Can't fetch user playcount"
end
end

end
Binary file added app/assets/images/logos/lastfm.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions app/metrics/lastfm/scrobbles.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
PROVIDERS.fetch(:lastfm).register_metric :scrobbles do |metric|
metric.title = "Scrobbles"
metric.description = "Number of scrobbles"

metric.block = proc do |adapter|
Datapoint.new value: adapter.scrobbles
end
end
2 changes: 1 addition & 1 deletion config/initializers/providers.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
PROVIDERS = %i(
googlefit trello pocket beeminder bcycle stackoverflow
googlefit trello pocket beeminder bcycle stackoverflow lastfm
).map do |p_key|
adapter = "#{p_key}_adapter".camelize.constantize
[p_key, Provider.new(p_key, adapter)]
Expand Down