freesound-ruby 0.0.1 → 0.0.2

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: a44df7e389656a0a045268aa1217fe64039453c1
4
+ data.tar.gz: d226a4e0cd78bfd94aa9fa8f2dd32c02c2e5852b
5
+ SHA512:
6
+ metadata.gz: a808b2d015f66f653fd2ae59cbfa37c904e8ec5dbc60de2658f2b52d2de7bf646ff750e285d81af097b3a4ac46fd2343fcad68b77c83c1941ae50eaae8f82d0e
7
+ data.tar.gz: 46d3d5bd4c404dd547abb9ced16855a3cc9bfa9c17ffce806a193c6ae865c8247c2d5fad755d8dbc7a0d1eadaeccdfa0ab53c20c2764a2930bccfd861eadfec2
data/.gitignore CHANGED
@@ -11,9 +11,6 @@ doc
11
11
  # bundler
12
12
  .bundle
13
13
 
14
- # jeweler generated
15
- pkg
16
-
17
14
  # Have editor/IDE/OS specific files you need to ignore? Consider using a global gitignore:
18
15
  #
19
16
  # * Create a file at ~/.gitignore
@@ -39,10 +36,12 @@ pkg
39
36
  #.\#*
40
37
 
41
38
  # For vim:
42
- #*.swp
39
+ *.swp
43
40
 
44
41
  # For redcar:
45
42
  #.redcar
46
43
 
47
44
  # For rubinius:
48
45
  #*.rbc
