cobregratis 0.1.0

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.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
22
+ cobregratis.gemspec
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Rafael Lima
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.mkdn ADDED
@@ -0,0 +1,71 @@
1
+ # Cobregratis (0.1.0)
2
+
3
+ ## What is it?
4
+
5
+ This gem provides a set of classes to access information on [Cobre Grátis][cg] via the published [API][api].
6
+
7
+ All these classes are inherited from ActiveResouce::Base. Refer to the [ActiveResouce][ar] documentation for more information.
8
+
9
+ ## Installing
10
+
11
+ gem install cobregratis
12
+
13
+ ### Dependencies (see the Rakefile or run <code>rake check_dependencies</code>)
14
+
15
+ ### Documentation
16
+
17
+ I'm on [rdoc.info][rdoc]
18
+
19
+ ### Configure your key
20
+
21
+ require 'rubygems'
22
+ require 'cobregratis'
23
+
24
+ Cobregratis::Base.site = 'https://app.cobregratis.com.br'
25
+ Cobregratis::Base.user = 'your_api_username'
26
+ Cobregratis::Base.password = 'your_api_password'
27
+
28
+ and, if you want [caching][c]:
29
+
30
+ Cobregratis::Base.connection.cache_store= <your normal ActiveSupport::Caching options>
31
+
32
+ If you are using this in a Rails application, putting this code in a config/initializers/cobregratis.rb
33
+ file is recommended. See config_initializers_cobregratis.rb in the examples/ directory.
34
+
35
+ ## Usage
36
+
37
+ @billets = Cobregratis::Billet.find(:all)
38
+
39
+ ## License
40
+
41
+ This code is free to be used under the terms of the [MIT license][mit].
42
+
43
+ ## Bugs, Issues, Kudos and Catcalls
44
+
45
+ Comments are welcome. Send your feedback through the [issue tracker on GitHub][i]
46
+
47
+ ## Author
48
+
49
+ ### **Rafael Lima**
50
+
51
+ Working at [BielSystems](http://bielsystems.com.br)
52
+
53
+ Blog: [http://rafael.adm.br](http://rafael.adm.br)
54
+
55
+ Podcast: [http://rafael.adm.br/voltandopracasa](http://rafael.adm.br/voltandopracasa)
56
+
57
+ Github: [http://github.com/rafaelp](http://github.com/rafaelp)
58
+
59
+ Twitter: [http://twitter.com/rafaelp](http://twitter.com/rafaelp)
60
+
61
+ ### Did you like?
62
+
63
+ [Recommend me at Working With Rails](http://workingwithrails.com/recommendation/new/person/14248-rafael-lima)
64
+
65
+ [api]: http://app.cobregratis.com.br/api
66
+ [ar]: http://api.rubyonrails.org/classes/ActiveResource/Base.html
67
+ [c]: http://api.rubyonrails.org/classes/ActiveSupport/Cache
68
+ [cg]: http://cobregratis.com.br/
69
+ [i]: http://github.com/rafaelp/cobregratis/issues
70
+ [mit]:http://www.opensource.org/licenses/mit-license.php
71
+ [rdoc]: http://rdoc.info/projects/rafaelp/cobregratis
data/Rakefile ADDED
@@ -0,0 +1,81 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "cobregratis"
8
+ gem.summary = %Q{Ruby Wrapper around Cobre Grátis API}
9
+ gem.description = %{
10
+ Cobre Grátis
11
+
12
+ Configure by adding the following:
13
+
14
+ require 'cobregratis'
15
+ Cobregratis::Base.site = 'https://app.cobregratis.com.br/'
16
+ Cobregratis::Base.user = 'your_api_username'
17
+ Cobregratis::Base.user = 'your_api_password'
18
+ }
19
+ gem.email = "contato@rafael.adm.br"
20
+ gem.homepage = "http://github.com/rafaelp/cobregratis"
21
+ gem.authors = ["Rafael Lima"]
22
+ gem.add_development_dependency "rspec", ">= 1.2.9"
23
+ gem.add_dependency('activeresource', '>= 2.1')
24
+ gem.add_dependency('activesupport', '>= 2.1')
25
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
26
+ end
27
+ Jeweler::GemcutterTasks.new
28
+ rescue LoadError
29
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
30
+ end
31
+
32
+ require 'spec/rake/spectask'
33
+ Spec::Rake::SpecTask.new(:spec) do |spec|
34
+ spec.libs << 'lib' << 'spec'
35
+ spec.spec_files = FileList['spec/**/*_spec.rb']
36
+ end
37
+
38
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
39
+ spec.libs << 'lib' << 'spec'
40
+ spec.pattern = 'spec/**/*_spec.rb'
41
+ spec.rcov = true
42
+ end
43
+
44
+ task :spec => :check_dependencies
45
+
46
+ begin
47
+ require 'reek/adapters/rake_task'
48
+ Reek::RakeTask.new do |t|
49
+ t.fail_on_error = true
50
+ t.verbose = false
51
+ t.source_files = 'lib/**/*.rb'
52
+ end
53
+ rescue LoadError
54
+ task :reek do
55
+ abort "Reek is not available. In order to run reek, you must: sudo gem install reek"
56
+ end
57
+ end
58
+
59
+ begin
60
+ require 'roodi'
61
+ require 'roodi_task'
62
+ RoodiTask.new do |t|
63
+ t.verbose = false
64
+ end
65
+ rescue LoadError
66
+ task :roodi do
67
+ abort "Roodi is not available. In order to run roodi, you must: sudo gem install roodi"
68
+ end
69
+ end
70
+
71
+ task :default => :spec
72
+
73
+ require 'rake/rdoctask'
74
+ Rake::RDocTask.new do |rdoc|
75
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
76
+
77
+ rdoc.rdoc_dir = 'rdoc'
78
+ rdoc.title = "cobregratis #{version}"
79
+ rdoc.rdoc_files.include('README*')
80
+ rdoc.rdoc_files.include('lib/**/*.rb')
81
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,3 @@
1
+ Autotest.add_discovery do
2
+ "rspec"
3
+ end
@@ -0,0 +1,13 @@
1
+ if Rails.env != 'test' then
2
+ Cobregratis::Base.site = 'https://example.com.i'
3
+ # For backward compatability
4
+ if Cobregratis::Base.respond_to? :user
5
+ Cobregratis::Base.user = 'my_fancy_username'
6
+ Cobregratis::Base.password = 'my_fancy_password'
7
+ else
8
+ Cobregratis::Base.site = 'https://my_fancy_username:my_fancy_password@example.com.i'
9
+ end
10
+ # The cache store can be anything that ActiveSupport can handle
11
+ Cobregratis::Base.connection.cache_store = ActiveSupport::Cache.lookup_store :mem_cache_store
12
+ Cobregratis::Base.connection.store_options = { :expires_in => 60.seconds }
13
+ end
@@ -0,0 +1,11 @@
1
+ require 'cobregratis'
2
+ require 'pp'
3
+
4
+ Cobregratis::Base.site = 'https://app.cobregratis.com.br'
5
+ Cobregratis::Base.user = 'login'
6
+ Cobregratis::Base.password = 'xxx'
7
+ Cobregratis::Base.connection.cache_store = :memory_store
8
+
9
+ @billets = Cobregratis::Billet.find(:all)
10
+
11
+ pp @billets
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'cobregratis'
data/lib/cachable.rb ADDED
@@ -0,0 +1,83 @@
1
+ # Caching is a way to speed up slow ActiveResource queries by keeping the result of
2
+ # a request around to be reused by subsequent requests.
3
+ #
4
+ # Caching is turned OFF by default.
5
+ #
6
+ # == Usage
7
+ #
8
+ # require 'cachable'
9
+ #
10
+ # module CachedResource < ActiveResource::Base
11
+ # include ::Cachable
12
+ # end
13
+ #
14
+ #
15
+ # == Caching stores
16
+ #
17
+ # All the caching stores from ActiveSupport::Cache are available
18
+ # as backends for caching. See the Rails rdoc for more information on
19
+ # these stores
20
+ #
21
+ # === Configuration examples ('off' is the default):
22
+ # CachedResource.connection.cache_store = ActiveSupport::Cache.lookup_store :memory_store
23
+ # CachedResource.connection.cache_store = ActiveSupport::Cache.lookup_store :file_store, "/path/to/cache/directory"
24
+ # CachedResource.connection.cache_store = ActiveSupport::Cache.lookup_store :drb_store, "druby://localhost:9192"
25
+ # CachedResource.connection.cache_store = ActiveSupport::Cache.lookup_store :mem_cache_store, "localhost"
26
+ # CachedResource.connection.cache_store = MyOwnStore.new("parameter")
27
+ #
28
+ # === If you are using a store that has write options, you can set them
29
+ # CachedResource.connection.cache_options = { :expires_in => 60.seconds }
30
+ #
31
+ # Note: To ensure that caching is turned off, set CachedResource.connection.cache_store = nil
32
+ #
33
+ # FYI: You can use this with *any* active resource class, not just Cobregratis.
34
+
35
+ module Cachable
36
+ def self.included(base)
37
+ ActiveResource::Connection.class_eval do
38
+ def cache_store
39
+ @cache_store ||= nil
40
+ end
41
+
42
+ def cache_store=(store)
43
+ @cache_store = store
44
+ end
45
+
46
+ def cache_options
47
+ @cache_options ||= {}
48
+ end
49
+ alias :store_options :cache_options
50
+
51
+ def cache_options=(options)
52
+ @cache_options = options
53
+ end
54
+ alias :store_options= :cache_options=
55
+
56
+ def is_caching?
57
+ !@cache_store.nil?
58
+ end
59
+
60
+ def get_with_cache(path, headers = {})
61
+ return get_without_cache(path, headers) unless is_caching?
62
+ cache_store.fetch(cache_key(path), cache_options) {get_without_cache(path, headers)}
63
+ end
64
+ alias_method_chain :get, :cache
65
+
66
+ def put_with_cache(path, body = '', headers = {})
67
+ cache_store.delete(cache_key(path))
68
+ put_without_cache(path, body, headers)
69
+ end
70
+ alias_method_chain :put, :cache
71
+
72
+ def delete_with_cache(path, headers = {})
73
+ cache_store.delete(cache_key(path))
74
+ delete_without_cache(path, headers)
75
+ end
76
+ alias_method_chain :delete, :cache
77
+
78
+ def cache_key(*args)
79
+ args.join(':')
80
+ end
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,8 @@
1
+ require File.dirname(__FILE__) + '/../cachable'
2
+
3
+ module Cobregratis
4
+ class Base < ActiveResource::Base
5
+ self.site = "https://app.cobregratis.com.br"
6
+ include ::Cachable
7
+ end
8
+ end
@@ -0,0 +1,4 @@
1
+ module Cobregratis
2
+ class Billet < Base
3
+ end
4
+ end
@@ -0,0 +1,5 @@
1
+ require 'active_resource'
2
+ $:.unshift(File.dirname(__FILE__))
3
+
4
+ require 'cobregratis/base'
5
+ require 'cobregratis/billet'
@@ -0,0 +1,84 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ class CachedResource < ActiveResource::Base
4
+ include ::Cachable
5
+ end
6
+
7
+ describe CachedResource, "class configuration" do
8
+ before(:each) do
9
+ CachedResource.site = 'http://example.com.i:3000'
10
+ end
11
+
12
+ it "should tell us if caching is active" do
13
+ CachedResource.connection.cache_store = ActiveSupport::Cache.lookup_store :memory_store
14
+ CachedResource.connection.is_caching?.should == true
15
+ end
16
+
17
+ it "should tell us if caching is not active" do
18
+ CachedResource.connection.cache_store = nil
19
+ CachedResource.connection.is_caching?.should == false
20
+ end
21
+ end
22
+
23
+ describe CachedResource do
24
+ before(:all) do
25
+ CachedResource.site = 'http://example.com.i:3000'
26
+ CachedResource.connection.cache_store = ActiveSupport::Cache.lookup_store :memory_store
27
+ end
28
+
29
+ after(:all) do
30
+ CachedResource.connection.cache_store = :none
31
+ end
32
+
33
+ before(:each) do
34
+ @thing = CachedResource.new(:id => 1)
35
+ @key = :key
36
+ CachedResource.connection.stub!(:cache_key).and_return(@key)
37
+ end
38
+
39
+ context "when a cached response is available" do
40
+ before(:each) do
41
+ CachedResource.connection.cache_store.write(@key, @thing.attributes)
42
+ end
43
+
44
+ it "should NOT make a request to the RESTful server" do
45
+ CachedResource.connection.should_not_receive(:get_without_cache)
46
+ CachedResource.find(1)
47
+ end
48
+
49
+ it "should read from the cache" do
50
+ CachedResource.find(1).should == @thing
51
+ end
52
+
53
+ it "should delete from the cache when an object is DELETEd" do
54
+ CachedResource.connection.should_receive(:delete_without_cache)
55
+ CachedResource.delete(1)
56
+ CachedResource.connection.cache_store.read(@key).should == nil
57
+ end
58
+
59
+ it "should delete from the cache when an object is modified" do
60
+ thing = CachedResource.find(1)
61
+ thing.stub(:load_attributes_from_response).and_return(@thing.attributes)
62
+ CachedResource.connection.should_receive(:put_without_cache)
63
+ thing.save
64
+ CachedResource.connection.cache_store.read(@key).should == nil
65
+ end
66
+ end
67
+
68
+ context "when a cached response is NOT available" do
69
+ before(:each) do
70
+ CachedResource.connection.cache_store.delete(@key)
71
+ end
72
+
73
+ it "SHOULD perform an ActiveResource request" do
74
+ CachedResource.connection.should_receive(:get_without_cache).and_return(@thing.attributes)
75
+ CachedResource.find(1)
76
+ end
77
+
78
+ it "should cache the response using the caching key" do
79
+ CachedResource.connection.should_receive(:get_without_cache).and_return(@thing.attributes)
80
+ CachedResource.find(1)
81
+ CachedResource.connection.cache_store.read(@key).should == @thing.attributes
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,13 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe Cobregratis::Base do
4
+
5
+ before(:each) do
6
+ @base = Cobregratis::Base.new
7
+ end
8
+
9
+ it "should be instance of ActiveResource::Base" do
10
+ @base.kind_of?(ActiveResource::Base).should be_true
11
+ end
12
+
13
+ end
@@ -0,0 +1,12 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe Cobregratis::Billet do
4
+ before(:each) do
5
+ @billet = Cobregratis::Billet.new
6
+ end
7
+
8
+ it "should be instance of Cobregratis::Base" do
9
+ @billet.kind_of?(Cobregratis::Base).should be_true
10
+ end
11
+
12
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1,8 @@
1
+ --colour
2
+ --format
3
+ progress
4
+ --loadby
5
+ mtime
6
+ --reverse
7
+ -rubygems
8
+ --debugger
@@ -0,0 +1,9 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'cobregratis'
4
+ require 'spec'
5
+ require 'spec/autorun'
6
+
7
+ Spec::Runner.configure do |config|
8
+
9
+ end
metadata ADDED
@@ -0,0 +1,142 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cobregratis
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Rafael Lima
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-08-22 00:00:00 -03: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: activeresource
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ hash: 1
46
+ segments:
47
+ - 2
48
+ - 1
49
+ version: "2.1"
50
+ type: :runtime
51
+ version_requirements: *id002
52
+ - !ruby/object:Gem::Dependency
53
+ name: activesupport
54
+ prerelease: false
55
+ requirement: &id003 !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ hash: 1
61
+ segments:
62
+ - 2
63
+ - 1
64
+ version: "2.1"
65
+ type: :runtime
66
+ version_requirements: *id003
67
+ description: "\n\
68
+ Cobre Gr\xC3\xA1tis\n\n\
69
+ Configure by adding the following:\n\n\
70
+ require 'cobregratis'\n\
71
+ Cobregratis::Base.site = 'https://app.cobregratis.com.br/'\n\
72
+ Cobregratis::Base.user = 'your_api_username'\n\
73
+ Cobregratis::Base.user = 'your_api_password'\n "
74
+ email: contato@rafael.adm.br
75
+ executables: []
76
+
77
+ extensions: []
78
+
79
+ extra_rdoc_files:
80
+ - LICENSE
81
+ - README.mkdn
82
+ files:
83
+ - .document
84
+ - .gitignore
85
+ - LICENSE
86
+ - README.mkdn
87
+ - Rakefile
88
+ - VERSION
89
+ - autotest/discover.rb
90
+ - examples/config_initializers_cobregratis.rb
91
+ - examples/sample.rb
92
+ - init.rb
93
+ - lib/cachable.rb
94
+ - lib/cobregratis.rb
95
+ - lib/cobregratis/base.rb
96
+ - lib/cobregratis/billet.rb
97
+ - spec/cachable_spec.rb
98
+ - spec/cobregratis/base_spec.rb
99
+ - spec/cobregratis/billet_spec.rb
100
+ - spec/spec.opts
101
+ - spec/spec_helper.rb
102
+ has_rdoc: true
103
+ homepage: http://github.com/rafaelp/cobregratis
104
+ licenses: []
105
+
106
+ post_install_message:
107
+ rdoc_options:
108
+ - --charset=UTF-8
109
+ require_paths:
110
+ - lib
111
+ required_ruby_version: !ruby/object:Gem::Requirement
112
+ none: false
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ hash: 3
117
+ segments:
118
+ - 0
119
+ version: "0"
120
+ required_rubygems_version: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ">="
124
+ - !ruby/object:Gem::Version
125
+ hash: 3
126
+ segments:
127
+ - 0
128
+ version: "0"
129
+ requirements: []
130
+
131
+ rubyforge_project:
132
+ rubygems_version: 1.3.7
133
+ signing_key:
134
+ specification_version: 3
135
+ summary: "Ruby Wrapper around Cobre Gr\xC3\xA1tis API"
136
+ test_files:
137
+ - spec/cachable_spec.rb
138
+ - spec/cobregratis/base_spec.rb
139
+ - spec/cobregratis/billet_spec.rb
140
+ - spec/spec_helper.rb
141
+ - examples/config_initializers_cobregratis.rb
142
+ - examples/sample.rb