solidus_melhor_envio 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (42) hide show
  1. checksums.yaml +7 -0
  2. data/.circleci/config.yml +53 -0
  3. data/.gem_release.yml +5 -0
  4. data/.github/stale.yml +1 -0
  5. data/.github_changelog_generator +2 -0
  6. data/.gitignore +21 -0
  7. data/.rspec +2 -0
  8. data/.rubocop.yml +5 -0
  9. data/CHANGELOG.md +3 -0
  10. data/Gemfile +48 -0
  11. data/LICENSE +26 -0
  12. data/README.md +73 -0
  13. data/Rakefile +7 -0
  14. data/app/assets/javascripts/spree/backend/solidus_melhor_envio.js +2 -0
  15. data/app/assets/javascripts/spree/frontend/solidus_melhor_envio.js +2 -0
  16. data/app/assets/stylesheets/spree/backend/solidus_melhor_envio.css +4 -0
  17. data/app/assets/stylesheets/spree/frontend/solidus_melhor_envio.css +4 -0
  18. data/app/models/solidus_melhor_envio/account.rb +5 -0
  19. data/app/models/solidus_melhor_envio/api.rb +50 -0
  20. data/app/models/solidus_melhor_envio/shipping_estimator.rb +78 -0
  21. data/bin/console +17 -0
  22. data/bin/rails +7 -0
  23. data/bin/rails-engine +13 -0
  24. data/bin/rails-sandbox +16 -0
  25. data/bin/rake +7 -0
  26. data/bin/sandbox +78 -0
  27. data/bin/setup +8 -0
  28. data/config/locales/en.yml +5 -0
  29. data/config/routes.rb +5 -0
  30. data/db/migrate/20230929133307_create_solidus_melhor_envio_accounts.rb +16 -0
  31. data/db/migrate/20230929180714_add_delivery_time_to_spree_shipping_rate.rb +6 -0
  32. data/lib/generators/solidus_melhor_envio/install/install_generator.rb +41 -0
  33. data/lib/generators/solidus_melhor_envio/install/templates/initializer.rb +9 -0
  34. data/lib/solidus_melhor_envio/configuration.rb +22 -0
  35. data/lib/solidus_melhor_envio/engine.rb +19 -0
  36. data/lib/solidus_melhor_envio/testing_support/factories.rb +4 -0
  37. data/lib/solidus_melhor_envio/version.rb +5 -0
  38. data/lib/solidus_melhor_envio.rb +6 -0
  39. data/solidus_melhor_envio.gemspec +37 -0
  40. data/spec/models/solidus_melhor_envio/account_spec.rb +7 -0
  41. data/spec/spec_helper.rb +32 -0
  42. metadata +152 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 1ad45c61e6fda2543564f9139188b9b06cd3c64d8b8b60dece02ce1503d095d4
