mopidy 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 081da1b64b16da1f6880b35e4eb208fa8d7fd400
4
- data.tar.gz: 6f76451a4df365c8bb1f9413e8e3106b1be7a32a
3
+ metadata.gz: eeb054524f44a01b1636a160fefaf37a2f1f3e98
4
+ data.tar.gz: c3b32bfbd837e4866e71ccacecbb5580bdb99414
5
5
  SHA512:
6
- metadata.gz: b31801cacd9ffbe42cddd7a4fa3344d861d6256085f6a513296a27e65501076f776b71bee2cf4e83dd3236e0f366becafaa2b8f4d81035ce1058ea7c0557ea8a
7
- data.tar.gz: 96b8af968719bf5ee949a9389beae50e6905a7d21234ccbe06c2f1b6f262439857bbace552d2fdb0f79c6a25df2acc2424e684428f86dfe461e5ec8a45ebc82c
6
+ metadata.gz: 160c377c501b708c2d2ee35459f8880a6b872e78dfba95a78f820a966e8638712947de3446742f69d0be0f3bcd1c7018a0b60721b9b15e194ee37d3c2db148c4
7
+ data.tar.gz: 286faf4452ee45b1355c735198b5cbcd6a862137a5623f183c6d82dc1d44df8a55004f21518dd6e729871413824fef54533c5a7307c7664b765927a314984eb5
data/README.md CHANGED
@@ -1,8 +1,6 @@
1
1
  # Mopidy
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/mopidy`. To experiment with that code, run `bin/console` for an interactive prompt.
4
-
5
- TODO: Delete this and the text above, and describe your gem
3
+ A lightweight wrapper around the Mopidy music server API.
6
4
 
7
5
  ## Installation
8
6
 
@@ -22,7 +20,135 @@ Or install it yourself as:
22
20
 
23
21
  ## Usage
24
22
 
25
- TODO: Write usage instructions here
23
+ The gem is divided into modules that reflect the controllers in the [Mopidy core API](https://docs.mopidy.com/en/latest/api/core).
24
+ Currently, the supported modules are `Library`, `Playback`, `Playlist`, and `Tracklist`.
25
+
26
+ ### Configuration
27
+
28
+ To configure the url that the gem makes requests to:
29
+ ```ruby
30
+ Mopidy.configure do |config|
31
+ config.mopidy_url = 'http://localhost:6680/mopidy/rpc' # This is the default
32
+ end
33
+ ```
34
+
35
+ ### Tracklist
36
+
37
+ This is the module that interfaces with [Mopidy's tracklist controller](https://docs.mopidy.com/en/latest/api/core/#tracklist-controller)
38
+
39
+ To get the tracks in the tracklist:
40
+ ```ruby
41
+ Mopidy::Tracklist.tracks
42
+ ```
43
+
44
+ To get the index of the currently playing track:
45
+ ```ruby
46
+ Mopidy::Tracklist.index
47
+ ```
48
+
49
+ To get the tracks in the tracklist as `TLTRack`s
50
+ ```ruby
51
+ Mopidy::Tracklist.tl_tracks
52
+ ```
53
+
54
+ To add a track to the tracklist:
55
+ ```ruby
56
+ Mopidy::Tracklist.add(uri: 'example-track-uri')
57
+ ```
58
+
59
+ To get the length of the tracklist:
60
+ ```ruby
61
+ Mopidy::Tracklist.length
62
+ ```
63
+
64
+ To clear the tracks from the tracklist:
65
+ ```ruby
66
+ Mopidy::Tracklist.clear
67
+ ```
68
+
69
+ ### Library
70
+
71
+ This is the module that interfaces with [Mopidy's Library controller](https://docs.mopidy.com/en/latest/api/core/#library-controller)
72
+
73
+ To search for tracks, artists, playlists, and albums using a keyword:
74
+ ```ruby
75
+ Mopidy::Library.search('queen')
76
+ ```
77
+
78
+ To search for tracks by artist, album, or track:
79
+ ```ruby
80
+ Mopidy::Library.search_tracks('bicycle')
81
+ ```
82
+
83
+ To lookup a track by it's uri:
84
+
85
+ ```ruby
86
+ Mopidy::Library.lookup('example-uri')
87
+ ```
88
+
89
+ ### Playlist
90
+
91
+ This is the module that interfaces with [Mopidy's Playlist controller](https://docs.mopidy.com/en/latest/api/core/#playlist-controller)
92
+
93
+ To get a list of playlists:
94
+ ```ruby
95
+ Mopidy::Playlist.as_list
96
+ ```
97
+
98
+ To lookup a playlist by it's uri
99
+ ```ruby
100
+ Mopidy::Playlist.lookup('playlist-uri')
101
+ ```
102
+
103
+ ### Playback
104
+
105
+ This is the module that interfaces with [Mopidy's Playback controller](https://docs.mopidy.com/en/latest/api/core/#playlist-controller)
106
+
107
+ To play:
108
+ ```ruby
109
+ Mopidy::Playlist.play
110
+ ```
111
+
112
+ To pause:
113
+ ```ruby
114
+ Mopidy::Playlist.pause
115
+ ```
116
+
117
+ To resume:
118
+ ```ruby
119
+ Mopidy::Playlist.resume
120
+ ```
121
+
122
+ To stop:
123
+ ```ruby
124
+ Mopidy::Playlist.stop
125
+ ```
126
+
127
+ To get the playback state:
128
+ ```ruby
129
+ Mopidy::Playlist.state
130
+ ```
131
+
132
+ To get the current time position:
133
+ ```ruby
134
+ Mopidy::Playlist.time_position
135
+ ```
136
+
137
+ To seek to a time position:
138
+ ```ruby
139
+ time_position = 1000 # Time position in milliseconds
140
+ Mopidy::Playlist.seek(time_position)
141
+ ```
142
+
143
+ To get the currently playing track:
144
+ ```ruby
145
+ Mopidy::Playlist.current_track
146
+ ```
147
+
148
+ To skip to the next track:
149
+ ```ruby
150
+ Mopidy::Playlist.next
151
+ ```
26
152
 
27
153
  ## Development
28
154
 
@@ -32,10 +158,9 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
32
158
 
33
159
  ## Contributing
34
160
 
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/mopidy.
161
+ Bug reports and pull requests are welcome on GitHub at https://github.com/khisakuni/mopidy.
36
162
 
37
163
 
38
164
  ## License
39
165
 
40
166
  The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
-
@@ -3,6 +3,8 @@ require 'mopidy/library'
3
3
  require 'mopidy/playback'
4
4
  require 'mopidy/playlist'
5
5
  require 'mopidy/tracklist'
6
+ require 'mopidy/response'
7
+ require 'mopidy/http'
6
8
  require 'json'
7
9
  require 'httparty'
8
10
 
@@ -18,9 +20,11 @@ module Mopidy
18
20
 
19
21
  class Configuration
20
22
  attr_accessor :mopidy_url
23
+ attr_accessor :http_provider
21
24
 
22
25
  def initialize
23
- @mopidy_url ||= 'http://localhost:6680/mopidy/rpc'
26
+ @mopidy_url = 'http://localhost:6680/mopidy/rpc'
27
+ @http_provider = HTTParty
24
28
  end
25
29
  end
26
30
 
@@ -34,15 +38,7 @@ module Mopidy
34
38
  end
35
39
 
36
40
  def self.post(body)
37
- post = HTTParty.post(
38
- configuration.mopidy_url,
39
- body: body,
40
- headers: {
41
- 'Content-Type' => 'application/json'
42
- }
43
- )
44
- result = post.parsed_response['result']
45
- return {} if result.nil?
46
- result
41
+ headers = { 'Content-Type' => 'application/json' }
42
+ res = Http.post(configuration.mopidy_url, body, headers)
47
43
  end
48
44
  end
@@ -0,0 +1,11 @@
1
+ require 'mopidy/response'
2
+
3
+ module Mopidy
4
+ class Http
5
+ def self.post(url, body, headers)
6
+ http_provider = Mopidy.configuration.http_provider
7
+ res = http_provider.post(url, body: body, headers: headers)
8
+ Response.new(res)
9
+ end
10
+ end
11
+ end
@@ -1,28 +1,36 @@
1
+ require 'mopidy/response'
2
+
1
3
  module Mopidy
2
4
  module Library
3
5
  def self.search_tracks(keyword)
4
- results = search(keyword)
5
- return [] if results.nil?
6
- results['tracks']
6
+ res = search(keyword)
7
+ tracks = parse_search_response(res, 'tracks')
8
+ format_response(tracks, res.status)
7
9
  end
8
10
 
9
- def self.get_track(uri)
11
+ def self.lookup(uri)
10
12
  json = Mopidy.format_json(1, 'core.library.lookup', 'uri': uri)
11
- response = Mopidy.post(json)
12
- return response.first unless response.empty?
13
- {}
13
+ res = Mopidy.post(json)
14
+ res.body.empty? ? res : format_response(res.body.first, res.status)
14
15
  end
15
16
 
16
- private
17
-
18
17
  def self.search(keyword)
19
18
  json = Mopidy.format_json(1, 'core.library.search', [{ 'any': keyword }])
20
- Mopidy.post(json).first
19
+ Mopidy.post(json)
20
+ end
21
+
22
+ private
23
+
24
+ def self.parse_search_response(response, type)
25
+ data = response.body.first[type]
21
26
  end
22
27
 
23
- def self.parse_search_response(response)
24
- result = response.parsed_response['result']
25
- result.first unless result.nil?
28
+ def self.format_response(data, status_code)
29
+ new_response = OpenStruct.new(
30
+ parsed_response: { 'result' => data },
31
+ code: status_code
32
+ )
33
+ Mopidy::Response.new(new_response)
26
34
  end
27
35
  end
28
36
  end
@@ -5,12 +5,12 @@ module Mopidy
5
5
  Mopidy.post(json)
6
6
  end
7
7
 
8
- def self.playlist(uri)
8
+ def self.lookup(uri)
9
9
  json = Mopidy.format_json(1, 'core.playlists.lookup', [uri])
10
10
  Mopidy.post(json)
11
11
  end
12
12
 
13
- def self.save_playlist(playlist)
13
+ def self.save(playlist)
14
14
  json = Mopidy.format_json(1, 'core.playlists.save', playlist: playlist)
15
15
  Mopidy.post(json)
16
16
  end
@@ -0,0 +1,29 @@
1
+ module Mopidy
2
+ class Response
3
+ def initialize(response)
4
+ @response = response
5
+ end
6
+
7
+ def status
8
+ @response.code
9
+ end
10
+
11
+ def body
12
+ has_error?(@response) ? parse_error(@response) : parse_response(@response)
13
+ end
14
+
15
+ private
16
+
17
+ def has_error?(response)
18
+ response.parsed_response['result'].nil?
19
+ end
20
+
21
+ def parse_response(response)
22
+ response.parsed_response['result']
23
+ end
24
+
25
+ def parse_error(response)
26
+ response.parsed_response['error']
27
+ end
28
+ end
29
+ end
@@ -7,7 +7,7 @@ module Mopidy
7
7
  end
8
8
  end
9
9
 
10
- def self.tracklist
10
+ def self.tracks
11
11
  json = Mopidy.format_json(1, 'core.tracklist.get_tracks')
12
12
  Mopidy.post(json)
13
13
  end
@@ -23,7 +23,7 @@ module Mopidy
23
23
  Mopidy.post(json)
24
24
  end
25
25
 
26
- def self.tracks
26
+ def self.tl_tracks
27
27
  json = Mopidy.format_json(1, 'core.tracklist.get_tl_tracks')
28
28
  Mopidy.post(json)
29
29
  end
@@ -1,3 +1,3 @@
1
1
  module Mopidy
2
- VERSION = "0.1.1"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mopidy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - khisakuni
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-07-13 00:00:00.000000000 Z
11
+ date: 2016-10-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httparty
@@ -98,9 +98,11 @@ files:
98
98
  - bin/rspec
99
99
  - bin/setup
100
100
  - lib/mopidy.rb
101
+ - lib/mopidy/http.rb
101
102
  - lib/mopidy/library.rb
102
103
  - lib/mopidy/playback.rb
103
104
  - lib/mopidy/playlist.rb
105
+ - lib/mopidy/response.rb
104
106
  - lib/mopidy/tracklist.rb
105
107
  - lib/mopidy/version.rb
106
108
  - mopidy.gemspec
@@ -124,8 +126,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
124
126
  version: '0'
125
127
  requirements: []
126
128
  rubyforge_project:
127
- rubygems_version: 2.5.1
129
+ rubygems_version: 2.4.5
128
130
  signing_key:
129
131
  specification_version: 4
130
132
  summary: A wrapper around the Mopidy API.
131
133
  test_files: []
134
+ has_rdoc: