blitzr 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 15443180400e38c1fead56fed6513ce4f947247a
4
+ data.tar.gz: 771bad462202823904ef62b009e5c012338d60c1
5
+ SHA512:
6
+ metadata.gz: fbe2627f1991fe1665cf3e67c7bad78b2f78e6a307a04e0459c2266a804efeb2ee0a2d9bf0fe8e6c5d7f75cbd1bfbfa20d83ec2715c1980c7dbf7c7ff99a57d5
7
+ data.tar.gz: 6be113bfefcf3303dad40fd2377c01e87de9fd7a900da5877a1cee37b835c1bea8cbccf0fbd7fd2ce41a7a06d720e3fe6a4b1cbd5ab8db142b4bb861aef07060
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
@@ -0,0 +1 @@
1
+ 2.2.1
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in siilar.gemspec
4
+ gemspec
@@ -0,0 +1,47 @@
1
+ # Blitzr Ruby Client
2
+
3
+ A Ruby client for the [Blitzr API](http://api.blitzr.com/doc/).
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'blitzr'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install blitzr
18
+
19
+ ## Usage
20
+
21
+ This library is a Ruby client you can use to interact with the [Blitzr API](http://api.blitzr.com/doc/).
22
+
23
+ Here's a short example.
24
+
25
+ ```ruby
26
+ require 'blitzr'
27
+
28
+ client = Blitzr::Client.new(api_key: 'YOUR_KEY')
29
+
30
+ # Get an artist biography
31
+ biography = client.artist.biography({slug: 'oxmo-puccino')
32
+ puts "Biography: %s (lang: %s)" % [biography.text, biography.lang]
33
+ ```
34
+
35
+ ## Development
36
+
37
+ 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.
38
+
39
+ 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).
40
+
41
+ ## Contributing
42
+
43
+ 1. Fork it ( https://github.com/craftsmen/niland-blitzr-ruby/fork )
44
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
45
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
46
+ 4. Push to the branch (`git push origin my-new-feature`)
47
+ 5. Create a new Pull Request
@@ -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
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "blitzr"
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
@@ -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,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'blitzr/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'blitzr'
8
+ spec.version = Blitzr::VERSION
9
+ spec.authors = ['Grégoire Colson']
10
+ spec.email = ['gregoire.colson@1d-lab.eu']
11
+
12
+ spec.summary = 'A Ruby client for the Blitzr API'
13
+ spec.description = 'A Ruby client for the Blitzr API'
14
+ spec.homepage = 'https://github.com/craftsmen/niland-blitzr-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
@@ -0,0 +1,10 @@
1
+ require 'httparty'
2
+
3
+ module Blitzr
4
+ # Your code goes here...
5
+ end
6
+
7
+ require 'blitzr/version'
8
+ require 'blitzr/default'
9
+ require 'blitzr/client'
10
+ require 'blitzr/error'
@@ -0,0 +1,93 @@
1
+ require 'blitzr/extra'
2
+ require 'blitzr/struct'
3
+ require 'blitzr/client/clients'
4
+
5
+ module Blitzr
6
+ class Client
7
+ attr_accessor :api_endpoint, :api_key, :user_agent, :requests_timeout
8
+
9
+ def initialize(options = {})
10
+ defaults = Blitzr::Default.options
11
+
12
+ Blitzr::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
+ HTTParty.send(method, api_endpoint + path, Extra.deep_merge!(base_options, options))
59
+ end
60
+
61
+ def api_endpoint
62
+ File.join(@api_endpoint, '')
63
+ end
64
+
65
+ private
66
+
67
+ def base_options
68
+ options = {
69
+ format: :json,
70
+ headers: {
71
+ 'Accept' => 'application/json',
72
+ 'Content-type' => 'application/json',
73
+ 'User-Agent' => user_agent
74
+ },
75
+ query: {
76
+ _format: 'json'
77
+ }
78
+ }
79
+
80
+ if requests_timeout
81
+ options.merge!(timeout: requests_timeout)
82
+ end
83
+
84
+ if api_key
85
+ Extra.deep_merge!(options, {query: { key: api_key }})
86
+ else
87
+ raise Error, 'An API key is required for all API requests.'
88
+ end
89
+
90
+ options
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,36 @@
1
+ module Blitzr
2
+ class Client
3
+ module Artist
4
+
5
+ # Search for an artist
6
+ #
7
+ # @see http://api.blitzr.com/doc#get--artist-
8
+ def details(query = {})
9
+ options = { query: query }
10
+ response = client.get('artist', options)
11
+
12
+ Struct::Artist.new(response)
13
+ end
14
+
15
+ # Search for an artist biography
16
+ #
17
+ # @see http://api.blitzr.com/doc#get--artist-biography-
18
+ def biography(query = {})
19
+ options = { query: query }
20
+ response = client.get('artist/biography', options)
21
+
22
+ Struct::Biography.new(response['biography'])
23
+ end
24
+
25
+ # Search for an artist summary
26
+ #
27
+ # @see http://api.blitzr.com/doc#get--artist-summary-
28
+ def summary(query = {})
29
+ options = { query: query }
30
+ response = client.get('artist/summary', options)
31
+
32
+ response['summary']
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,26 @@
1
+ module Blitzr
2
+ class Client
3
+
4
+ def artist
5
+ @services[:artist] ||= Client::ArtistService.new(self)
6
+ end
7
+
8
+ def label
9
+ @services[:label] ||= Client::LabelService.new(self)
10
+ end
11
+
12
+ class ClientService < ::Struct.new(:client)
13
+ end
14
+
15
+ require 'blitzr/client/artist'
16
+ require 'blitzr/client/label'
17
+
18
+ class ArtistService < ClientService
19
+ include Client::Artist
20
+ end
21
+
22
+ class LabelService < ClientService
23
+ include Client::Label
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,26 @@
1
+ module Blitzr
2
+ class Client
3
+ module Label
4
+
5
+ # Search for a label
6
+ #
7
+ # @see http://api.blitzr.com/doc#get--label-
8
+ def details(query = {})
9
+ options = { query: query }
10
+ response = client.get('label', options)
11
+
12
+ Struct::Label.new(response)
13
+ end
14
+
15
+ # Search for a label biography
16
+ #
17
+ # @see http://api.blitzr.com/doc#get--label-biography-
18
+ def biography(query = {})
19
+ options = { query: query }
20
+ response = client.get('label/biography', options)
21
+
22
+ Struct::Biography.new(response['biography'])
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,32 @@
1
+ module Blitzr
2
+ module Default
3
+ API_ENDPOINT = "http://api.blitzr.com/".freeze
4
+ USER_AGENT = "blitzr-ruby/#{VERSION}".freeze
5
+
6
+ class << self
7
+ def keys
8
+ @keys ||= [:api_endpoint, :api_key, :user_agent, :requests_timeout]
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 requests_timeout
24
+ ENV['SIILAR_REQUESTS_TIMEOUT'] || 10
25
+ end
26
+
27
+ def user_agent
28
+ ENV['SIILAR_USER_AGENT'] || USER_AGENT
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,22 @@
1
+ module Blitzr
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 Blitzr
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,18 @@
1
+ module Blitzr
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 'blitzr/struct/biography'
16
+ require 'blitzr/struct/tag'
17
+ require 'blitzr/struct/artist'
18
+ require 'blitzr/struct/label'
@@ -0,0 +1,15 @@
1
+ module Blitzr
2
+ module Struct
3
+ class Artist < Base
4
+ attr_accessor :image, :thumb_300, :thumb, :uuid, :name, :real_name, :slug, :type, :begin_date, :location, :location_code, :has_duplicate
5
+
6
+ def tags
7
+ @tags ||= []
8
+ end
9
+
10
+ def tags=(attrs)
11
+ @tags = attrs.map { |tag| Struct::Tag.new(tag) }
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,7 @@
1
+ module Blitzr
2
+ module Struct
3
+ class Biography < Base
4
+ attr_accessor :text, :url, :lang, :source
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,15 @@
1
+ module Blitzr
2
+ module Struct
3
+ class Label < Base
4
+ attr_accessor :image, :thumb_300, :thumb, :uuid, :name, :slug, :location, :location_code, :has_duplicate
5
+
6
+ def tags
7
+ @tags ||= []
8
+ end
9
+
10
+ def tags=(attrs)
11
+ @tags = attrs.map { |tag| Struct::Tag.new(tag) }
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,7 @@
1
+ module Blitzr
2
+ module Struct
3
+ class Tag < Base
4
+ attr_accessor :weight, :name, :slug
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,3 @@
1
+ module Blitzr
2
+ VERSION = '0.1.0'
3
+ end
metadata ADDED
@@ -0,0 +1,121 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: blitzr
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Grégoire Colson
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-10-12 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 Blitzr API
70
+ email:
71
+ - gregoire.colson@1d-lab.eu
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".ruby-version"
78
+ - Gemfile
79
+ - README.md
80
+ - Rakefile
81
+ - bin/console
82
+ - bin/setup
83
+ - blitzr.gemspec
84
+ - lib/blitzr.rb
85
+ - lib/blitzr/client.rb
86
+ - lib/blitzr/client/artist.rb
87
+ - lib/blitzr/client/clients.rb
88
+ - lib/blitzr/client/label.rb
89
+ - lib/blitzr/default.rb
90
+ - lib/blitzr/error.rb
91
+ - lib/blitzr/extra.rb
92
+ - lib/blitzr/struct.rb
93
+ - lib/blitzr/struct/artist.rb
94
+ - lib/blitzr/struct/biography.rb
95
+ - lib/blitzr/struct/label.rb
96
+ - lib/blitzr/struct/tag.rb
97
+ - lib/blitzr/version.rb
98
+ homepage: https://github.com/craftsmen/niland-blitzr-ruby
99
+ licenses: []
100
+ metadata: {}
101
+ post_install_message:
102
+ rdoc_options: []
103
+ require_paths:
104
+ - lib
105
+ required_ruby_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ required_rubygems_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ requirements: []
116
+ rubyforge_project:
117
+ rubygems_version: 2.4.6
118
+ signing_key:
119
+ specification_version: 4
120
+ summary: A Ruby client for the Blitzr API
121
+ test_files: []