4
+ data.tar.gz: 2728d6275ae9c6d520924fe7591990b4d35b05195ada580bab675997d3e2a287
5
+ SHA512:
6
+ metadata.gz: 977782b31359d3fe2ae205eba17c671e1e251d02b190fd12899c8f11e2ccc91df77ff75f8ddb7fa17aea524015872c63185d7d2edb3625628c911349dc7aca02
7
+ data.tar.gz: 056d4c17829312baa57dd11fa3a48002b94f6efcc87f731e1a25b318c715f1156388574b5317c200efbd74f80184261b63cfae7f4b60dcae23ce5ca08fa9c192
@@ -0,0 +1,53 @@
1
+ version: 2.1
2
+
3
+ orbs:
4
+ # Required for feature specs.
5
+ browser-tools: circleci/browser-tools@1.1
6
+
7
+ # Always take the latest version of the orb, this allows us to
8
+ # run specs against Solidus supported versions only without the need
9
+ # to change this configuration every time a Solidus version is released
10
+ # or goes EOL.
11
+ solidusio_extensions: solidusio/extensions@volatile
12
+
13
+ jobs:
14
+ run-specs-with-sqlite:
15
+ executor: solidusio_extensions/sqlite
16
+ steps:
17
+ - browser-tools/install-chrome
18
+ - solidusio_extensions/run-tests
19
+ run-specs-with-postgres:
20
+ executor: solidusio_extensions/postgres
21
+ steps:
22
+ - browser-tools/install-chrome
23
+ - solidusio_extensions/run-tests
24
+ run-specs-with-mysql:
25
+ executor: solidusio_extensions/mysql
26
+ steps:
27
+ - browser-tools/install-chrome
28
+ - solidusio_extensions/run-tests
29
+ lint-code:
30
+ executor: solidusio_extensions/sqlite-memory
31
+ steps:
32
+ - solidusio_extensions/lint-code
33
+
34
+ workflows:
35
+ "Run specs on supported Solidus versions":
36
+ jobs:
37
+ - run-specs-with-sqlite
38
+ - run-specs-with-postgres
39
+ - run-specs-with-mysql
40
+ - lint-code
41
+
42
+ "Weekly run specs against main":
43
+ triggers:
44
+ - schedule:
45
+ cron: "0 0 * * 4" # every Thursday
46
+ filters:
47
+ branches:
48
+ only:
49
+ - main
50
+ jobs:
51
+ - run-specs-with-sqlite
52
+ - run-specs-with-postgres
53
+ - run-specs-with-mysql
data/.gem_release.yml ADDED
@@ -0,0 +1,5 @@
1
+ bump:
2
+ recurse: false
3
+ file: 'lib/solidus_melhor_envio/version.rb'
4
+ message: Bump SolidusMelhorEnvio to %{version}
5
+ tag: true
data/.github/stale.yml ADDED
@@ -0,0 +1 @@
1
+ _extends: .github
@@ -0,0 +1,2 @@
1
+ issues=false
2
+ exclude-labels=infrastructure
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ *.gem
2
+ \#*
3
+ *~
4
+ .#*
5
+ .DS_Store
6
+ .idea
7
+ .project
8
+ .sass-cache
9
+ coverage
10
+ Gemfile.lock
11
+ Gemfile-local
12
+ tmp
13
+ nbproject
14
+ pkg
15
+ *.swp
16
+ spec/dummy
17
+ spec/examples.txt
18
+ /sandbox
19
+ .rvmrc
20
+ .ruby-version
21
+ .ruby-gemset
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,5 @@
1
+ require:
2
+ - solidus_dev_support/rubocop
3
+
4
+ AllCops:
5
+ NewCops: disable
data/CHANGELOG.md ADDED
@@ -0,0 +1,3 @@
1
+ # Changelog
2
+
3
+ See https://github.com/solidusio-contrib/solidus_melhor_envio/releases or OLD_CHANGELOG.md for older versions.
data/Gemfile ADDED
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+ git_source(:github) { |repo| "https://github.com/#{repo}.git" }
5
+
6
+ branch = ENV.fetch('SOLIDUS_BRANCH', 'main')
7
+ gem 'solidus', github: 'solidusio/solidus', branch: branch
8
+
9
+ # The solidus_frontend gem has been pulled out since v3.2
10
+ if branch >= 'v3.2'
11
+ gem 'solidus_frontend'
12
+ elsif branch == 'main'
13
+ gem 'solidus_frontend', github: 'solidusio/solidus_frontend'
14
+ else
15
+ gem 'solidus_frontend', github: 'solidusio/solidus', branch: branch
16
+ end
17
+
18
+ # Needed to help Bundler figure out how to resolve dependencies,
19
+ # otherwise it takes forever to resolve them.
20
+ # See https://github.com/bundler/bundler/issues/6677
21
+ gem 'rails', '>0.a'
22
+
23
+
24
+ # Provides basic authentication functionality for testing parts of your engine
25
+ gem 'solidus_auth_devise'
26
+
27
+ case ENV.fetch('DB', nil)
28
+ when 'mysql'
29
+ gem 'mysql2'
30
+ when 'postgresql'
31
+ gem 'pg'
32
+ else
33
+ gem 'sqlite3'
34
+ end
35
+
36
+ # While we still support Ruby < 3 we need to workaround a limitation in
37
+ # the 'async' gem that relies on the latest ruby, since RubyGems doesn't
38
+ # resolve gems based on the required ruby version.
39
+ gem 'async', '< 3' if Gem::Version.new(RUBY_VERSION) < Gem::Version.new('3')
40
+
41
+ gemspec
42
+
43
+ # Use a local Gemfile to include development dependencies that might not be
44
+ # relevant for the project or for other contributors, e.g. pry-byebug.
45
+ #
46
+ # We use `send` instead of calling `eval_gemfile` to work around an issue with
47
+ # how Dependabot parses projects: https://github.com/dependabot/dependabot-core/issues/1658.
48
+ send(:eval_gemfile, 'Gemfile-local') if File.exist? 'Gemfile-local'
data/LICENSE ADDED
@@ -0,0 +1,26 @@
1
+ Copyright (c) 2023 Hamilton Tumenas Borges
2
+ All rights reserved.
3
+
4
+ Redistribution and use in source and binary forms, with or without modification,
5
+ are permitted provided that the following conditions are met:
6
+
7
+ * Redistributions of source code must retain the above copyright notice,
8
+ this list of conditions and the following disclaimer.
9
+ * Redistributions in binary form must reproduce the above copyright notice,
10
+ this list of conditions and the following disclaimer in the documentation
11
+ and/or other materials provided with the distribution.
12
+ * Neither the name Solidus nor the names of its contributors may be used to
13
+ endorse or promote products derived from this software without specific
14
+ prior written permission.
15
+
16
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17
+ "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18
+ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19
+ A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
20
+ CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21
+ EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22
+ PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23
+ PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24
+ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25
+ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26
+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
data/README.md ADDED
@@ -0,0 +1,73 @@
1
+ # Solidus Melhor Envio
2
+
3
+ [![CircleCI](https://circleci.com/gh/solidusio-contrib/solidus_melhor_envio.svg?style=shield)](https://circleci.com/gh/solidusio-contrib/solidus_melhor_envio)
4
+ [![codecov](https://codecov.io/gh/solidusio-contrib/solidus_melhor_envio/branch/master/graph/badge.svg)](https://codecov.io/gh/solidusio-contrib/solidus_melhor_envio)
5
+
6
+ <!-- Explain what your extension does. -->
7
+
8
+ ## Installation
9
+
10
+ Add solidus_melhor_envio to your Gemfile:
11
+
12
+ ```ruby
13
+ gem 'solidus_melhor_envio'
14
+ ```
15
+
16
+ Bundle your dependencies and run the installation generator:
17
+
18
+ ```shell
19
+ bin/rails generate solidus_melhor_envio:install
20
+ ```
21
+
22
+ ## Usage
23
+
24
+ <!-- Explain how to use your extension once it's been installed. -->
25
+
26
+ ## Development
27
+
28
+ ### Testing the extension
29
+
30
+ First bundle your dependencies, then run `bin/rake`. `bin/rake` will default to building the dummy
31
+ app if it does not exist, then it will run specs. The dummy app can be regenerated by using
32
+ `bin/rake extension:test_app`.
33
+
34
+ ```shell
35
+ bin/rake
36
+ ```
37
+
38
+ To run [Rubocop](https://github.com/bbatsov/rubocop) static code analysis run
39
+
40
+ ```shell
41
+ bundle exec rubocop
42
+ ```
43
+
44
+ When testing your application's integration with this extension you may use its factories.
45
+ You can load Solidus core factories along with this extension's factories using this statement:
46
+
47
+ ```ruby
48
+ SolidusDevSupport::TestingSupport::Factories.load_for(SolidusMelhorEnvio::Engine)
49
+ ```
50
+
51
+ ### Running the sandbox
52
+
53
+ To run this extension in a sandboxed Solidus application, you can run `bin/sandbox`. The path for
54
+ the sandbox app is `./sandbox` and `bin/rails` will forward any Rails commands to
55
+ `sandbox/bin/rails`.
56
+
57
+ Here's an example:
58
+
59
+ ```
60
+ $ bin/rails server
61
+ => Booting Puma
62
+ => Rails 6.0.2.1 application starting in development
63
+ * Listening on tcp://127.0.0.1:3000
64
+ Use Ctrl-C to stop
65
+ ```
66
+
67
+ ### Releasing new versions
68
+
69
+ Please refer to the [dedicated page](https://github.com/solidusio/solidus/wiki/How-to-release-extensions) in the Solidus wiki.
70
+
71
+ ## License
72
+
73
+ Copyright (c) 2023 Hamilton Tumenas Borges, released under the New BSD License.
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require 'solidus_dev_support/rake_tasks'
5
+ SolidusDevSupport::RakeTasks.install
6
+
7
+ task default: 'extension:specs'
@@ -0,0 +1,2 @@
1
+ // Placeholder manifest file.
2
+ // the installer will append this file to the app vendored assets here: vendor/assets/javascripts/spree/backend/all.js'
@@ -0,0 +1,2 @@
1
+ // Placeholder manifest file.
2
+ // the installer will append this file to the app vendored assets here: vendor/assets/javascripts/spree/frontend/all.js'
@@ -0,0 +1,4 @@
1
+ /*
2
+ Placeholder manifest file.
3
+ the installer will append this file to the app vendored assets here: 'vendor/assets/stylesheets/spree/backend/all.css'
4
+ */
@@ -0,0 +1,4 @@
1
+ /*
2
+ Placeholder manifest file.
3
+ the installer will append this file to the app vendored assets here: 'vendor/assets/stylesheets/spree/frontend/all.css'
4
+ */
@@ -0,0 +1,5 @@
1
+ module SolidusMelhorEnvio
2
+ class Account < ApplicationRecord
3
+
4
+ end
5
+ end
@@ -0,0 +1,50 @@
1
+ module SolidusMelhorEnvio
2
+ class Api
3
+
4
+ attr_reader :account
5
+ def initialize account
6
+ @account = account
7
+ verify_token
8
+ end
9
+
10
+ private
11
+ def refresh_token
12
+ headers = {
13
+ "Accept": "application/json",
14
+ "Content-Type": "application/json",
15
+ "User-Agent": "Aplicação #{@account.user_agent}"
16
+ }
17
+ request_body = {
18
+ "grant_type": "refresh_token",
19
+ "refresh_token": @account.refresh_token,
20
+ "client_id": @account.client_id,
21
+ "client_secret": @account.client_secret
22
+ }
23
+ request = ::Typhoeus.post("#{@account.api_base_url}/oauth/token", headers: headers, body: JSON.dump(request_body))
24
+ body = JSON.parse(request.body)
25
+ response_has_error? body
26
+ expire_datetime = request.headers["date"].to_datetime.utc + body["expires_in"].seconds
27
+ @account.update(
28
+ access_token: body["access_token"],
29
+ refresh_token: body["refresh_token"],
30
+ token_expires_in: expire_datetime
31
+ )
32
+ end
33
+
34
+ def response_has_error? json
35
+ if json.include? "error"
36
+ error = json["error"]
37
+ raise "ERROR: #{json}"
38
+ end
39
+ end
40
+
41
+ def need_refresh_token?
42
+ return true if @account.token_expires_in.nil?
43
+ DateTime.now.utc > (@account.token_expires_in - 2.hours)
44
+ end
45
+
46
+ def verify_token
47
+ refresh_token if need_refresh_token?
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,78 @@
1
+ module SolidusMelhorEnvio
2
+ class ShippingEstimator < SolidusMelhorEnvio::Api
3
+
4
+ def initialize
5
+ super(SolidusMelhorEnvio::Account.find_by(app_name: SolidusMelhorEnvio.config.app_name))
6
+ end
7
+
8
+
9
+ def shipping_rates(package, _frontend_only = true)
10
+ melhor_envio_rates = get_rates_from_melhor_envio(package)
11
+ shipping_rates = melhor_envio_rates.map do |melhor_envio_rate|
12
+ build_shipping_rate(melhor_envio_rate, package)
13
+ end
14
+
15
+ unless shipping_rates.empty?
16
+ default_shipping_rate = ::Spree::Config.shipping_rate_selector_class.new(shipping_rates).find_default
17
+ default_shipping_rate.selected = true
18
+ end
19
+
20
+ shipping_rates
21
+ end
22
+
23
+ private
24
+
25
+ def get_rates_from_melhor_envio(package)
26
+ postal_code_from = SolidusMelhorEnvio.config.postal_code_from
27
+ services = SolidusMelhorEnvio.config.services
28
+ weight = package.contents.map {|content| content.weight }.sum
29
+ price = package.contents.map {|content| content.price }.sum
30
+ zipcode = package.order.ship_address.zipcode
31
+
32
+ request_body = {
33
+ "from" => {
34
+ "postal_code" => postal_code_from
35
+ },
36
+ "to" => {
37
+ "postal_code" => zipcode
38
+ },
39
+ "package" => {
40
+ "weight" => (weight/1000),
41
+ "height" => 24,
42
+ "width" => 16,
43
+ "length" => 10
44
+ },
45
+ "options" => {
46
+ "insurance_value" => price,
47
+ "receipt" => false,
48
+ "own_hand" => false
49
+ },
50
+ "services": services
51
+ }
52
+ request = ::Typhoeus.post("#{@account.api_base_url}/api/v2/me/shipment/calculate", headers: {'Authorization' => "Bearer #{@account.access_token}"}, body: JSON.dump(request_body))
53
+ res_json = JSON.parse(request.body)
54
+ response_has_error? res_json
55
+ res_json.select { |a| a["price"] }
56
+ end
57
+
58
+ def build_shipping_rate(melhor_envio_rate, package)
59
+
60
+ shipping_method = ::Spree::ShippingMethod.find_or_create_by(
61
+ carrier: melhor_envio_rate["company"]["name"],
62
+ service_level: melhor_envio_rate["name"],
63
+ ) do |shipping_method|
64
+ shipping_method.name = "#{melhor_envio_rate["company"]["name"]} #{melhor_envio_rate["name"]}"
65
+ shipping_method.calculator = ::Spree::Calculator::Shipping::FlatRate.create
66
+ shipping_method.shipping_categories = ::Spree::ShippingCategory.all
67
+ shipping_method.available_to_users = true
68
+ end
69
+
70
+ ::Spree::ShippingRate.new(
71
+ shipping_method: shipping_method,
72
+ cost: melhor_envio_rate["price"],
73
+ min_delivery_time: melhor_envio_rate["delivery_range"]["min"],
74
+ max_delivery_time: melhor_envio_rate["delivery_range"]["max"]
75
+ )
76
+ end
77
+ end
78
+ end
data/bin/console ADDED
@@ -0,0 +1,17 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # frozen_string_literal: true
4
+
5
+ require "bundler/setup"
6
+ require "solidus_melhor_envio"
7
+
8
+ # You can add fixtures and/or initialization code here to make experimenting
9
+ # with your gem easier. You can also use a different console, if you like.
10
+ $LOAD_PATH.unshift(*Dir["#{__dir__}/../app/*"])
11
+
12
+ # (If you use this, don't forget to add pry to your Gemfile!)
13
+ # require "pry"
14
+ # Pry.start
15
+
16
+ require "irb"
17
+ IRB.start(__FILE__)
data/bin/rails ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ if %w[g generate].include? ARGV.first
4
+ exec "#{__dir__}/rails-engine", *ARGV
5
+ else
6
+ exec "#{__dir__}/rails-sandbox", *ARGV
7
+ end
data/bin/rails-engine ADDED
@@ -0,0 +1,13 @@
1
+ #!/usr/bin/env ruby
2
+ # This command will automatically be run when you run "rails" with Rails gems
3
+ # installed from the root of your application.
4
+
5
+ ENGINE_ROOT = File.expand_path('..', __dir__)
6
+ ENGINE_PATH = File.expand_path('../lib/solidus_melhor_envio/engine', __dir__)
7
+
8
+ # Set up gems listed in the Gemfile.
9
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../Gemfile', __dir__)
10
+ require 'bundler/setup' if File.exist?(ENV['BUNDLE_GEMFILE'])
11
+
12
+ require 'rails/all'
13
+ require 'rails/engine/commands'
data/bin/rails-sandbox ADDED
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ app_root = 'sandbox'
4
+
5
+ unless File.exist? "#{app_root}/bin/rails"
6
+ warn 'Creating the sandbox app...'
7
+ Dir.chdir "#{__dir__}/.." do
8
+ system "#{__dir__}/sandbox" or begin
9
+ warn 'Automatic creation of the sandbox app failed'
10
+ exit 1
11
+ end
12
+ end
13
+ end
14
+
15
+ Dir.chdir app_root
16
+ exec 'bin/rails', *ARGV
data/bin/rake ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "rubygems"
5
+ require "bundler/setup"
6
+
7
+ load Gem.bin_path("rake", "rake")
data/bin/sandbox ADDED
@@ -0,0 +1,78 @@
1
+ #!/usr/bin/env bash
2
+
3
+ set -e
4
+ test -z "${DEBUG+empty_string}" || set -x
5
+
6
+ test "$DB" = "sqlite" && export DB="sqlite3"
7
+
8
+ if [ -z "$SOLIDUS_BRANCH" ]
9
+ then
10
+ echo "~~> Use 'export SOLIDUS_BRANCH=[main|v3.2|...]' to control the Solidus branch"
11
+ SOLIDUS_BRANCH="main"
12
+ fi
13
+ echo "~~> Using branch $SOLIDUS_BRANCH of solidus"
14
+
15
+ if [ -z "$SOLIDUS_FRONTEND" ]
16
+ then
17
+ echo "~~> Use 'export SOLIDUS_FRONTEND=[solidus_frontend|solidus_starter_frontend]' to control the Solidus frontend"
18
+ SOLIDUS_FRONTEND="solidus_frontend"
19
+ fi
20
+ echo "~~> Using branch $SOLIDUS_FRONTEND as the solidus frontend"
21
+
22
+ extension_name="solidus_melhor_envio"
23
+
24
+ # Stay away from the bundler env of the containing extension.
25
+ function unbundled {
26
+ ruby -rbundler -e'b = proc {system *ARGV}; Bundler.respond_to?(:with_unbundled_env) ? Bundler.with_unbundled_env(&b) : Bundler.with_clean_env(&b)' -- $@
27
+ }
28
+
29
+ rm -rf ./sandbox
30
+ unbundled bundle exec rails new sandbox \
31
+ --database="${DB:-sqlite3}" \
32
+ --skip-bundle \
33
+ --skip-git \
34
+ --skip-keeps \
35
+ --skip-rc \
36
+ --skip-spring \
37
+ --skip-test \
38
+ --skip-javascript
39
+
40
+ if [ ! -d "sandbox" ]; then
41
+ echo 'sandbox rails application failed'
42
+ exit 1
43
+ fi
44
+
45
+ cd ./sandbox
46
+ cat <<RUBY >> Gemfile
47
+ gem 'solidus', github: 'solidusio/solidus', branch: '$SOLIDUS_BRANCH'
48
+ gem 'rails-i18n'
49
+ gem 'solidus_i18n'
50
+
51
+ gem '$extension_name', path: '..'
52
+
53
+ group :test, :development do
54
+ platforms :mri do
55
+ gem 'pry-byebug'
56
+ end
57
+ end
58
+ RUBY
59
+
60
+ unbundled bundle install --gemfile Gemfile
61
+
62
+ unbundled bundle exec rake db:drop db:create
63
+
64
+ unbundled bundle exec rails generate solidus:install \
65
+ --auto-accept \
66
+ --user_class=Spree::User \
67
+ --enforce_available_locales=true \
68
+ --with-authentication=true \
69
+ --payment-method=none \
70
+ --frontend=${SOLIDUS_FRONTEND} \
71
+ $@
72
+
73
+ unbundled bundle exec rails generate solidus:auth:install --auto-run-migrations
74
+ unbundled bundle exec rails generate ${extension_name}:install --auto-run-migrations
75
+
76
+ echo
77
+ echo "🚀 Sandbox app successfully created for $extension_name!"
78
+ echo "🧪 This app is intended for test purposes."
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ gem install bundler --conservative
7
+ bundle update
8
+ bin/rake clobber
@@ -0,0 +1,5 @@
1
+ # Sample localization file for English. Add more files in this directory for other locales.
2
+ # See https://github.com/svenfuchs/rails-i18n/tree/master/rails%2Flocale for starting points.
3
+
4
+ en:
5
+ hello: Hello world
data/config/routes.rb ADDED
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ Spree::Core::Engine.routes.draw do
4
+ # Add your extension routes here
5
+ end
@@ -0,0 +1,16 @@
1
+ class CreateSolidusMelhorEnvioAccounts < ActiveRecord::Migration[7.0]
2
+ def change
3
+ create_table :solidus_melhor_envio_accounts do |t|
4
+ t.string :app_name
5
+ t.string :api_base_url
6
+ t.string :client_id
7
+ t.string :client_secret
8
+ t.string :refresh_token
9
+ t.string :access_token
10
+ t.string :user_agent
11
+ t.datetime :token_expires_in
12
+
13
+ t.timestamps
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,6 @@
1
+ class AddDeliveryTimeToSpreeShippingRate < ActiveRecord::Migration[7.0]
2
+ def change
3
+ add_column :spree_shipping_rates, :min_delivery_time, :integer
4
+ add_column :spree_shipping_rates, :max_delivery_time, :integer
5
+ end
6
+ end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SolidusMelhorEnvio
4
+ module Generators
5
+ class InstallGenerator < Rails::Generators::Base
6
+ class_option :auto_run_migrations, type: :boolean, default: false
7
+ source_root File.expand_path('templates', __dir__)
8
+
9
+ def self.exit_on_failure?
10
+ true
11
+ end
12
+
13
+ def copy_initializer
14
+ template 'initializer.rb', 'config/initializers/solidus_melhor_envio.rb'
15
+ end
16
+
17
+ def add_javascripts
18
+ append_file 'vendor/assets/javascripts/spree/frontend/all.js', "//= require spree/frontend/solidus_melhor_envio\n"
19
+ append_file 'vendor/assets/javascripts/spree/backend/all.js', "//= require spree/backend/solidus_melhor_envio\n"
20
+ end
21
+
22
+ def add_stylesheets
23
+ inject_into_file 'vendor/assets/stylesheets/spree/frontend/all.css', " *= require spree/frontend/solidus_melhor_envio\n", before: %r{\*/}, verbose: true # rubocop:disable Layout/LineLength
24
+ inject_into_file 'vendor/assets/stylesheets/spree/backend/all.css', " *= require spree/backend/solidus_melhor_envio\n", before: %r{\*/}, verbose: true # rubocop:disable Layout/LineLength
25
+ end
26
+
27
+ def add_migrations
28
+ run 'bin/rails railties:install:migrations FROM=solidus_melhor_envio'
29
+ end
30
+
31
+ def run_migrations
32
+ run_migrations = options[:auto_run_migrations] || ['', 'y', 'Y'].include?(ask('Would you like to run the migrations now? [Y/n]')) # rubocop:disable Layout/LineLength
33
+ if run_migrations
34
+ run 'bin/rails db:migrate'
35
+ else
36
+ puts 'Skipping bin/rails db:migrate, don\'t forget to run it!' # rubocop:disable Rails/Output
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ SolidusMelhorEnvio.configure do |config|
4
+ # TODO: Remember to change this with the actual preferences you have implemented!
5
+ # config.sample_preference = 'sample_value'
6
+ config.app_name = ''
7
+ config.postal_code_from = ''
8
+ config.services = '' # id of services you want to use, separated by comma, p.e. "1,2,3"
9
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SolidusMelhorEnvio
4
+ class Configuration
5
+ # Define here the settings for this extension, e.g.:
6
+ #
7
+ # attr_accessor :my_setting
8
+ attr_accessor :app_name, :services, :postal_code_from
9
+ end
10
+
11
+ class << self
12
+ def configuration
13
+ @configuration ||= Configuration.new
14
+ end
15
+
16
+ alias config configuration
17
+
18
+ def configure
19
+ yield configuration
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'solidus_core'
4
+ require 'solidus_support'
5
+
6
+ module SolidusMelhorEnvio
7
+ class Engine < Rails::Engine
8
+ include SolidusSupport::EngineExtensions
9
+
10
+ isolate_namespace SolidusMelhorEnvio
11
+
12
+ engine_name 'solidus_melhor_envio'
13
+
14
+ # use rspec for tests
15
+ config.generators do |g|
16
+ g.test_framework :rspec
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ FactoryBot.define do
4
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SolidusMelhorEnvio
4
+ VERSION = '1.0.0'
5
+ end
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+ require 'typhoeus'
3
+
4
+ require 'solidus_melhor_envio/configuration'
5
+ require 'solidus_melhor_envio/version'
6
+ require 'solidus_melhor_envio/engine'
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/solidus_melhor_envio/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'solidus_melhor_envio'
7
+ spec.version = SolidusMelhorEnvio::VERSION
8
+ spec.authors = ['Hamilton Tumenas Borges']
9
+ spec.email = 'hamiltontubo@gmail.com'
10
+
11
+ spec.summary = 'Gem para integração com a API do Melhor Envio'
12
+ spec.description = 'Gem para integração com a API do Melhor Envio'
13
+ spec.homepage = 'https://github.com/solidusio-contrib/solidus_melhor_envio#readme'
14
+ spec.license = 'BSD-3-Clause'
15
+
16
+ spec.metadata['homepage_uri'] = spec.homepage
17
+ spec.metadata['source_code_uri'] = 'https://github.com/solidusio-contrib/solidus_melhor_envio'
18
+ spec.metadata['changelog_uri'] = 'https://github.com/solidusio-contrib/solidus_melhor_envio/blob/master/CHANGELOG.md'
19
+
20
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.5', '< 4')
21
+
22
+ # Specify which files should be added to the gem when it is released.
23
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
24
+ files = Dir.chdir(__dir__) { `git ls-files -z`.split("\x0") }
25
+
26
+ spec.files = files.grep_v(%r{^(test|spec|features)/})
27
+ spec.test_files = files.grep(%r{^(test|spec|features)/})
28
+ spec.bindir = "exe"
29
+ spec.executables = files.grep(%r{^exe/}) { |f| File.basename(f) }
30
+ spec.require_paths = ["lib"]
31
+
32
+ spec.add_dependency 'solidus_core', ['>= 2.0.0', '< 5']
33
+ spec.add_dependency 'solidus_support', '~> 0.5'
34
+ spec.add_dependency 'typhoeus', '~> 1.3'
35
+
36
+ spec.add_development_dependency 'solidus_dev_support', '~> 2.7'
37
+ end
@@ -0,0 +1,7 @@
1
+ require 'rails_helper'
2
+
3
+ module SolidusMelhorEnvio
4
+ RSpec.describe Account, type: :model do
5
+ pending "add some examples to (or delete) #{__FILE__}"
6
+ end
7
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Configure Rails Environment
4
+ ENV['RAILS_ENV'] = 'test'
5
+
6
+ # Run Coverage report
7
+ require 'solidus_dev_support/rspec/coverage'
8
+
9
+ # Create the dummy app if it's still missing.
10
+ dummy_env = "#{__dir__}/dummy/config/environment.rb"
11
+ system 'bin/rake extension:test_app' unless File.exist? dummy_env
12
+ require dummy_env
13
+
14
+ # Requires factories and other useful helpers defined in spree_core.
15
+ require 'solidus_dev_support/rspec/feature_helper'
16
+
17
+ # Requires supporting ruby files with custom matchers and macros, etc,
18
+ # in spec/support/ and its subdirectories.
19
+ Dir["#{__dir__}/support/**/*.rb"].sort.each { |f| require f }
20
+
21
+ # Requires factories defined in Solidus core and this extension.
22
+ # See: lib/solidus_melhor_envio/testing_support/factories.rb
23
+ SolidusDevSupport::TestingSupport::Factories.load_for(SolidusMelhorEnvio::Engine)
24
+
25
+ RSpec.configure do |config|
26
+ config.infer_spec_type_from_file_location!
27
+ config.use_transactional_fixtures = false
28
+
29
+ if Spree.solidus_gem_version < Gem::Version.new('2.11')
30
+ config.extend Spree::TestingSupport::AuthorizationHelpers::Request, type: :system
31
+ end
32
+ end
metadata ADDED
@@ -0,0 +1,152 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: solidus_melhor_envio
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Hamilton Tumenas Borges
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-09-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: solidus_core
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 2.0.0
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '5'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: 2.0.0
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '5'
33
+ - !ruby/object:Gem::Dependency
34
+ name: solidus_support
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '0.5'
40
+ type: :runtime
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '0.5'
47
+ - !ruby/object:Gem::Dependency
48
+ name: typhoeus
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '1.3'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '1.3'
61
+ - !ruby/object:Gem::Dependency
62
+ name: solidus_dev_support
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '2.7'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: '2.7'
75
+ description: Gem para integração com a API do Melhor Envio
76
+ email: hamiltontubo@gmail.com
77
+ executables: []
78
+ extensions: []
79
+ extra_rdoc_files: []
80
+ files:
81
+ - ".circleci/config.yml"
82
+ - ".gem_release.yml"
83
+ - ".github/stale.yml"
84
+ - ".github_changelog_generator"
85
+ - ".gitignore"
86
+ - ".rspec"
87
+ - ".rubocop.yml"
88
+ - CHANGELOG.md
89
+ - Gemfile
90
+ - LICENSE
91
+ - README.md
92
+ - Rakefile
93
+ - app/assets/javascripts/spree/backend/solidus_melhor_envio.js
94
+ - app/assets/javascripts/spree/frontend/solidus_melhor_envio.js
95
+ - app/assets/stylesheets/spree/backend/solidus_melhor_envio.css
96
+ - app/assets/stylesheets/spree/frontend/solidus_melhor_envio.css
97
+ - app/models/solidus_melhor_envio/account.rb
98
+ - app/models/solidus_melhor_envio/api.rb
99
+ - app/models/solidus_melhor_envio/shipping_estimator.rb
100
+ - bin/console
101
+ - bin/rails
102
+ - bin/rails-engine
103
+ - bin/rails-sandbox
104
+ - bin/rake
105
+ - bin/sandbox
106
+ - bin/setup
107
+ - config/locales/en.yml
108
+ - config/routes.rb
109
+ - db/migrate/20230929133307_create_solidus_melhor_envio_accounts.rb
110
+ - db/migrate/20230929180714_add_delivery_time_to_spree_shipping_rate.rb
111
+ - lib/generators/solidus_melhor_envio/install/install_generator.rb
112
+ - lib/generators/solidus_melhor_envio/install/templates/initializer.rb
113
+ - lib/solidus_melhor_envio.rb
114
+ - lib/solidus_melhor_envio/configuration.rb
115
+ - lib/solidus_melhor_envio/engine.rb
116
+ - lib/solidus_melhor_envio/testing_support/factories.rb
117
+ - lib/solidus_melhor_envio/version.rb
118
+ - solidus_melhor_envio.gemspec
119
+ - spec/models/solidus_melhor_envio/account_spec.rb
120
+ - spec/spec_helper.rb
121
+ homepage: https://github.com/solidusio-contrib/solidus_melhor_envio#readme
122
+ licenses:
123
+ - BSD-3-Clause
124
+ metadata:
125
+ homepage_uri: https://github.com/solidusio-contrib/solidus_melhor_envio#readme
126
+ source_code_uri: https://github.com/solidusio-contrib/solidus_melhor_envio
127
+ changelog_uri: https://github.com/solidusio-contrib/solidus_melhor_envio/blob/master/CHANGELOG.md
128
+ post_install_message:
129
+ rdoc_options: []
130
+ require_paths:
131
+ - lib
132
+ required_ruby_version: !ruby/object:Gem::Requirement
133
+ requirements:
134
+ - - ">="
135
+ - !ruby/object:Gem::Version
136
+ version: '2.5'
137
+ - - "<"
138
+ - !ruby/object:Gem::Version
139
+ version: '4'
140
+ required_rubygems_version: !ruby/object:Gem::Requirement
141
+ requirements:
142
+ - - ">="
143
+ - !ruby/object:Gem::Version
144
+ version: '0'
145
+ requirements: []
146
+ rubygems_version: 3.4.20
147
+ signing_key:
148
+ specification_version: 4
149
+ summary: Gem para integração com a API do Melhor Envio
150
+ test_files:
151
+ - spec/models/solidus_melhor_envio/account_spec.rb
152
+ - spec/spec_helper.rb