insound_api 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.travis.yml +3 -0
- data/Gemfile +12 -0
- data/LICENSE.txt +20 -0
- data/README.md +36 -0
- data/Rakefile +52 -0
- data/VERSION +1 -0
- data/insound_api.gemspec +83 -0
- data/lib/insound_api.rb +41 -0
- data/lib/insound_api/artist.rb +18 -0
- data/lib/insound_api/config.rb +5 -0
- data/lib/insound_api/object_base.rb +27 -0
- data/lib/insound_api/product.rb +17 -0
- data/lib/insound_api/query.rb +17 -0
- data/lib/insound_api/request.rb +76 -0
- data/lib/insound_api/response.rb +70 -0
- data/lib/insound_api/results.rb +68 -0
- data/test/config_test.rb +11 -0
- data/test/helper.rb +69 -0
- data/test/query_test.rb +140 -0
- data/test/request_test.rb +41 -0
- data/test/response_test.rb +40 -0
- data/test/webmock/bad_credentials.xml +8 -0
- data/test/webmock/jimmy_eat_world_vinyl.xml +45 -0
- data/test/webmock/maxresults.xml +332 -0
- data/test/webmock/no_results_found.xml +12 -0
- data/test/webmock/zen_arcade.xml +36 -0
- metadata +159 -0
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2013 Zachary Kloepping
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
# insound_api
|
2
|
+
|
3
|
+
|
4
|
+
[![Build Status](https://secure.travis-ci.org/spilliton/insound_api.png?branch=master)](http://travis-ci.org/spilliton/insound_api)
|
5
|
+
[![Code Climate](https://codeclimate.com/github/spilliton/insound_api.png)](https://codeclimate.com/github/spilliton/insound_api)
|
6
|
+
|
7
|
+
A ruby gem for accessing the [insound.com Web Service API for affiliates][api_docs].
|
8
|
+
|
9
|
+
|
10
|
+
|
11
|
+
## Install
|
12
|
+
|
13
|
+
``` ruby
|
14
|
+
# Add the following to you Gemfile
|
15
|
+
gem 'insound_api'
|
16
|
+
|
17
|
+
# Update your bundle
|
18
|
+
bundle install
|
19
|
+
```
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
You will need to setup insound_api to use your credentials. If you are using rails, a good place to do this is in a initializer:
|
24
|
+
|
25
|
+
``` ruby
|
26
|
+
# /config/initializers/insound_api.rb
|
27
|
+
|
28
|
+
InsoundApi.setup do |config|
|
29
|
+
config.affiliate_id = 42
|
30
|
+
config.api_password = 'my_password'
|
31
|
+
end
|
32
|
+
|
33
|
+
```
|
34
|
+
|
35
|
+
|
36
|
+
[api_docs]: https://www.insound.com/affiliate/webservices.php
|
data/Rakefile
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bundler'
|
5
|
+
begin
|
6
|
+
Bundler.setup(:default, :development)
|
7
|
+
rescue Bundler::BundlerError => e
|
8
|
+
$stderr.puts e.message
|
9
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
10
|
+
exit e.status_code
|
11
|
+
end
|
12
|
+
require 'rake'
|
13
|
+
|
14
|
+
require 'jeweler'
|
15
|
+
Jeweler::Tasks.new do |gem|
|
16
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
17
|
+
gem.name = "insound_api"
|
18
|
+
gem.homepage = "http://github.com/spilliton/insound_api"
|
19
|
+
gem.license = "MIT"
|
20
|
+
gem.summary = %Q{A ruby gem for accessing the insound.com Web Service API for affiliates}
|
21
|
+
gem.description = %Q{TA ruby gem for accessing the insound.com Web Service API for affiliates}
|
22
|
+
gem.email = "kloeppingzd@gmail.com"
|
23
|
+
gem.authors = ["Zachary Kloepping"]
|
24
|
+
# dependencies defined in Gemfile
|
25
|
+
end
|
26
|
+
Jeweler::RubygemsDotOrgTasks.new
|
27
|
+
|
28
|
+
require 'rake/testtask'
|
29
|
+
Rake::TestTask.new(:test) do |test|
|
30
|
+
test.libs << 'lib' << 'test'
|
31
|
+
test.pattern = 'test/**/test_*.rb'
|
32
|
+
test.verbose = true
|
33
|
+
end
|
34
|
+
|
35
|
+
require 'rake/testtask'
|
36
|
+
Rake::TestTask.new(:test) do |test|
|
37
|
+
test.libs << 'lib' << 'test'
|
38
|
+
test.pattern = 'test/**/*_test.rb'
|
39
|
+
test.verbose = true
|
40
|
+
end
|
41
|
+
|
42
|
+
task :default => :test
|
43
|
+
|
44
|
+
require 'rdoc/task'
|
45
|
+
Rake::RDocTask.new do |rdoc|
|
46
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
47
|
+
|
48
|
+
rdoc.rdoc_dir = 'rdoc'
|
49
|
+
rdoc.title = "insound_api #{version}"
|
50
|
+
rdoc.rdoc_files.include('README*')
|
51
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
52
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.2
|
data/insound_api.gemspec
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = "insound_api"
|
8
|
+
s.version = "0.0.2"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Zachary Kloepping"]
|
12
|
+
s.date = "2013-05-06"
|
13
|
+
s.description = "TA ruby gem for accessing the insound.com Web Service API for affiliates"
|
14
|
+
s.email = "kloeppingzd@gmail.com"
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE.txt",
|
17
|
+
"README.md"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".travis.yml",
|
21
|
+
"Gemfile",
|
22
|
+
"LICENSE.txt",
|
23
|
+
"README.md",
|
24
|
+
"Rakefile",
|
25
|
+
"VERSION",
|
26
|
+
"insound_api.gemspec",
|
27
|
+
"lib/insound_api.rb",
|
28
|
+
"lib/insound_api/artist.rb",
|
29
|
+
"lib/insound_api/config.rb",
|
30
|
+
"lib/insound_api/object_base.rb",
|
31
|
+
"lib/insound_api/product.rb",
|
32
|
+
"lib/insound_api/query.rb",
|
33
|
+
"lib/insound_api/request.rb",
|
34
|
+
"lib/insound_api/response.rb",
|
35
|
+
"lib/insound_api/results.rb",
|
36
|
+
"test/config_test.rb",
|
37
|
+
"test/helper.rb",
|
38
|
+
"test/query_test.rb",
|
39
|
+
"test/request_test.rb",
|
40
|
+
"test/response_test.rb",
|
41
|
+
"test/webmock/bad_credentials.xml",
|
42
|
+
"test/webmock/jimmy_eat_world_vinyl.xml",
|
43
|
+
"test/webmock/maxresults.xml",
|
44
|
+
"test/webmock/no_results_found.xml",
|
45
|
+
"test/webmock/zen_arcade.xml"
|
46
|
+
]
|
47
|
+
s.homepage = "http://github.com/spilliton/insound_api"
|
48
|
+
s.licenses = ["MIT"]
|
49
|
+
s.require_paths = ["lib"]
|
50
|
+
s.rubygems_version = "1.8.24"
|
51
|
+
s.summary = "A ruby gem for accessing the insound.com Web Service API for affiliates"
|
52
|
+
|
53
|
+
if s.respond_to? :specification_version then
|
54
|
+
s.specification_version = 3
|
55
|
+
|
56
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
57
|
+
s.add_runtime_dependency(%q<rest-client>, [">= 0"])
|
58
|
+
s.add_runtime_dependency(%q<nokogiri>, [">= 0"])
|
59
|
+
s.add_development_dependency(%q<shoulda>, [">= 0"])
|
60
|
+
s.add_development_dependency(%q<rdoc>, ["~> 3.12"])
|
61
|
+
s.add_development_dependency(%q<bundler>, [">= 0"])
|
62
|
+
s.add_development_dependency(%q<jeweler>, ["~> 1.8.4"])
|
63
|
+
s.add_development_dependency(%q<webmock>, [">= 0"])
|
64
|
+
else
|
65
|
+
s.add_dependency(%q<rest-client>, [">= 0"])
|
66
|
+
s.add_dependency(%q<nokogiri>, [">= 0"])
|
67
|
+
s.add_dependency(%q<shoulda>, [">= 0"])
|
68
|
+
s.add_dependency(%q<rdoc>, ["~> 3.12"])
|
69
|
+
s.add_dependency(%q<bundler>, [">= 0"])
|
70
|
+
s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
|
71
|
+
s.add_dependency(%q<webmock>, [">= 0"])
|
72
|
+
end
|
73
|
+
else
|
74
|
+
s.add_dependency(%q<rest-client>, [">= 0"])
|
75
|
+
s.add_dependency(%q<nokogiri>, [">= 0"])
|
76
|
+
s.add_dependency(%q<shoulda>, [">= 0"])
|
77
|
+
s.add_dependency(%q<rdoc>, ["~> 3.12"])
|
78
|
+
s.add_dependency(%q<bundler>, [">= 0"])
|
79
|
+
s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
|
80
|
+
s.add_dependency(%q<webmock>, [">= 0"])
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
data/lib/insound_api.rb
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
module InsoundApi
|
2
|
+
autoload :Config, 'insound_api/config'
|
3
|
+
autoload :Request, 'insound_api/request'
|
4
|
+
autoload :Query, 'insound_api/query'
|
5
|
+
autoload :Results, 'insound_api/results'
|
6
|
+
autoload :RequestException, 'insound_api/request'
|
7
|
+
autoload :Response, 'insound_api/response'
|
8
|
+
|
9
|
+
autoload :ObjectBase, 'insound_api/object_base'
|
10
|
+
autoload :Product, 'insound_api/product'
|
11
|
+
autoload :Artist, 'insound_api/artist'
|
12
|
+
|
13
|
+
|
14
|
+
class ConfigException < Exception
|
15
|
+
EXCEPTION_TEXT = <<-eos
|
16
|
+
You must setup InsoundApi before you can use! Ex:
|
17
|
+
|
18
|
+
InsoundApi.setup do |config|
|
19
|
+
config.affiliate_id = 2343234
|
20
|
+
config.api_password = 'aklsdfjlksdajflks'
|
21
|
+
end
|
22
|
+
eos
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.config
|
26
|
+
if @config
|
27
|
+
@config
|
28
|
+
else
|
29
|
+
raise ConfigException, ConfigException::EXCEPTION_TEXT
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def self.setup(&block)
|
34
|
+
@config = Config.new
|
35
|
+
yield @config
|
36
|
+
end
|
37
|
+
|
38
|
+
def self.search(params)
|
39
|
+
Query.search(params)
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module InsoundApi
|
2
|
+
class ObjectBase
|
3
|
+
|
4
|
+
def initialize(node)
|
5
|
+
@node = node
|
6
|
+
end
|
7
|
+
|
8
|
+
def url
|
9
|
+
@url ||= parse_str('url')
|
10
|
+
end
|
11
|
+
|
12
|
+
def title
|
13
|
+
@title ||= parse_str('title')
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def parse_str(selector)
|
19
|
+
Results.parse_str(selector, @node)
|
20
|
+
end
|
21
|
+
|
22
|
+
def parse_int(selector)
|
23
|
+
Results.parse_int(selector, @node)
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module InsoundApi
|
2
|
+
class Query
|
3
|
+
|
4
|
+
def self.search(params)
|
5
|
+
response = Request.get(params)
|
6
|
+
response.results
|
7
|
+
end
|
8
|
+
|
9
|
+
# not yet supported
|
10
|
+
# def self.product_search(params={})
|
11
|
+
# params[:requesttype] = 'product'
|
12
|
+
# response = Request.get(params)
|
13
|
+
# response.results
|
14
|
+
# end
|
15
|
+
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,76 @@
|
|
1
|
+
require 'rest_client'
|
2
|
+
|
3
|
+
module InsoundApi
|
4
|
+
|
5
|
+
class RequestException < Exception; end
|
6
|
+
|
7
|
+
class Request
|
8
|
+
|
9
|
+
BASE_URL = "https://www.insound.com/ws/affiliate/"
|
10
|
+
VALID_FORMATS = %w{all vinyl seveninch digital poster shirt cd}
|
11
|
+
|
12
|
+
attr_reader :params
|
13
|
+
|
14
|
+
def initialize(opts={})
|
15
|
+
unless opts[:artist]
|
16
|
+
raise RequestException, ":artist is a required param for any request"
|
17
|
+
end
|
18
|
+
|
19
|
+
if opts[:format] && !VALID_FORMATS.include?(opts[:format].downcase)
|
20
|
+
raise RequestException, ":format must be one of the follwing: #{VALID_FORMATS.join(', ')}"
|
21
|
+
end
|
22
|
+
|
23
|
+
@params = opts
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.get(opts={})
|
27
|
+
request = Request.new(opts)
|
28
|
+
request.get_response
|
29
|
+
end
|
30
|
+
|
31
|
+
def get_response
|
32
|
+
data = RestClient.get(build_url)
|
33
|
+
response = data ? build_response(data) : nil
|
34
|
+
|
35
|
+
if response.errors?
|
36
|
+
raise RequestException, "The following errors were returned: #{response.errors.inspect}"
|
37
|
+
else
|
38
|
+
response
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def build_response(data)
|
43
|
+
Response.new(:raw_xml => data.to_str, :request => self)
|
44
|
+
end
|
45
|
+
|
46
|
+
def build_params
|
47
|
+
{param_name => params}
|
48
|
+
end
|
49
|
+
|
50
|
+
def build_url
|
51
|
+
url = base_url_with_creds
|
52
|
+
if params.any?
|
53
|
+
parts = []
|
54
|
+
params.each_pair do |name, value|
|
55
|
+
parts << "#{URI.escape(name.to_s)}=#{URI.escape(value.to_s)}"
|
56
|
+
end
|
57
|
+
url = "#{url}&#{parts.join('&')}"
|
58
|
+
end
|
59
|
+
url
|
60
|
+
end
|
61
|
+
|
62
|
+
|
63
|
+
def affiliate_id
|
64
|
+
InsoundApi.config.affiliate_id
|
65
|
+
end
|
66
|
+
|
67
|
+
def api_password
|
68
|
+
InsoundApi.config.api_password
|
69
|
+
end
|
70
|
+
|
71
|
+
def base_url_with_creds
|
72
|
+
"#{BASE_URL}?id=#{affiliate_id}&password=#{api_password}"
|
73
|
+
end
|
74
|
+
|
75
|
+
end
|
76
|
+
end
|
@@ -0,0 +1,70 @@
|
|
1
|
+
require 'nokogiri'
|
2
|
+
require 'rest-client'
|
3
|
+
|
4
|
+
module InsoundApi
|
5
|
+
class Response
|
6
|
+
|
7
|
+
attr_reader :request, :raw_xml, :doc, :results
|
8
|
+
|
9
|
+
def initialize(opts={})
|
10
|
+
@request = opts[:request]
|
11
|
+
@raw_xml = opts[:raw_xml]
|
12
|
+
@doc = Nokogiri::XML(@raw_xml)
|
13
|
+
@results = Results.new(@doc)
|
14
|
+
end
|
15
|
+
|
16
|
+
def errors?
|
17
|
+
errors.any?
|
18
|
+
end
|
19
|
+
|
20
|
+
def errors
|
21
|
+
unless @errors
|
22
|
+
@errors = doc.css('errors request_error').map{ |node|
|
23
|
+
code = node.css('error_code').first.inner_html
|
24
|
+
text = node.css('error_text').first.inner_html
|
25
|
+
{:code => code, :text => text}
|
26
|
+
}
|
27
|
+
end
|
28
|
+
@errors
|
29
|
+
end
|
30
|
+
|
31
|
+
# # used by non-array returning responses
|
32
|
+
# def to_struct
|
33
|
+
# struct = struct_from_hash(json)
|
34
|
+
# struct.new(*deserialize_values(json))
|
35
|
+
# end
|
36
|
+
|
37
|
+
# # used by array returning resonses
|
38
|
+
# def to_a
|
39
|
+
# struct = nil
|
40
|
+
# self.json.map{|obj|
|
41
|
+
# struct ||= struct_from_hash(obj)
|
42
|
+
# struct.new(*deserialize_values(obj))
|
43
|
+
# }
|
44
|
+
# end
|
45
|
+
|
46
|
+
private
|
47
|
+
|
48
|
+
# def struct_from_hash(hash)
|
49
|
+
# attributes = hash.keys.map{|k|k.to_sym}
|
50
|
+
# Struct.new(*attributes)
|
51
|
+
# end
|
52
|
+
|
53
|
+
# def deserialize_values(hash)
|
54
|
+
# hash.values.map{|v| from_json(v) }
|
55
|
+
# end
|
56
|
+
|
57
|
+
# def from_xml(obj)
|
58
|
+
# if is_date?(obj)
|
59
|
+
# DateTime.strptime(obj, '%Y-%m-%dT%H:%M:%SZ')
|
60
|
+
# else
|
61
|
+
# obj
|
62
|
+
# end
|
63
|
+
# end
|
64
|
+
|
65
|
+
# def is_date?(obj)
|
66
|
+
# obj.is_a?(String) && obj =~ /\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z/i
|
67
|
+
# end
|
68
|
+
|
69
|
+
end
|
70
|
+
end
|