kodi_client 0.6.1 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (149) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +2 -0
  3. data/README.md +1 -0
  4. data/kodi_client.gemspec +2 -2
  5. data/lib/kodi_client/extensions/chainable.rb +71 -0
  6. data/lib/kodi_client/extensions/comparable.rb +29 -0
  7. data/lib/kodi_client/extensions/creatable.rb +137 -0
  8. data/lib/kodi_client/extensions/iterable.rb +14 -0
  9. data/lib/kodi_client/kodi_error.rb +14 -0
  10. data/lib/kodi_client/kodi_method.rb +34 -0
  11. data/lib/kodi_client/{options.rb → kodi_options.rb} +1 -1
  12. data/lib/kodi_client/kodi_request.rb +16 -0
  13. data/lib/kodi_client/kodi_response.rb +21 -0
  14. data/lib/kodi_client/{method → methods}/addons.rb +6 -10
  15. data/lib/kodi_client/{method → methods}/application.rb +2 -5
  16. data/lib/kodi_client/methods/audio_library.rb +334 -0
  17. data/lib/kodi_client/{method → methods}/favourites.rb +2 -5
  18. data/lib/kodi_client/{method → methods}/files.rb +6 -14
  19. data/lib/kodi_client/{method → methods}/gui.rb +2 -5
  20. data/lib/kodi_client/{method → methods}/input.rb +2 -6
  21. data/lib/kodi_client/{method → methods}/player.rb +2 -7
  22. data/lib/kodi_client/{method → methods}/profiles.rb +5 -10
  23. data/lib/kodi_client/{method → methods}/system.rb +2 -6
  24. data/lib/kodi_client/types/addons/addon_content_type.rb +18 -0
  25. data/lib/kodi_client/types/addons/addon_dependency_type.rb +21 -0
  26. data/lib/kodi_client/types/addons/addon_details_type.rb +44 -0
  27. data/lib/kodi_client/types/addons/addon_extra_info_type.rb +20 -0
  28. data/lib/kodi_client/types/addons/addon_fields_type.rb +28 -0
  29. data/lib/kodi_client/types/addons/addon_types_type.rb +48 -0
  30. data/lib/kodi_client/types/addons/get_addon_returned_type.rb +21 -0
  31. data/lib/kodi_client/types/addons/get_addons_returned_type.rb +22 -0
  32. data/lib/kodi_client/types/application/property_name_type.rb +19 -0
  33. data/lib/kodi_client/types/application/property_value_type.rb +24 -0
  34. data/lib/kodi_client/types/application/version_type.rb +22 -0
  35. data/lib/kodi_client/types/audio/audio_album_release_type_type.rb +15 -0
  36. data/lib/kodi_client/types/audio/audio_artist_role_type.rb +21 -0
  37. data/lib/kodi_client/types/audio/audio_contributor_type.rb +22 -0
  38. data/lib/kodi_client/types/audio/audio_details_album_type.rb +58 -0
  39. data/lib/kodi_client/types/audio/audio_details_artist_type.rb +54 -0
  40. data/lib/kodi_client/types/audio/audio_details_base_type.rb +31 -0
  41. data/lib/kodi_client/types/audio/audio_details_media_type.rb +46 -0
  42. data/lib/kodi_client/types/audio/audio_details_role_type.rb +25 -0
  43. data/lib/kodi_client/types/audio/audio_details_song_type.rb +76 -0
  44. data/lib/kodi_client/types/audio/audio_fields_album_type.rb +50 -0
  45. data/lib/kodi_client/types/audio/audio_fields_artist_type.rb +39 -0
  46. data/lib/kodi_client/types/audio/audio_fields_role_type.rb +15 -0
  47. data/lib/kodi_client/types/audio/audio_fields_song_type.rb +61 -0
  48. data/lib/kodi_client/types/audio/available_art_type.rb +21 -0
  49. data/lib/kodi_client/types/audio/genre_type.rb +20 -0
  50. data/lib/kodi_client/types/audio/get_albums_returned_type.rb +22 -0
  51. data/lib/kodi_client/types/audio/get_artists_returned_type.rb +23 -0
  52. data/lib/kodi_client/types/audio/get_genres_returned_type.rb +24 -0
  53. data/lib/kodi_client/types/audio/get_roles_returned_type.rb +25 -0
  54. data/lib/kodi_client/types/audio/get_songs_returned_type.rb +25 -0
  55. data/lib/kodi_client/types/audio/get_sources_return_type.rb +25 -0
  56. data/lib/kodi_client/types/audio/property_name_type.rb +25 -0
  57. data/lib/kodi_client/types/audio/property_value_type.rb +33 -0
  58. data/lib/kodi_client/types/favourites/details_favourite_type.rb +24 -0
  59. data/lib/kodi_client/types/favourites/fields_favourite_type.rb +17 -0
  60. data/lib/kodi_client/types/favourites/get_favourite_returned_type.rb +22 -0
  61. data/lib/kodi_client/types/favourites/type_type.rb +18 -0
  62. data/lib/kodi_client/types/files/file_label_type.rb +20 -0
  63. data/lib/kodi_client/types/files/get_directory_returned_type.rb +22 -0
  64. data/lib/kodi_client/types/files/get_sources_returned_type.rb +22 -0
  65. data/lib/kodi_client/types/files/media_type.rb +18 -0
  66. data/lib/kodi_client/types/files/prepare_download_returned_type.rb +21 -0
  67. data/lib/kodi_client/types/global/direction_type.rb +17 -0
  68. data/lib/kodi_client/types/global/global_time_type.rb +22 -0
  69. data/lib/kodi_client/types/global/id_label_type.rb +20 -0
  70. data/lib/kodi_client/types/global/id_name_type.rb +20 -0
  71. data/lib/kodi_client/types/global/increment_decrement_type.rb +15 -0
  72. data/lib/kodi_client/types/global/next_prev_type.rb +17 -0
  73. data/lib/kodi_client/types/global/password_encryption_type.rb +16 -0
  74. data/lib/kodi_client/types/global/rotate_type.rb +15 -0
  75. data/lib/kodi_client/types/global/toggle_type.rb +16 -0
  76. data/lib/kodi_client/types/gui/gui_window_type.rb +124 -0
  77. data/lib/kodi_client/types/gui/property_name_type.rb +18 -0
  78. data/lib/kodi_client/types/gui/property_value_type.rb +26 -0
  79. data/lib/kodi_client/types/gui/stereoscopy_mode_type.rb +31 -0
  80. data/lib/kodi_client/types/input/input_action_type.rb +212 -0
  81. data/lib/kodi_client/{global_types/item_types.rb → types/items/item_details_base_type.rb} +1 -3
  82. data/lib/kodi_client/types/library/library_details_genre_type.rb +27 -0
  83. data/lib/kodi_client/types/library/library_details_source_type.rb +26 -0
  84. data/lib/kodi_client/types/library/library_fields_genre_type.rb +17 -0
  85. data/lib/kodi_client/types/library/library_fields_source_type.rb +17 -0
  86. data/lib/kodi_client/types/list/list_field_files_type.rb +83 -0
  87. data/lib/kodi_client/types/list/list_fields_all_type.rb +96 -0
  88. data/lib/kodi_client/types/list/list_item_all_type.rb +73 -0
  89. data/lib/kodi_client/types/list/list_item_base_type.rb +138 -0
  90. data/lib/kodi_client/types/list/list_item_file_type.rb +68 -0
  91. data/lib/kodi_client/types/list/list_limits_returned_type.rb +23 -0
  92. data/lib/kodi_client/types/list/list_limits_type.rb +23 -0
  93. data/lib/kodi_client/types/list/list_sort_method_type.rb +54 -0
  94. data/lib/kodi_client/types/list/list_sort_type.rb +28 -0
  95. data/lib/kodi_client/types/list/sort_order_type.rb +15 -0
  96. data/lib/kodi_client/types/media/media_artwork_type.rb +26 -0
  97. data/lib/kodi_client/types/media/media_details_base_type.rb +28 -0
  98. data/lib/kodi_client/types/media/media_type_type.rb +16 -0
  99. data/lib/kodi_client/types/player/audio_stream_type.rb +29 -0
  100. data/lib/kodi_client/types/player/player_position_time_type.rb +22 -0
  101. data/lib/kodi_client/types/player/player_repeat_type.rb +17 -0
  102. data/lib/kodi_client/types/player/player_speed_type.rb +26 -0
  103. data/lib/kodi_client/types/player/player_type.rb +24 -0
  104. data/lib/kodi_client/types/player/player_type_type.rb +16 -0
  105. data/lib/kodi_client/types/player/player_view_mode_type.rb +23 -0
  106. data/lib/kodi_client/types/player/player_visibility_type.rb +16 -0
  107. data/lib/kodi_client/types/player/property_name_type.rb +38 -0
  108. data/lib/kodi_client/types/player/property_value_type.rb +60 -0
  109. data/lib/kodi_client/types/player/seek_jump_type.rb +17 -0
  110. data/lib/kodi_client/types/player/seek_returned_type.rb +23 -0
  111. data/lib/kodi_client/types/player/subtitle_type.rb +24 -0
  112. data/lib/kodi_client/types/player/video_stream_type.rb +26 -0
  113. data/lib/kodi_client/types/player/view_mode_type.rb +22 -0
  114. data/lib/kodi_client/types/player/zoom_type.rb +15 -0
  115. data/lib/kodi_client/types/profiles/details_profile_type.rb +24 -0
  116. data/lib/kodi_client/types/profiles/fields_profile_type.rb +15 -0
  117. data/lib/kodi_client/types/profiles/get_profiles_returned_type.rb +22 -0
  118. data/lib/kodi_client/types/profiles/profile_password_type.rb +20 -0
  119. data/lib/kodi_client/{global_types/pvr_type.rb → types/pvr/channel_type_type.rb} +2 -6
  120. data/lib/kodi_client/types/system/property_name_type.rb +17 -0
  121. data/lib/kodi_client/{global_types/system_types.rb → types/system/property_value_type.rb} +2 -17
  122. data/lib/kodi_client/types/video/stream_details_type.rb +24 -0
  123. data/lib/kodi_client/types/video/video_cast_type.rb +22 -0
  124. data/lib/kodi_client/types/video/video_details_base_type.rb +29 -0
  125. data/lib/kodi_client/types/video/video_details_file_type.rb +37 -0
  126. data/lib/kodi_client/types/video/video_details_item_type.rb +32 -0
  127. data/lib/kodi_client/types/video/video_details_media_type.rb +28 -0
  128. data/lib/kodi_client/types/video/video_resume_type.rb +20 -0
  129. data/lib/kodi_client.rb +156 -24
  130. metadata +129 -37
  131. data/lib/kodi_client/Chainable.rb +0 -49
  132. data/lib/kodi_client/global_types/addon_types.rb +0 -180
  133. data/lib/kodi_client/global_types/application_types.rb +0 -56
  134. data/lib/kodi_client/global_types/audio_types.rb +0 -226
  135. data/lib/kodi_client/global_types/favourites_types.rb +0 -66
  136. data/lib/kodi_client/global_types/files_types.rb +0 -68
  137. data/lib/kodi_client/global_types/global_types.rb +0 -106
  138. data/lib/kodi_client/global_types/gui_types.rb +0 -184
  139. data/lib/kodi_client/global_types/input_types.rb +0 -216
  140. data/lib/kodi_client/global_types/list_types.rb +0 -548
  141. data/lib/kodi_client/global_types/media_types.rb +0 -58
  142. data/lib/kodi_client/global_types/player_type.rb +0 -300
  143. data/lib/kodi_client/global_types/profiles_types.rb +0 -70
  144. data/lib/kodi_client/global_types/video_types.rb +0 -155
  145. data/lib/kodi_client/kodi_module.rb +0 -87
  146. data/lib/kodi_client/method/audio_library.rb +0 -49
  147. data/lib/kodi_client/util/comparable.rb +0 -20
  148. data/lib/kodi_client/util/creatable.rb +0 -106
  149. data/lib/kodi_client/util/iterable.rb +0 -11
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5fea7dad06fe723225e4d8bf268db09490af87c98a74f653e5966f170d1156fd
4
- data.tar.gz: a4d8a3dfcac1a85abbf1c073b0c2f6016de9de9d07249eabc5067fe47c829727
3
+ metadata.gz: 7bbe8ae31b1c2f5092c6e6fa02a4641d3a73393dcedb44cd08e75f977b85b19b
4
+ data.tar.gz: 2d5362b89b5ef4e02542a2fcf9e548a8d33f38575496bbcd48a2b40c1c378003
5
5
  SHA512:
