fedexvichuge 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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: d663b0c8fc2b90efe1785aff9742d007b073edac339d699e9459ad6cd7780b8b
4
+ data.tar.gz: 3d0f71deca0bc71f20db63131927c56c4644a8660c87bad3930ff9d8d55fe24b
5
+ SHA512:
6
+ metadata.gz: d624f09c8cd95e0798a65dbf018280206b0e58b09df5d160655261e6ed6503bc260574e09dbb17c489c6f8bedb6c97d2ed667e9dcba8184c3e4f51e03f2f644c
7
+ data.tar.gz: 0b489ae737da3f64ac8132ae54c0c6cb1dd3c88ab22b5e916e50dee2095927e1bd486b34aa753fff46a8a11367a9206a7f51f161aa3c29c2fc70453860eb0537
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 3.1.2
6
+ before_install: gem install bundler -v 2.1.4
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in fedexvichuge.gemspec
4
+ gemspec
5
+
6
+ gem 'activesupport', '~> 7.0', '>= 7.0.3.1'
7
+ gem 'faraday', '~> 2.5', '>= 2.5.2'
8
+ gem 'nokogiri', '~> 1.13', '>= 1.13.8'
9
+ gem 'rubocop', '~> 1.34', '>= 1.34.1'
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 vichuge
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,61 @@
1
+ # Fedexvichuge
2
+
3
+ This gem has the purpose to be a coding challenge for Manuable.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'fedexvichuge'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle install
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install fedexvichuge
20
+
21
+ ## Usage
22
+
23
+ - First of all, you need to create a credentials variable. This is a hash that contains your credentials for accessing the FedEx API.
24
+
25
+ ```
26
+ credentials = {
27
+ key: "xxxxxxxxxxxxxxxx",
28
+ password: "xxxxxxxxxxxxxxxxxxxxxxxxx",
29
+ account_number: "xxxxxxxxx",
30
+ meter_number: "xxxxxxxxx"
31
+ }
32
+ ```
33
+
34
+ - Now, create a new variable with the info for delivery, like:
35
+
36
+ ```
37
+ quote_params = {
38
+ address_from: {
39
+ zip: "64000",
40
+ country: "MX"
41
+ },
42
+ address_to: {
43
+ zip: "64000",
44
+ country: "MX"
45
+ },
46
+ parcel: {
47
+ length: 25.0,
48
+ width: 28.0,
49
+ height: 46.0,
50
+ distance_unit: "cm",
51
+ weight: 6.5,
52
+ mass_unit: "kg"
53
+ }
54
+ }
55
+ ```
56
+
57
+ - Once the credentials are set, you can use the `Fedexvichuge` class to access the FedEx API. Just type on the console `Fedex::Rates.get(credentials, quote_params)` and you'll get the response from the API with the rates on different service levels.
58
+
59
+ ## License
60
+
61
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'fedexvichuge'
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require 'irb'
14
+ IRB.start(__FILE__)
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
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,29 @@
1
+ require_relative 'lib/fedexvichuge/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = 'fedexvichuge'
5
+ spec.version = Fedexvichuge::VERSION
6
+ spec.authors = ['vichuge']
7
+ spec.email = ['victor.hugo.pacheco.flores@gmail.com']
8
+
9
+ spec.summary = 'Summarize your fedex packages'
10
+ spec.description = 'This is a test for fedexvichuge gem'
11
+ spec.homepage = 'https://github.com/vichuge'
12
+ spec.license = 'MIT'
13
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.3.0')
14
+
15
+ spec.metadata['allowed_push_host'] = "https://rubygems.org/"
16
+
17
+ spec.metadata['homepage_uri'] = spec.homepage
18
+ spec.metadata['source_code_uri'] = 'https://github.com/vichuge'
19
+ spec.metadata['changelog_uri'] = 'https://github.com/vichuge'
20
+
21
+ # Specify which files should be added to the gem when it is released.
22
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
24
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
25
+ end
26
+ spec.bindir = 'exe'
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ['lib']
29
+ end
@@ -0,0 +1,3 @@
1
+ module Fedexvichuge
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,131 @@
1
+ require 'active_support/all'
2
+ require 'fedexvichuge/version'
3
+ require 'Faraday'
4
+
5
+ module Fedex
6
+ module Rates
7
+ class Error < StandardError; end
8
+ @@quote_params = {
9
+ address_from: {
10
+ zip: "64000",
11
+ country: "MX"
12
+ },
13
+ address_to: {
14
+ zip: "64000",
15
+ country: "MX"
16
+ },
17
+ parcel: {
18
+ length: 25.0,
19
+ width: 28.0,
20
+ height: 46.0,
21
+ distance_unit: "cm",
22
+ weight: 6.5,
23
+ mass_unit: "kg"
24
+ }
25
+ }
26
+
27
+ def self.get(credentials, quote_params=@@quote_params)
28
+ params = "
29
+ <RateRequest xmlns='http://fedex.com/ws/rate/v13'>
30
+ <WebAuthenticationDetail>
31
+ <UserCredential>
32
+ <Key>#{credentials[:key]}</Key>
33
+ <Password>#{credentials[:password]}</Password>
34
+ </UserCredential>
35
+ </WebAuthenticationDetail>
36
+ <ClientDetail>
37
+ <AccountNumber>#{credentials[:account_number]}</AccountNumber>
38
+ <MeterNumber>#{credentials[:meter_number]}</MeterNumber>
39
+ <Localization>
40
+ <LanguageCode>es</LanguageCode>
41
+ <LocaleCode>mx</LocaleCode>
42
+ </Localization>
43
+ </ClientDetail>
44
+ <Version>
45
+ <ServiceId>crs</ServiceId>
46
+ <Major>13</Major>
47
+ <Intermediate>0</Intermediate>
48
+ <Minor>0</Minor>
49
+ </Version>
50
+ <ReturnTransitAndCommit>true</ReturnTransitAndCommit>
51
+ <RequestedShipment>
52
+ <DropoffType>REGULAR_PICKUP</DropoffType>
53
+ <PackagingType>YOUR_PACKAGING</PackagingType>
54
+ <Shipper>
55
+ <Address>
56
+ <StreetLines></StreetLines>
57
+ <City></City>
58
+ <StateOrProvinceCode>XX</StateOrProvinceCode>
59
+ <PostalCode>#{quote_params[:address_from][:zip]}</PostalCode>
60
+ <CountryCode>#{quote_params[:address_from][:country]}</CountryCode>
61
+ </Address>
62
+ </Shipper>
63
+ <Recipient>
64
+ <Address>
65
+ <StreetLines></StreetLines>
66
+ <City></City>
67
+ <StateOrProvinceCode>XX</StateOrProvinceCode>
68
+ <PostalCode>#{quote_params[:address_to][:zip]}</PostalCode>
69
+ <CountryCode>#{quote_params[:address_to][:country]}</CountryCode>
70
+ <Residential>false</Residential>
71
+ </Address>
72
+ </Recipient>
73
+ <ShippingChargesPayment>
74
+ <PaymentType>SENDER</PaymentType>
75
+ </ShippingChargesPayment>
76
+ <RateRequestTypes>ACCOUNT</RateRequestTypes>
77
+ <PackageCount>1</PackageCount>
78
+ <RequestedPackageLineItems>
79
+ <GroupPackageCount>1</GroupPackageCount>
80
+ <Weight>
81
+ <Units>#{quote_params[:parcel][:mass_unit].upcase}</Units>
82
+ <Value>#{quote_params[:parcel][:weight]}</Value>
83
+ </Weight>
84
+ <Dimensions>
85
+ <Length>#{quote_params[:parcel][:length].ceil}</Length>
86
+ <Width>#{quote_params[:parcel][:width].ceil}</Width>
87
+ <Height>#{quote_params[:parcel][:height].ceil}</Height>
88
+ <Units>#{quote_params[:parcel][:distance_unit].upcase}</Units>
89
+ </Dimensions>
90
+ </RequestedPackageLineItems>
91
+ </RequestedShipment>
92
+ </RateRequest>"
93
+ res = request(params)
94
+ format(res)
95
+ end
96
+
97
+ def self.request(params)
98
+ response = client.post do |req|
99
+ req.body = params
100
+ end
101
+ response.body
102
+ end
103
+
104
+ def self.client
105
+ Faraday.new(
106
+ url: 'https://wsbeta.fedex.com:443/xml',
107
+ headers: {
108
+ 'Content-Type' => 'application/xml'
109
+ }
110
+ )
111
+ end
112
+
113
+ def self.format(res)
114
+ # xml = Nokogiri::XML(res).to_xml
115
+ h = Hash.from_xml(res)
116
+ r = []
117
+ h['RateReply']['RateReplyDetails'].each do |i|
118
+ token = i['ServiceType']
119
+ r.push({
120
+ "price": i['RatedShipmentDetails'][0]['ShipmentRateDetail']['TotalNetChargeWithDutiesAndTaxes']['Amount'].to_f,
121
+ "currency": i['RatedShipmentDetails'][0]['ShipmentRateDetail']['TotalNetChargeWithDutiesAndTaxes']['Currency'],
122
+ "service_level": {
123
+ "name": token.downcase.gsub('_', ' '),
124
+ "token": token,
125
+ }
126
+ })
127
+ end
128
+ r
129
+ end
130
+ end
131
+ end
metadata ADDED
@@ -0,0 +1,59 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fedexvichuge
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - vichuge
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-08-12 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: This is a test for fedexvichuge gem
14
+ email:
15
+ - victor.hugo.pacheco.flores@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".gitignore"
21
+ - ".rspec"
22
+ - ".travis.yml"
23
+ - Gemfile
24
+ - LICENSE.txt
25
+ - README.md
26
+ - Rakefile
27
+ - bin/console
28
+ - bin/setup
29
+ - fedexvichuge.gemspec
30
+ - lib/fedexvichuge.rb
31
+ - lib/fedexvichuge/version.rb
32
+ homepage: https://github.com/vichuge
33
+ licenses:
34
+ - MIT
35
+ metadata:
36
+ allowed_push_host: https://rubygems.org/
37
+ homepage_uri: https://github.com/vichuge
38
+ source_code_uri: https://github.com/vichuge
39
+ changelog_uri: https://github.com/vichuge
40
+ post_install_message:
41
+ rdoc_options: []
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: 2.3.0
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ requirements: []
55
+ rubygems_version: 3.3.7
56
+ signing_key:
57
+ specification_version: 4
58
+ summary: Summarize your fedex packages
59
+ test_files: []