qbo_rails 1.0.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.
Files changed (87) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/Rakefile +23 -0
  4. data/lib/generators/qbo_rails/install/install_generator.rb +32 -0
  5. data/lib/generators/qbo_rails/install/templates/config/qbo_rails.rb +17 -0
  6. data/lib/generators/qbo_rails/install/templates/db/migrate/create_qbo_errors.rb +12 -0
  7. data/lib/generators/qbo_rails/install/templates/models/qbo_error.rb +3 -0
  8. data/lib/generators/qbo_rails/install/usage.md +3 -0
  9. data/lib/qbo_rails.rb +106 -0
  10. data/lib/qbo_rails/error_handler.rb +4 -0
  11. data/lib/qbo_rails/version.rb +3 -0
  12. data/lib/tasks/qbo_rails_tasks.rake +4 -0
  13. data/spec/dummy/README.rdoc +28 -0
  14. data/spec/dummy/Rakefile +6 -0
  15. data/spec/dummy/app/assets/javascripts/application.js +13 -0
  16. data/spec/dummy/app/assets/stylesheets/application.css +15 -0
  17. data/spec/dummy/app/controllers/application_controller.rb +5 -0
  18. data/spec/dummy/app/controllers/settings_controller.rb +33 -0
  19. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  20. data/spec/dummy/app/models/account.rb +2 -0
  21. data/spec/dummy/app/models/customer.rb +2 -0
  22. data/spec/dummy/app/models/qbo_error.rb +3 -0
  23. data/spec/dummy/app/services/qbo_rails.rb +15 -0
  24. data/spec/dummy/app/services/qbo_rails/error_handler.rb +32 -0
  25. data/spec/dummy/app/views/layouts/application.html.erb +26 -0
  26. data/spec/dummy/app/views/settings/close_and_redirect.html.erb +14 -0
  27. data/spec/dummy/app/views/settings/index.html.erb +29 -0
  28. data/spec/dummy/bin/bundle +3 -0
  29. data/spec/dummy/bin/rails +8 -0
  30. data/spec/dummy/bin/rake +8 -0
  31. data/spec/dummy/bin/setup +29 -0
  32. data/spec/dummy/config.ru +4 -0
  33. data/spec/dummy/config/application.rb +41 -0
  34. data/spec/dummy/config/boot.rb +5 -0
  35. data/spec/dummy/config/database.yml +25 -0
  36. data/spec/dummy/config/environment.rb +5 -0
  37. data/spec/dummy/config/environments/development.rb +41 -0
  38. data/spec/dummy/config/environments/production.rb +79 -0
  39. data/spec/dummy/config/environments/test.rb +42 -0
  40. data/spec/dummy/config/initializers/assets.rb +11 -0
  41. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  42. data/spec/dummy/config/initializers/cookies_serializer.rb +3 -0
  43. data/spec/dummy/config/initializers/dotenv.rb +3 -0
  44. data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  45. data/spec/dummy/config/initializers/inflections.rb +16 -0
  46. data/spec/dummy/config/initializers/mime_types.rb +4 -0
  47. data/spec/dummy/config/initializers/qbo_rails.rb +17 -0
  48. data/spec/dummy/config/initializers/session_store.rb +3 -0
  49. data/spec/dummy/config/initializers/webmock.rb +2 -0
  50. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  51. data/spec/dummy/config/locales/en.yml +23 -0
  52. data/spec/dummy/config/routes.rb +5 -0
  53. data/spec/dummy/config/secrets.yml +22 -0
  54. data/spec/dummy/db/development.sqlite3 +0 -0
  55. data/spec/dummy/db/migrate/20150319132530_create_accounts.rb +12 -0
  56. data/spec/dummy/db/migrate/20150319132650_create_customers.rb +17 -0
  57. data/spec/dummy/db/migrate/20150328125013_add_qbo_id_to_customer.rb +5 -0
  58. data/spec/dummy/db/migrate/20150418134516_create_qbo_errors.rb +12 -0
  59. data/spec/dummy/db/production.sqlite3 +0 -0
  60. data/spec/dummy/db/schema.rb +50 -0
  61. data/spec/dummy/db/test.sqlite3 +0 -0
  62. data/spec/dummy/log/development.log +584 -0
  63. data/spec/dummy/log/production.log +0 -0
  64. data/spec/dummy/log/test.log +6502 -0
  65. data/spec/dummy/public/404.html +67 -0
  66. data/spec/dummy/public/422.html +67 -0
  67. data/spec/dummy/public/500.html +66 -0
  68. data/spec/dummy/public/favicon.ico +0 -0
  69. data/spec/dummy/tmp/cache/222/7E0/token +2 -0
  70. data/spec/dummy/tmp/cache/assets/development/sprockets/13fe41fee1fe35b49d145bcc06610705 +0 -0
  71. data/spec/dummy/tmp/cache/assets/development/sprockets/2f5173deea6c795b8fdde723bb4b63af +0 -0
  72. data/spec/dummy/tmp/cache/assets/development/sprockets/357970feca3ac29060c1e3861e2c0953 +0 -0
  73. data/spec/dummy/tmp/cache/assets/development/sprockets/cffd775d018f68ce5dba1ee0d951a994 +0 -0
  74. data/spec/dummy/tmp/cache/assets/development/sprockets/d771ace226fc8215a3572e0aa35bb0d6 +0 -0
  75. data/spec/dummy/tmp/cache/assets/development/sprockets/f7cbd26ba1d28d48de824f0e94586655 +0 -0
  76. data/spec/factories/accounts.rb +8 -0
  77. data/spec/factories/customers.rb +15 -0
  78. data/spec/rails_helper.rb +92 -0
  79. data/spec/requests/qbo_rails_spec.rb +118 -0
  80. data/spec/spec_helper.rb +86 -0
  81. data/spec/vcr/qbo_rails/auth/forbidden.yml +119 -0
  82. data/spec/vcr/qbo_rails/customer/create.yml +75 -0
  83. data/spec/vcr/qbo_rails/customer/create_and_then_delete.yml +225 -0
  84. data/spec/vcr/qbo_rails/customer/delete.yml +167 -0
  85. data/spec/vcr/qbo_rails/customer/error_customer_in_use.yml +257 -0
  86. data/spec/vcr/qbo_rails/customer/update.yml +138 -0
  87. metadata +358 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c4789a72cda5576a533ca48cc89c098f5552ef63