6
- metadata.gz: 031fcbe6dc03750fa90d3b8d395f903082d6b2b8586f3fe49ff3d9d5ca3375501e2b8e34305808399026de10bd62a5cfc00bf3ad5fc505cb5043e47ba2cdc85f
7
- data.tar.gz: 6e9622971b0e8737130feddefca5ac49a8efd4073dd4ec9719b524c2cb7ba3781e915322a03bd1ffac63fe4ab48b3ed1428a83135a69223add9a041b8ff1288d
6
+ metadata.gz: 89796e76db1800a22e165a2bec14b2c3365318f87d7f57cf2861cdf2646ff0ca1e1108b47f0399782f4491fa9c2604e8fadec6f83f878b15e420bd74901510e8
7
+ data.tar.gz: f42bf33a0651081ec388e7bf5142d5284e00cd5d8e8d0b07d5766c05ea18197f7c523b97642fe993e6e789576c677945b1949764d592bdecab0d635d661d7edc
data/.rubocop.yml CHANGED
@@ -12,5 +12,7 @@ Metrics/MethodLength:
12
12
  Enabled: false
13
13
  Metrics/PerceivedComplexity:
14
14
  Enabled: false
15
+ Metrics/ModuleLength:
16
+ Enabled: false
15
17
  Style/OptionalBooleanParameter:
