scrobbler-ng-utils 2.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Uwe L. Korn
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.rdoc ADDED
@@ -0,0 +1,17 @@
1
+ = scrobbler-ng-utils
2
+
3
+ Description goes here.
4
+
5
+ == Note on Patches/Pull Requests
6
+
7
+ * Fork the project.
8
+ * Make your feature addition or bug fix.
9
+ * Add tests for it. This is important so I don't break it in a
10
+ future version unintentionally.
11
+ * Commit, do not mess with rakefile, version, or history.
12
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
13
+ * Send me a pull request. Bonus points for topic branches.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2010 Uwe L. Korn. See LICENSE for details.
@@ -0,0 +1,94 @@
1
+ # encoding: utf-8
2
+
3
+ require 'mongo'
4
+
5
+ module Scrobbler
6
+ module Cache
7
+ # Caching Module which stores everything in a MongoDB Collection
8
+ #
9
+ # @author Uwe L. Korn
10
+ class Mongo
11
+
12
+ # Create a new Instance for caching Last.fmn requests.
13
+ #
14
+ # @param [Mongo:Collecion] collection The MongoDB collection for storage
15
+ def initialize(collection)
16
+ @hits = 0
17
+ @misses = 0
18
+ @collection = collection
19
+ @write = true
20
+ end
21
+
22
+ # Could we write to this cache?
23
+ #
24
+ # @return [boolean]
25
+ def writable?
26
+ @write
27
+ end
28
+
29
+ # Is there already an entry for these query?
30
+ #
31
+ # @param [Hash] params The parameters for the Last.fm Request
32
+ # @return [boolean]
33
+ def has?(params={})
34
+ # Check if there is a matching chache entry with no extra parameters
35
+ @collection.find(params).each do |doc|
36
+ if verify(doc, params) then
37
+ @hits += 1
38
+ return true
39
+ end
40
+ end
41
+
42
+ # We have not found a mathing cache entry.
43
+ @misses += 1
44
+ false
45
+ end
46
+
47
+ # Store a result into the database.
48
+ #
49
+ # @param [String] data
50
+ # @param [Hash] params
51
+ # @return [void]
52
+ def set(data, params={})
53
+ if not has?(params) then
54
+ @collection.insert(params.merge({:xml => data, :timestamp => Time.now.to_i}))
55
+ end
56
+ end
57
+
58
+ # Get a cached result
59
+ #
60
+ # @param [Hash] params
61
+ # @return [String]
62
+ def get(params={})
63
+ @collection.find(params).each do |doc|
64
+ return doc['xml'] if verify(doc)
65
+ end
66
+ end
67
+
68
+ # Verify that a specific document matches exactly the request cache.
69
+ #
70
+ # @param [Hash] params
71
+ # @param [Hash] doc
72
+ # @return [boolean]
73
+ def verify(doc, params={})
74
+ matches = true
75
+ doc.each do |k,v|
76
+ next if ['xml', 'timestamp', '_id'].include?(k)
77
+ matches &&= (not (params[k].nil? && params[k.to_sym].nil?))
78
+ end
79
+ matches
80
+ end
81
+
82
+ # Print statistics how efficent this cache was
83
+ #
84
+ # @param [String] prefix The prefix which will be printed before each line
85
+ # @return [void]
86
+ def print_stats(prefix="")
87
+ puts prefix + "Hits: #{@hits}"
88
+ puts prefix + "Misses: #{@misses}"
89
+ hitrate = @hits * 1.0 / (@misses + @hits)
90
+ puts prefix + "Hit-Rate: #{hitrate}"
91
+ end
92
+ end
93
+ end
94
+ end
File without changes
@@ -0,0 +1,7 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "ScrobblerNgUtils" do
4
+ it "fails" do
5
+ fail "hey buddy, you should probably rename this file and start specing for real"
6
+ end
7
+ end
@@ -0,0 +1,9 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'scrobbler-ng-utils'
4
+ require 'spec'
5
+ require 'spec/autorun'
6
+
7
+ Spec::Runner.configure do |config|
8
+
9
+ end
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: scrobbler-ng-utils
3
+ version: !ruby/object:Gem::Version
4
+ hash: 11
5
+ prerelease: false
6
+ segments:
7
+ - 2
8
+ - 0
9
+ - 2
10
+ version: 2.0.2
11
+ platform: ruby
12
+ authors:
13
+ - Uwe L. Korn
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-08-05 00:00:00 +02:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rspec
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 13
30
+ segments:
31
+ - 1
32
+ - 2
33
+ - 9
34
+ version: 1.2.9
35
+ type: :development
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: yard
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ hash: 3
46
+ segments:
47
+ - 0
48
+ version: "0"
49
+ type: :development
50
+ version_requirements: *id002
51
+ description: Utilities like caching, rate limiting, ... for usage in combination with the scrobbler gem. These will be not included in the main gem as they provide extra functionality that is not always required.
52
+ email: uwelk@xhochy.org
53
+ executables: []
54
+
55
+ extensions: []
56
+
57
+ extra_rdoc_files:
58
+ - LICENSE
59
+ - README.rdoc
60
+ files:
61
+ - lib/scrobbler-ng-utils.rb
62
+ - lib/scrobbler-ng-utils/cache/mongo.rb
63
+ - LICENSE
64
+ - README.rdoc
65
+ - spec/spec_helper.rb
66
+ - spec/scrobbler-ng-utils_spec.rb
67
+ has_rdoc: true
68
+ homepage: http://github.com/xhochy/scrobbler-ng-utils
69
+ licenses: []
70
+
71
+ post_install_message:
72
+ rdoc_options:
73
+ - --charset=UTF-8
74
+ require_paths:
75
+ - lib
76
+ required_ruby_version: !ruby/object:Gem::Requirement
77
+ none: false
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ hash: 3
82
+ segments:
83
+ - 0
84
+ version: "0"
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ none: false
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ hash: 3
91
+ segments:
92
+ - 0
93
+ version: "0"
94
+ requirements: []
95
+
96
+ rubyforge_project:
97
+ rubygems_version: 1.3.7
98
+ signing_key:
99
+ specification_version: 3
100
+ summary: Utilities for usage in combination with the scrobbler-ng gem
101
+ test_files:
102
+ - spec/spec_helper.rb
103
+ - spec/scrobbler-ng-utils_spec.rb