4
+ data.tar.gz: a7a5fb31c24f07dc2795c40aa2b33aae284ce18d
5
+ SHA512:
6
+ metadata.gz: 3dad384c0da81655114194358205e33296a52b28ad1665ffa386a2061587633ab6d7eb44b3316008fd0d61feb9537467c372f24860ca5e85cdcfc91f86556d81
7
+ data.tar.gz: 0928f1615572dd845e5be490f09623598849c8e26760ac4c9df348a777f77aa12b8567b737a4f22a2bad23b1205546bde9a083f8767414e3073ea3290e9d5671
@@ -0,0 +1,20 @@
1
+ Copyright 2015 Christian
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.
@@ -0,0 +1,23 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'QboRails'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.rdoc')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+
18
+
19
+
20
+
21
+
22
+ Bundler::GemHelper.install_tasks
23
+
@@ -0,0 +1,32 @@
1
+ require 'rails/generators'
2
+ require 'rails/generators/active_record'
3
+
4
+ class QboRails
5
+ module Generators
6
+ class InstallGenerator < Rails::Generators::Base
7
+ include Rails::Generators::Migration
8
+ extend Rails::Generators::Migration
9
+
10
+ namespace "qbo_rails:install"
11
+ desc "Copy QboRails default files"
12
+ source_root File.expand_path('../templates', __FILE__)
13
+
14
+ def copy_config
15
+ template('config/qbo_rails.rb', "config/initializers/qbo_rails.rb")
16
+ end
17
+
18
+ def copy_model
19
+ template('models/qbo_error.rb', "app/models/qbo_error.rb")
20
+ end
21
+
22
+ def active_record
23
+ migration_template 'db/migrate/create_qbo_errors.rb', 'db/migrate/create_qbo_errors.rb'
24
+ end
25
+
26
+ def self.next_migration_number(dirname)
27
+ ::ActiveRecord::Generators::Base.next_migration_number(dirname)
28
+ end
29
+
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,17 @@
1
+ QB_KEY = Rails.application.secrets.qbo_app_consumer_key
2
+ QB_SECRET = Rails.application.secrets.qbo_app_consumer_secret
3
+
4
+ $qb_oauth_consumer = OAuth::Consumer.new(QB_KEY, QB_SECRET, {
5
+ :site => "https://oauth.intuit.com",
6
+ :request_token_path => "/oauth/v1/get_request_token",
7
+ :authorize_url => "https://appcenter.intuit.com/Connect/Begin",
8
+ :access_token_path => "/oauth/v1/get_access_token"
9
+ })
10
+
11
+ Quickbooks.sandbox_mode = true unless Rails.env.production?
12
+
13
+ Quickbooks::Base.configure do |c|
14
+ c.persistent_token = 'qb_token'
15
+ c.persistent_secret = 'qb_secret'
16
+ c.persistent_company_id = 'qb_company_id'
17
+ end
@@ -0,0 +1,12 @@
1
+ class CreateQboErrors < ActiveRecord::Migration
2
+ def change
3
+ create_table :qbo_errors, force: true do |t|
4
+ t.string :message
5
+ t.text :body
6
+ t.string :resource_type, limit: 100
7
+ t.integer :resource_id
8
+ t.text :request_xml
9
+ t.timestamps :null => false
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,3 @@
1
+ class QboError < ActiveRecord::Base
2
+ belongs_to :resource, polymorphic: true
3
+ end
@@ -0,0 +1,3 @@
1
+ To copy a QboRails initializer to your Rails App, with some configuration values, just do:
2
+
3
+ rails generate qbo_rails:install
@@ -0,0 +1,106 @@
1
+ require 'quickbooks-ruby'
2
+ require 'quickbooks-ruby-base'
3
+ require_relative 'qbo_rails/error_handler'
4
+ class QboRails
5
+ attr_accessor :error, :only_run_once
6
+ attr_reader :result, :base
7
+ cattr_accessor(:foreign_key) { "qbo_id" }
8
+
9
+ include ActiveSupport::Rescuable
10
+ include ErrorHandler
11
+ rescue_from Quickbooks::IntuitRequestException,
12
+ Quickbooks::InvalidModelException,
13
+ Quickbooks::AuthorizationFailure,
14
+ Quickbooks::Forbidden,
15
+ Quickbooks::MissingRealmError, with: :record_error
16
+
17
+ def initialize(account, type = nil)
18
+ @base = Quickbooks::Base.new(account, type)
19
+ end
20
+
21
+ def create(qb_record)
22
+ with_rescued_exception do
23
+ @qb_record = qb_record
24
+ @result = @base.service.create(@qb_record)
25
+ @result
26
+ end
27
+ end
28
+
29
+ def create_or_update(record, qb_record)
30
+ with_rescued_exception do
31
+ @record = record
32
+ @qb_record = qb_record
33
+ if id = @record.send(foreign_key)
34
+ prepare_update(id, qb_record)
35
+ @result = @base.service.update(qb_record)
36
+ else
37
+ @result = @base.service.create(@qb_record)
38
+ set_qbo_id
39
+ end
40
+ end
41
+ end
42
+
43
+ def delete(record_or_qbo_id)
44
+ with_rescued_exception do
45
+ id = record_or_qbo_id.try(foreign_key) || record_or_qbo_id
46
+ @qb_record = @base.find_by_id(id)
47
+ @result = @base.service.delete(@qb_record)
48
+ end
49
+ end
50
+
51
+ protected
52
+
53
+ def with_rescued_exception
54
+ yield
55
+ rescue => e
56
+ if @only_run_once
57
+ rescue_with_handler(e) || raise
58
+ else
59
+ unless run_error_handlers(e)
60
+ rescue_with_handler(e) || raise
61
+ end
62
+ end
63
+ end
64
+
65
+ def run_error_handlers(e)
66
+ methods.detect do |method|
67
+ if method =~ /^handle_error_/
68
+ send(method, e)
69
+ end
70
+ end
71
+ end
72
+
73
+ def qbo_error_params(exception)
74
+ @error = {
75
+ message: exception.message[0..250],
76
+ body: "#{exception}\n\n#{exception.message}\n\n#{exception.backtrace}",
77
+ resource_type: @record.try(:class).try(:name),
78
+ resource_id: @record.try(:id),
79
+ request_xml: request_xml(exception)
80
+ }
81
+ end
82
+
83
+ def record_error(exception)
84
+ QboError.create!(qbo_error_params(exception))
85
+ end
86
+
87
+ def request_xml(exception)
88
+ if exception.respond_to?(:request_xml)
89
+ exception.request_xml
90
+ elsif defined?(@qb_record)
91
+ @qb_record.to_xml.to_s
92
+ end
93
+ end
94
+
95
+ def prepare_update(id, qb_record)
96
+ result = @base.find_by_id(id)
97
+ qb_record.id = result.id
98
+ qb_record.sync_token = result.sync_token
99
+ qb_record
100
+ end
101
+
102
+ def set_qbo_id
103
+ @record.update(self.foreign_key => @result.id)
104
+ @record
105
+ end
106
+ end
@@ -0,0 +1,4 @@
1
+ class QboRails
2
+ module ErrorHandler
3
+ end
4
+ end
@@ -0,0 +1,3 @@
1
+ class QboRails
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :qbo_rails do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,28 @@
1
+ == README
2
+
3
+ This README would normally document whatever steps are necessary to get the
4
+ application up and running.
5
+
6
+ Things you may want to cover:
7
+
8
+ * Ruby version
9
+
10
+ * System dependencies
11
+
12
+ * Configuration
13
+
14
+ * Database creation
15
+
16
+ * Database initialization
17
+
18
+ * How to run the test suite
19
+
20
+ * Services (job queues, cache servers, search engines, etc.)
21
+
22
+ * Deployment instructions
23
+
24
+ * ...
25
+
26
+
27
+ Please feel free to use a different markup language if you do not plan to run
28
+ <tt>rake doc:app</tt>.
@@ -0,0 +1,6 @@
1
+ # Add your own tasks in files placed in lib/tasks ending in .rake,
2
+ # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3
+
4
+ require File.expand_path('../config/application', __FILE__)
5
+
6
+ Rails.application.load_tasks
@@ -0,0 +1,13 @@
1
+ // This is a manifest file that'll be compiled into application.js, which will include all the files
2
+ // listed below.
3
+ //
4
+ // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
+ // or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
6
+ //
7
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
+ // compiled file.
9
+ //
10
+ // Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-directives) for details
11
+ // about supported directives.
12
+ //
13
+ //= require_tree .
@@ -0,0 +1,15 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the bottom of the
9
+ * compiled file so the styles you add here take precedence over styles defined in any styles
10
+ * defined in the other CSS/SCSS files in this directory. It is generally better to create a new
11
+ * file per style scope.
12
+ *
13
+ *= require_tree .
14
+ *= require_self
15
+ */
@@ -0,0 +1,5 @@
1
+ class ApplicationController < ActionController::Base
2
+ # Prevent CSRF attacks by raising an exception.
3
+ # For APIs, you may want to use :null_session instead.
4
+ protect_from_forgery with: :exception
5
+ end
@@ -0,0 +1,33 @@
1
+ class SettingsController < ApplicationController
2
+
3
+ def oauth_start
4
+ token = $qb_oauth_consumer.get_request_token(oauth_callback: oauth_callback_url)
5
+ cache_token(token)
6
+ redirect_to("https://appcenter.intuit.com/Connect/Begin?oauth_token=#{token.token}") and return
7
+ end
8
+
9
+ def oauth_callback
10
+ at = cache_token.get_access_token(:oauth_verifier => params[:oauth_verifier])
11
+ @url = root_url
12
+ session[:at] = { token: at.token, secret: at.secret, companyid: params['realmId'] }
13
+ msg = "Your QuickBooks account has been successfully linked."
14
+ flash[:notice] = msg
15
+ render 'close_and_redirect'
16
+ rescue => e
17
+ flash[:alert] = "There was a problem linking Your QuickBooks account with error: #{e.message}"
18
+ render 'close_and_redirect'
19
+ end
20
+
21
+ private
22
+
23
+ def cache_token(value = nil)
24
+ key = "token"
25
+ unless value
26
+ Rails.cache.read(key)
27
+ else
28
+ Rails.cache.write(key, value, expire_in: 1.hour)
29
+ end
30
+ end
31
+ end
32
+
33
+
@@ -0,0 +1,2 @@
1
+ module ApplicationHelper
2
+ end
@@ -0,0 +1,2 @@
1
+ class Account < ActiveRecord::Base
2
+ end
@@ -0,0 +1,2 @@
1
+ class Customer < ActiveRecord::Base
2
+ end
@@ -0,0 +1,3 @@
1
+ class QboError < ActiveRecord::Base
2
+ belongs_to :resource, polymorphic: true
3
+ end
@@ -0,0 +1,15 @@
1
+ #cass QboRails
2
+ # #module ErrorHandler
3
+ # put 'HIX'
4
+ # def handle_error_no_line_items(exception)
5
+ # puts 'HI'
6
+ # if exception.message =~ /Forbidden/
7
+ # @only_run_once = true
8
+ # puts 'hi'
9
+ # true
10
+ # else
11
+ # false
12
+ # end
13
+ # end
14
+ # #end
15
+ #end
@@ -0,0 +1,32 @@
1
+ class QboRails
2
+ module ErrorHandler
3
+
4
+ def handle_error_name_entity_already_exists(exception)
5
+ if exception.message =~ /Duplicate Name Exists Error.*Another (customer|vendor|employee)/m
6
+ display_name = Nokogiri::XML(exception.request_xml).at('DisplayName').content
7
+ result = @base.find_by_display_name(display_name)
8
+ if result.entries.size == 1
9
+ @record.update_column(foreign_key, result.entries.first.id)
10
+ @only_run_once = true
11
+ @record.reload
12
+ create_or_update(@record, @qb_record)
13
+ true
14
+ else
15
+ false
16
+ end
17
+ else
18
+ false
19
+ end
20
+ end
21
+
22
+ def handle_error_no_line_items(exception)
23
+ if exception.message =~ /Forbidden/
24
+ @only_run_once = true
25
+ create(@qb_record)
26
+ true
27
+ else
28
+ false
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,26 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>QboRails dummy app</title>
5
+ <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
6
+ <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
7
+ <%= csrf_meta_tags %>
8
+ <%= yield :head %>
9
+ </head>
10
+ <body>
11
+
12
+ <% flash.each do |name, msg| %>
13
+ <% if msg.is_a?(String) && name.to_s =~ /^(notice|alert)$/ %>
14
+ <div class="alert alert-<%= name.to_s == 'notice' ? 'success' : 'danger' %>">
15
+ <%= content_tag :div, msg, :id => "flash_#{name}" %>
16
+ </div>
17
+ <% end %>
18
+ <% end %>
19
+
20
+ <%= yield %>
21
+
22
+ </body>
23
+ <script>
24
+ <%= yield :script %>
25
+ </script>
26
+ </html>