16
18
  Enabled: false
data/README.md CHANGED
@@ -5,6 +5,7 @@ RubyKodiClient is a work-in-progress client for [Kodi JSON API v12](https://kodi
5
5
  Currently implemented are the following Methods:
6
6
  * Addons
7
7
  * Application
8
+ * AudioLibrary
8
9
  * Favourites
9
10
  * Files
10
11
  * GUI
data/kodi_client.gemspec CHANGED
@@ -2,12 +2,12 @@
2
2
 
3
3
  Gem::Specification.new do |spec|
4
4
  spec.name = 'kodi_client'
5
- spec.version = '0.6.1'
5
+ spec.version = '0.7.0'
6
6
  spec.authors = ['Christian Feier']
7
7
  spec.email = ['christian.feier@gmail.com']
8
8
 
9
9
  spec.summary = 'A work-in-progress client for Kodi JSON API v2.'
10
- spec.description = 'A client for the Kodi JSON API v12, currently implemented methods are Addons, Application, '\
10
+ spec.description = 'A client for the Kodi JSON API v12, currently implemented methods are Addons, Application, AudioLibrary '\
11
11
  'Favourites, Files, GUI, Input, Player, Profiles and System (more will be added with the time). '\
12
12
  'For more information how to use it and how to activate Remote Control in Kodi, '\
13
13
  'please check the github page https://github.com/cfe86/RubyKodiClient'
@@ -0,0 +1,71 @@
1
+ # frozen_string_literal: true
2
+
3
+ module KodiClient
4
+ module Extensions
5
+ # Offers methods like connect, auth and tls to be chained together
6
+ module Chainable
7
+
8
+ attr_accessor :options, :client
9
+
10
+ # Creates a new Client instance and sets the given ip and port to connect to it
11
+ # @param ip [String] the ip to use
12
+ # @param port [Integer] the port to connect to
13
+ # @raise [ArgumentError] thrown if ip or port is nil
14
+ # @return [Client] the created client
15
+ def connect(ip, port)
16
+ throw ArgumentError('ip or port can\'t be nil.') if ip.nil? || port.nil?
17
+ @client = KodiClient::Client.new
18
+ merge_options(->(options) { options.with_connection(ip, port) })
19
+ end
20
+
21
+ # sets username and password if required
22
+ # @param username [String] the username
23
+ # @param password [String] the password
24
+ # @return [Client] the existing, or if not existed, a new created client
25
+ def auth(username, password)
26
+ throw ArgumentError('username or password can\'t be nil.') if username.nil? || password.nil?
27
+ merge_options(->(options) { options.with_auth(username, password) })
28
+ end
29
+
30
+ # enables/disables the use of TLS
31
+ # @param enabled [Boolean] true to enable it, false to disable
32
+ # @return [Client] the existing, or if not existed, a new created client
33
+ def use_tls(enabled: true)
34
+ merge_options(->(options) { options.with_tls(enabled) })
35
+ end
36
+
37
+ # merges the existing options with the new options
38
+ # @param [Proc] set_command so command to execute on the existing options to merge them
39
+ # @return [Client] the existing, or if not existed, a new created client
40
+ def merge_options(set_command)
41
+ client = def_client
42
+ options = def_options
43
+ set_command.call(options)
44
+ client.apply_options_to_methods(options)
45
+ client
46
+ end
47
+
48
+ # Creates a new, or if one already exists, returns the existing client
49
+ # @return [Client] the new or existing client
50
+ def def_client
51
+ if @client.nil?
52
+ @client = KodiClient::Client.new
53
+ @client.client = @client
54
+ end
55
+ @client
56
+ end
57
+
58
+ # Creates a new, or if one already exists, returns the existing Options
59
+ # @return [KodiOptions] the new or existing Options
60
+ def def_options
61
+ if @options.nil?
62
+ @options ||= KodiClient::KodiOptions.new
63
+ @client.options = @options
64
+ end
65
+ @options
66
+ end
67
+
68
+ private :def_client, :def_options, :merge_options
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module KodiClient
4
+ module Extensions
5
+ # offers compare methods
6
+ module Comparable
7
+
8
+ # compares the instance variables of 2 objects for equality
9
+ # @param obj1 object 1
10
+ # @param obj2 object 2
11
+ # @return [Boolean] true if both instance variables are the same, else false
12
+ def compare(obj1, obj2)
13
+ is_same = true
14
+ obj1.instance_variables.each do |it|
15
+ is_same = false if obj1.instance_variable_get(it) != obj2.instance_variable_get(it)
16
+ end
17
+
18
+ is_same
19
+ end
20
+
21
+ # compares all Comparables using the defined compare method
22
+ # @param other [Object] the object to compare with
23
+ # @return [Boolean] true if equal, else false
24
+ def ==(other)
25
+ compare(self, other)
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,137 @@
1
+ # frozen_string_literal: true
2
+
3
+ module KodiClient
4
+ module Extensions
5
+ # creates a create and create_list method by hash
6
+ module Creatable
7
+
8
+ # sets the fields to fetch from the given hash for the #create method. This is optional and only to use, if
9
+ # more fields should be fetched than are provided using attr_reader
10
+ # @param [Array<String>] fields the fields given as a string array
11
+ def fields_to_map(fields)
12
+ @fields_to_map = fields
13
+ end
14
+
15
+ # defines mappings in case an attribute in the hash is not primitive and requires further processing. The argument
16
+ # is an Array of Arrays. Each array contains ['field_name', [type_to_map_to], [opt if field is a list]]
17
+ # @param args [Array<Array>] mappings. Example mapping ['art', Media::ArtWork, true], in this case
18
+ # Media::ArtWork.create_list will be called
19
+ def type_mapping(*args)
20
+ return if args.nil?
21
+
22
+ @type_mapping = {}
23
+ args.map { |it| @type_mapping[it[0]] = Creatable::CreateMap.new(it[1], it[2].nil? ? false : it[2]) }
24
+ end
25
+
26
+ # if @type_mapping is nil, it tries to fetch the mapping from here instead
27
+ # @return [Hash<String, CreateMap>] an hash containing all the mappings
28
+ def lazy_type_mapping
29
+ {}
30
+ end
31
+
32
+ # expects the given hash is a list of hashes and calls #create on each element
33
+ # @param [Array<Hash>] hash given list of hashes
34
+ # @return [Array] empty if hash is nil, otherwise array containing the elements
35
+ def create_list(hash)
36
+ hash.nil? ? [] : hash.map { |it| create(it) }.reject(&:nil?)
37
+ end
38
+
39
+ # creates a Creatable using the provided hash and the attr_reader fields, or if given #fields_to_map instead
40
+ # The parameters will be passed to initialize the object in the same order as they are passed to attr_reader
41
+ # or #fields_to_map
42
+ # @param hash [Hash, nil] the given hash to convert to an object
43
+ # @return the created object, or nil if the given hash is nil
44
+ def create(hash)
45
+ return nil if hash.nil?
46
+
47
+ if @fields_to_map.nil? || @fields_to_map.empty?
48
+ fields = @kodi_fields
49
+ return nil if @kodi_fields.none? { |it| !hash[it.to_s.gsub('_', '')].nil? }
50
+ else
51
+ fields = @fields_to_map
52
+ end
53
+
54
+ mapping = @type_mapping.nil? ? lazy_type_mapping : @type_mapping
55
+ args = fields.map do |it|
56
+ field = it.to_s.gsub('_', '')
57
+ Creatable.extract_field_from_hash(field, hash, mapping)
58
+ end
59
+
60
+ new(*args)
61
+ end
62
+
63
+ # takes a hash and a list of strings and an optional mapping and creates a new array with the created objects
64
+ # of the hash and fields. If no mapping is provided, the value of this field is returned, otherwise the mapping is
65
+ # used
66
+ # @param hash [Hash] the given hash
67
+ # @param fields [Array<String>] the fields to extract
68
+ # @param mapping [Hash<String, CreateMap>] optional mapping if a field contains a complex type
69
+ # @return [Array] an array containing the values of hash[field] in the given field order
70
+ def self.hash_to_arr(hash, fields, mapping = {})
71
+ return nil if hash.nil?
72
+
73
+ fields.map do |it|
74
+ field = it.to_s.gsub('_', '')
75
+ extract_field_from_hash(field, hash, mapping)
76
+ end
77
+ end
78
+
79
+ # map to self.hash_to_arr. The mapping is taken from #type_mapping
80
+ def hash_to_arr(hash, fields)
81
+ Creatable.hash_to_arr(hash, fields, @type_mapping.nil? ? {} : @type_mapping)
82
+ end
83
+
84
+ # extracts the given field from the given hash and optionally applies mapping if given
85
+ # @param field [String] the field name
86
+ # @param hash [Hash, nil] the hash where to extract the field from
87
+ # @param mapping [Hash<String, CreateMap>] optional mapping for complex types
88
+ # @return the extracted object or nil, if the hash is empty, or [] if the expected value was a list and was nil
89
+ def self.extract_field_from_hash(field, hash, mapping)
90
+ map = mapping[field]
91
+ return nil if hash[field].nil? && map.nil?
92
+
93
+ return hash[field] if map.nil?
94
+
95
+ return map.type.create_list(hash[field]) if map.is_list
96
+
97
+ map.type.create(hash[field])
98
+ end
99
+
100
+ def attr_accessor(*args)
101
+ attr_reader(*args)
102
+ attr_writer(*args)
103
+ end
104
+
105
+ def attr_reader(*args)
106
+ @kodi_fields = args.map { |it| it.to_s.gsub('_', '') }
107
+
108
+ args.each do |it|
109
+ inst_variable_name = "@#{it}".to_sym
110
+ define_method it do
111
+ instance_variable_get(inst_variable_name)
112
+ end
113
+ end
114
+ end
115
+
116
+ def attr_writer(*args)
117
+ args.each do |it|
118
+ inst_variable_name = "@#{it}".to_sym
119
+ define_method "#{it}=" do |new_value|
120
+ instance_variable_set(inst_variable_name, new_value)
121
+ end
122
+ end
123
+ end
124
+
125
+ # mapping class with the type and if it is a list field or not
126
+ class CreateMap
127
+
128
+ attr_reader :type, :is_list
129
+
130
+ def initialize(type, is_list = false)
131
+ @type = type
132
+ @is_list = is_list
133
+ end
134
+ end
135
+ end
136
+ end
137
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module KodiClient
4
+ module Extensions
5
+ # offers iterable methods
6
+ module Iterable
7
+
8
+ # @return [Array<String>] returns all constant values if sorted order
9
+ def all_properties
10
+ constants.map { |it| const_get(it) }.sort!
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module KodiClient
4
+ # represents an error response
5
+ class KodiError
6
+ include Extensions::Comparable
7
+ attr_reader :code, :message
8
+
9
+ def initialize(hash)
10
+ @code = hash['code']
11
+ @message = hash['message']
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ module KodiClient
4
+
5
+ # the client that stores
6
+ class KodiMethod
7
+
8
+ def apply_options(options)
9
+ @endpoint_url = "#{options.tls ? 'https://' : 'http://'}#{options.ip}:#{options.port}/jsonrpc"
10
+ @options = options
11
+ end
12
+
13
+ def invoke_api(request)
14
+ h = request.instance_variables.each_with_object({}) do |var, hash|
15
+ hash[var.to_s.delete('@')] = request.instance_variable_get(var)
16
+ end
17
+
18
+ @http_client ||= build_client
19
+ response = @http_client.post(@endpoint_url, json: h).to_s
20
+ JSON.parse(response)
21
+ end
22
+
23
+ def build_client
24
+ client = HTTP.headers(Content_Type: 'application/json')
25
+ if !@options.username.nil? && !@options.password.nil?
26
+ client = client.basic_auth({ user: @options.username, pass: @options.password })
27
+ end
28
+
29
+ client
30
+ end
31
+
32
+ private :build_client
33
+ end
34
+ end
@@ -2,7 +2,7 @@
2
2
 
3
3
  module KodiClient
4
4
  # holds all options like ip, port, credentials
5
- class Options
5
+ class KodiOptions
6
6
 
7
7
  attr_accessor :ip, :port, :username, :password, :tls
8
8
 
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module KodiClient
4
+ # represents a kodi requests
5
+ class KodiRequest
6
+
7
+ attr_accessor :id, :jsonrpc, :method, :params
8
+
9
+ def initialize(kodi_id, method, params = {}, jsonrpc = '2.0')
10
+ @id = kodi_id
11
+ @jsonrpc = jsonrpc
12
+ @method = method
13
+ @params = params
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module KodiClient
4
+ # represents a kodi response
5
+ class KodiResponse
6
+ include Extensions::Comparable
7
+
8
+ attr_reader :id, :jsonrpc, :result, :error
9
+
10
+ def initialize(hash)
11
+ @id = hash['id']
12
+ @jsonrpc = hash['jsonrpc']
13
+ @result = hash['result']
14
+ @error = KodiError.new(hash['error']) unless hash['error'].nil?
15
+ end
16
+
17
+ def error?
18
+ !@error.nil?
19
+ end
20
+ end
21
+ end
@@ -1,13 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'kodi_client/kodi_module'
4
- require 'kodi_client/global_types/addon_types'
5
- require 'kodi_client/global_types/list_types'
6
-
7
3
  module KodiClient
8
- module Modules
4
+ module Methods
9
5
  # contains all Kodi Application methods
10
- class Addons < KodiModule
6
+ class Addons < KodiMethod
11
7
 
12
8
  EXECUTE_ADDON = 'Addons.ExecuteAddon'
13
9
  GET_ADDON_DETAILS = 'Addons.GetAddonDetails'
@@ -17,18 +13,18 @@ module KodiClient
17
13
  def get_addons(type = Types::Addons::AddonTypes::UNKNOWN,
18
14
  content = Types::Addons::AddonContent::UNKNOWN,
19
15
  enabled = nil, properties = [],
20
- limit = Types::List::ListLimits.new(0, 50), installed = nil, kodi_id = 1)
16
+ limits = Types::List::ListLimits.new(0, 50), installed = nil, kodi_id = 1)
21
17
  request = KodiRequest.new(kodi_id, GET_ADDONS,
22
18
  {
23
19
  'type' => type,
24
20
  'content' => content,
25
21
  'enabled' => enabled.nil? || !enabled ? 'all' : 'enabled',
26
22
  'properties' => properties,
27
- 'limits' => { 'start' => limit.list_start, 'end' => limit.list_end },
23
+ 'limits' => limits.to_h,
28
24
  'installed' => installed.nil? || !installed ? 'all' : 'installed'
29
25
  })
30
26
  json = invoke_api(request)
31
- result = Types::Addons::Addons.create(json['result'])
27
+ result = Types::Addons::GetAddonsReturned.create(json['result'])
32
28
  json['result'] = result
33
29
  KodiResponse.new(json)
34
30
  end
@@ -38,7 +34,7 @@ module KodiClient
38
34
  'properties' => properties })
39
35
  json = invoke_api(request)
40
36
 
41
- result = Types::Addons::Addon.create(json['result'])
37
+ result = Types::Addons::GetAddonReturned.create(json['result'])
42
38
  json['result'] = result
43
39
  KodiResponse.new(json)
44
40
  end
@@ -1,12 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'kodi_client/kodi_module'
4
- require 'kodi_client/global_types/application_types'
5
-
6
3
  module KodiClient
7
- module Modules
4
+ module Methods
8
5
  # contains all Kodi Application methods
9
- class Application < KodiModule
6
+ class Application < KodiMethod
10
7
 
11
8
  QUIT = 'Application.Quit'
12
9
  SET_MUTE = 'Application.SetMute'