faturando_api 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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License
2
+
3
+ Copyright (c) 2009 Grasshopper Group, LLC
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,51 @@
1
+ Faturando Api (usando ActiveResource)
2
+ ====================================================
3
+
4
+ faturando_api
5
+ -----------------
6
+
7
+ Permite acessar a API do [Faturando](http://faturan.do) usando ActiveResource.
8
+
9
+
10
+ ### Instalação
11
+
12
+ Esta biblioteca pode ser instalada como uma e está disponível no [Rubygems](http://rubygems.org).
13
+
14
+ Para instalar basta executar o seguinte comando:
15
+
16
+ $ gem install faturando_api
17
+
18
+
19
+ ### Requisitos
20
+
21
+ Esta biblioteca depende do ActiveResource com versão 2.3.4 ou maior.
22
+
23
+ $ gem install activeresource
24
+
25
+
26
+ ### Utilização
27
+
28
+ Para utilizar a biblioteca basta importá-la utilizando:
29
+
30
+ gem 'faturando_api'
31
+
32
+
33
+ Se você está usando Rails 3 basta adicionar a seguinte linha no Gemfile:
34
+
35
+ gem 'faturando_api'
36
+
37
+ Se você está usando Rails 2.3.X basta adicionar a seguinte linha na configuração da aplicação,
38
+ por exemplo, no `environment.rb`
39
+
40
+ config.gem 'faturando_api'
41
+
42
+ ### Configurando
43
+
44
+ Para poder se comunicar com o Faturando você precisa configurar a chave da API e o código do produto a ser utilizado.
45
+
46
+ Faturando.configure do |c|
47
+ c.project_key = '1234'
48
+ c.api_key = 'XYZ'
49
+ end
50
+
51
+ Se você estiver utilizando Rails deve preferêncialmente criar um initializer.
@@ -0,0 +1,3 @@
1
+ module Faturando
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,116 @@
1
+ begin
2
+ require 'active_resource'
3
+ rescue LoadError
4
+ begin
5
+ require 'rubygems'
6
+ require 'active_resource'
7
+ rescue LoadError
8
+ abort <<-ERROR
9
+ The 'activeresource' library could not be loaded. If you have RubyGems
10
+ installed you can install ActiveResource by doing "gem install activeresource".
11
+ ERROR
12
+ end
13
+ end
14
+
15
+
16
+ # Version check
17
+ module Faturando
18
+ MIN_VERSION = '2.3.4'
19
+ end
20
+ require 'active_resource/version'
21
+ unless Gem::Version.new(ActiveResource::VERSION::STRING) >= Gem::Version.new(Faturando::MIN_VERSION)
22
+ abort <<-ERROR
23
+ ActiveResource version #{Faturando::MIN_VERSION} or greater is required.
24
+ ERROR
25
+ end
26
+
27
+ # Patch ActiveResource version 2.3.4
28
+ if ActiveResource::VERSION::STRING == '2.3.4'
29
+ module ActiveResource
30
+ class Base
31
+ def save
32
+ save_without_validation
33
+ true
34
+ rescue ResourceInvalid => error
35
+ case error.response['Content-Type']
36
+ when /application\/xml/
37
+ errors.from_xml(error.response.body)
38
+ when /application\/json/
39
+ errors.from_json(error.response.body)
40
+ end
41
+ false
42
+ end
43
+ end
44
+ end
45
+ end
46
+
47
+ module Faturando
48
+ class << self
49
+ attr_accessor :project_key, :api_key, :site, :format, :timeout
50
+
51
+ def configure
52
+ yield self
53
+
54
+ Base.user = api_key
55
+ Base.password = 'X'
56
+ Base.timeout = timeout unless (timeout.blank?)
57
+
58
+ self.site ||= "https://faturan.do/"
59
+
60
+ base_path = site + "/projects/#{project_key}"
61
+ Base.site = base_path
62
+ FeatureValue.site = base_path + "/plans/:plan_id"
63
+ end
64
+ end
65
+
66
+ class Base < ActiveResource::Base
67
+ self.format = :xml
68
+
69
+ def self.element_name
70
+ name.split(/::/).last.underscore
71
+ end
72
+
73
+ def to_xml(options = {})
74
+ options.merge!(:dasherize => false)
75
+ super
76
+ end
77
+ end
78
+
79
+ class Feature < Base
80
+ end
81
+
82
+ class FeatureValue < Base
83
+ end
84
+
85
+ class Plan < Base
86
+ def self.subscribible
87
+ self.all.select(&:subscribible)
88
+ end
89
+
90
+ def feature_value_by_handle(handle)
91
+ found = feature_values.select do |feature_value|
92
+ feature_value.feature.handle == handle
93
+ end
94
+ found.first
95
+ end
96
+
97
+ def method_missing(method, *args, &block)
98
+ if method.to_s.match(/^unlimited_(.+)\?$/)
99
+ feature_value = feature_value_by_handle($1)
100
+ if feature_value
101
+ return feature_value.unlimited
102
+ end
103
+ end
104
+
105
+ begin
106
+ super
107
+ rescue NoMethodError => e
108
+ feature_value = feature_value_by_handle(method.to_s)
109
+ if feature_value
110
+ return feature_value.value
111
+ end
112
+ raise(e)
113
+ end
114
+ end
115
+ end
116
+ end
data/spec/base_spec.rb ADDED
@@ -0,0 +1,8 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe Faturando::Base do
4
+ it 'parses element names' do
5
+ Faturando::Base.stub!(:name).and_return("Test::Namespace::ElementName")
6
+ Faturando::Base.element_name.should eql('element_name')
7
+ end
8
+ end
data/spec/factories.rb ADDED
@@ -0,0 +1,17 @@
1
+ FactoryGirl.define do
2
+ sequence :plan_name do |n|
3
+ "Plan #{n}"
4
+ end
5
+
6
+ factory :plan, :class => Faturando::Plan do |p|
7
+ p.name { FactoryGirl.generate(:plan_name) }
8
+ p.value { 19.99 }
9
+ p.subscribible { true }
10
+ end
11
+
12
+ factory :feature, :class => Faturando::Feature do |f|
13
+ f.name { Faker::Name.name }
14
+ f.type { 'Features::Numeric' }
15
+ f.description { Faker::Lorem.sentence }
16
+ end
17
+ end
@@ -0,0 +1,82 @@
1
+ # Taken from https://gist.github.com/238158/487a411c392e1fb2a0c00347e32444320f5cdd49
2
+ require 'fakeweb'
3
+
4
+ module ActiveResource
5
+
6
+ #
7
+ # The FakeResource fakes ActiveResources so that no real resources are transferred. It catches the creation methods
8
+ # and stores the resources internally instead of sending to a remote service and responds these resources back on
9
+ # request.
10
+ #
11
+ # Additionally it adds a save! method and can be used in conjunction with Cucumber/Pickle/FactoryGirl to fully
12
+ # fake a back end service in the BDD cycle
13
+ #
14
+ module FakeResource
15
+
16
+ @@fake_resources = []
17
+
18
+ def self.clean
19
+ FakeWeb.clean_registry
20
+ @@fake_resources = []
21
+ end
22
+
23
+ def self.included(base)
24
+ base.class_eval do
25
+
26
+ def save
27
+ @@fake_resources << self
28
+ update_fake_responses
29
+ end
30
+
31
+ def destroy
32
+ @@fake_resources.delete(self)
33
+ update_fake_responses
34
+ end
35
+
36
+ def self.delete(id, options = {})
37
+ puts "delete"
38
+ @@fake_resources.delete_if {|r| r.id == id }
39
+ #update_fake_responses
40
+ end
41
+
42
+ def self.exists?(id, options = {})
43
+ not @@fake_resources.select {|r| r.id == id}.blank?
44
+ end
45
+
46
+ end
47
+ end
48
+
49
+ def save!
50
+ save
51
+ end
52
+
53
+ def save
54
+ super
55
+ end
56
+
57
+ private
58
+
59
+ def update_fake_responses
60
+ FakeWeb.clean_registry
61
+
62
+ @@fake_resources.each do |r|
63
+ FakeWeb.register_uri(:get, element_uri, :body => r.to_xml)
64
+ end
65
+
66
+ FakeWeb.register_uri(:get, collection_uri, :body => @@fake_resources.to_xml)
67
+ end
68
+
69
+ def element_uri
70
+ "#{base_uri}#{element_path}"
71
+ end
72
+
73
+ def collection_uri
74
+ "#{base_uri}#{collection_path}"
75
+ end
76
+
77
+ def base_uri
78
+ "#{connection.site.scheme}://#{connection.user}:#{connection.password}@#{connection.site.host}:#{connection.site.port}"
79
+ end
80
+
81
+ end
82
+ end
data/spec/plan_spec.rb ADDED
@@ -0,0 +1,61 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe Faturando::Plan do
4
+
5
+ context 'subscribible' do
6
+ before do
7
+ @subscribible_plan = Factory(:plan, :subscribible => true)
8
+ @non_subscribible_plan = Factory(:plan, :subscribible => false)
9
+
10
+ @all = [@subscribible_plan, @non_subscribible_plan]
11
+ FakeWeb.register_uri(:get, "/project/1234/#{test_domain}/plans.xml", :body => @all.to_xml)
12
+ end
13
+
14
+ it 'finds only subscribible plans' do
15
+ Faturando::Plan.subscribible.should == [@subscribible_plan]
16
+ end
17
+ end
18
+
19
+ it "find a feature value based on handle" do
20
+ plan = Factory(:plan)
21
+ feature = Factory(:feature, handle: 'notas_fiscais')
22
+ plan.feature_values = [
23
+ Faturando::FeatureValue.create(plan: plan, feature: feature, value: 'XXXX')
24
+ ]
25
+
26
+ plan.feature_value_by_handle('notas_fiscais').should == plan.feature_values.first
27
+ plan.feature_value_by_handle('non_exising').should == nil
28
+ end
29
+
30
+ it "has virtual methods based on feature handles" do
31
+ plan = Factory(:plan)
32
+ feature_notas = Factory(:feature, handle: 'notas_fiscais')
33
+ feature_email = Factory(:feature, handle: 'emails_limit')
34
+ plan.feature_values = [
35
+ Faturando::FeatureValue.create(plan: plan, feature: feature_notas, value: 'XXXX'),
36
+ Faturando::FeatureValue.create(plan: plan, feature: feature_email, value: 'YYYY')
37
+ ]
38
+
39
+ plan.notas_fiscais.should == 'XXXX'
40
+ plan.emails_limit.should == 'YYYY'
41
+
42
+ expect {
43
+ plan.notas_fiscais_limit
44
+ }.to raise_error(NoMethodError)
45
+ end
46
+
47
+ it "has virtual methods to discover if a feature is unlimited" do
48
+ plan = Factory(:plan)
49
+ feature_notas = Factory(:feature, handle: 'notas_fiscais')
50
+ feature_email = Factory(:feature, handle: 'emails_limit')
51
+ plan.feature_values = [
52
+ Faturando::FeatureValue.create(plan: plan, feature: feature_notas, value: 'XXXX', unlimited: true),
53
+ Faturando::FeatureValue.create(plan: plan, feature: feature_email, value: 'YYYY', unlimited: false)
54
+ ]
55
+
56
+ plan.unlimited_notas_fiscais?.should be_true
57
+ plan.unlimited_emails_limit?.should be_false
58
+
59
+ plan.unlimited_notas_fiscais_test? be_nil
60
+ end
61
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1,2 @@
1
+ --colour
2
+ --format documentation
@@ -0,0 +1,29 @@
1
+ require 'rubygems'
2
+ require 'rspec'
3
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
4
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
5
+
6
+ require 'faturando_api'
7
+
8
+ require 'fakeweb'
9
+ require 'mocks/fake_resource'
10
+ ActiveResource::Base.send :include, ActiveResource::FakeResource
11
+ FakeWeb.allow_net_connect = false
12
+ require 'factory_girl'
13
+ require 'faker'
14
+ require 'factories'
15
+
16
+ Faturando.configure do |c|
17
+ c.project_key = '1234'
18
+ c.api_key = 'test'
19
+ end
20
+
21
+ RSpec.configure do |config|
22
+ config.after(:each) do
23
+ ActiveResource::FakeResource.clean
24
+ end
25
+ end
26
+
27
+ def test_domain
28
+ "#{Faturando::Base.connection.site.scheme}://#{Faturando::Base.connection.user}:#{Faturando::Base.connection.password}@#{Faturando::Base.connection.site.host}:#{Faturando::Base.connection.site.port}"
29
+ end
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: faturando_api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Anderson Dias
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-08-19 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: &85280580 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *85280580
25
+ - !ruby/object:Gem::Dependency
26
+ name: factory_girl
27
+ requirement: &85280340 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *85280340
36
+ - !ruby/object:Gem::Dependency
37
+ name: fakeweb
38
+ requirement: &85280120 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *85280120
47
+ - !ruby/object:Gem::Dependency
48
+ name: faker
49
+ requirement: &85279890 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *85279890
58
+ - !ruby/object:Gem::Dependency
59
+ name: active_resource
60
+ requirement: &85279670 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ type: :runtime
67
+ prerelease: false
68
+ version_requirements: *85279670
69
+ description: A Faturando API wrapper for Ruby using ActiveResource
70
+ email:
71
+ - andersondaraujo@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - lib/faturando/version.rb
77
+ - lib/faturando_api.rb
78
+ - LICENSE
79
+ - README.md
80
+ - spec/mocks/fake_resource.rb
81
+ - spec/spec_helper.rb
82
+ - spec/plan_spec.rb
83
+ - spec/factories.rb
84
+ - spec/spec.opts
85
+ - spec/base_spec.rb
86
+ homepage: http://github.com/tink/faturando_api
87
+ licenses: []
88
+ post_install_message:
89
+ rdoc_options: []
90
+ require_paths:
91
+ - lib
92
+ required_ruby_version: !ruby/object:Gem::Requirement
93
+ none: false
94
+ requirements:
95
+ - - ! '>='
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ required_rubygems_version: !ruby/object:Gem::Requirement
99
+ none: false
100
+ requirements:
101
+ - - ! '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ requirements: []
105
+ rubyforge_project: faturando_api
106
+ rubygems_version: 1.8.6
107
+ signing_key:
108
+ specification_version: 3
109
+ summary: A Faturando API wrapper for Ruby using ActiveResource
110
+ test_files:
111
+ - spec/mocks/fake_resource.rb
112
+ - spec/spec_helper.rb
113
+ - spec/plan_spec.rb
114
+ - spec/factories.rb
115
+ - spec/spec.opts
116
+ - spec/base_spec.rb