curdbee 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (48) hide show
  1. data/Gemfile +16 -0
  2. data/Gemfile.lock +35 -0
  3. data/LICENSE +22 -0
  4. data/README.markdown +43 -0
  5. data/Rakefile +19 -0
  6. data/VERSION +1 -0
  7. data/curdbee.gemspec +103 -0
  8. data/examples/create_invoice_for_new_client.rb +58 -0
  9. data/lib/curdbee.rb +20 -0
  10. data/lib/curdbee/base.rb +75 -0
  11. data/lib/curdbee/client.rb +6 -0
  12. data/lib/curdbee/config.rb +33 -0
  13. data/lib/curdbee/error.rb +23 -0
  14. data/lib/curdbee/estimate.rb +14 -0
  15. data/lib/curdbee/invoice.rb +10 -0
  16. data/lib/curdbee/invoiceable.rb +35 -0
  17. data/lib/curdbee/item.rb +6 -0
  18. data/lib/curdbee/parser.rb +13 -0
  19. data/lib/curdbee/payment.rb +18 -0
  20. data/lib/curdbee/recurring_profile.rb +6 -0
  21. data/spec/client_spec.rb +144 -0
  22. data/spec/estimate_spec.rb +312 -0
  23. data/spec/fixtures/client.json +20 -0
  24. data/spec/fixtures/clients.json +20 -0
  25. data/spec/fixtures/customers.json +12 -0
  26. data/spec/fixtures/estimate.json +24 -0
  27. data/spec/fixtures/estimates.json +26 -0
  28. data/spec/fixtures/invoice.json +24 -0
  29. data/spec/fixtures/invoices.json +26 -0
  30. data/spec/fixtures/item.json +11 -0
  31. data/spec/fixtures/items.json +11 -0
  32. data/spec/fixtures/new_client.json +20 -0
  33. data/spec/fixtures/new_estimate.json +24 -0
  34. data/spec/fixtures/new_invoice.json +24 -0
  35. data/spec/fixtures/new_item.json +11 -0
  36. data/spec/fixtures/new_payment.json +10 -0
  37. data/spec/fixtures/new_recurring_profile.json +26 -0
  38. data/spec/fixtures/payment.json +10 -0
  39. data/spec/fixtures/payments.json +10 -0
  40. data/spec/fixtures/recurring_profile.json +26 -0
  41. data/spec/fixtures/recurring_profiles.json +28 -0
  42. data/spec/invoice_spec.rb +280 -0
  43. data/spec/item_spec.rb +137 -0
  44. data/spec/payment_spec.rb +143 -0
  45. data/spec/recurring_profile_spec.rb +140 -0
  46. data/spec/spec_helper.rb +24 -0
  47. data/spec/support/fakeweb_stubs.rb +33 -0
  48. metadata +156 -0
