seametrix_ruby 1.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: ab619bbcccf423355a65848f71b75cd259ea43c6cc1c7c025ff5099300f713e2
4
+ data.tar.gz: 8a3ada354e71ce2f853545b5b6b9407b18d032bc32e19259f0f1894809ab1094
5
+ SHA512:
6
+ metadata.gz: 2b53ad6e579c9ad5e1781adfe5d5299a589ecf30555738bd18eb8a6064e0c3f00ca75e9cdc26ed644e8a3e77dc66bcac038e39b0c51665dce17e2c6f909a47dc
7
+ data.tar.gz: 2b34e0677a0763e9e3ca6acfef0fd666eef09d8be41b538e3a9901f5719a7fc0dcc08d41d229aff6a91fadd5c0b0f143d4703cd8ac728f53db39d674e3f42765
@@ -0,0 +1,46 @@
1
+ name: Test-Build-Publish
2
+ on:
3
+ push:
4
+ branches: [ master ]
5
+ pull_request:
6
+ branches: [ master ]
7
+ permissions:
8
+ contents: read
9
+ jobs:
10
+ test:
11
+ runs-on: ubuntu-latest
12
+ strategy:
13
+ matrix:
14
+ ruby-version: ['2.7', '3.0', '3.1']
15
+ steps:
16
+ - uses: actions/checkout@v3
17
+ - name: Set up Ruby
18
+ uses: ruby/setup-ruby@v1
19
+ with:
20
+ ruby-version: ${{ matrix.ruby-version }}
21
+ bundler-cache: true
22
+ - name: Run tests
23
+ run: bundle exec rspec --format=progress
24
+ build:
25
+ name: Build + Publish
26
+ runs-on: ubuntu-latest
27
+ needs: [test]
28
+ permissions:
29
+ contents: read
30
+ packages: write
31
+ steps:
32
+ - uses: actions/checkout@v3
33
+ - name: Set up Ruby 2.7
34
+ uses: actions/setup-ruby@v1
35
+ with:
36
+ ruby-version: 2.7.x
37
+ - name: Publish to RubyGems
38
+ run: |
39
+ mkdir -p $HOME/.gem
40
+ touch $HOME/.gem/credentials
41
+ chmod 0600 $HOME/.gem/credentials
42
+ printf -- "---\n:rubygems_api_key: ${GEM_HOST_API_KEY}\n" > $HOME/.gem/credentials
43
+ gem build *.gemspec
44
+ gem push *.gem
45
+ env:
46
+ GEM_HOST_API_KEY: "${{secrets.RUBYGEMS_AUTH_TOKEN}}"
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ /.ruby-version
10
+ /coverage
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in seametrix_ruby.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,101 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ seametrix_ruby (1.0.3)
5
+ activesupport (~> 6.1)
6
+ faraday (~> 1.0)
7
+ faraday_middleware (~> 1.2.0)
8
+ hashie (~> 5.0.0)
9
+ multi_json (>= 1.11.2)
10
+
11
+ GEM
12
+ remote: https://rubygems.org/
13
+ specs:
14
+ activesupport (6.1.5)
15
+ concurrent-ruby (~> 1.0, >= 1.0.2)
16
+ i18n (>= 1.6, < 2)
17
+ minitest (>= 5.1)
18
+ tzinfo (~> 2.0)
19
+ zeitwerk (~> 2.3)
20
+ addressable (2.8.0)
21
+ public_suffix (>= 2.0.2, < 5.0)
22
+ concurrent-ruby (1.1.10)
23
+ crack (0.4.5)
24
+ rexml
25
+ diff-lcs (1.5.0)
26
+ docile (1.4.0)
27
+ faraday (1.10.0)
28
+ faraday-em_http (~> 1.0)
29
+ faraday-em_synchrony (~> 1.0)
30
+ faraday-excon (~> 1.1)
31
+ faraday-httpclient (~> 1.0)
32
+ faraday-multipart (~> 1.0)
33
+ faraday-net_http (~> 1.0)
34
+ faraday-net_http_persistent (~> 1.0)
35
+ faraday-patron (~> 1.0)
36
+ faraday-rack (~> 1.0)
37
+ faraday-retry (~> 1.0)
38
+ ruby2_keywords (>= 0.0.4)
39
+ faraday-em_http (1.0.0)
40
+ faraday-em_synchrony (1.0.0)
41
+ faraday-excon (1.1.0)
42
+ faraday-httpclient (1.0.1)
43
+ faraday-multipart (1.0.3)
44
+ multipart-post (>= 1.2, < 3)
45
+ faraday-net_http (1.0.1)
46
+ faraday-net_http_persistent (1.2.0)
47
+ faraday-patron (1.0.0)
48
+ faraday-rack (1.0.0)
49
+ faraday-retry (1.0.3)
50
+ faraday_middleware (1.2.0)
51
+ faraday (~> 1.0)
52
+ hashdiff (1.0.1)
53
+ hashie (5.0.0)
54
+ i18n (1.10.0)
55
+ concurrent-ruby (~> 1.0)
56
+ minitest (5.15.0)
57
+ multi_json (1.15.0)
58
+ multipart-post (2.1.1)
59
+ public_suffix (4.0.7)
60
+ rake (13.0.6)
61
+ rexml (3.2.5)
62
+ rspec (3.11.0)
63
+ rspec-core (~> 3.11.0)
64
+ rspec-expectations (~> 3.11.0)
65
+ rspec-mocks (~> 3.11.0)
66
+ rspec-core (3.11.0)
67
+ rspec-support (~> 3.11.0)
68
+ rspec-expectations (3.11.0)
69
+ diff-lcs (>= 1.2.0, < 2.0)
70
+ rspec-support (~> 3.11.0)
71
+ rspec-mocks (3.11.1)
72
+ diff-lcs (>= 1.2.0, < 2.0)
73
+ rspec-support (~> 3.11.0)
74
+ rspec-support (3.11.0)
75
+ ruby2_keywords (0.0.5)
76
+ simplecov (0.21.2)
77
+ docile (~> 1.1)
78
+ simplecov-html (~> 0.11)
79
+ simplecov_json_formatter (~> 0.1)
80
+ simplecov-html (0.12.3)
81
+ simplecov_json_formatter (0.1.4)
82
+ tzinfo (2.0.4)
83
+ concurrent-ruby (~> 1.0)
84
+ webmock (3.12.2)
85
+ addressable (>= 2.3.6)
86
+ crack (>= 0.3.2)
87
+ hashdiff (>= 0.4.0, < 2.0.0)
88
+ zeitwerk (2.5.4)
89
+
90
+ PLATFORMS
91
+ ruby
92
+
93
+ DEPENDENCIES
94
+ rake
95
+ rspec
96
+ seametrix_ruby!
97
+ simplecov (>= 0.19.1)
98
+ webmock (~> 3.12.1)
99
+
100
+ BUNDLED WITH
101
+ 1.17.3
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 adiwids
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,97 @@
1
+ # SeametrixRuby
2
+
3
+ Unofficial ruby gem for [Seametrix API](https://seametrix.net/sea-distances-api/).
4
+ ## Installation
5
+
6
+ Add this line to your application's Gemfile:
7
+
8
+ ```ruby
9
+ gem 'seametrix_ruby', git: 'https://github.com/adiwids/seametrix_ruby.git', branch: 'master', require: 'seametrix'
10
+ ```
11
+
12
+ And then execute:
13
+
14
+ $ bundle install
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install seametrix_ruby
19
+
20
+ ## Usage
21
+
22
+ :anchor: **Ports Service**
23
+
24
+ *Search ports by LOCODE or port name (min. 3 characters of LOCODE or port name).*
25
+
26
+ ```ruby
27
+ port_result = Seametrix::Ports.get('MTX')
28
+ ```
29
+
30
+ `port_result` is an instance of [PortResult](https://github.com/adiwids/seametrix_ruby/blob/master/lib/seametrix_ruby/responses/port_result.rb) class, plain Ruby object version of JSON response.
31
+
32
+ ---
33
+
34
+ :map: **Routes Service**
35
+
36
+ ```ruby
37
+ # array of starting and end points pairs with <longitude, latitude> format
38
+ legs = [
39
+ ["-101.123456, 14.772125", "117.443101, 1.123456"]
40
+ ]
41
+ route_requests = legs.map do |leg|
42
+ start_coord, end_coord = leg
43
+ start_lon, start_lat = start_coord.split(',').map(&:strip)
44
+ end_lon, end_lat = start_coord.split(',').map(&:strip)
45
+ attrs = {
46
+ start_lon: start_lon,
47
+ start_lat: start_lat,
48
+ end_lon: end_lon,
49
+ end_lat: end_lat
50
+ }
51
+ # Other parameter can be added too
52
+
53
+ SeametrixRuby::Requests::RouteRequest.new(attrs)
54
+ end
55
+ route_result = Seametrix::Routes.get(route_requests)
56
+ ```
57
+
58
+ `route_result` is an instance of [RouteResult](https://github.com/adiwids/seametrix_ruby/blob/master/lib/seametrix_ruby/responses/route_result.rb) class, plain Ruby object version of JSON response.
59
+
60
+ ---
61
+ ## Configuration
62
+
63
+ ```ruby
64
+ Seametrix.configure do |config|
65
+ config.base_url = "<URL of API endpoint> or see Configuration::DEFAULT_BASE_URL value"
66
+ config.access_key = "validAccessKey"
67
+ config.logger = Logger.new(STDOUT) #optional
68
+ config.debugging = false #optional
69
+ end
70
+ ```
71
+
72
+ ## Test
73
+
74
+ $ bundle exec rake test
75
+
76
+ ## Supported Ruby Versions
77
+
78
+ * MRI Ruby 2.5.0 and above
79
+
80
+ ## Contributing
81
+
82
+ Bug reports and pull requests are welcome on GitHub at https://github.com/adiwids/seametrix_ruby. This project is intended to be a safe, and welcoming space for collaboration.
83
+
84
+ 1. Fork this repository to your Github's.
85
+ 2. Create feature branch `git checkout -b feature/feature_name`.
86
+ 3. Commit your changes with descriptive commit message.
87
+ 4. Push to the branch to the forked repository on your Github.
88
+ 5. Create a Pull Request.
89
+
90
+ ## License
91
+
92
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
93
+
94
+
95
+ ## Credits
96
+
97
+ * API Services by [Seametrix Team - Seanergix Ltd](https://seametrix.net)
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new :spec
5
+
6
+ task test: :spec
7
+ task default: :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "seametrix_ruby"
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
data/lib/seametrix.rb ADDED
@@ -0,0 +1,64 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'faraday'
4
+ require 'faraday_middleware'
5
+ require 'multi_json'
6
+ require 'active_support/core_ext/string'
7
+
8
+ require 'seametrix_ruby/version'
9
+ require 'seametrix_ruby/configuration'
10
+ require 'seametrix_ruby/responses/result_code'
11
+ require 'seametrix_ruby/services/routes'
12
+ require 'seametrix_ruby/services/ports'
13
+
14
+ module Seametrix
15
+ extend SeametrixRuby::Configuration
16
+
17
+ class << self
18
+ attr_accessor *SeametrixRuby::Configuration::OPTIONS
19
+ end
20
+
21
+ def self.request(path, method: :get, raw: false, params: {})
22
+ options = { 'AccessKey' => access_key }
23
+
24
+ resp = connection(base_url, raw: raw).send(method) do |req|
25
+ case method
26
+ when :post
27
+ body = params.delete(:body)
28
+ req.path = path
29
+ req.params = options
30
+ req.body = body.to_json if body
31
+ else
32
+ req.url path, options.merge(params.try(:[], :body) || params)
33
+ end
34
+ end
35
+
36
+ raw ? resp : resp.body
37
+ end
38
+
39
+ def self.connection(base_url, raw: false)
40
+ options = {
41
+ ssl: { verify: false },
42
+ url: base_url,
43
+ headers: {
44
+ 'User-Agent' => "SeametrixRuby #{SeametrixRuby::VERSION}",
45
+ 'Content-Type' => 'application/json',
46
+ 'Accept' => 'application/json'
47
+ }
48
+ }
49
+
50
+ Faraday::Connection.new(options) do |conn|
51
+ conn.request :json
52
+ conn.use Faraday::Request::UrlEncoded
53
+ conn.use Faraday::Response::Logger, logger, { bodies: debugging }
54
+ unless raw
55
+ conn.use FaradayMiddleware::Mashify
56
+ conn.use Faraday::Response::ParseJson
57
+ end
58
+ end
59
+ end
60
+
61
+ Seametrix::Routes = SeametrixRuby::Services::Routes
62
+ Seametrix::Ports = SeametrixRuby::Services::Ports
63
+ Seametrix::ResultCode = SeametrixRuby::Responses::ResultCode
64
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SeametrixRuby
4
+ module Configuration
5
+ DEFAULT_BASE_URL = 'https://apipro.seametrix.net'.freeze
6
+
7
+ OPTIONS = [
8
+ :base_url,
9
+ :access_key,
10
+ :logger,
11
+ :debugging
12
+ ].freeze
13
+
14
+ attr_accessor *OPTIONS
15
+
16
+ def configure
17
+ yield self
18
+ end
19
+
20
+ def reset
21
+ self.base_url = DEFAULT_BASE_URL
22
+ self.access_key = nil
23
+ self.logger = nil
24
+ self.debugging = false
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'seametrix_ruby/models/waypoint'
4
+
5
+ module SeametrixRuby
6
+ module Models
7
+ class Port
8
+ attr_accessor :port_name, :port_code, :country
9
+ attr_reader :longitude, :latitude, :waypoint
10
+
11
+ def initialize(attributes = {})
12
+ @port_name = attributes[:port_name]
13
+ @port_code = attributes[:port_code]
14
+ @country = attributes[:country]
15
+
16
+ self.longitude = attributes[:longitude].to_f
17
+ self.latitude = attributes[:latitude].to_f
18
+ end
19
+
20
+ def self.from_json(json_string)
21
+ json = MultiJson.load(json_string)
22
+
23
+ new(port_name: json['portName'],
24
+ port_code: json['portCode'],
25
+ country: json['country'],
26
+ longitude: json['longitude'].to_f,
27
+ latitude: json['latitude'].to_f)
28
+ end
29
+
30
+ def longitude=(lon)
31
+ @longitude = lon.to_f
32
+ update_waypoint
33
+ end
34
+
35
+ def latitude=(lat)
36
+ @latitude = lat.to_f
37
+ update_waypoint
38
+ end
39
+
40
+ private
41
+
42
+ def has_lat_lon?
43
+ latitude.is_a?(Numeric) && longitude.is_a?(Numeric)
44
+ end
45
+
46
+ def update_waypoint
47
+ @waypoint = Waypoint.new(lon: longitude, lat: latitude) if has_lat_lon?
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SeametrixRuby
4
+ module Models
5
+ class Waypoint
6
+ attr_accessor :lon, :lat
7
+
8
+ def initialize(attributes = {})
9
+ @lon = attributes[:lon].to_f
10
+ @lat = attributes[:lat].to_f
11
+ end
12
+
13
+ def self.from_json(json_string)
14
+ json = MultiJson.load(json_string)
15
+
16
+ new(lon: json['lon'].to_f, lat: json['lat'].to_f)
17
+ end
18
+
19
+ def latitude
20
+ lat
21
+ end
22
+
23
+ def longitude
24
+ lon
25
+ end
26
+
27
+ def latitude=(value)
28
+ @lat = value.to_f
29
+ end
30
+
31
+ def longitude=(value)
32
+ @lon = value.to_f
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,113 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SeametrixRuby
4
+ module Requests
5
+ class RouteRequest
6
+ DOUBLE_ATTRS = [:start_lon, :start_lat, :end_lon, :end_lat].freeze
7
+ INTEGER_ATTRS = [:great_circle_interval, :seca_avoidance, :asl_compliance].freeze
8
+ ARRAY_ATTRS = [:allowed_areas].freeze
9
+
10
+ attr_accessor :start_lon,
11
+ :start_lat,
12
+ :start_port_code,
13
+ :end_lon,
14
+ :end_lat,
15
+ :end_port_code,
16
+ :great_circle_interval,
17
+ :allowed_areas,
18
+ :seca_avoidance,
19
+ :asl_compliance
20
+
21
+ def initialize(attributes = {})
22
+ if attributes[:start_lon] && attributes[:start_lat]
23
+ @start_lon = attributes[:start_lon]
24
+ @start_lat = attributes[:start_lat]
25
+ else
26
+ @start_port_code = attributes[:start_port_code]
27
+ end
28
+
29
+ if attributes[:end_lon] && attributes[:end_lat]
30
+ @end_lon = attributes[:end_lon]
31
+ @end_lat = attributes[:end_lat]
32
+ else
33
+ @end_port_code = attributes[:end_port_code]
34
+ end
35
+
36
+ @great_circle_interval = attributes[:great_circle_interval]
37
+ @allowed_areas = attributes[:allowed_areas]
38
+ @seca_avoidance = attributes[:seca_avoidance]
39
+ @asl_compliance = attributes[:asl_compliance]
40
+ end
41
+
42
+ def self.build(port_start:,
43
+ port_end:,
44
+ great_circle_interval: nil,
45
+ allowed_areas: [],
46
+ seca_avoidance: nil,
47
+ asl_compliance: nil)
48
+
49
+ new(start_lon: port_start.longitude.to_f,
50
+ start_lat: port_start.latitude.to_f,
51
+ start_port_code: port_start.port_code,
52
+ end_lon: port_end.longitude.to_f,
53
+ end_lat: port_end.latitude.to_f,
54
+ end_port_code: port_end.port_code,
55
+ great_circle_interval: great_circle_interval,
56
+ allowed_areas: allowed_areas,
57
+ seca_avoidance: seca_avoidance,
58
+ asl_compliance: asl_compliance)
59
+ end
60
+
61
+ def self.from_json(json_string)
62
+ json = MultiJson.load(json_string)
63
+
64
+ new(start_lon: json['startLon'].to_f,
65
+ start_lat: json['startLat'].to_f,
66
+ start_port_code: json['startPortCode'],
67
+ end_lon: json['endLon'].to_f,
68
+ end_lat: json['endLat'].to_f,
69
+ end_port_code: json['endPortCode'],
70
+ great_circle_interval: json['greatCircleInterval'],
71
+ allowed_areas: json['allowedAreas'],
72
+ seca_avoidance: json['secaAvoidance'],
73
+ asl_compliance: json['aslCompliance'])
74
+ end
75
+
76
+ def self.attributes
77
+ [
78
+ :start_lon,
79
+ :start_lat,
80
+ :start_port_code,
81
+ :end_lon,
82
+ :end_lat,
83
+ :end_port_code,
84
+ :great_circle_interval,
85
+ :allowed_areas,
86
+ :seca_avoidance,
87
+ :asl_compliance
88
+ ].freeze
89
+ end
90
+
91
+ def to_params
92
+ params = {}
93
+ self.class.attributes.each do |key|
94
+ value = send key
95
+ next unless value
96
+
97
+ hash_key = key.to_s.camelize
98
+ hash_key = [hash_key[0].downcase, hash_key[1..hash_key.size - 1]].join
99
+
100
+ params[hash_key] = if DOUBLE_ATTRS.include?(key)
101
+ value.to_f
102
+ elsif INTEGER_ATTRS.include?(key)
103
+ value.to_i
104
+ else
105
+ value
106
+ end
107
+ end
108
+
109
+ params
110
+ end
111
+ end
112
+ end
113
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SeametrixRuby
4
+ module Responses
5
+ class BadRequest
6
+ attr_accessor :type, :title, :status, :trace_id, :errors
7
+
8
+ def initialize(attributes = {})
9
+ @type = attributes[:type]
10
+ @title = attributes[:title]
11
+ @status = attributes[:status]
12
+ @trace_id = attributes[:trace_id]
13
+ @errors = attributes[:errors] || {}
14
+ end
15
+
16
+ def self.from_json(json_string)
17
+ json = MultiJson.load(json_string)
18
+
19
+ new(type: json['type'],
20
+ title: json['title'],
21
+ status: json['status'].to_i,
22
+ trace_id: json['traceId'],
23
+ errors: json['errors'])
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'seametrix_ruby/models/port'
4
+
5
+ module SeametrixRuby
6
+ module Responses
7
+ class PortResult
8
+ attr_accessor :result_code,
9
+ :result_text
10
+ attr_reader :ports
11
+
12
+ def initialize(attributes = {})
13
+ @result_code = attributes[:result_code]
14
+ @result_text = attributes[:result_text]
15
+ self.ports = attributes[:ports]
16
+ end
17
+
18
+ def self.from_json(json_string)
19
+ json = MultiJson.load(json_string)
20
+
21
+ new(result_code: json['resultCode'],
22
+ result_text: json['resultText'],
23
+ ports: json['ports'])
24
+ end
25
+
26
+
27
+ def ports=(args)
28
+ return unless args
29
+ raise ArgumentError.new("Unsupported argument: #{args.class}") unless args.is_a?(Enumerable)
30
+
31
+ @ports = args.map do |arg|
32
+ if arg.is_a?(Hash)
33
+ port_attrs = {}
34
+ arg.each do |key, value|
35
+ port_attrs[key.underscore.to_sym] = value
36
+ end
37
+
38
+ SeametrixRuby::Models::Port.new(port_attrs)
39
+ elsif arg.is_a?(SeametrixRuby::Models::Port)
40
+ arg
41
+ else
42
+ raise ArgumentError.new("Unsupported argument: #{arg.class}")
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'seametrix_ruby/models/port'
4
+
5
+ module SeametrixRuby
6
+ module Responses
7
+ module ResultCode
8
+ SUCCESS = 1
9
+ INVALID_LICENSE = 2
10
+ ERROR = 3
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,56 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'seametrix_ruby/models/waypoint'
4
+
5
+ module SeametrixRuby
6
+ module Responses
7
+ class RouteResult
8
+ attr_accessor :result_code,
9
+ :result_text,
10
+ :total_distance,
11
+ :seca_distance,
12
+ :metadata
13
+ attr_reader :waypoints
14
+
15
+ def initialize(attributes = {})
16
+ @result_code = attributes[:result_code]
17
+ @result_text = attributes[:result_text]
18
+ @total_distance = attributes[:total_distance].to_f
19
+ @seca_distance = attributes[:seca_distance].to_f
20
+ @metadata = attributes[:metadata]
21
+ self.waypoints = attributes[:waypoints] || []
22
+ end
23
+
24
+ def self.from_json(json_string)
25
+ json = MultiJson.load(json_string)
26
+
27
+ new(result_code: json['resultCode'],
28
+ result_text: json['resultText'],
29
+ total_distance: json['totalDistance'].to_f,
30
+ seca_distance: json['secaDistance'].to_f,
31
+ metadata: json['metadata'],
32
+ waypoints: json['waypoints'])
33
+ end
34
+
35
+ def waypoints=(args)
36
+ return unless args
37
+ raise ArgumentError.new("Unsupported argument: #{args.class}") unless args.is_a?(Enumerable)
38
+
39
+ @waypoints = args.map do |arg|
40
+ if arg.is_a?(Hash)
41
+ wp_attrs = {}
42
+ arg.each do |key, value|
43
+ wp_attrs[key.underscore.to_sym] = value
44
+ end
45
+
46
+ SeametrixRuby::Models::Waypoint.new(wp_attrs)
47
+ elsif arg.is_a?(SeametrixRuby::Models::Waypoint)
48
+ arg
49
+ else
50
+ raise ArgumentError.new("Unsupported argument: #{arg.class}")
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'seametrix_ruby/responses/port_result'
4
+
5
+ module SeametrixRuby
6
+ module Services
7
+ class Ports
8
+ def self.get(in_text)
9
+ result = Seametrix.request('/api/GetPorts',
10
+ method: :get,
11
+ params: { 'inText' => in_text })
12
+
13
+ format(result)
14
+ end
15
+
16
+ def self.format(result)
17
+ raise ArgumentError.new("Unable to format empty response") unless result
18
+
19
+ if result.is_a?(Array)
20
+ result.map do |res|
21
+ SeametrixRuby::Responses::PortResult.from_json(res.to_json)
22
+ end
23
+ else
24
+ SeametrixRuby::Responses::PortResult.from_json(result.to_json)
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'seametrix_ruby/responses/bad_request'
4
+ require 'seametrix_ruby/responses/route_result'
5
+
6
+ module SeametrixRuby
7
+ module Services
8
+ class Routes
9
+ def self.get(route_requests = [])
10
+ params = if route_requests.is_a?(Array)
11
+ route_requests.map(&:to_params)
12
+ elsif route_requests.is_a?(SeametrixRuby::Requests::RouteRequest)
13
+ route_requests.to_params
14
+ else
15
+ raise ArgumentError.new("Unsupported argument: #{route_requests.class}")
16
+ end
17
+
18
+ result = Seametrix.request('/api/GetRoutes', method: :post, params: { body: params })
19
+
20
+ format(result)
21
+ rescue NoMethodError => exception
22
+ Seametrix.logger.error("Routes.get with #{route_requests.inspect} failed: #{exception.message}")
23
+ end
24
+
25
+ def self.format(result)
26
+ if result.is_a?(Array)
27
+ result.map do |res|
28
+ SeametrixRuby::Responses::RouteResult.from_json(res.to_json)
29
+ end
30
+ elsif result.is_a?(Hash) && result['resultCode']
31
+ SeametrixRuby::Responses::RouteResult.from_json(result.to_json)
32
+ else
33
+ SeametrixRuby::Responses::BadRequest.from_json(result.to_json)
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SeametrixRuby
4
+ VERSION = "1.0.3"
5
+ end
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "./seametrix"
@@ -0,0 +1,39 @@
1
+ require_relative 'lib/seametrix_ruby/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "seametrix_ruby"
5
+ spec.version = SeametrixRuby::VERSION
6
+ spec.authors = ["adiwids"]
7
+ spec.email = ["adi.widyawan@bocistudio.com"]
8
+
9
+ spec.summary = %q{Ruby wrapper for Seametrix API}
10
+ spec.description = %q{Ruby wrapper for Seametrix API}
11
+ spec.homepage = "https://www.bocistudio.com"
12
+ spec.license = "MIT"
13
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
14
+
15
+ #spec.metadata["homepage_uri"] = spec.homepage
16
+ #spec.metadata["source_code_uri"] = ""
17
+ #spec.metadata["changelog_uri"] = ""
18
+
19
+ # Specify which files should be added to the gem when it is released.
20
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
21
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
22
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
23
+ end
24
+ spec.require_paths = ["lib"]
25
+
26
+ # Dependencies
27
+ spec.required_ruby_version = '>= 2.7.0'
28
+
29
+ spec.add_development_dependency 'rake'
30
+ spec.add_development_dependency 'rspec'
31
+ spec.add_development_dependency 'webmock', '~> 3.12.1'
32
+ spec.add_development_dependency 'simplecov', '>= 0.19.1'
33
+
34
+ spec.add_dependency 'faraday', '~> 1.0'
35
+ spec.add_dependency 'faraday_middleware', '~> 1.2.0'
36
+ spec.add_dependency 'multi_json', '>= 1.11.2'
37
+ spec.add_dependency 'hashie', '~> 5.0.0'
38
+ spec.add_dependency 'activesupport', '~> 6.1'
39
+ end
metadata ADDED
@@ -0,0 +1,193 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: seametrix_ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.3
5
+ platform: ruby
6
+ authors:
7
+ - adiwids
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2022-04-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: webmock
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 3.12.1
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 3.12.1
55
+ - !ruby/object:Gem::Dependency
56
+ name: simplecov
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: 0.19.1
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: 0.19.1
69
+ - !ruby/object:Gem::Dependency
70
+ name: faraday
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: faraday_middleware
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 1.2.0
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 1.2.0
97
+ - !ruby/object:Gem::Dependency
98
+ name: multi_json
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: 1.11.2
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: 1.11.2
111
+ - !ruby/object:Gem::Dependency
112
+ name: hashie
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: 5.0.0
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: 5.0.0
125
+ - !ruby/object:Gem::Dependency
126
+ name: activesupport
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: '6.1'
132
+ type: :runtime
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: '6.1'
139
+ description: Ruby wrapper for Seametrix API
140
+ email:
141
+ - adi.widyawan@bocistudio.com
142
+ executables: []
143
+ extensions: []
144
+ extra_rdoc_files: []
145
+ files:
146
+ - ".github/workflows/build.yml"
147
+ - ".gitignore"
148
+ - ".rspec"
149
+ - Gemfile
150
+ - Gemfile.lock
151
+ - LICENSE.txt
152
+ - README.md
153
+ - Rakefile
154
+ - bin/console
155
+ - bin/setup
156
+ - lib/seametrix.rb
157
+ - lib/seametrix_ruby.rb
158
+ - lib/seametrix_ruby/configuration.rb
159
+ - lib/seametrix_ruby/models/port.rb
160
+ - lib/seametrix_ruby/models/waypoint.rb
161
+ - lib/seametrix_ruby/requests/route_request.rb
162
+ - lib/seametrix_ruby/responses/bad_request.rb
163
+ - lib/seametrix_ruby/responses/port_result.rb
164
+ - lib/seametrix_ruby/responses/result_code.rb
165
+ - lib/seametrix_ruby/responses/route_result.rb
166
+ - lib/seametrix_ruby/services/ports.rb
167
+ - lib/seametrix_ruby/services/routes.rb
168
+ - lib/seametrix_ruby/version.rb
169
+ - seametrix_ruby.gemspec
170
+ homepage: https://www.bocistudio.com
171
+ licenses:
172
+ - MIT
173
+ metadata: {}
174
+ post_install_message:
175
+ rdoc_options: []
176
+ require_paths:
177
+ - lib
178
+ required_ruby_version: !ruby/object:Gem::Requirement
179
+ requirements:
180
+ - - ">="
181
+ - !ruby/object:Gem::Version
182
+ version: 2.7.0
183
+ required_rubygems_version: !ruby/object:Gem::Requirement
184
+ requirements:
185
+ - - ">="
186
+ - !ruby/object:Gem::Version
187
+ version: '0'
188
+ requirements: []
189
+ rubygems_version: 3.1.6
190
+ signing_key:
191
+ specification_version: 4
192
+ summary: Ruby wrapper for Seametrix API
193
+ test_files: []