siilar 1.0.0.pre

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
+ SHA1:
3
+ metadata.gz: 5f06b4bdcda7a709d1f1e6e065eb88315abffc1c
4
+ data.tar.gz: ef394621c6c509e794e7c8465bd60215d8f409fd
5
+ SHA512:
6
+ metadata.gz: 5862668d5246a2cff2ea72354083f435b708a396282e6604e2940491294d4d6f820e4852f42fb61a3908ae7ee0df8b79a728566528fcbec17ee3ba8c1b464da5
7
+ data.tar.gz: 2c0ba73ad89122416a6a5203745b976cdcb4d126a1d579ccd070b0da815b61c29ddef45f5b261a2f4756e06c85d223b860cab56f8258cebdfe113e90fc9f2baa
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.2.1
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.1
4
+
5
+ notifications:
6
+ email: false
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in siilar.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,53 @@
1
+ # Siilar Ruby Client [![Build Status](https://travis-ci.org/craftsmen/niland-siilar-ruby.svg?branch=better-methods-names)](https://travis-ci.org/craftsmen/niland-siilar-ruby)
2
+
3
+ A Ruby client for the [Siilar API](http://api.siilar.com/1.0/doc/).
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'siilar'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install siilar
18
+
19
+ ## Usage
20
+
21
+ This library is a Ruby client you can use to interact with the [Siilar API](http://api.siilar.com/1.0/doc/).
22
+
23
+ Here's a short example.
24
+
25
+ ```ruby
26
+ require 'siilar'
27
+
28
+ client = Siilar::Client.new(api_key: 'YOUR_KEY')
29
+
30
+ # Create a track
31
+ track = client.tracks.create(title: 'Nine Lives', external_id: '123')
32
+ puts "Track: %s (id: %d)" % [track.title, track.id]
33
+
34
+ # Search for similar tracks
35
+ tracks = client.search.similar(similar_ids: '1234')
36
+ tracks.each do |track|
37
+ puts "Track: %s (id: %d)" % [track.title, track.id]
38
+ end
39
+ ```
40
+
41
+ ## Development
42
+
43
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
44
+
45
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
46
+
47
+ ## Contributing
48
+
49
+ 1. Fork it ( https://github.com/craftsmen/niland-siilar-ruby/fork )
50
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
51
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
52
+ 4. Push to the branch (`git push origin my-new-feature`)
53
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require 'bundler/gem_tasks'
2
+
3
+ require 'rspec/core/rake_task'
4
+
5
+ RSpec::Core::RakeTask.new do |t|
6
+ t.verbose = !!ENV['VERBOSE']
7
+ end
8
+
9
+ task test: :spec
10
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "siilar"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,27 @@
1
+ module Siilar
2
+ class Client
3
+
4
+ def tracks
5
+ @services[:tracks] ||= Client::TracksService.new(self)
6
+ end
7
+
8
+ def search
9
+ @services[:search] ||= Client::SearchService.new(self)
10
+ end
11
+
12
+ class ClientService < ::Struct.new(:client)
13
+ end
14
+
15
+ require 'siilar/client/tracks'
16
+
17
+ class TracksService < ClientService
18
+ include Client::Tracks
19
+ end
20
+
21
+ require 'siilar/client/search'
22
+
23
+ class SearchService < ClientService
24
+ include Client::Search
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,16 @@
1
+ module Siilar
2
+ class Client
3
+ module Search
4
+
5
+ # Search for a track
6
+ #
7
+ # @see http://api.siilar.com/1.0/doc/search-and-analyze#search
8
+ def similar(query = {})
9
+ options = { query: query }
10
+ response = client.get('1.0/search', options)
11
+
12
+ response.map { |r| Struct::Track.new(r) }
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,41 @@
1
+ module Siilar
2
+ class Client
3
+ module Tracks
4
+
5
+ # Gets a track.
6
+ #
7
+ # @see http://api.siilar.com/1.0/doc/tracks#get-a-track
8
+ def track(track)
9
+ response = client.get("1.0/tracks/#{track}")
10
+
11
+ Struct::track.new(response)
12
+ end
13
+
14
+ # Creates a track.
15
+ #
16
+ # @see http://api.siilar.com/1.0/doc/tracks#create-a-track
17
+ def create(attributes = {})
18
+ Extra.validate_mandatory_attributes(attributes, [:title, :external_id])
19
+ response = client.post('1.0/tracks', attributes)
20
+
21
+ Struct::Track.new(response)
22
+ end
23
+
24
+ # Updates a track.
25
+ #
26
+ # @see http://api.siilar.com/1.0/doc/tracks#edit-a-track
27
+ def update(track, attributes = {})
28
+ response = client.patch("1.0/tracks/#{track}", attributes)
29
+
30
+ Struct::Track.new(response)
31
+ end
32
+
33
+ # Deletes a track.
34
+ #
35
+ # @see http://api.siilar.com/1.0/doc/tracks#delete-a-track
36
+ def delete(track)
37
+ client.delete("1.0/tracks/#{track}")
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,87 @@
1
+ require 'siilar/extra'
2
+ require 'siilar/struct'
3
+ require 'siilar/client/clients'
4
+
5
+ module Siilar
6
+ class Client
7
+ attr_accessor :api_endpoint, :api_key, :user_agent
8
+
9
+ def initialize(options = {})
10
+ defaults = Siilar::Default.options
11
+
12
+ Siilar::Default.keys.each do |key|
13
+ instance_variable_set(:"@#{key}", options[key] || defaults[key])
14
+ end
15
+
16
+ @services = {}
17
+ end
18
+
19
+ def get(path, options = {})
20
+ execute :get, path, options
21
+ end
22
+
23
+ def post(path, options = {})
24
+ execute :post, path, options
25
+ end
26
+
27
+ def patch(path, options = {})
28
+ execute :patch, path, options
29
+ end
30
+
31
+ def delete(path, options = {})
32
+ execute :delete, path, options
33
+ end
34
+
35
+ def execute(method, path, data, options = {})
36
+ response = request(method, path, data, options)
37
+
38
+ case response.code
39
+ when 200..299
40
+ response
41
+ when 401
42
+ raise AuthenticationFailed, response['message']
43
+ when 404
44
+ raise NotFoundError.new(response)
45
+ else
46
+ raise RequestError.new(response)
47
+ end
48
+ end
49
+
50
+ def request(method, path, data, options = {})
51
+ if data.is_a?(Hash)
52
+ options[:query] = data.delete(:query) if data.key?(:query)
53
+ options[:headers] = data.delete(:headers) if data.key?(:headers)
54
+ end
55
+ if !data.empty?
56
+ options[:body] = data.to_json
57
+ end
58
+
59
+ HTTParty.send(method, api_endpoint + path, Extra.deep_merge!(base_options, options))
60
+ end
61
+
62
+ def api_endpoint
63
+ File.join(@api_endpoint, '')
64
+ end
65
+
66
+ private
67
+
68
+ def base_options
69
+ options = {
70
+ format: :json,
71
+ headers: {
72
+ 'Accept' => 'application/json',
73
+ 'Content-type' => 'application/json',
74
+ 'User-Agent' => user_agent
75
+ }
76
+ }
77
+
78
+ if api_key
79
+ options.merge!(query: { key: api_key })
80
+ else
81
+ raise Error, 'An API key is required for all API requests.'
82
+ end
83
+
84
+ options
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,28 @@
1
+ module Siilar
2
+ module Default
3
+ API_ENDPOINT = "http://api.siilar.com/".freeze
4
+ USER_AGENT = "niland-siilar-ruby/#{VERSION}".freeze
5
+
6
+ class << self
7
+ def keys
8
+ @keys ||= [:api_endpoint, :api_key, :user_agent]
9
+ end
10
+
11
+ def options
12
+ Hash[keys.map { |key| [key, send(key)] }]
13
+ end
14
+
15
+ def api_endpoint
16
+ ENV['SIILAR_API_ENDPOINT'] || API_ENDPOINT
17
+ end
18
+
19
+ def api_key
20
+ ENV['SIILAR_API_KEY']
21
+ end
22
+
23
+ def user_agent
24
+ ENV['SIILAR_USER_AGENT'] || USER_AGENT
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,22 @@
1
+ module Siilar
2
+ class Error < StandardError
3
+ end
4
+
5
+ class RequestError < Error
6
+ attr_reader :response
7
+
8
+ def initialize(response)
9
+ @response = response
10
+ super("#{response.code}")
11
+ end
12
+ end
13
+
14
+ class NotFoundError < RequestError
15
+ end
16
+
17
+ class AuthenticationError < Error
18
+ end
19
+
20
+ class AuthenticationFailed < AuthenticationError
21
+ end
22
+ end
@@ -0,0 +1,48 @@
1
+ module Siilar
2
+ module Extra
3
+
4
+ # Returns a new hash with `self` and `other` merged recursively.
5
+ #
6
+ # h1 = { a: true, b: { c: [1, 2, 3] } }
7
+ # h2 = { a: false, b: { x: [3, 4, 5] } }
8
+ #
9
+ # h1.deep_merge(h2) #=> { a: false, b: { c: [1, 2, 3], x: [3, 4, 5] } }
10
+ #
11
+ # Like with Hash#merge in the standard library, a block can be provided
12
+ # to merge values:
13
+ #
14
+ # h1 = { a: 100, b: 200, c: { c1: 100 } }
15
+ # h2 = { b: 250, c: { c1: 200 } }
16
+ # h1.deep_merge(h2) { |key, this_val, other_val| this_val + other_val }
17
+ # # => { a: 100, b: 450, c: { c1: 300 } }
18
+ def self.deep_merge(this, other, &block)
19
+ deep_merge!(this.dup, other, &block)
20
+ end
21
+
22
+ # Same as `deep_merge`, but modifies `self`.
23
+ def self.deep_merge!(this, other, &block)
24
+ other.each_pair do |current_key, other_value|
25
+ this_value = this[current_key]
26
+
27
+ this[current_key] = if this_value.is_a?(Hash) && other_value.is_a?(Hash)
28
+ deep_merge(this_value, other_value, &block)
29
+ else
30
+ if block_given? && key?(current_key)
31
+ block.call(current_key, this_value, other_value)
32
+ else
33
+ other_value
34
+ end
35
+ end
36
+ end
37
+
38
+ this
39
+ end
40
+
41
+ # Validates the presence of mandatory attributes.
42
+ def self.validate_mandatory_attributes(attributes, required)
43
+ required.each do |name|
44
+ attributes.key?(name) or raise(ArgumentError, ":#{name} is required")
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,8 @@
1
+ module Siilar
2
+ module Struct
3
+
4
+ class Album < Base
5
+ attr_accessor :name
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ module Siilar
2
+ module Struct
3
+
4
+ class Artist < Base
5
+ attr_accessor :name
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,28 @@
1
+ module Siilar
2
+ module Struct
3
+
4
+ class Track < Base
5
+ attr_accessor :id, :hash, :title, :popularity, :duration, :external_id, :isrc, :waveform_url
6
+
7
+ def album
8
+ @album ||= {}
9
+ end
10
+
11
+ def album=(attrs)
12
+ if attrs
13
+ @album = Struct::Album.new(attrs)
14
+ end
15
+ end
16
+
17
+ def artist
18
+ @artist ||= {}
19
+ end
20
+
21
+ def artist=(attrs)
22
+ if attrs
23
+ @artist = Struct::Artist.new(attrs)
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,17 @@
1
+ module Siilar
2
+ module Struct
3
+
4
+ class Base
5
+ def initialize(attributes = {})
6
+ attributes.each do |key, value|
7
+ m = "#{key}=".to_sym
8
+ self.send(m, value) if self.respond_to?(m)
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
14
+
15
+ require 'siilar/struct/track'
16
+ require 'siilar/struct/album'
17
+ require 'siilar/struct/artist'
@@ -0,0 +1,3 @@
1
+ module Siilar
2
+ VERSION = '1.0.0.pre'
3
+ end
data/lib/siilar.rb ADDED
@@ -0,0 +1,10 @@
1
+ require 'httparty'
2
+
3
+ module Siilar
4
+ # Your code goes here...
5
+ end
6
+
7
+ require 'siilar/version'
8
+ require 'siilar/default'
9
+ require 'siilar/client'
10
+ require 'siilar/error'
data/siilar.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'siilar/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'siilar'
8
+ spec.version = Siilar::VERSION
9
+ spec.authors = ['Mehdi Lahmam']
10
+ spec.email = ['mehdi@craftsmen.io']
11
+
12
+ spec.summary = 'A Ruby client for the Siilar API'
13
+ spec.description = 'A Ruby client for the Siilar API'
14
+ spec.homepage = 'https://github.com/craftsmen/niland-siilar-ruby'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.require_paths = ['lib']
18
+
19
+ spec.add_dependency 'httparty'
20
+
21
+ spec.add_development_dependency 'rake'
22
+ spec.add_development_dependency 'rspec'
23
+ spec.add_development_dependency 'webmock'
24
+ end
metadata ADDED
@@ -0,0 +1,123 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: siilar
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0.pre
5
+ platform: ruby
6
+ authors:
7
+ - Mehdi Lahmam
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-03-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: httparty
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: webmock
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: A Ruby client for the Siilar API
70
+ email:
71
+ - mehdi@craftsmen.io
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".ruby-version"
79
+ - ".travis.yml"
80
+ - Gemfile
81
+ - README.md
82
+ - Rakefile
83
+ - bin/console
84
+ - bin/setup
85
+ - lib/siilar.rb
86
+ - lib/siilar/client.rb
87
+ - lib/siilar/client/clients.rb
88
+ - lib/siilar/client/search.rb
89
+ - lib/siilar/client/tracks.rb
90
+ - lib/siilar/default.rb
91
+ - lib/siilar/error.rb
92
+ - lib/siilar/extra.rb
93
+ - lib/siilar/struct.rb
94
+ - lib/siilar/struct/album.rb
95
+ - lib/siilar/struct/artist.rb
96
+ - lib/siilar/struct/track.rb
97
+ - lib/siilar/version.rb
98
+ - siilar.gemspec
99
+ homepage: https://github.com/craftsmen/niland-siilar-ruby
100
+ licenses: []
101
+ metadata: {}
102
+ post_install_message:
103
+ rdoc_options: []
104
+ require_paths:
105
+ - lib
106
+ required_ruby_version: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ required_rubygems_version: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - ">"
114
+ - !ruby/object:Gem::Version
115
+ version: 1.3.1
116
+ requirements: []
117
+ rubyforge_project:
118
+ rubygems_version: 2.4.5
119
+ signing_key:
120
+ specification_version: 4
121
+ summary: A Ruby client for the Siilar API
122
+ test_files: []
123
+ has_rdoc: