listenbrainz 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: aa91d4869adbba2530463a1a70dac945046de4f49701cda50e28fd3d87c243bb
4
+ data.tar.gz: 921fbcd3b84c5cb3212fc713c8662f31a216784d6c874b5fddba0527c67f5a5b
5
+ SHA512:
6
+ metadata.gz: 6c32fdddb83cff2bbbcd74f36904ae4d8869cf5718cd4c05f0c6313afa9532b485b888965b828c72660a79316540ccecd670932aa9d30abc3d2ac4b63b47ff78
7
+ data.tar.gz: 9b11e3008645817ba78d4d00aaa8322057e7d3da3dcfe4c842acdd44da3f46fa80a35b4e6e9f58c8e6be672281c70b6b289774fc0738a96c60748e4036c4b223
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.byebug_history
3
+ /.env
4
+ /.rspec_status
5
+ /.yardoc
6
+ /coverage/
7
+ /doc/
8
+ /pkg/
9
+ /tmp/
data/.gitlab-ci.yml ADDED
@@ -0,0 +1,76 @@
1
+ ---
2
+ stages:
3
+ - test
4
+ - release
5
+ - deploy
6
+
7
+ spec:
8
+ stage: test
9
+ image: ruby:${RUBY_VERSION}
10
+ before_script:
11
+ - gem install bundler -v 2.4.19
12
+ - bundle config set --local path '.bundle'
13
+ - bundle install
14
+ script:
15
+ - bundle exec rake spec
16
+ coverage: '/Line Coverage: \d+.\d+\%/'
17
+ artifacts:
18
+ reports:
19
+ coverage_report:
20
+ coverage_format: cobertura
21
+ path: coverage/coverage.xml
22
+ cache:
23
+ paths:
24
+ - .bundle
25
+ parallel:
26
+ # FIXME: Matrices seem to break Cobertura from displaying.
27
+ matrix:
28
+ - RUBY_VERSION: ['3.2', '3.3', '3.4']
29
+
30
+ .on-release:
31
+ rules:
32
+ # Only for tags, i.e. releases.
33
+ - if: $CI_COMMIT_TAG
34
+
35
+ release:
36
+ extends:
37
+ - .on-release
38
+ stage: release
39
+ image: ruby:3.2
40
+ before_script:
41
+ - gem install bundler -v 2.4.19
42
+ - bundle config set --local path '.bundle'
43
+ - bundle install
44
+ script:
45
+ - bundle exec rake release:rubygem_push
46
+
47
+ gitlab:
48
+ extends:
49
+ - .on-release
50
+ stage: deploy
51
+ image: registry.gitlab.com/gitlab-org/release-cli:latest
52
+ script:
53
+ - echo "Creating GitLab release for $CI_COMMIT_TAG"
54
+ release:
55
+ tag_name: $CI_COMMIT_TAG
56
+ description: ./CHANGELOG.md
57
+ ref: '$CI_COMMIT_SHA'
58
+ assets:
59
+ links:
60
+ - name: RubyGems
61
+ url: https://rubygems.org/gems/listenbrainz/versions/$CI_COMMIT_TAG
62
+ pages:
63
+ extends:
64
+ - .on-release
65
+ stage: deploy
66
+ image: ruby:3.2
67
+ before_script:
68
+ - gem install bundler -v 2.4.19
69
+ - bundle config set --local path '.bundle'
70
+ - bundle install
71
+ script:
72
+ - bundle exec rake yard
73
+ artifacts:
74
+ paths:
75
+ - doc
76
+ publish: doc
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,33 @@
1
+ ---
2
+ require:
3
+ - rubocop-factory_bot
4
+ - rubocop-rake
5
+ - rubocop-rspec
6
+ - rubocop-performance
7
+
8
+ AllCops:
9
+ DisplayCopNames: true
10
+ NewCops: enable
11
+ TargetRubyVersion: 3.2
12
+
13
+ Layout/ArgumentAlignment:
14
+ EnforcedStyle: with_fixed_indentation
15
+ Layout/CaseIndentation:
16
+ EnforcedStyle: end
17
+ Layout/FirstArgumentIndentation:
18
+ EnforcedStyle: consistent
19
+ Layout/LineLength:
20
+ Max: 80
21
+ Layout/MultilineMethodCallIndentation:
22
+ EnforcedStyle: indented
23
+
24
+ RSpec/ExampleLength:
25
+ Max: 10
26
+ RSpec/MultipleMemoizedHelpers:
27
+ Max: 10
28
+ RSpec/SpecFilePathFormat:
29
+ Exclude:
30
+ - spec/listenbrainz/models/**
31
+ CustomTransform:
32
+ ListenBrainz: listenbrainz
33
+
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 3.2
@@ -0,0 +1,29 @@
1
+ {
2
+ // Place your listenbrainz workspace snippets here. Each snippet is defined under a snippet name and has a scope, prefix, body and
3
+ // description. Add comma separated ids of the languages where the snippet is applicable in the scope field. If scope
4
+ // is left empty or omitted, the snippet gets applied to all languages. The prefix is what is
5
+ // used to trigger the snippet and the body will be expanded and inserted. Possible variables are:
6
+ // $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders.
7
+ // Placeholders with the same ids are connected.
8
+ // Example:
9
+ // "Print to console": {
10
+ // "scope": "javascript,typescript",
11
+ // "prefix": "log",
12
+ // "body": [
13
+ // "console.log('$1');",
14
+ // "$2"
15
+ // ],
16
+ // "description": "Log output to console"
17
+ // }
18
+ "Ruby template file": {
19
+ "isFileTemplate": true,
20
+ "scope": "ruby",
21
+ "prefix": "rubyTemplate",
22
+ "body": [
23
+ "# frozen_string_literal: true",
24
+ "",
25
+ "$0"
26
+ ],
27
+ "description": "Ruby template"
28
+ }
29
+ }
data/CHANGELOG.md ADDED
@@ -0,0 +1,13 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog][kac], and this project adheres to
6
+ [Semantic Versioning][semver].
7
+
8
+ [kac]: https://keepachangelog.com/en/1.1.0/
9
+ [semver]: https://semver.org/spec/v2.0.0.html
10
+
11
+ ## [0.1.0] - 2025-01-31
12
+
13
+ - Initial release
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at hello@richarddegenne.fr. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Guardfile ADDED
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ guard :rspec, cmd: 'bundle exec rspec' do
4
+ require 'guard/rspec/dsl'
5
+ dsl = Guard::RSpec::Dsl.new(self)
6
+
7
+ # Feel free to open issues for suggestions and improvements
8
+ notification :off
9
+
10
+ # RSpec files
11
+ rspec = dsl.rspec
12
+ watch(rspec.spec_helper) { rspec.spec_dir }
13
+ watch(rspec.spec_support) { rspec.spec_dir }
14
+ watch(rspec.spec_files)
15
+
16
+ # Ruby files
17
+ ruby = dsl.ruby
18
+ dsl.watch_spec_files_for(ruby.lib_files)
19
+ end
20
+
21
+ guard :yard, server: false do
22
+ watch(%r{lib/.+\.rb})
23
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2025 Richard Degenne
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,142 @@
1
+ # ListenBrainz
2
+
3
+ A Ruby wrapper to the [ListenBrainz][lb] API.
4
+
5
+ [lb]: https://listenbrainz.org
6
+
7
+ [![pipeline status](https://gitlab.com/Richard-Degenne/listenbrainz/badges/dev/pipeline.svg)](https://gitlab.com/Richard-Degenne/listenbrainz/-/commits/dev)
8
+ [![coverage report](https://gitlab.com/Richard-Degenne/listenbrainz/badges/dev/coverage.svg)](https://gitlab.com/Richard-Degenne/listenbrainz/-/commits/dev)
9
+ [![Latest Release](https://gitlab.com/Richard-Degenne/listenbrainz/-/badges/release.svg)](https://gitlab.com/Richard-Degenne/listenbrainz/-/releases)
10
+
11
+ ## Installation
12
+
13
+ Install the gem and add to the application's Gemfile by executing:
14
+
15
+ ```bash
16
+ bundle add listenbrainz
17
+ ```
18
+
19
+ If bundler is not being used to manage dependencies, install the gem by
20
+ executing:
21
+
22
+ ```bash
23
+ gem install listenbrainz
24
+ ```
25
+
26
+ ## Usage
27
+
28
+ ### Configuration
29
+
30
+ The ListenBrainz gem can be configured either through environment variables or
31
+ programatically.
32
+
33
+ #### Environment variables
34
+
35
+ | Name | Default | Description |
36
+ |------|---------|-------------|
37
+ | `LISTENBRAINZ_API_ENDPOINT` | `https://api.listenbrainz.org` | URL to the ListenBrainz API |
38
+ | `LISTENBRAINZ_USER_TOKEN` | Required | Used for authentication. You can find your user token on [ListenBrainz's settings page][lb-settings]. |
39
+
40
+ [lb-settings]: https://listenbrainz.org/settings/
41
+
42
+ #### Programatically
43
+
44
+ ```ruby
45
+ ListenBrainz.configure do |config|
46
+ config.endpoint = '<CUSTOM_LISTENBRAINZ_URL>'
47
+ config.user_token = '<USER_TOKEN>'
48
+ end
49
+ ```
50
+
51
+ ### Retrieving listens
52
+
53
+ ```ruby
54
+ # Retrieve listens for the current user.
55
+ ListenBrainz::Listen.get
56
+ ListenBrainz::Listen.get(count: 10)
57
+
58
+ # Retrieve listens for a different user
59
+ ListenBrainz::Listen.get('Bobby')
60
+ ListenBrainz::Listen.get('Bobby', count: 10)
61
+
62
+ # Retrieve "Now playing" listens for the current user
63
+ ListenBrainz::Listen.now_playing
64
+
65
+ # Retrieve "Now playing" listens for a different user
66
+ ListenBrainz::Listen.now_playing('Bobby')
67
+ ```
68
+
69
+ ### Submitting listens
70
+
71
+ ```ruby
72
+ listen = ListenBrainz::Listen.new(
73
+ listened_at: Time.now,
74
+ track_metadata: {
75
+ artist_name: 'Joan Jett and the Blackhearts',
76
+ release_name: 'Sinner',
77
+ track_name: 'Naked',
78
+ additional_info: {
79
+ listening_from: 'Plex',
80
+ media_player: 'Plex',
81
+ track_mbid: '2b66948e-fd60-3312-a8cb-22fdecbc98b9'
82
+ }
83
+ }
84
+ )
85
+
86
+ # Submit a single listen
87
+ listen.submit
88
+ listen.submit(mode: :single)
89
+
90
+ # Submit a "Now playing" listen
91
+ listen.submit(mode: :playing_now)
92
+
93
+ # Submit a batch of listens
94
+ ListenBrainz::Listen.sumbit([listen])
95
+ ```
96
+
97
+ ## Documentation
98
+
99
+ A complete YARD documentation is available below.
100
+
101
+ <https://listenbrainz.richarddegenne.fr>
102
+
103
+ ## Development
104
+
105
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run
106
+ `rake spec` to run the tests. You can also run `bin/console` for an interactive
107
+ prompt that will allow you to experiment.
108
+
109
+ ### Visual Studio Code workspace
110
+
111
+ The `listenbrainz.code-workspace` file contains recommended settings and
112
+ extensions to work with Visual Studio Code. Open the workspace by running `code
113
+ listenbrainz.code-workspace`.
114
+
115
+ ### Hot test runs
116
+
117
+ The development dependencies include [Guard][guard] to run tests and generate
118
+ documentation whenever you edit a file.
119
+
120
+ [guard]: https://github.com/guard/guard
121
+
122
+ Run `bundle exec guard` to start watching.
123
+
124
+ ## Contributing
125
+
126
+ Bug reports and pull requests are welcome on GitLab at
127
+ <https://gitlab.com/Richard-Degenne/listenbrainz>. This project is intended to
128
+ be a safe, welcoming space for collaboration, and contributors are expected to
129
+ adhere to the [code of conduct][coc].
130
+
131
+ [coc]: https://gitlab.com/Richard-Degenne/listenbrainz/blob/master/CODE_OF_CONDUCT.md
132
+
133
+ ## License
134
+
135
+ The gem is available as open source under the terms of the [MIT License][mit].
136
+
137
+ [mit]: https://opensource.org/licenses/MIT
138
+
139
+ ## Code of Conduct
140
+
141
+ Everyone interacting in the ListenBrainz project's codebases, issue trackers,
142
+ chat rooms and mailing lists is expected to follow the [code of conduct][coc].
data/Rakefile ADDED
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require 'rubocop/rake_task'
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ require 'yard'
13
+
14
+ YARD::Rake::YardocTask.new
15
+
16
+ task default: %i[spec rubocop yard]
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ListenBrainz
4
+ ##
5
+ # HTTP client for ListenBrainz.
6
+ class Client
7
+ ##
8
+ # @param [Configuration] configuration
9
+ def initialize(configuration = ListenBrainz.configuration)
10
+ @configuration = configuration
11
+ end
12
+
13
+ ##
14
+ # @return [Configuration]
15
+ attr_reader :configuration
16
+
17
+ ##
18
+ # Retrieves the username associated with the configured token.
19
+ #
20
+ # @return [String]
21
+ def username
22
+ @username ||= query(:get, '/1/validate-token').fetch(:user_name)
23
+ end
24
+
25
+ ##
26
+ # @param [String, Symbol] method
27
+ # @param [String] path
28
+ # @param [Hash{String, Symbol => Object}] params
29
+ # @param [Array, Hash, nil] json JSON object to be sent as a body
30
+ #
31
+ # @return [Array, Hash]
32
+ # @raise [HTTPX::Error]
33
+ def query(method, path, params: {}, json: nil)
34
+ httpx.request(method, path, params:, json:).raise_for_status
35
+ .json(symbolize_names: true)
36
+ end
37
+
38
+ private
39
+
40
+ ##
41
+ # @return [HTTPX::Session]
42
+ def httpx
43
+ @httpx ||= HTTPX.with(
44
+ origin: configuration.endpoint,
45
+ headers: { authorization: "Token #{configuration.user_token}" }
46
+ )
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ListenBrainz
4
+ ##
5
+ # Stores configuration for the ListenBrainz gem
6
+ class Configuration
7
+ ##
8
+ # @return [URI]
9
+ def endpoint
10
+ @endpoint ||= URI('https://api.listenbrainz.org')
11
+ end
12
+
13
+ ##
14
+ # @param [String, URI, nil] new_endpoint
15
+ def endpoint=(new_endpoint)
16
+ @endpoint = new_endpoint && URI(new_endpoint)
17
+ end
18
+
19
+ ##
20
+ # @return [String, nil]
21
+ attr_accessor :user_token
22
+ end
23
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ListenBrainz
4
+ ##
5
+ # Base class for ListenBrainz models.
6
+ class Base < Dry::Struct
7
+ ##
8
+ # Returns the model as a ListenBrainz JSON object.
9
+ #
10
+ # @return [Hash{Symbol => Object}]
11
+ def as_json
12
+ attributes.transform_values do |value|
13
+ case value
14
+ when Array
15
+ value.map { |item| item.try(:as_json) || item }
16
+ when Time
17
+ # HACK: ListenBrainz expects timestamps as Unix timestamps.
18
+ #
19
+ # TODO: Embed this in the `Types::Timestamp` class and use
20
+ # introspection here instead?
21
+ value.to_i
22
+ else
23
+ value.try(:as_json) || value
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,190 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ListenBrainz
4
+ ##
5
+ # Model class for listens.
6
+ #
7
+ # @see https://rain0r.github.io/listenbrainz-openapi/index.html#/lbCore/listensForUser
8
+ class Listen < Base
9
+ ##
10
+ # Retrieves listens for a given user.
11
+ #
12
+ # @param [String] username
13
+ # @param [Integer, nil] count Number of listens to retrieve
14
+ # @param [Time, Integer, nil] before Only retrieve listens before this time
15
+ # @param [Time, Integer, nil] after Only retrieve listens after this time
16
+ #
17
+ # @return [Payload]
18
+ def self.get(
19
+ username = ListenBrainz.client.username, count: nil, before: nil,
20
+ after: nil
21
+ )
22
+ max_ts = before && Types::Timestamp[before].to_i
23
+ min_ts = after && Types::Timestamp[after].to_i
24
+ params = { count:, min_ts:, max_ts: }.compact
25
+
26
+ ListenBrainz.client.query(:get, "1/user/#{username}/listens", params:)
27
+ .fetch(:payload).then { Payload.new(_1) }
28
+ end
29
+
30
+ ##
31
+ # Retrieves the "now playing" listens for a given user.
32
+ #
33
+ # @param [String] username
34
+ #
35
+ # @return [Payload]
36
+ def self.now_playing(username = ListenBrainz.client.username)
37
+ ListenBrainz.client.query(:get, "1/user/#{username}/playing-now")
38
+ .fetch(:payload).then { Payload.new(_1) }
39
+ end
40
+
41
+ ##
42
+ # Imports a batch of listens.
43
+ #
44
+ # @param [Listen, Array<Listen>] listens
45
+ # @param [Symbol] mode +single+, +playing_now+ or +import+
46
+ #
47
+ # @return [Void]
48
+ def self.submit(listens, mode: :import)
49
+ ListenBrainz.client.query(:post, '1/submit-listens',
50
+ json: { listen_type: mode, payload: Array(listens).map(&:as_json) })
51
+ end
52
+
53
+ ##
54
+ # Submits the listen to ListenBrainz.
55
+ #
56
+ # @param [Symbol] mode +single+ or +playing_now+
57
+ #
58
+ # @return [Void]
59
+ def submit(mode: :single)
60
+ self.class.submit(self, mode:)
61
+ end
62
+
63
+ ##
64
+ # Model class for listen payloads.
65
+ #
66
+ # These payloads contain the actual listens, as well as additional metadata
67
+ # for pagination.
68
+ class Payload < Base
69
+ ##
70
+ # @!attribute [r] latest_listen_ts
71
+ # @return [Time, nil]
72
+ attribute? :latest_listen_ts, Types::Timestamp
73
+ ##
74
+ # @!attribute [r] oldest_listen_ts
75
+ # @return [Time, nil]
76
+ attribute? :oldest_listen_ts, Types::Timestamp
77
+ ##
78
+ # @!attribute [r] user_id
79
+ # @return [String]
80
+ attribute :user_id, Types::String
81
+
82
+ ##
83
+ # @!attribute [r] listens
84
+ # @return [Array<Listen>]
85
+ attribute :listens, Types::Array.of(Listen)
86
+
87
+ ##
88
+ # Retrieves older listens.
89
+ #
90
+ # @param [Integer, nil] count
91
+ #
92
+ # @return [Payload] A new payload with older listens
93
+ def older(count = nil)
94
+ before = listens.map(&:listened_at).min
95
+ Listen.get(user_id, count:, before:)
96
+ end
97
+
98
+ ##
99
+ # Retrieves newer listens.
100
+ #
101
+ # @param [Integer, nil] count
102
+ #
103
+ # @return [Payload] A new payload with newer listens
104
+ def newer(count = nil)
105
+ after = listens.map(&:listened_at).max
106
+ Listen.get(user_id, count:, after:)
107
+ end
108
+ end
109
+
110
+ ##
111
+ # @!attribute [r] inserted_at
112
+ # @return [Time, nil]
113
+ attribute? :inserted_at, Types::Timestamp
114
+ ##
115
+ # @!attribute [r] listened_at
116
+ # @return [Time, nil]
117
+ attribute? :listened_at, Types::Timestamp
118
+ ##
119
+ # @!attribute [r] recording_msid
120
+ # @return [String, nil]
121
+ attribute? :recording_msid, Types::String
122
+
123
+ ##
124
+ # Model class for track metadata.
125
+ class TrackMetadata < Base
126
+ ##
127
+ # @!attribute [r] artist_name
128
+ # @return [String]
129
+ attribute :artist_name, Types::String
130
+ ##
131
+ # @!attribute [r] release_name
132
+ # @return [String]
133
+ attribute :release_name, Types::String
134
+ ##
135
+ # @!attribute [r] track_name
136
+ # @return [String]
137
+ attribute :track_name, Types::String
138
+
139
+ ##
140
+ # Model class for listen MBID mappings.
141
+ class MBIDMapping < Base
142
+ ##
143
+ # @!attribute [r] artist_mbids
144
+ # @return [Array<String>, nil]
145
+ attribute? :artist_mbids, Types::Array.of(Types::String)
146
+ ##
147
+ # @!attribute [r] release_mbid
148
+ # @return [String, nil]
149
+ attribute? :release_mbid, Types::String
150
+ ##
151
+ # @!attribute [r] recording_mbid
152
+ # @return [String]
153
+ attribute :recording_mbid, Types::String
154
+ end
155
+ ##
156
+ # @!attribute [r] mbid_mapping
157
+ # @return [MBIDMapping, nil]
158
+ attribute? :mbid_mapping, MBIDMapping
159
+
160
+ ##
161
+ # Model class for listen additional info.
162
+ class AdditionalInfo < Base
163
+ ##
164
+ # @!attribute [r] listening_from
165
+ # @return [String]
166
+ attribute :listening_from, Types::String
167
+ ##
168
+ # @!attribute [r] media_player
169
+ # @return [String]
170
+ attribute :media_player, Types::String
171
+ ##
172
+ # @!attribute [r] recording_msid
173
+ # @return [String, nil]
174
+ attribute? :recording_msid, Types::String
175
+ ##
176
+ # @!attribute [r] track_mbid
177
+ # @return [String, nil]
178
+ attribute? :track_mbid, Types::String
179
+ end
180
+ ##
181
+ # @!attribute [r] additional_info
182
+ # @return [AdditionalInfo]
183
+ attribute :additional_info, AdditionalInfo
184
+ end
185
+ ##
186
+ # @!attribute [r] track_metadata
187
+ # @return [TrackMetadata]
188
+ attribute :track_metadata, TrackMetadata
189
+ end
190
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ListenBrainz
4
+ ##
5
+ # Module for Dry types.
6
+ module Types
7
+ include Dry.Types
8
+
9
+ ##
10
+ # Unix timestamp.
11
+ Timestamp = Types.Constructor(Time, ::Time.method(:at))
12
+ end
13
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ListenBrainz; end # :nodoc:
4
+
5
+ require_relative 'models/base'
6
+ require_relative 'models/types'
7
+
8
+ require_relative 'models/listen'
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ListenBrainz
4
+ ##
5
+ # Current version of the gem.
6
+ VERSION = '0.1.0'
7
+ end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'dry-struct'
4
+ require 'httpx'
5
+
6
+ require_relative 'listenbrainz/configuration'
7
+ require_relative 'listenbrainz/version'
8
+
9
+ ##
10
+ # Top-level module for the ListenBrainz gem.
11
+ module ListenBrainz
12
+ ##
13
+ # @return [Configuration]
14
+ def self.configuration
15
+ @configuration ||= Configuration.new
16
+ end
17
+
18
+ ##
19
+ # @return [Client]
20
+ def self.client
21
+ @client ||= Client.new
22
+ end
23
+
24
+ ##
25
+ # @yieldparam config [Configuration]
26
+ def self.configure
27
+ yield configuration
28
+ end
29
+
30
+ ##
31
+ # Configures the gem from the following environment variables.
32
+ #
33
+ # * +LISTENBRAINZ_API_ENDPOINT+: URL to the ListenBrainz API;
34
+ # * +LISTENBRAINZ_USER_TOKEN+: ListenBrainz user token used to authenticate.
35
+ def self.configure_from_env
36
+ configuration.endpoint = ENV.fetch('LISTENBRAINZ_API_ENDPOINT', nil)
37
+ configuration.user_token = ENV.fetch('LISTENBRAINZ_USER_TOKEN', nil)
38
+ end
39
+ end
40
+
41
+ ListenBrainz.configure_from_env
42
+
43
+ require_relative 'listenbrainz/client'
44
+ require_relative 'listenbrainz/models'
@@ -0,0 +1,19 @@
1
+ {
2
+ "folders": [
3
+ {
4
+ "path": "."
5
+ }
6
+ ],
7
+ "extensions": {
8
+ "recommendations": [
9
+ "shopify.ruby-lsp",
10
+ "davidanson.vscode-markdownlint"
11
+ ]
12
+ },
13
+ "settings": {
14
+ "editor.rulers": [
15
+ 80
16
+ ],
17
+ "rubyLsp.bundleGemfile": "Gemfile"
18
+ }
19
+ }
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: listenbrainz
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Richard Degenne
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2025-02-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: dry-struct
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: httpx
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.4'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.4'
41
+ description:
42
+ email:
43
+ - hello@richarddegenne.fr
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - ".gitlab-ci.yml"
50
+ - ".rspec"
51
+ - ".rubocop.yml"
52
+ - ".ruby-version"
53
+ - ".vscode/templates.code-snippets"
54
+ - CHANGELOG.md
55
+ - CODE_OF_CONDUCT.md
56
+ - Guardfile
57
+ - LICENSE.txt
58
+ - README.md
59
+ - Rakefile
60
+ - lib/listenbrainz.rb
61
+ - lib/listenbrainz/client.rb
62
+ - lib/listenbrainz/configuration.rb
63
+ - lib/listenbrainz/models.rb
64
+ - lib/listenbrainz/models/base.rb
65
+ - lib/listenbrainz/models/listen.rb
66
+ - lib/listenbrainz/models/types.rb
67
+ - lib/listenbrainz/version.rb
68
+ - listenbrainz.code-workspace
69
+ homepage: https://gitlab.com/Richard-Degenne/listenbrainz
70
+ licenses:
71
+ - MIT
72
+ metadata:
73
+ bug_tracker_uri: https://gitlab.com/Richard-Degenne/listenbrainz/issues
74
+ changelog_uri: https://gitlab.com/Richard-Degenne/listenbrainz/-/blob/master/CHANGELOG.md
75
+ documentation_uri: https://listenbrainz.richarddegenne.fr
76
+ funding_uri: https://ko-fi.com/richarddegenne
77
+ homepage_uri: https://gitlab.com/Richard-Degenne/listenbrainz
78
+ source_code_uri: https://gitlab.com/Richard-Degenne/listenbrainz
79
+ rubygems_mfa_required: 'false'
80
+ post_install_message:
81
+ rdoc_options: []
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '3.2'
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ requirements: []
95
+ rubygems_version: 3.4.19
96
+ signing_key:
97
+ specification_version: 4
98
+ summary: Ruby wrapper for the ListenBrainz API.
99
+ test_files: []