data/Gemfile ADDED
@@ -0,0 +1,16 @@
1
+ source 'http://rubygems.org'
2
+
3
+ group :runtime do
4
+ gem 'httparty', '~> 0.5.2'
5
+ gem 'hashie', '~> 0.1.8'
6
+ gem 'json'
7
+ end
8
+
9
+ group :test do
10
+ gem 'jeweler'
11
+ gem 'rake'
12
+ gem 'bundler', '~> 1.0.0'
13
+ gem 'fakeweb', '>= 1.2.8'
14
+ gem 'rspec', '>= 1.3.0 '
15
+ gem 'activesupport', '~> 2.3.5', :require => 'active_support'
16
+ end
@@ -0,0 +1,35 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ activesupport (2.3.5)
5
+ crack (0.1.6)
6
+ fakeweb (1.2.8)
7
+ gemcutter (0.6.1)
8
+ git (1.2.5)
9
+ hashie (0.1.8)
10
+ httparty (0.5.2)
11
+ crack (= 0.1.6)
12
+ jeweler (1.4.0)
13
+ gemcutter (>= 0.1.0)
14
+ git (>= 1.2.5)
15
+ rubyforge (>= 2.0.0)
16
+ json (1.4.6)
17
+ json_pure (1.4.6)
18
+ rake (0.8.7)
19
+ rspec (1.3.0)
20
+ rubyforge (2.0.3)
21
+ json_pure (>= 1.1.7)
22
+
23
+ PLATFORMS
24
+ ruby
25
+
26
+ DEPENDENCIES
27
+ activesupport (~> 2.3.5)
28
+ bundler (~> 1.0.0)
29
+ fakeweb (>= 1.2.8)
30
+ hashie (~> 0.1.8)
31
+ httparty (~> 0.5.2)
32
+ jeweler
33
+ json
34
+ rake
35
+ rspec (>= 1.3.0)
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2010 Vesess Inc.
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.
21
+
22
+
@@ -0,0 +1,43 @@
1
+ # CurdBee
2
+
3
+ Ruby wrapper for the CurdBee API. Through this library you can perform actions on following resources:
4
+
5
+ * Invoices
6
+ * Estimates
7
+ * Recurring Profiles
8
+ * Clients
9
+ * Items
10
+ * Payments
11
+
12
+ ## Installation
13
+
14
+ sudo gem install curdbee
15
+
16
+ ## Configuration
17
+
18
+ Before your code communicate with the CurdBee API, do the following:
19
+
20
+ require 'curdbee'
21
+
22
+ # set the API key and subdomain for your account.
23
+ CurdBee::Config.api_key = "Your API Key"
24
+ CurdBee::Config.subdomain = "Your Subdomain"
25
+
26
+ To find your API Token, please login to your CurdBee account and visit Settings > Account Information
27
+
28
+ ## Examples
29
+
30
+ Check `examples/` directory for example usage.
31
+
32
+ ## How to Contribute?
33
+
34
+ You will need to have bundler gem installed in your system.
35
+
36
+ $ sudo gem install bundler
37
+ $ git clone git://github.com/vesess/curdbee.git
38
+ $ cd curdbee
39
+ $ bundle install
40
+
41
+ ### Copyright
42
+
43
+ See LICENSE for details.
@@ -0,0 +1,19 @@
1
+ require 'rake'
2
+
3
+ begin
4
+ require 'jeweler'
5
+ Jeweler::Tasks.new do |gem|
6
+ gem.name = "curdbee"
7
+ gem.summary = %Q{Ruby wrapper for the Chargify API}
8
+ gem.email = "lakshan@vesess.com"
9
+ gem.homepage = "http://github.com/vesess/curdbee"
10
+ gem.authors = ["Lakshan Perera"]
11
+
12
+ gem.add_dependency('httparty', '~> 0.5.2')
13
+ gem.add_dependency('hashie', '~> 0.1.8')
14
+ gem.add_dependency('json')
15
+ end
16
+ Jeweler::GemcutterTasks.new
17
+ rescue LoadError
18
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
19
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
@@ -0,0 +1,103 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{curdbee}
8
+ s.version = "0.0.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Lakshan Perera"]
12
+ s.date = %q{2010-09-28}
13
+ s.email = %q{lakshan@vesess.com}
14
+ s.extra_rdoc_files = [
15
+ "LICENSE",
16
+ "README.markdown"
17
+ ]
18
+ s.files = [
19
+ "Gemfile",
20
+ "Gemfile.lock",
21
+ "LICENSE",
22
+ "README.markdown",
23
+ "Rakefile",
24
+ "VERSION",
25
+ "curdbee.gemspec",
26
+ "examples/create_invoice_for_new_client.rb",
27
+ "lib/curdbee.rb",
28
+ "lib/curdbee/base.rb",
29
+ "lib/curdbee/client.rb",
30
+ "lib/curdbee/config.rb",
31
+ "lib/curdbee/error.rb",
32
+ "lib/curdbee/estimate.rb",
33
+ "lib/curdbee/invoice.rb",
34
+ "lib/curdbee/invoiceable.rb",
35
+ "lib/curdbee/item.rb",
36
+ "lib/curdbee/parser.rb",
37
+ "lib/curdbee/payment.rb",
38
+ "lib/curdbee/recurring_profile.rb",
39
+ "spec/client_spec.rb",
40
+ "spec/estimate_spec.rb",
41
+ "spec/fixtures/client.json",
42
+ "spec/fixtures/clients.json",
43
+ "spec/fixtures/customers.json",
44
+ "spec/fixtures/estimate.json",
45
+ "spec/fixtures/estimates.json",
46
+ "spec/fixtures/invoice.json",
47
+ "spec/fixtures/invoices.json",
48
+ "spec/fixtures/item.json",
49
+ "spec/fixtures/items.json",
50
+ "spec/fixtures/new_client.json",
51
+ "spec/fixtures/new_estimate.json",
52
+ "spec/fixtures/new_invoice.json",
53
+ "spec/fixtures/new_item.json",
54
+ "spec/fixtures/new_payment.json",
55
+ "spec/fixtures/new_recurring_profile.json",
56
+ "spec/fixtures/payment.json",
57
+ "spec/fixtures/payments.json",
58
+ "spec/fixtures/recurring_profile.json",
59
+ "spec/fixtures/recurring_profiles.json",
60
+ "spec/invoice_spec.rb",
61
+ "spec/item_spec.rb",
62
+ "spec/payment_spec.rb",
63
+ "spec/recurring_profile_spec.rb",
64
+ "spec/spec_helper.rb",
65
+ "spec/support/fakeweb_stubs.rb"
66
+ ]
67
+ s.homepage = %q{http://github.com/vesess/curdbee}
68
+ s.rdoc_options = ["--charset=UTF-8"]
69
+ s.require_paths = ["lib"]
70
+ s.rubygems_version = %q{1.3.6}
71
+ s.summary = %q{Ruby wrapper for the Chargify API}
72
+ s.test_files = [
73
+ "spec/client_spec.rb",
74
+ "spec/estimate_spec.rb",
75
+ "spec/invoice_spec.rb",
76
+ "spec/item_spec.rb",
77
+ "spec/payment_spec.rb",
78
+ "spec/recurring_profile_spec.rb",
79
+ "spec/spec_helper.rb",
80
+ "spec/support/fakeweb_stubs.rb",
81
+ "examples/create_invoice_for_new_client.rb"
82
+ ]
83
+
84
+ if s.respond_to? :specification_version then
85
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
86
+ s.specification_version = 3
87
+
88
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
89
+ s.add_runtime_dependency(%q<httparty>, ["~> 0.5.2"])
90
+ s.add_runtime_dependency(%q<hashie>, ["~> 0.1.8"])
91
+ s.add_runtime_dependency(%q<json>, [">= 0"])
92
+ else
93
+ s.add_dependency(%q<httparty>, ["~> 0.5.2"])
94
+ s.add_dependency(%q<hashie>, ["~> 0.1.8"])
95
+ s.add_dependency(%q<json>, [">= 0"])
96
+ end
97
+ else
98
+ s.add_dependency(%q<httparty>, ["~> 0.5.2"])
99
+ s.add_dependency(%q<hashie>, ["~> 0.1.8"])
100
+ s.add_dependency(%q<json>, [">= 0"])
101
+ end
102
+ end
103
+
@@ -0,0 +1,58 @@
1
+ # In this example we add a new client to your CurdBee account,
2
+ # Then create an invoice to him.
3
+
4
+ require 'rubygems'
5
+
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
8
+
9
+ require 'curdbee'
10
+
11
+ # set the API key and subdomain for your account.
12
+ CurdBee::Config.api_key = "Your API Key"
13
+ CurdBee::Config.subdomain = "Your Subdomain"
14
+
15
+ begin
16
+ # Create the client
17
+ @client = CurdBee::Client.new(:name => "ABC Inc.",
18
+ :email => "abc@example.com"
19
+ )
20
+ @client.create
21
+ puts "Created the client."
22
+
23
+ # Create the invoice
24
+ @invoice = CurdBee::Invoice.new(
25
+ :date => Date.today,
26
+ :due_date => (Date.today + 30),
27
+ :invoice_no => "IN-0001",
28
+ :client_id => @client.id,
29
+ :summary => "Online Presence for the company",
30
+ :line_items_attributes => [{:name_and_description => "Site Revamp", :quantity => 25, :price => 60.00, :unit => "hour"},
31
+ {:name_and_description => "Domain Registration", :quantity => 10, :price => 10.00, :unit => "domain"}
32
+ ],
33
+ :notes => "Please write a cheque in favour of MyCompany Inc."
34
+ )
35
+ @invoice.create
36
+
37
+ puts "Created the invoice"
38
+
39
+ # Add a payment
40
+ @invoice = CurdBee::Invoice.show(@invoice.id)
41
+ CurdBee::Payment.invoice_id = @invoice.id
42
+ @payment = CurdBee::Payment.new(:amount => 70, :date => Date.today, :payment_method => "Manual")
43
+ @payment.create
44
+ puts "Added the payment."
45
+
46
+ # Send the invoice
47
+ if @invoice.deliver
48
+ puts "Your invoice was successfully sent to the client."
49
+ #reload the invoice
50
+ @invoice = CurdBee::Invoice.show(@invoice.id)
51
+ puts "To view the invoice visit #{@invoice.permalink}"
52
+ else
53
+ puts "Error sending the invoice."
54
+ end
55
+
56
+ rescue => e
57
+ puts "Following error occured - #{e}"
58
+ end
@@ -0,0 +1,20 @@
1
+ require 'hashie'
2
+ require 'httparty'
3
+ require 'json'
4
+
5
+ module CurdBee
6
+ VERSION = '0.0.1'.freeze
7
+
8
+ autoload :Base, 'curdbee/base'
9
+ autoload :Config, 'curdbee/config'
10
+ autoload :Client, 'curdbee/client'
11
+ autoload :Error, 'curdbee/error'
12
+ autoload :Estimate, 'curdbee/estimate'
13
+ autoload :Invoice, 'curdbee/invoice'
14
+ autoload :Invoiceable, 'curdbee/invoiceable'
15
+ autoload :Item, 'curdbee/item'
16
+ autoload :Parser, 'curdbee/parser'
17
+ autoload :Payment, 'curdbee/payment'
18
+ autoload :RecurringProfile, 'curdbee/recurring_profile'
19
+
20
+ end
@@ -0,0 +1,75 @@
1
+ module CurdBee
2
+ class Base < Hashie::Mash
3
+ include HTTParty
4
+
5
+ parser CurdBee::Parser
6
+ headers 'Content-Type' => 'application/json'
7
+
8
+ class << self; attr_accessor :resource, :element end
9
+
10
+ def self.list(opts = {})
11
+ limit = opts[:limit] || 20
12
+ page = opts[:page] || 1
13
+
14
+ response = send_request(:get, "/#{self.resource}.json", :query => {'per_page' => limit, 'page' => page })
15
+ response.map {|c| self.new c["#{self.element}"]}
16
+ end
17
+
18
+ def self.show(id)
19
+ response = send_request(:get, "/#{self.resource}/#{id}.json")
20
+ #Hashie::Mash.new(response["#{@element}"])
21
+ self.new(response["#{self.element}"])
22
+ end
23
+
24
+ def create
25
+ body = {"#{self.class.element}" => self}.to_json
26
+ response = self.class.send_request(:post, "/#{self.class.resource}", :body => body)
27
+
28
+ self.id = response["#{self.class.element}"]["id"]
29
+ return true if (response.code.to_i == 200)
30
+ end
31
+
32
+ def update
33
+ body = {"#{self.class.element}" => self}.to_json
34
+ response = self.class.send_request(:put, "/#{self.class.resource}/#{self[:id]}", :body => body)
35
+ #Hashie::Mash.new(response["#{@element}"])
36
+ return true if (response.code.to_i == 200)
37
+ end
38
+
39
+ def delete
40
+ response = self.class.send_request(:delete, "/#{self.class.resource}/#{self[:id]}")
41
+ return true if response.code.to_i == 200
42
+ end
43
+
44
+ def self.send_request(method, path, opts={})
45
+ api_key = CurdBee::Config.api_key
46
+ subdomain = CurdBee::Config.subdomain
47
+ use_https = CurdBee::Config.use_https
48
+ scheme = use_https ? "https" : "http"
49
+
50
+ self.base_uri "#{scheme}://#{subdomain}.curdbee.com"
51
+
52
+ opts[:query] = opts.has_key?(:query) ? opts[:query].merge({'api_token' => api_key}) : {'api_token' => api_key}
53
+ begin
54
+ response = self.send(method, path, opts)
55
+ rescue
56
+ raise(CurdBee::Error::ConnectionFailed.new, "Failed to connect to server.")
57
+ end
58
+
59
+ case response.code.to_i
60
+ when 401
61
+ raise(CurdBee::Error::AccessDenied.new(response), "Access Denied.")
62
+ when 403
63
+ raise(CurdBee::Error::Forbidden.new(response), "Your action was forbidden.")
64
+ when 422
65
+ raise(CurdBee::Error::BadRequest.new(response), response.body)
66
+ when 404
67
+ raise(CurdBee::Error::NotFound.new(response), "Resource not found.")
68
+ when 500
69
+ raise(CurdBee::Error::ServerError.new(response), "Error occured in server.")
70
+ end
71
+
72
+ response
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,6 @@
1
+ module CurdBee
2
+ class Client < Base
3
+ @resource = "clients"
4
+ @element = "client"
5
+ end
6
+ end
@@ -0,0 +1,33 @@
1
+ module CurdBee
2
+ class Config
3
+
4
+ @@api_key = "your_api_key"
5
+ @@subdomain = "subdomain"
6
+ @@use_https = false
7
+
8
+ def self.api_key=(api_key)
9
+ @@api_key = api_key
10
+ end
11
+
12
+ def self.api_key
13
+ @@api_key
14
+ end
15
+
16
+ def self.subdomain=(subdomain)
17
+ @@subdomain = subdomain
18
+ end
19
+
20
+ def self.subdomain
21
+ @@subdomain
22
+ end
23
+
24
+ def self.use_https=(use_https)
25
+ @@use_https = use_https
26
+ end
27
+
28
+ def self.use_https
29
+ @@use_https
30
+ end
31
+
32
+ end
33
+ end