46
+
47
+ *.gem
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # freesound-ruby
2
- [freesound.org](http://www.freesound.org) api wrapper.
1
+ # DEPRECATED!
3
2
 
4
- ###
3
+ ### You should use [freesound_ruby](https://github.com/alexgenco/freesound_ruby) (with an underscore) instead.
4
+ ### This gem will not be maintained.
@@ -0,0 +1,6 @@
1
+ {
2
+ "status_code": 401,
3
+ "explanation": "Please include your api key as the api_key GET parameter",
4
+ "type": "AuthenticationError",
5
+ "error": true
6
+ }
@@ -0,0 +1,7 @@
1
+ <?xml version="1.0" encoding="utf-8"?>
2
+ <response>
3
+ <status_code>401</status_code>
4
+ <explanation>Please include your api key as the api_key GET parameter</explanation>
5
+ <type>AuthenticationError</type>
6
+ <error>True</error>
7
+ </response>
@@ -0,0 +1,4 @@
1
+ error: true,
2
+ explanation: Please include your api key as the api_key GET parameter,
3
+ status_code: 401,
4
+ type: AuthenticationError
@@ -7,9 +7,9 @@ Gem::Specification.new do |s|
7
7
  s.version = Freesound::VERSION
8
8
  s.authors = ["Alex Genco"]
9
9
  s.email = ["alexgenco@gmail.com"]
10
- s.homepage = ""
10
+ s.homepage = "https://github.com/alexgenco/freesound-ruby"
11
11
  s.summary = %q{Freesound.org API wrapper in Ruby.}
12
- s.description = %q{Freesound.org API wrapper in Ruby.}
12
+ s.description = %q{A wrapper for the Freesound.org API, to provide access to their extensive audio database.}
13
13
 
14
14
  s.rubyforge_project = "freesound-ruby"
15
15
 
@@ -22,4 +22,6 @@ Gem::Specification.new do |s|
22
22
  s.add_development_dependency("bundler", "~> 1.0.0")
23
23
  s.add_development_dependency("webmock")
24
24
  s.add_runtime_dependency("crack")
25
+
26
+ s.post_install_message = "THIS GEM IS NO LONGER MAINTAINED. You should use 'freesound_ruby' (with an underscore) instead."
25
27
  end
@@ -1,20 +1,28 @@
1
1
  require 'net/http'
2
2
  require 'uri'
3
3
  require 'crack'
4
- require 'core_ext'
5
4
 
6
5
  YAML::ENGINE.yamler = 'syck'
7
6
 
8
7
  module Freesound
8
+
9
+ require 'freesound/core_ext/hash'
10
+ require 'freesound/core_ext/string'
11
+ require 'freesound/version'
12
+ require 'freesound/client'
13
+ require 'freesound/request'
14
+ require 'freesound/uri_compiler'
15
+ require 'freesound/response'
16
+ require 'freesound/response_parser'
17
+ require 'freesound/sound'
18
+
9
19
  def self.root_dir
10
20
  File.expand_path('..', File.dirname(__FILE__))
11
21
  end
12
22
 
13
- class InvalidConfigurationParameterError < ArgumentError; end
14
-
15
23
  module Configuration
16
24
  DEFAULTS = {
17
- :api_key => ENV['FREESOUND_KEY'],
25
+ :api_key => nil,
18
26
  :base_url => 'http://www.freesound.org/api',
19
27
  :sounds_url => 'http://www.freesound.org/api/sounds'
20
28
  }
@@ -31,6 +39,8 @@ module Freesound
31
39
  EOS
32
40
  end
33
41
 
42
+ class InvalidConfigurationParameterError < ArgumentError; end
43
+
34
44
  def self.method_missing(name, *args)
35
45
  raise(InvalidConfigurationParameterError, "#{name} is not a recognized configuration parameter")
36
46
  end
@@ -48,11 +58,3 @@ module Freesound
48
58
  Configuration
49
59
  end
50
60
  end # module Freesound
51
-
52
- require 'freesound/version'
53
- require 'freesound/client'
54
- require 'freesound/request'
55
- require 'freesound/uri_compiler'
56
- require 'freesound/response'
57
- require 'freesound/response_parser'
58
- require 'freesound/sound'
@@ -1,6 +1,4 @@
1
1
  module Freesound
2
- class InvalidApiKeyError < ArgumentError; end
3
-
4
2
  class Client
5
3
  attr_reader :api_key, :requests, :responses
6
4
 
@@ -11,16 +9,22 @@ module Freesound
11
9
  end
12
10
 
13
11
  def get_sound(id)
14
- request = Request.new(:sound_id => id)
12
+ request = Request.new(@api_key, {:sound_id => id})
15
13
  response = request.get!
14
+
16
15
  @requests << request
17
16
  @responses << response
17
+
18
18
  response.sounds.first
19
19
  end
20
20
 
21
21
  def search_sounds(query, options={})
22
- request = Request.new(:search => {:q => query})
22
+ request = Request.new(@api_key, {:search => {:q => query}})
23
23
  response = request.get!
24
+
25
+ @requests << request
26
+ @responses << response
27
+
24
28
  response.sounds
25
29
  end
26
30
  end
@@ -0,0 +1,24 @@
1
+ class Hash
2
+ def symbolize_keys
3
+ Hash[ map { |k, v| [k.underscore.to_sym, v.is_a?(Hash) ? v.symbolize_keys : v] } ]
4
+ end
5
+
6
+ # converting xml into a hash sometimes gives you things like:
7
+ # { 'element' => ['just_one_value'] }
8
+ # this method converts this to the more useful
9
+ # { 'element' => 'just_one_value' }
10
+ def flatten_single_element_array_values
11
+ Hash[ map do |k, v|
12
+ [k, (v.is_a?(Array) && v.size == 1) ? v[0] : (v.flatten_single_element_array_values rescue v)]
13
+ end ]
14
+ end
15
+
16
+ # convert to a uri string
17
+ def to_uri
18
+ map { |k, v| "#{k}=#{v}" }.join("&")
19
+ end
20
+
21
+ def numberize_values
22
+ Hash[ map { |k, v| [k, ((v.numberizeable? rescue false) ? v.numberize : v)] } ]
23
+ end
24
+ end
@@ -0,0 +1,21 @@
1
+ class String
2
+ def numberize
3
+ if numberizeable?
4
+ float? ? to_f : to_i
5
+ end
6
+ end
7
+
8
+ def numberizeable?
9
+ !scan(/^\-?\d*\.?\d*$/).empty?
10
+ end
11
+
12
+ def underscore
13
+ gsub(/\W/, '_')
14
+ end
15
+
16
+ private
17
+
18
+ def float?
19
+ scan(/\./).size == 1
20
+ end
21
+ end
@@ -1,27 +1,36 @@
1
1
  module Freesound
2
2
  class InvalidRequestFormatError < ArgumentError; end
3
+ class InvalidApiKeyError < ArgumentError; end
3
4
 
4
5
  class Request
5
- attr_reader :uri, :params, :response, :format
6
+ attr_reader :uri, :uri_compiler, :params, :response, :format, :api_key
6
7
 
7
- def initialize(params={})
8
+ def initialize(api_key, params={})
8
9
  self.format = params[:format] || :json
10
+ @api_key = api_key
9
11
  @params = params
10
12
  @response = nil
11
13
  end
12
14
 
13
- def format=(format)
14
- raise(InvalidRequestFormatError, "#{format} is not a valid request format") unless [:json, :xml, :yaml, :yml].include?(format.to_sym)
15
- @format = format.to_sym
15
+ def format=(form)
16
+ raise(InvalidRequestFormatError, "#{form} is not a valid request format") unless [:json, :xml, :yaml, :yml].include?(form.to_sym)
17
+ @format = form.to_sym
18
+ end
19
+
20
+ def uri_compiler
21
+ @uri_compiler ||= URICompiler.new(@api_key, @params.dup.merge({:format => @format}))
16
22
  end
17
23
 
18
24
  def uri
19
- @uri ||= URICompiler.new(@params.dup.merge({:format => @format})).uri
25
+ @uri ||= self.uri_compiler.uri
20
26
  end
21
27
 
22
28
  def get!
23
29
  response_body = Net::HTTP.get_response(self.uri).body
24
30
  @response ||= Response.new(response_body, @format)
31
+
32
+ raise(InvalidApiKeyError, "#{@api_key} is not a valid API key") if @response.errors[:error]
33
+ @response
25
34
  end
26
35
  end
27
36
  end
@@ -2,25 +2,35 @@ module Freesound
2
2
  class Response
3
3
  attr_reader :content, :parser
4
4
 
5
- def initialize(raw, format=:json)
5
+ def initialize(raw, form=:json)
6
6
  @content = raw
7
- @format = format
8
- @parser = ResponseParser.new(format)
7
+ @format = form
8
+ @parser = ResponseParser.new(form)
9
9
  end
10
10
 
11
11
  def data
12
12
  @data ||= @parser.parse(@content)
13
13
  end
14
14
 
15
+ def errors
16
+ @errors ||= (self.data[:error] rescue false) ? self.data : {}
17
+ end
18
+
15
19
  def num_results
16
- self.data[:num_results] || 1
20
+ self.data.is_a?(Array) ? self.data.size : 1
17
21
  end
18
22
 
19
23
  def sounds
24
+ return [] if self.errors[:error]
25
+
20
26
  @sounds ||= if num_results == 1
21
27
  [ Sound.new(*self.data.values) ]
22
28
  else
23
- self.data[:sounds].map { |data| Sound.new(data) }
29
+ self.data.map do |sound|
30
+ result = Sound.new
31
+ sound.each { |k, v| result.send("#{k}=", v) }
32
+ result
33
+ end
24
34
  end
25
35
  end
26
36
  end
@@ -2,8 +2,8 @@ module Freesound
2
2
  class ResponseParser
3
3
  attr_reader :format
4
4
 
5
- def initialize(format)
6
- @format = format
5
+ def initialize(form)
6
+ @format = form
7
7
  end
8
8
 
9
9
  def parse(raw)
@@ -20,7 +20,13 @@ module Freesound
20
20
  private
21
21
 
22
22
  def parse_json(raw)
23
- ::Crack::JSON.parse(raw).underscore_keys.symbolize_keys
23
+ result = ::Crack::JSON.parse(raw)
24
+
25
+ if result["sounds"]
26
+ result["sounds"].map { |sound| sound.symbolize_keys }
27
+ else
28
+ result.symbolize_keys
29
+ end
24
30
  end
25
31
 
26
32
  # xml gets parsed slightly differently than json and yaml.
@@ -28,16 +34,32 @@ module Freesound
28
34
  # convert strings like "2" to their corresponding number object i.e. 2
29
35
  # and remove the nesting of the :tags key under "resource"
30
36
  def parse_xml(raw)
31
- result = ::Crack::XML.parse(raw)["response"].underscore_keys
32
- .symbolize_keys
33
- .flatten_single_element_array_values
34
- .numberize_values
35
- result[:tags] = result[:tags]["resource"]
36
- result
37
+ result = ::Crack::XML.parse(raw)["response"]
38
+
39
+ # put necessary changes in a lambda
40
+ fix_sound_hash = lambda do |hash|
41
+ fixed = hash.symbolize_keys.flatten_single_element_array_values.numberize_values
42
+ fixed[:tags] = fixed[:tags][:resource]
43
+ fixed
44
+ end
45
+
46
+ if result["sounds"]
47
+ result["sounds"].map do |sound|
48
+ fix_sound_hash.call(sound)
49
+ end
50
+ else
51
+ fix_sound_hash.call(result)
52
+ end
37
53
  end
38
54
 
39
55
  def parse_yaml(raw)
40
- ::YAML::load(raw).underscore_keys.symbolize_keys
56
+ result = ::YAML::load(raw)
57
+
58
+ if result["sounds"]
59
+ result["sounds"].map { |sound| sound.symbolize_keys }
60
+ else
61
+ result.symbolize_keys
62
+ end
41
63
  end
42
64
  end
43
65
  end
@@ -1,16 +1,21 @@
1
1
  module Freesound
2
2
  class Freesound::URICompiler
3
- attr_reader :uri
3
+ attr_reader :uri_string
4
4
 
5
- def initialize(params)
5
+ def initialize(api_key, params)
6
6
  search_params = params.delete(:search)
7
7
  sound_id = params.delete(:sound_id)
8
- format = params.delete(:format)
9
- @uri = URI.parse(compile_uri_string(search_params, sound_id, format))
8
+ form = params.delete(:format)
9
+ @api_key = api_key
10
+ @uri_string = compile_uri_string(search_params, sound_id, form)
10
11
  end
11
12
 
12
- def compile_uri_string(search_params, sound_id, format)
13
- format_string = format ? "&format=#{format}" : ""
13
+ def uri
14
+ @uri ||= URI.parse(@uri_string)
15
+ end
16
+
17
+ def compile_uri_string(search_params, sound_id, form)
18
+ format_string = form ? "&format=#{form}" : ""
14
19
 
15
20
  if search_params
16
21
  search_uri(search_params, format_string)
@@ -22,11 +27,15 @@ module Freesound
22
27
  private
23
28
 
24
29
  def search_uri(search_params, format_string)
25
- "#{Freesound.config.sounds_url}/search/?api_key=#{Freesound.config.api_key}#{format_string}&#{search_params.to_uri}"
30
+ "#{Freesound.config.sounds_url}/search/?#{api_key_string}#{format_string}&#{search_params.to_uri}"
26
31
  end
27
32
 
28
33
  def sound_id_uri(id, format_string)
29
- "#{Freesound.config.sounds_url}/#{id}/?api_key=#{Freesound.config.api_key}#{format_string}"
34
+ "#{Freesound.config.sounds_url}/#{id}/?#{api_key_string}#{format_string}"
35
+ end
36
+
37
+ def api_key_string
38
+ @api_key.empty? ? "" : "api_key=#{@api_key}"
30
39
  end
31
40
  end
32
41
  end
@@ -1,3 +1,3 @@
1
1
  module Freesound
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -8,13 +8,6 @@ describe Client do
8
8
  context 'with valid api key' do
9
9
  subject { Client.new(Freesound.config.api_key) }
10
10
 
11
- before do
12
- json = File.read("#{Freesound.root_dir}/data/sample.json")
13
- path = "http://www.freesound.org/api/sounds/10/?api_key=#{Freesound.config.api_key}&format=json"
14
-
15
- stub_request(:get, path).to_return(:body => json)
16
- end
17
-
18
11
  describe '#get_sound' do
19
12
  it 'returns a Sound' do
20
13
  subject.get_sound(10).should be_a(Sound)
@@ -35,14 +28,21 @@ describe Client do
35
28
 
36
29
  describe '#search_sounds' do
37
30
  before do
38
- json = File.read("#{Freesound.root_dir}/data/sample_query.json")
39
- path = "http://www.freesound.org/api/sounds/search/?api_key=#{Freesound.config.api_key}&format=json&q=badass"
40
- stub_request(:get, path).to_return(:body => json)
41
31
  end
42
32
 
43
- it 'returns a response' do
44
- subject.search_sounds("badass").should be_a(Array)
45
- subject.search_sounds("badass").first.should be_a(Sound)
33
+ it 'returns an array of properly parsed sounds' do
34
+ result = subject.search_sounds("badass")
35
+ result.should be_a(Array)
36
+ result.first.should be_a(Sound)
37
+ result.first.id.should == 80449
38
+ end
39
+
40
+ it 'adds to @requests' do
41
+ expect { subject.search_sounds("badass") }.to change(subject.requests, :size).by(1)
42
+ end
43
+
44
+ it 'adds to @responses' do
45
+ expect { subject.search_sounds("badass") }.to change(subject.responses, :size).by(1)
46
46
  end
47
47
  end
48
48
  end
@@ -1,10 +1,15 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
2
 
3
3
  describe Hash do
4
4
  describe '#symbolize_keys' do
5
5
  it 'symbolizes keys' do
6
6
  {"one" => 1, "two" => 2}.symbolize_keys.should == {:one => 1, :two => 2}
7
7
  end
8
+
9
+ it 'symbolizes keys to an arbitrary level of nestedness' do
10
+ {"one" => 1, "two" => { "three" => 3, "four" => { "five" => 5}}}.symbolize_keys.should ==
11
+ {:one => 1, :two => { :three => 3, :four => { :five => 5}}}
12
+ end
8
13
  end
9
14
 
10
15
  describe '#flatten_single_element_array_values' do
@@ -12,6 +17,11 @@ describe Hash do
12
17
  {"one" => [1], "two_and_three" => [2, 3], "four" => 4}
13
18
  .flatten_single_element_array_values.should == {"one" => 1, "two_and_three" => [2, 3], "four" => 4}
14
19
  end
20
+
21
+ it 'does it for nested hashes' do
22
+ {"one" => [1], :nest => { "two" => [2], "three" => [1,2,3]}, "four" => 4}
23
+ .flatten_single_element_array_values.should == {"one" => 1, :nest => { "two" => 2, "three" => [1,2,3] }, "four" => 4}
24
+ end
15
25
  end
16
26
 
17
27
  describe '#to_uri' do
@@ -26,46 +36,4 @@ describe Hash do
26
36
  {:one => 1, :one_point_two => 1.2, :negative_three => -3, :something => "anything"}
27
37
  end
28
38
  end
29
-
30
- describe '#underscore_keys' do
31
- it 'underscores keys' do
32
- {"one-two" => 12}.underscore_keys.should == {"one_two" => 12}
33
- end
34
- end
35
- end
36
-
37
- describe String do
38
- describe '#numberize' do
39
- it 'converts to an integer' do
40
- '123'.numberize.should == 123
41
- end
42
-
43
- it 'converts to a float' do
44
- '123.456'.numberize.should == 123.456
45
- end
46
-
47
- it 'returns nil for non-numerical strings' do
48
- 'abc123'.numberize.should be_nil
49
- end
50
- end
51
-
52
- describe '#numberizeable?' do
53
- it 'returns true' do
54
- %w{ 123 -2 1.23 -1.56 .900 23. 023 }.each { |str| str.should be_numberizeable }
55
- end
56
-
57
- it 'returns false' do
58
- %w{ 1.2.3 12a3 alex 1-23 --345 ..45 98..}.each { |str| str.should_not be_numberizeable }
59
- end
60
- end
61
-
62
- describe '#underscore' do
63
- it 'underscores -s' do
64
- 'one-two'.underscore.should == 'one_two'
65
- end
66
-
67
- it 'underscores whitespace' do
68
- 'one two'.underscore.should == 'one_two'
69
- end
70
- end
71
39
  end
@@ -0,0 +1,37 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
+
3
+ describe String do
4
+ describe '#numberize' do
5
+ it 'converts to an integer' do
6
+ '123'.numberize.should == 123
7
+ end
8
+
9
+ it 'converts to a float' do
10
+ '123.456'.numberize.should == 123.456
11
+ end
12
+
13
+ it 'returns nil for non-numerical strings' do
14
+ 'abc123'.numberize.should be_nil
15
+ end
16
+ end
17
+
18
+ describe '#numberizeable?' do
19
+ it 'returns true' do
20
+ %w{ 123 -2 1.23 -1.56 .900 23. 023 }.each { |str| str.should be_numberizeable }
21
+ end
22
+
23
+ it 'returns false' do
24
+ %w{ 1.2.3 12a3 alex 1-23 --345 ..45 98..}.each { |str| str.should_not be_numberizeable }
25
+ end
26
+ end
27
+
28
+ describe '#underscore' do
29
+ it 'underscores -s' do
30
+ 'one-two'.underscore.should == 'one_two'
31
+ end
32
+
33
+ it 'underscores whitespace' do
34
+ 'one two'.underscore.should == 'one_two'
35
+ end
36
+ end
37
+ end
@@ -1,26 +1,24 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
2
 
3
3
  describe Request do
4
- subject { Request.new({:sound_id => 10}) }
4
+ subject { Request.new(Freesound.config.api_key, {:sound_id => 10}) }
5
5
 
6
6
  it 'initializes with a params hash' do
7
7
  subject.params.should == {:sound_id => 10}
8
8
  end
9
9
 
10
10
  it 'fails with invalid format' do
11
- expect { Request.new({:format => :pdf}) }.to raise_error(InvalidRequestFormatError)
11
+ expect { Request.new(Freesound.config.api_key, {:format => :pdf}) }.to raise_error(InvalidRequestFormatError)
12
12
  end
13
13
 
14
14
  describe '#get!' do
15
- before do
16
- json = File.read("#{Freesound.root_dir}/data/sample.json")
17
- path = "http://www.freesound.org/api/sounds/10/?api_key=#{Freesound.config.api_key}&format=json"
18
-
19
- stub_request(:get, path).to_return(:body => json)
20
- end
21
-
22
15
  it 'returns a Response' do
23
16
  subject.get!.should be_a(Response)
24
17
  end
18
+
19
+ it 'fails with invalid api key' do
20
+ req = Request.new("invalidapikey", {:sound_id => 10})
21
+ expect { req.get! }.to raise_error(InvalidApiKeyError)
22
+ end
25
23
  end
26
24
  end
@@ -2,22 +2,19 @@ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
2
 
3
3
  describe ResponseParser do
4
4
  it 'initializes with a format' do
5
- ResponseParser.new(:xml).format.should == :xml
5
+ ResponseParser.new(:xml).format.should == :xml
6
+ ResponseParser.new(:yaml).format.should == :yaml
6
7
  end
7
8
 
8
9
  describe '#parse' do
9
10
  before do
10
- @json = File.read("#{Freesound.root_dir}/data/sample.json")
11
- @xml = File.read("#{Freesound.root_dir}/data/sample.xml")
12
- @yaml = File.read("#{Freesound.root_dir}/data/sample.yaml")
13
-
14
11
  @json_parser = ResponseParser.new(:json)
15
12
  @xml_parser = ResponseParser.new(:xml)
16
13
  @yaml_parser = ResponseParser.new(:yaml)
17
14
 
18
- @json_result = @json_parser.parse(@json)
19
- @xml_result = @xml_parser.parse(@xml)
20
- @yaml_result = @yaml_parser.parse(@yaml)
15
+ @json_result = @json_parser.parse(@sound_json)
16
+ @xml_result = @xml_parser.parse(@sound_xml)
17
+ @yaml_result = @yaml_parser.parse(@sound_yaml)
21
18
 
22
19
  # these are the expected tags if the above files are parsed correctly
23
20
  @tags = [ "transformation", "speech", "voice", "plane", "morph" ]
@@ -1,27 +1,40 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
2
 
3
3
  describe Response do
4
- before do
5
- @single = File.read("#{Freesound.root_dir}/data/sample.json")
6
- @multiple = File.read("#{Freesound.root_dir}/data/sample_query.json")
7
- end
8
-
9
- let(:single) { Response.new(@single) }
10
- let(:multiple) { Response.new(@multiple) }
11
-
12
4
  it 'takes a raw hash' do
13
5
  Response.new('{"id": 3}', :json).data.should == {:id => 3}
14
6
  end
15
7
 
16
- it 'has sounds' do
17
- [single, multiple].each do |res|
18
- res.sounds.should be_a(Array)
19
- res.sounds.first.should be_a(Sound)
8
+ context 'with invalid api key' do
9
+ before { @error = Response.new(@error_json) }
10
+
11
+ it 'contains error information' do
12
+ @error.errors[:status_code].should == 401
13
+ @error.errors[:explanation].should == "Please include your api key as the api_key GET parameter"
14
+ @error.errors[:type].should == "AuthenticationError"
15
+ @error.errors[:error].should == true
20
16
  end
21
17
  end
22
18
 
23
- it 'has number of results' do
24
- single.num_results.should == 1
25
- multiple.num_results.should == 12
19
+ context 'with valid api key' do
20
+ before do
21
+ @single = Response.new(@sound_json)
22
+ @multiple = Response.new(@search_json)
23
+ end
24
+
25
+ it 'has no errors' do
26
+ @single.errors.should == {}
27
+ @multiple.errors.should == {}
28
+ end
29
+
30
+ it 'has sounds' do
31
+ @single.sounds.first.should be_a(Sound)
32
+ @multiple.sounds.first.should be_a(Sound)
33
+ end
34
+
35
+ it 'has number of results' do
36
+ @single.num_results.should == 1
37
+ @multiple.num_results.should == 12
38
+ end
26
39
  end
27
40
  end
@@ -3,17 +3,17 @@ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
3
3
  describe URICompiler do
4
4
  describe '#compile_uri_string' do
5
5
  it 'compiles a proper sound-by-id uri' do
6
- URICompiler.new({:sound_id => 12, :format => :json}).uri.should ==
6
+ URICompiler.new(Freesound.config.api_key, {:sound_id => 12, :format => :json}).uri.should ==
7
7
  URI.parse("http://www.freesound.org/api/sounds/12/?api_key=#{Freesound.config.api_key}&format=json")
8
8
  end
9
9
 
10
10
  it 'compiles a proper search uri' do
11
- URICompiler.new({:search => {:q => 'bass', :p => 2}, :format => :json}).uri.should ==
11
+ URICompiler.new(Freesound.config.api_key, {:search => {:q => 'bass', :p => 2}, :format => :json}).uri.should ==
12
12
  URI.parse("http://www.freesound.org/api/sounds/search/?api_key=#{Freesound.config.api_key}&format=json&q=bass&p=2")
13
13
  end
14
14
 
15
15
  it 'adds non-default format parameter' do
16
- URICompiler.new({:format => :xml, :sound_id => 12}).uri.should ==
16
+ URICompiler.new(Freesound.config.api_key, {:format => :xml, :sound_id => 12}).uri.should ==
17
17
  URI.parse("http://www.freesound.org/api/sounds/12/?api_key=#{Freesound.config.api_key}&format=xml")
18
18
  end
19
19
  end
@@ -38,8 +38,8 @@ describe Freesound do
38
38
  end
39
39
 
40
40
  it 'fails with unknown configuration parameters' do
41
- expect { Freesound.configure(:api_ley => '123') }.to raise_error(Freesound::InvalidConfigurationParameterError)
42
- expect { Freesound.configure { |c| c.api_ley = '123' } }.to raise_error(Freesound::InvalidConfigurationParameterError)
41
+ expect { Freesound.configure(:api_ley => '123') }.to raise_error(Freesound::Configuration::InvalidConfigurationParameterError)
42
+ expect { Freesound.configure { |c| c.api_ley = '123' } }.to raise_error(Freesound::Configuration::InvalidConfigurationParameterError)
43
43
  end
44
44
  end
45
45
  end
@@ -12,6 +12,26 @@ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
12
12
 
13
13
  RSpec.configure do |config|
14
14
  config.before do
15
- Freesound::Configuration.api_key = ENV['FREESOUND_KEY'] || fail("Register your app at freesound.org and store your key in the $FREESOUND_KEY variable.")
15
+ Freesound.config.api_key = ENV['FREESOUND_KEY'] || fail("Register your app at freesound.org and store your key in the $FREESOUND_KEY variable.")
16
+
17
+ @sound_json = File.read("#{Freesound.root_dir}/data/sample.json")
18
+ @sound_xml = File.read("#{Freesound.root_dir}/data/sample.xml")
19
+ @sound_yaml = File.read("#{Freesound.root_dir}/data/sample.yaml")
20
+ @search_json = File.read("#{Freesound.root_dir}/data/sample_query.json")
21
+ @error_json = File.read("#{Freesound.root_dir}/data/auth_error.json")
22
+
23
+ @json_url = "http://www.freesound.org/api/sounds/10/?api_key=#{Freesound.config.api_key}&format=json"
24
+ @xml_url = "http://www.freesound.org/api/sounds/10/?api_key=#{Freesound.config.api_key}&format=xml"
25
+ @yaml_url = "http://www.freesound.org/api/sounds/10/?api_key=#{Freesound.config.api_key}&format=yaml"
26
+ @search_url = "http://www.freesound.org/api/sounds/search/?api_key=#{Freesound.config.api_key}&format=yaml"
27
+ @search_url = "http://www.freesound.org/api/sounds/search/?api_key=#{Freesound.config.api_key}&format=json&q=badass"
28
+ @error_url = "http://www.freesound.org/api/sounds/10/?api_key=invalidapikey&format=json"
29
+
30
+ # use web mock to stub http requests
31
+ stub_request(:get, @json_url).to_return(:body => @sound_json)
32
+ stub_request(:get, @xml_url).to_return(:body => @sound_xml)
33
+ stub_request(:get, @yaml_url).to_return(:body => @sound_yaml)
34
+ stub_request(:get, @search_url).to_return(:body => @search_json)
35
+ stub_request(:get, @error_url).to_return(:body => @error_json)
16
36
  end
17
37
  end
metadata CHANGED
@@ -1,61 +1,73 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: freesound-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
5
- prerelease:
4
+ version: 0.0.2
6
5
  platform: ruby
7
6
  authors:
8
7
  - Alex Genco
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2012-01-10 00:00:00.000000000 Z
11
+ date: 2013-12-05 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: rspec
16
- requirement: &21019320 !ruby/object:Gem::Requirement
17
- none: false
15
+ requirement: !ruby/object:Gem::Requirement
18
16
  requirements:
19
- - - ! '>='
17
+ - - '>='
20
18
  - !ruby/object:Gem::Version
21
19
  version: '0'
22
20
  type: :development
23
21
  prerelease: false
24
- version_requirements: *21019320
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
25
27
  - !ruby/object:Gem::Dependency
26
28
  name: bundler
27
- requirement: &21018800 !ruby/object:Gem::Requirement
28
- none: false
29
+ requirement: !ruby/object:Gem::Requirement
29
30
  requirements:
30
31
  - - ~>
31
32
  - !ruby/object:Gem::Version
32
33
  version: 1.0.0
33
34
  type: :development
34
35
  prerelease: false
35
- version_requirements: *21018800
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: 1.0.0
36
41
  - !ruby/object:Gem::Dependency
37
42
  name: webmock
38
- requirement: &21018360 !ruby/object:Gem::Requirement
39
- none: false
43
+ requirement: !ruby/object:Gem::Requirement
40
44
  requirements:
41
- - - ! '>='
45
+ - - '>='
42
46
  - !ruby/object:Gem::Version
43
47
  version: '0'
44
48
  type: :development
45
49
  prerelease: false
46
- version_requirements: *21018360
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
47
55
  - !ruby/object:Gem::Dependency
48
56
  name: crack
49
- requirement: &21017880 !ruby/object:Gem::Requirement
50
- none: false
57
+ requirement: !ruby/object:Gem::Requirement
51
58
  requirements:
52
- - - ! '>='
59
+ - - '>='
53
60
  - !ruby/object:Gem::Version
54
61
  version: '0'
55
62
  type: :runtime
56
63
  prerelease: false
57
- version_requirements: *21017880
58
- description: Freesound.org API wrapper in Ruby.
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: A wrapper for the Freesound.org API, to provide access to their extensive
70
+ audio database.
59
71
  email:
60
72
  - alexgenco@gmail.com
61
73
  executables: []
@@ -70,22 +82,27 @@ files:
70
82
  - LICENSE.txt
71
83
  - README.md
72
84
  - Rakefile
85
+ - data/auth_error.json
86
+ - data/auth_error.xml
87
+ - data/auth_error.yaml
73
88
  - data/sample.json
74
89
  - data/sample.xml
75
90
  - data/sample.yaml
76
91
  - data/sample_query.json
77
92
  - freesound-ruby.gemspec
78
- - lib/core_ext.rb
79
93
  - lib/freesound.rb
80
94
  - lib/freesound/client.rb
95
+ - lib/freesound/core_ext/hash.rb
96
+ - lib/freesound/core_ext/string.rb
81
97
  - lib/freesound/request.rb
82
98
  - lib/freesound/response.rb
83
99
  - lib/freesound/response_parser.rb
84
100
  - lib/freesound/sound.rb
85
101
  - lib/freesound/uri_compiler.rb
86
102
  - lib/freesound/version.rb
87
- - spec/core_ext_spec.rb
88
103
  - spec/freesound/client_spec.rb
104
+ - spec/freesound/core_ext/hash_spec.rb
105
+ - spec/freesound/core_ext/string_spec.rb
89
106
  - spec/freesound/request_spec.rb
90
107
  - spec/freesound/response_parser_spec.rb
91
108
  - spec/freesound/response_spec.rb
@@ -93,29 +110,28 @@ files:
93
110
  - spec/freesound/uri_compiler_spec.rb
94
111
  - spec/freesound_spec.rb
95
112
  - spec/spec_helper.rb
96
- homepage: ''
113
+ homepage: https://github.com/alexgenco/freesound-ruby
97
114
  licenses: []
98
- post_install_message:
115
+ metadata: {}
116
+ post_install_message: THIS GEM IS NO LONGER MAINTAINED. You should use 'freesound_ruby'
117
+ (with an underscore) instead.
99
118
  rdoc_options: []
100
119
  require_paths:
101
120
  - lib
102
121
  required_ruby_version: !ruby/object:Gem::Requirement
103
- none: false
104
122
  requirements:
105
- - - ! '>='
123
+ - - '>='
106
124
  - !ruby/object:Gem::Version
107
125
  version: '0'
108
126
  required_rubygems_version: !ruby/object:Gem::Requirement
109
- none: false
110
127
  requirements:
111
- - - ! '>='
128
+ - - '>='
112
129
  - !ruby/object:Gem::Version
113
130
  version: '0'
114
131
  requirements: []
115
132
  rubyforge_project: freesound-ruby
116
- rubygems_version: 1.8.11
133
+ rubygems_version: 2.0.3
117
134
  signing_key:
118
- specification_version: 3
135
+ specification_version: 4
119
136
  summary: Freesound.org API wrapper in Ruby.
120
137
  test_files: []
121
- has_rdoc:
@@ -1,50 +0,0 @@
1
- class Hash
2
- def symbolize_keys
3
- Hash[ map { |k, v| [k.to_sym, v] } ]
4
- end
5
-
6
- # converting xml into a hash sometimes gives you things like:
7
- # { 'element' => ['just_one_value'] }
8
- # this method converts this to the more useful
9
- # { 'element' => 'just_one_value' }
10
- def flatten_single_element_array_values
11
- Hash[ map do |k, v|
12
- [k, (v.is_a?(Array) && v.size == 1) ? v[0] : v]
13
- end ]
14
- end
15
-
16
- # convert to a uri string
17
- def to_uri
18
- map { |k, v| "#{k}=#{v}" }.join("&")
19
- end
20
-
21
- def numberize_values
22
- Hash[ map { |k, v| [k, ( (v.numberizeable? rescue false) ? v.numberize : v )] } ]
23
- end
24
-
25
- def underscore_keys
26
- Hash[ map { |k, v| [(k.underscore rescue k), v] } ]
27
- end
28
- end
29
-
30
- class String
31
- def numberize
32
- if numberizeable?
33
- float? ? to_f : to_i
34
- end
35
- end
36
-
37
- def numberizeable?
38
- !scan(/^\-?\d*\.?\d*$/).empty?
39
- end
40
-
41
- def underscore
42
- gsub(/\W/, '_')
43
- end
44
-
45
- private
46
-
47
- def float?
48
- scan(/\./).size == 1
49
- end
50
- end