rail-locator-api 0.0.16
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 +7 -0
 - data/LICENSE +21 -0
 - data/README.markdown +146 -0
 - data/lib/generators/rail_locator_api/install/USAGE +2 -0
 - data/lib/generators/rail_locator_api/install/install_generator.rb +13 -0
 - data/lib/generators/rail_locator_api/install/templates/rail_locator_api.rb +21 -0
 - data/lib/generators/rail_locator_api/install/templates/rail_locator_api.yml +14 -0
 - data/lib/rail-locator-api/api_request.rb +206 -0
 - data/lib/rail-locator-api/rail_locator_api_error.rb +29 -0
 - data/lib/rail-locator-api/request.rb +111 -0
 - data/lib/rail-locator-api/response.rb +10 -0
 - data/lib/rail-locator-api/version.rb +3 -0
 - data/lib/rail-locator-api.rb +61 -0
 - metadata +82 -0
 
    
        checksums.yaml
    ADDED
    
    | 
         @@ -0,0 +1,7 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            ---
         
     | 
| 
      
 2 
     | 
    
         
            +
            SHA256:
         
     | 
| 
      
 3 
     | 
    
         
            +
              metadata.gz: fc672ce3051a65a60ffede6614cdd260866e1b91431961764c4741d014aebc4d
         
     | 
| 
      
 4 
     | 
    
         
            +
              data.tar.gz: 71c29b61bb151fdef8ccc8fc76f458413d21bc7bddc70f055df7cb0dcc64926f
         
     | 
| 
      
 5 
     | 
    
         
            +
            SHA512:
         
     | 
| 
      
 6 
     | 
    
         
            +
              metadata.gz: 3180516cfffdbfc9ccba264333f171e4d9298c0f8c816ba42c00bcc60c3db22b0385a78f72cd65ea714794f444823e0bd2bd1b01afa7ecb62b01a278f49c713c
         
     | 
| 
      
 7 
     | 
    
         
            +
              data.tar.gz: 0ff7688d5e1109da597469c1f902a53d5f599f57c9cef814e61fae47191567ef159bd5e64a01f8925206e3571d262cfeb3a81704db26284331a705759b212008
         
     | 
    
        data/LICENSE
    ADDED
    
    | 
         @@ -0,0 +1,21 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            MIT License
         
     | 
| 
      
 2 
     | 
    
         
            +
             
     | 
| 
      
 3 
     | 
    
         
            +
            Copyright (c) 2022 CTM
         
     | 
| 
      
 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 all
         
     | 
| 
      
 13 
     | 
    
         
            +
            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 THE
         
     | 
| 
      
 21 
     | 
    
         
            +
            SOFTWARE.
         
     | 
    
        data/README.markdown
    ADDED
    
    | 
         @@ -0,0 +1,146 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            # RailLocator API
         
     | 
| 
      
 2 
     | 
    
         
            +
             
     | 
| 
      
 3 
     | 
    
         
            +
            API wrapper для RailLocator API v2
         
     | 
| 
      
 4 
     | 
    
         
            +
             
     | 
| 
      
 5 
     | 
    
         
            +
            # Оглавление
         
     | 
| 
      
 6 
     | 
    
         
            +
            0. [Установка](#install)
         
     | 
| 
      
 7 
     | 
    
         
            +
                1. [Использование Rails](#using_rails)
         
     | 
| 
      
 8 
     | 
    
         
            +
                2. [Использование Ruby](#using_ruby)
         
     | 
| 
      
 9 
     | 
    
         
            +
                3. [Debug Logging](#debug_logging)
         
     | 
| 
      
 10 
     | 
    
         
            +
                4. [Custom logger](#custom_logger)
         
     | 
| 
      
 11 
     | 
    
         
            +
            1. [Треки](#tracks)
         
     | 
| 
      
 12 
     | 
    
         
            +
                1. [Получение трека](#get-track-dislocation)
         
     | 
| 
      
 13 
     | 
    
         
            +
                2. [Получение несклько треков](#get-tracks-dislocation)
         
     | 
| 
      
 14 
     | 
    
         
            +
                3. [Постановка на слежение](#post-tracks)
         
     | 
| 
      
 15 
     | 
    
         
            +
                4. [Снятие со слежения](#delete-tracks)
         
     | 
| 
      
 16 
     | 
    
         
            +
                5. [Получение списка треков](#get-track-history)
         
     | 
| 
      
 17 
     | 
    
         
            +
                
         
     | 
| 
      
 18 
     | 
    
         
            +
            # <a name="install"></a> Установка
         
     | 
| 
      
 19 
     | 
    
         
            +
             
     | 
| 
      
 20 
     | 
    
         
            +
            ## Ruby
         
     | 
| 
      
 21 
     | 
    
         
            +
                $ gem install rail-locator-api
         
     | 
| 
      
 22 
     | 
    
         
            +
            ## Rails
         
     | 
| 
      
 23 
     | 
    
         
            +
            добавьте в Gemfile:
         
     | 
| 
      
 24 
     | 
    
         
            +
                gem 'rail-locator-api', git: 'https://gitlab.ctm.ru/locator/rail-locator-ruby-api-wrapper'
         
     | 
| 
      
 25 
     | 
    
         
            +
             
     | 
| 
      
 26 
     | 
    
         
            +
            и запустите `bundle install`.
         
     | 
| 
      
 27 
     | 
    
         
            +
             
     | 
| 
      
 28 
     | 
    
         
            +
            Затем:
         
     | 
| 
      
 29 
     | 
    
         
            +
                rails g rail_locator_api:install
         
     | 
| 
      
 30 
     | 
    
         
            +
            ## Требования
         
     | 
| 
      
 31 
     | 
    
         
            +
             
     | 
| 
      
 32 
     | 
    
         
            +
            Необходимо получить api key https://rail-locator.com/app/api-keys
         
     | 
| 
      
 33 
     | 
    
         
            +
             
     | 
| 
      
 34 
     | 
    
         
            +
            ## <a name="using_rails"></a> Использование Rails
         
     | 
| 
      
 35 
     | 
    
         
            +
             
     | 
| 
      
 36 
     | 
    
         
            +
            В файл `config/rail_locator_api.yml` вставьте ваши данные
         
     | 
| 
      
 37 
     | 
    
         
            +
             
     | 
| 
      
 38 
     | 
    
         
            +
            ## <a name="using_ruby"></a> Использование Ruby
         
     | 
| 
      
 39 
     | 
    
         
            +
             
     | 
| 
      
 40 
     | 
    
         
            +
            Сначала создайте экземпляр объекта `RailLocatorApi::Request`:
         
     | 
| 
      
 41 
     | 
    
         
            +
             
     | 
| 
      
 42 
     | 
    
         
            +
            ```ruby
         
     | 
| 
      
 43 
     | 
    
         
            +
            locator = RailLocatorApi::Request.new(api_key: "your_api_key", api_auth_method: :api_key)
         
     | 
| 
      
 44 
     | 
    
         
            +
            или
         
     | 
| 
      
 45 
     | 
    
         
            +
            locator = RailLocatorApi::Request.new(api_user_email: "email@ctm.ru", api_user_password: "12345678")
         
     | 
| 
      
 46 
     | 
    
         
            +
            ```
         
     | 
| 
      
 47 
     | 
    
         
            +
             
     | 
| 
      
 48 
     | 
    
         
            +
            Вы можете изменять `api_key`, `api_user_email`, `api_user_password`, `timeout`, `open_timeout`, `faraday_adapter`, `proxy`, `symbolize_keys`, `logger`, и `debug`:
         
     | 
| 
      
 49 
     | 
    
         
            +
             
     | 
| 
      
 50 
     | 
    
         
            +
            ```ruby
         
     | 
| 
      
 51 
     | 
    
         
            +
            RailLocatorApi::Request.api_key = "your_api_key"
         
     | 
| 
      
 52 
     | 
    
         
            +
            RailLocatorApi::Request.api_user_email = "email@ctm.ru"
         
     | 
| 
      
 53 
     | 
    
         
            +
            RailLocatorApi::Request.api_user_password = "12345678"
         
     | 
| 
      
 54 
     | 
    
         
            +
            RailLocatorApi::Request.timeout = 15
         
     | 
| 
      
 55 
     | 
    
         
            +
            RailLocatorApi::Request.open_timeout = 15
         
     | 
| 
      
 56 
     | 
    
         
            +
            RailLocatorApi::Request.symbolize_keys = true
         
     | 
| 
      
 57 
     | 
    
         
            +
            RailLocatorApi::Request.debug = false
         
     | 
| 
      
 58 
     | 
    
         
            +
            ```
         
     | 
| 
      
 59 
     | 
    
         
            +
             
     | 
| 
      
 60 
     | 
    
         
            +
            Либо в файле `config/initializers/rail_locator_api.rb` для Rails.
         
     | 
| 
      
 61 
     | 
    
         
            +
             
     | 
| 
      
 62 
     | 
    
         
            +
            ## <a name="debug_logging"></a> Debug Logging
         
     | 
| 
      
 63 
     | 
    
         
            +
             
     | 
| 
      
 64 
     | 
    
         
            +
            Измените `debug: true` чтобы включить логирование в STDOUT.
         
     | 
| 
      
 65 
     | 
    
         
            +
             
     | 
| 
      
 66 
     | 
    
         
            +
            ```ruby
         
     | 
| 
      
 67 
     | 
    
         
            +
            locator = RailLocatorApi::Request.new(api_key: "your_api_key", debug: true)
         
     | 
| 
      
 68 
     | 
    
         
            +
            ```
         
     | 
| 
      
 69 
     | 
    
         
            +
             
     | 
| 
      
 70 
     | 
    
         
            +
            ### <a name="custom_logger"></a> Custom logger
         
     | 
| 
      
 71 
     | 
    
         
            +
             
     | 
| 
      
 72 
     | 
    
         
            +
            `Logger.new` используется по умолчанию, но вы можете изменить на свой:
         
     | 
| 
      
 73 
     | 
    
         
            +
             
     | 
| 
      
 74 
     | 
    
         
            +
            ```ruby
         
     | 
| 
      
 75 
     | 
    
         
            +
            locator = RailLocatorApi::Request.new(api_key: "your_api_key", debug: true, logger: MyLogger.new)
         
     | 
| 
      
 76 
     | 
    
         
            +
            ```
         
     | 
| 
      
 77 
     | 
    
         
            +
             
     | 
| 
      
 78 
     | 
    
         
            +
            Или:
         
     | 
| 
      
 79 
     | 
    
         
            +
             
     | 
| 
      
 80 
     | 
    
         
            +
            ```ruby
         
     | 
| 
      
 81 
     | 
    
         
            +
            RailLocatorApi::Request.logger = MyLogger.new
         
     | 
| 
      
 82 
     | 
    
         
            +
            ```
         
     | 
| 
      
 83 
     | 
    
         
            +
             
     | 
| 
      
 84 
     | 
    
         
            +
            # <a name="examples"></a> Примеры
         
     | 
| 
      
 85 
     | 
    
         
            +
            ## <a name="tracks"></a> Треки
         
     | 
| 
      
 86 
     | 
    
         
            +
            ### <a name="get-track-dislocation"></a> [Получение трека](http://soap.ctm.ru:8112/api/v2.0/redoc#operation/__________________________________________tracks_dislocation_data_get)
         
     | 
| 
      
 87 
     | 
    
         
            +
            ```ruby
         
     | 
| 
      
 88 
     | 
    
         
            +
            track_id = 1
         
     | 
| 
      
 89 
     | 
    
         
            +
            #КАК ВЫГЛЯДИТ СЕЙЧАС
         
     | 
| 
      
 90 
     | 
    
         
            +
            RailLocatorApi::Request.tracks.dislocation(track_id).data.retrieve.body
         
     | 
| 
      
 91 
     | 
    
         
            +
            #КАК ЭТО ДОЛЖНО ВЫГЛЯДЕТЬ
         
     | 
| 
      
 92 
     | 
    
         
            +
            RailLocatorApi::Request.tracks(track_id).retrieve.body
         
     | 
| 
      
 93 
     | 
    
         
            +
            ```
         
     | 
| 
      
 94 
     | 
    
         
            +
            ### <a name="get-track-dislocation"></a> [Получение треков](http://soap.ctm.ru:8112/api/v2.0/redoc#operation/__________________________________________tracks_dislocation_data_get)
         
     | 
| 
      
 95 
     | 
    
         
            +
            ```ruby
         
     | 
| 
      
 96 
     | 
    
         
            +
            params = {
         
     | 
| 
      
 97 
     | 
    
         
            +
              track_ids: [1,2,3],
         
     | 
| 
      
 98 
     | 
    
         
            +
              operations_count: 100
         
     | 
| 
      
 99 
     | 
    
         
            +
            }
         
     | 
| 
      
 100 
     | 
    
         
            +
            #КАК ВЫГЛЯДИТ СЕЙЧАС
         
     | 
| 
      
 101 
     | 
    
         
            +
            RailLocatorApi::Request.tracks.dislocation.data.retrieve(body: params).body
         
     | 
| 
      
 102 
     | 
    
         
            +
            #КАК ЭТО ДОЛЖНО ВЫГЛЯДЕТЬ
         
     | 
| 
      
 103 
     | 
    
         
            +
            RailLocatorApi::Request.tracks.retrieve(params: params).body
         
     | 
| 
      
 104 
     | 
    
         
            +
            ```
         
     | 
| 
      
 105 
     | 
    
         
            +
            ### <a name="post-tracks"></a> [Постановка на слежение](http://soap.ctm.ru:8112/api/v2.0/redoc#operation/_______________________tracks_post)
         
     | 
| 
      
 106 
     | 
    
         
            +
            ```ruby
         
     | 
| 
      
 107 
     | 
    
         
            +
            body = [
         
     | 
| 
      
 108 
     | 
    
         
            +
              {
         
     | 
| 
      
 109 
     | 
    
         
            +
                is_validate_vehicle: true,
         
     | 
| 
      
 110 
     | 
    
         
            +
                cargo_transport_unit_number: "Test",
         
     | 
| 
      
 111 
     | 
    
         
            +
                tracking_type: 0,
         
     | 
| 
      
 112 
     | 
    
         
            +
                country_code: "RU",
         
     | 
| 
      
 113 
     | 
    
         
            +
                is_cross_country_tracking: true
         
     | 
| 
      
 114 
     | 
    
         
            +
              }
         
     | 
| 
      
 115 
     | 
    
         
            +
            ]
         
     | 
| 
      
 116 
     | 
    
         
            +
            RailLocatorApi::Request.tracks.create(body: body).body
         
     | 
| 
      
 117 
     | 
    
         
            +
            ```
         
     | 
| 
      
 118 
     | 
    
         
            +
            ### <a name="delete-tracks"></a> [Снятие со слежения](http://soap.ctm.ru:8112/api/v2.0/redoc#operation/___________________tracks_delete)
         
     | 
| 
      
 119 
     | 
    
         
            +
            ```ruby
         
     | 
| 
      
 120 
     | 
    
         
            +
            body = {
         
     | 
| 
      
 121 
     | 
    
         
            +
              track_ids: [
         
     | 
| 
      
 122 
     | 
    
         
            +
                123,
         
     | 
| 
      
 123 
     | 
    
         
            +
                1234
         
     | 
| 
      
 124 
     | 
    
         
            +
              ]
         
     | 
| 
      
 125 
     | 
    
         
            +
            }
         
     | 
| 
      
 126 
     | 
    
         
            +
            RailLocatorApi::Request.tracks.delete(body: body)
         
     | 
| 
      
 127 
     | 
    
         
            +
            #ОК, НО НУЖЕН ЕЩЕ МЕТОД
         
     | 
| 
      
 128 
     | 
    
         
            +
            track_id = 123
         
     | 
| 
      
 129 
     | 
    
         
            +
            RailLocatorApi::Request.tracks(track_id).delete
         
     | 
| 
      
 130 
     | 
    
         
            +
            ```
         
     | 
| 
      
 131 
     | 
    
         
            +
            ### <a name="get-track-history"></a> [Получение списка треков](http://soap.ctm.ru:8112/api/v2.0/redoc#operation/________________________tracks_dislocation_get)
         
     | 
| 
      
 132 
     | 
    
         
            +
            ```ruby
         
     | 
| 
      
 133 
     | 
    
         
            +
            params = {
         
     | 
| 
      
 134 
     | 
    
         
            +
              "limit": 10000,
         
     | 
| 
      
 135 
     | 
    
         
            +
              "offset": 0,
         
     | 
| 
      
 136 
     | 
    
         
            +
              "filters": [
         
     | 
| 
      
 137 
     | 
    
         
            +
                {}
         
     | 
| 
      
 138 
     | 
    
         
            +
              ],
         
     | 
| 
      
 139 
     | 
    
         
            +
              "fulltext_filter": "string"
         
     | 
| 
      
 140 
     | 
    
         
            +
            }
         
     | 
| 
      
 141 
     | 
    
         
            +
            RailLocatorApi::Request.tracks.dislocation.retrieve(body: params).body
         
     | 
| 
      
 142 
     | 
    
         
            +
             
     | 
| 
      
 143 
     | 
    
         
            +
            # чем это отличается от [Получение несклько треков]? Это или то история? поправьте нейминги, не понятно
         
     | 
| 
      
 144 
     | 
    
         
            +
            # КАК ДОЛЖНО ВЫГЛЯДЕТЬ ПОЛУЧЕНИЕ ИСТОРИИ ПО ТРЕКУ
         
     | 
| 
      
 145 
     | 
    
         
            +
            RailLocatorApi::Request.tracks(track_id).history.retrieve.body
         
     | 
| 
      
 146 
     | 
    
         
            +
            ```
         
     | 
| 
         @@ -0,0 +1,13 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            # frozen_string_literal: true
         
     | 
| 
      
 2 
     | 
    
         
            +
             
     | 
| 
      
 3 
     | 
    
         
            +
            module RailLocatorApi
         
     | 
| 
      
 4 
     | 
    
         
            +
              class InstallGenerator < Rails::Generators::Base
         
     | 
| 
      
 5 
     | 
    
         
            +
                source_root File.expand_path('templates', __dir__)
         
     | 
| 
      
 6 
     | 
    
         
            +
             
     | 
| 
      
 7 
     | 
    
         
            +
                def generate_install
         
     | 
| 
      
 8 
     | 
    
         
            +
                  copy_file 'rail_locator_api.yml', 'config/rail_locator_api.yml'
         
     | 
| 
      
 9 
     | 
    
         
            +
                  copy_file 'rail_locator_api.rb', 'config/initializers/rail_locator_api.rb'
         
     | 
| 
      
 10 
     | 
    
         
            +
                end
         
     | 
| 
      
 11 
     | 
    
         
            +
              end
         
     | 
| 
      
 12 
     | 
    
         
            +
            end
         
     | 
| 
      
 13 
     | 
    
         
            +
             
     | 
| 
         @@ -0,0 +1,21 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            require 'rail-locator-api'
         
     | 
| 
      
 2 
     | 
    
         
            +
             
     | 
| 
      
 3 
     | 
    
         
            +
            RailLocatorApi.setup do |config|
         
     | 
| 
      
 4 
     | 
    
         
            +
              if File.exist?('config/rail_locator_api.yml')
         
     | 
| 
      
 5 
     | 
    
         
            +
                processed = YAML.load_file('config/rail_locator_api.yml')[Rails.env]
         
     | 
| 
      
 6 
     | 
    
         
            +
             
     | 
| 
      
 7 
     | 
    
         
            +
                processed.each do |k, v|
         
     | 
| 
      
 8 
     | 
    
         
            +
                  config::register k.underscore.to_sym, v
         
     | 
| 
      
 9 
     | 
    
         
            +
                end
         
     | 
| 
      
 10 
     | 
    
         
            +
             
     | 
| 
      
 11 
     | 
    
         
            +
                config::Request.api_key ||= ENV['API_KEY']
         
     | 
| 
      
 12 
     | 
    
         
            +
             
     | 
| 
      
 13 
     | 
    
         
            +
                config::Request.api_user_email ||= ENV['API_USER_EMAIL']
         
     | 
| 
      
 14 
     | 
    
         
            +
                config::Request.api_user_password ||= ENV['API_USER_PASSWORD']
         
     | 
| 
      
 15 
     | 
    
         
            +
             
     | 
| 
      
 16 
     | 
    
         
            +
                config::Request.timeout = 60
         
     | 
| 
      
 17 
     | 
    
         
            +
                config::Request.open_timeout = 60
         
     | 
| 
      
 18 
     | 
    
         
            +
                config::Request.symbolize_keys = true
         
     | 
| 
      
 19 
     | 
    
         
            +
                config::Request.debug = false
         
     | 
| 
      
 20 
     | 
    
         
            +
              end
         
     | 
| 
      
 21 
     | 
    
         
            +
            end
         
     | 
| 
         @@ -0,0 +1,14 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            defaults: &defaults
         
     | 
| 
      
 2 
     | 
    
         
            +
              API_KEY: "***"
         
     | 
| 
      
 3 
     | 
    
         
            +
              API_ENDPOINT: "http://soap.ctm.ru:8112"
         
     | 
| 
      
 4 
     | 
    
         
            +
              API_USER_EMAIL: ""
         
     | 
| 
      
 5 
     | 
    
         
            +
              API_USER_PASSWORD: ""
         
     | 
| 
      
 6 
     | 
    
         
            +
              KEYCLOAK_TOKEN_URL: "https://iam.ctm.ru/auth/realms/ctm/protocol/openid-connect/token"
         
     | 
| 
      
 7 
     | 
    
         
            +
              KEYCLOAK_GRANT_TYPE: "password"
         
     | 
| 
      
 8 
     | 
    
         
            +
              KEYCLOAK_CLIENT_ID: "ctm_lk"
         
     | 
| 
      
 9 
     | 
    
         
            +
            production:
         
     | 
| 
      
 10 
     | 
    
         
            +
              <<: *defaults
         
     | 
| 
      
 11 
     | 
    
         
            +
            development:
         
     | 
| 
      
 12 
     | 
    
         
            +
              <<: *defaults
         
     | 
| 
      
 13 
     | 
    
         
            +
            test:
         
     | 
| 
      
 14 
     | 
    
         
            +
              <<: *defaults
         
     | 
| 
         @@ -0,0 +1,206 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            module RailLocatorApi
         
     | 
| 
      
 2 
     | 
    
         
            +
              class APIRequest
         
     | 
| 
      
 3 
     | 
    
         
            +
             
     | 
| 
      
 4 
     | 
    
         
            +
                def initialize(builder: nil)
         
     | 
| 
      
 5 
     | 
    
         
            +
                  @request_builder = builder
         
     | 
| 
      
 6 
     | 
    
         
            +
                end
         
     | 
| 
      
 7 
     | 
    
         
            +
             
     | 
| 
      
 8 
     | 
    
         
            +
                def post(params: nil, headers: nil, suffix: nil, body: {}, first_time: true)
         
     | 
| 
      
 9 
     | 
    
         
            +
                  validate_api_key
         
     | 
| 
      
 10 
     | 
    
         
            +
                  begin
         
     | 
| 
      
 11 
     | 
    
         
            +
                    response = self.rest_client(suffix).post do |request|
         
     | 
| 
      
 12 
     | 
    
         
            +
                      configure_request(request: request, params: params, headers: headers, body: body)
         
     | 
| 
      
 13 
     | 
    
         
            +
                    end
         
     | 
| 
      
 14 
     | 
    
         
            +
                    parse_response(response)
         
     | 
| 
      
 15 
     | 
    
         
            +
                  rescue => e
         
     | 
| 
      
 16 
     | 
    
         
            +
                    if [401, 404].include?(e.try(:response).try(:dig, :status)) && first_time
         
     | 
| 
      
 17 
     | 
    
         
            +
                      RailLocatorApi::Request.access_token = RailLocatorApi.generate_access_token.try(:dig, "access_token")
         
     | 
| 
      
 18 
     | 
    
         
            +
                      sleep(0.3.second)
         
     | 
| 
      
 19 
     | 
    
         
            +
                      self.post(params: params, headers: headers, suffix: suffix, body: body, first_time: false)
         
     | 
| 
      
 20 
     | 
    
         
            +
                    else
         
     | 
| 
      
 21 
     | 
    
         
            +
                      handle_error(e)
         
     | 
| 
      
 22 
     | 
    
         
            +
                    end
         
     | 
| 
      
 23 
     | 
    
         
            +
                  end
         
     | 
| 
      
 24 
     | 
    
         
            +
                end
         
     | 
| 
      
 25 
     | 
    
         
            +
             
     | 
| 
      
 26 
     | 
    
         
            +
                def get(params: nil, headers: nil, body: {}, first_time: true)
         
     | 
| 
      
 27 
     | 
    
         
            +
                  validate_api_key
         
     | 
| 
      
 28 
     | 
    
         
            +
             
     | 
| 
      
 29 
     | 
    
         
            +
                  begin
         
     | 
| 
      
 30 
     | 
    
         
            +
                    response = self.rest_client.get do |request|
         
     | 
| 
      
 31 
     | 
    
         
            +
                      configure_request(request: request, params: params, headers: headers, body: body)
         
     | 
| 
      
 32 
     | 
    
         
            +
                    end
         
     | 
| 
      
 33 
     | 
    
         
            +
                    parse_response(response)
         
     | 
| 
      
 34 
     | 
    
         
            +
                  rescue => e
         
     | 
| 
      
 35 
     | 
    
         
            +
                    if [401, 404].include?(e.try(:response).try(:dig, :status)) && first_time
         
     | 
| 
      
 36 
     | 
    
         
            +
                      RailLocatorApi::Request.access_token = RailLocatorApi.generate_access_token.try(:dig, "access_token")
         
     | 
| 
      
 37 
     | 
    
         
            +
                      sleep(0.3.second)
         
     | 
| 
      
 38 
     | 
    
         
            +
                      self.get(params: params, headers: headers, first_time: false)
         
     | 
| 
      
 39 
     | 
    
         
            +
                    else
         
     | 
| 
      
 40 
     | 
    
         
            +
                      handle_error(e)
         
     | 
| 
      
 41 
     | 
    
         
            +
                    end
         
     | 
| 
      
 42 
     | 
    
         
            +
                  end
         
     | 
| 
      
 43 
     | 
    
         
            +
                end
         
     | 
| 
      
 44 
     | 
    
         
            +
             
     | 
| 
      
 45 
     | 
    
         
            +
                protected
         
     | 
| 
      
 46 
     | 
    
         
            +
             
     | 
| 
      
 47 
     | 
    
         
            +
                # Convenience accessors
         
     | 
| 
      
 48 
     | 
    
         
            +
             
     | 
| 
      
 49 
     | 
    
         
            +
                def access_token
         
     | 
| 
      
 50 
     | 
    
         
            +
                  @request_builder.access_token
         
     | 
| 
      
 51 
     | 
    
         
            +
                end
         
     | 
| 
      
 52 
     | 
    
         
            +
             
     | 
| 
      
 53 
     | 
    
         
            +
                def api_key
         
     | 
| 
      
 54 
     | 
    
         
            +
                  @request_builder.api_key
         
     | 
| 
      
 55 
     | 
    
         
            +
                end
         
     | 
| 
      
 56 
     | 
    
         
            +
             
     | 
| 
      
 57 
     | 
    
         
            +
                def api_user_email
         
     | 
| 
      
 58 
     | 
    
         
            +
                  @request_builder.api_user_email
         
     | 
| 
      
 59 
     | 
    
         
            +
                end
         
     | 
| 
      
 60 
     | 
    
         
            +
             
     | 
| 
      
 61 
     | 
    
         
            +
                def api_user_password
         
     | 
| 
      
 62 
     | 
    
         
            +
                  @request_builder.api_user_password
         
     | 
| 
      
 63 
     | 
    
         
            +
                end
         
     | 
| 
      
 64 
     | 
    
         
            +
             
     | 
| 
      
 65 
     | 
    
         
            +
                def api_auth_method
         
     | 
| 
      
 66 
     | 
    
         
            +
                  @request_builder.api_auth_method
         
     | 
| 
      
 67 
     | 
    
         
            +
                end
         
     | 
| 
      
 68 
     | 
    
         
            +
             
     | 
| 
      
 69 
     | 
    
         
            +
                def api_endpoint
         
     | 
| 
      
 70 
     | 
    
         
            +
                  @request_builder.api_endpoint
         
     | 
| 
      
 71 
     | 
    
         
            +
                end
         
     | 
| 
      
 72 
     | 
    
         
            +
             
     | 
| 
      
 73 
     | 
    
         
            +
                def timeout
         
     | 
| 
      
 74 
     | 
    
         
            +
                  @request_builder.timeout
         
     | 
| 
      
 75 
     | 
    
         
            +
                end
         
     | 
| 
      
 76 
     | 
    
         
            +
             
     | 
| 
      
 77 
     | 
    
         
            +
                def open_timeout
         
     | 
| 
      
 78 
     | 
    
         
            +
                  @request_builder.open_timeout
         
     | 
| 
      
 79 
     | 
    
         
            +
                end
         
     | 
| 
      
 80 
     | 
    
         
            +
             
     | 
| 
      
 81 
     | 
    
         
            +
                def proxy
         
     | 
| 
      
 82 
     | 
    
         
            +
                  @request_builder.proxy
         
     | 
| 
      
 83 
     | 
    
         
            +
                end
         
     | 
| 
      
 84 
     | 
    
         
            +
             
     | 
| 
      
 85 
     | 
    
         
            +
                def ssl_options
         
     | 
| 
      
 86 
     | 
    
         
            +
                  @request_builder.ssl_options
         
     | 
| 
      
 87 
     | 
    
         
            +
                end
         
     | 
| 
      
 88 
     | 
    
         
            +
             
     | 
| 
      
 89 
     | 
    
         
            +
                def adapter
         
     | 
| 
      
 90 
     | 
    
         
            +
                  @request_builder.faraday_adapter
         
     | 
| 
      
 91 
     | 
    
         
            +
                end
         
     | 
| 
      
 92 
     | 
    
         
            +
             
     | 
| 
      
 93 
     | 
    
         
            +
                def symbolize_keys
         
     | 
| 
      
 94 
     | 
    
         
            +
                  @request_builder.symbolize_keys
         
     | 
| 
      
 95 
     | 
    
         
            +
                end
         
     | 
| 
      
 96 
     | 
    
         
            +
             
     | 
| 
      
 97 
     | 
    
         
            +
                # Helpers
         
     | 
| 
      
 98 
     | 
    
         
            +
             
     | 
| 
      
 99 
     | 
    
         
            +
                def handle_error(error)
         
     | 
| 
      
 100 
     | 
    
         
            +
                  error_params = {}
         
     | 
| 
      
 101 
     | 
    
         
            +
             
     | 
| 
      
 102 
     | 
    
         
            +
                  begin
         
     | 
| 
      
 103 
     | 
    
         
            +
                    if error.is_a?(Faraday::ClientError) && error.response
         
     | 
| 
      
 104 
     | 
    
         
            +
                      error_params[:status_code] = error.response[:status]
         
     | 
| 
      
 105 
     | 
    
         
            +
                      error_params[:raw_body] = error.response[:body]
         
     | 
| 
      
 106 
     | 
    
         
            +
             
     | 
| 
      
 107 
     | 
    
         
            +
                      parsed_response = MultiJson.load(error.response[:body], symbolize_keys: symbolize_keys)
         
     | 
| 
      
 108 
     | 
    
         
            +
             
     | 
| 
      
 109 
     | 
    
         
            +
                      if parsed_response
         
     | 
| 
      
 110 
     | 
    
         
            +
                        error_params[:body] = parsed_response
         
     | 
| 
      
 111 
     | 
    
         
            +
             
     | 
| 
      
 112 
     | 
    
         
            +
                        title_key = symbolize_keys ? :title : "title"
         
     | 
| 
      
 113 
     | 
    
         
            +
                        detail_key = symbolize_keys ? :detail : "detail"
         
     | 
| 
      
 114 
     | 
    
         
            +
             
     | 
| 
      
 115 
     | 
    
         
            +
                        error_params[:title] = parsed_response[title_key] if parsed_response[title_key]
         
     | 
| 
      
 116 
     | 
    
         
            +
                        error_params[:detail] = parsed_response[detail_key] if parsed_response[detail_key]
         
     | 
| 
      
 117 
     | 
    
         
            +
                      end
         
     | 
| 
      
 118 
     | 
    
         
            +
             
     | 
| 
      
 119 
     | 
    
         
            +
                    end
         
     | 
| 
      
 120 
     | 
    
         
            +
                  rescue MultiJson::ParseError
         
     | 
| 
      
 121 
     | 
    
         
            +
                  end
         
     | 
| 
      
 122 
     | 
    
         
            +
             
     | 
| 
      
 123 
     | 
    
         
            +
                  error_to_raise = RailLocatorApiError.new(error.message, error_params)
         
     | 
| 
      
 124 
     | 
    
         
            +
             
     | 
| 
      
 125 
     | 
    
         
            +
                  raise error_to_raise
         
     | 
| 
      
 126 
     | 
    
         
            +
                end
         
     | 
| 
      
 127 
     | 
    
         
            +
             
     | 
| 
      
 128 
     | 
    
         
            +
                def configure_request(request: nil, params: nil, headers: nil, body: nil)
         
     | 
| 
      
 129 
     | 
    
         
            +
                  if request
         
     | 
| 
      
 130 
     | 
    
         
            +
                    request.params.merge!(params) if params
         
     | 
| 
      
 131 
     | 
    
         
            +
                    request.headers['Content-Type'] = 'application/json'
         
     | 
| 
      
 132 
     | 
    
         
            +
                    if self.api_auth_method == :base64
         
     | 
| 
      
 133 
     | 
    
         
            +
                      request.headers['Authorization'] =  "Basic " + Base64::encode64("#{self.api_user_email}:#{self.api_user_password}")
         
     | 
| 
      
 134 
     | 
    
         
            +
                    end
         
     | 
| 
      
 135 
     | 
    
         
            +
                    if self.api_auth_method == :api_key
         
     | 
| 
      
 136 
     | 
    
         
            +
                      request.headers['X-API-KEY'] = "#{self.api_key}"
         
     | 
| 
      
 137 
     | 
    
         
            +
                    end
         
     | 
| 
      
 138 
     | 
    
         
            +
                    if self.api_auth_method == :keycloak
         
     | 
| 
      
 139 
     | 
    
         
            +
                      request.headers['Authorization'] = "Bearer #{RailLocatorApi::Request.access_token}"
         
     | 
| 
      
 140 
     | 
    
         
            +
                    end
         
     | 
| 
      
 141 
     | 
    
         
            +
                    request.headers['User-Agent'] = "RailLocatorApi/#{RailLocatorApi::VERSION} Ruby gem"
         
     | 
| 
      
 142 
     | 
    
         
            +
                    request.headers.merge!(headers) if headers
         
     | 
| 
      
 143 
     | 
    
         
            +
                    request.body = MultiJson.dump(body) if body
         
     | 
| 
      
 144 
     | 
    
         
            +
                    request.options.timeout = self.timeout
         
     | 
| 
      
 145 
     | 
    
         
            +
                    request.options.open_timeout = self.open_timeout
         
     | 
| 
      
 146 
     | 
    
         
            +
                  end
         
     | 
| 
      
 147 
     | 
    
         
            +
                end
         
     | 
| 
      
 148 
     | 
    
         
            +
             
     | 
| 
      
 149 
     | 
    
         
            +
                def rest_client(suffix=nil)
         
     | 
| 
      
 150 
     | 
    
         
            +
                  client = Faraday.new("#{self.api_url}#{suffix.present? ? "/#{suffix}": ""}", proxy: self.proxy,
         
     | 
| 
      
 151 
     | 
    
         
            +
                                       ssl: { version: self.ssl_options }) do |faraday|
         
     | 
| 
      
 152 
     | 
    
         
            +
                    faraday.response :raise_error
         
     | 
| 
      
 153 
     | 
    
         
            +
                    faraday.adapter adapter
         
     | 
| 
      
 154 
     | 
    
         
            +
                    if @request_builder.debug
         
     | 
| 
      
 155 
     | 
    
         
            +
                      faraday.response :logger, @request_builder.logger, bodies: true
         
     | 
| 
      
 156 
     | 
    
         
            +
                    end
         
     | 
| 
      
 157 
     | 
    
         
            +
                  end
         
     | 
| 
      
 158 
     | 
    
         
            +
                  client
         
     | 
| 
      
 159 
     | 
    
         
            +
                end
         
     | 
| 
      
 160 
     | 
    
         
            +
             
     | 
| 
      
 161 
     | 
    
         
            +
                def parse_response(response)
         
     | 
| 
      
 162 
     | 
    
         
            +
                  parsed_response = nil
         
     | 
| 
      
 163 
     | 
    
         
            +
             
     | 
| 
      
 164 
     | 
    
         
            +
                  if response.body && !response.body.empty?
         
     | 
| 
      
 165 
     | 
    
         
            +
                    begin
         
     | 
| 
      
 166 
     | 
    
         
            +
                      headers = response.headers
         
     | 
| 
      
 167 
     | 
    
         
            +
                      body = MultiJson.load(response.body, symbolize_keys: symbolize_keys)
         
     | 
| 
      
 168 
     | 
    
         
            +
                      parsed_response = Response.new(headers: headers, body: body)
         
     | 
| 
      
 169 
     | 
    
         
            +
                    rescue MultiJson::ParseError
         
     | 
| 
      
 170 
     | 
    
         
            +
                      error_params = { title: "UNPARSEABLE_RESPONSE", status_code: 500 }
         
     | 
| 
      
 171 
     | 
    
         
            +
                      error = RailLocatorApiError.new("Unparseable response: #{response.body}", error_params)
         
     | 
| 
      
 172 
     | 
    
         
            +
                      raise error
         
     | 
| 
      
 173 
     | 
    
         
            +
                    end
         
     | 
| 
      
 174 
     | 
    
         
            +
                  end
         
     | 
| 
      
 175 
     | 
    
         
            +
             
     | 
| 
      
 176 
     | 
    
         
            +
                  parsed_response
         
     | 
| 
      
 177 
     | 
    
         
            +
                end
         
     | 
| 
      
 178 
     | 
    
         
            +
             
     | 
| 
      
 179 
     | 
    
         
            +
                def validate_api_key
         
     | 
| 
      
 180 
     | 
    
         
            +
                  case self.api_auth_method
         
     | 
| 
      
 181 
     | 
    
         
            +
                  when :api_key
         
     | 
| 
      
 182 
     | 
    
         
            +
                    unless self.api_key && self.api_endpoint
         
     | 
| 
      
 183 
     | 
    
         
            +
                      raise RailLocatorApi::RailLocatorApiError, "You must set an api_key prior to making a call #{self.api_key} #{self.api_endpoint}"
         
     | 
| 
      
 184 
     | 
    
         
            +
                    end
         
     | 
| 
      
 185 
     | 
    
         
            +
                  when :base64
         
     | 
| 
      
 186 
     | 
    
         
            +
                    unless self.api_user_email && self.api_user_password
         
     | 
| 
      
 187 
     | 
    
         
            +
                      raise RailLocatorApi::RailLocatorApiError, "You must set an api_user_email and an api_user_password prior to making a call #{self.api_user_email} #{self.api_user_password} #{self.api_endpoint}"
         
     | 
| 
      
 188 
     | 
    
         
            +
                    end
         
     | 
| 
      
 189 
     | 
    
         
            +
                  when :keycloak
         
     | 
| 
      
 190 
     | 
    
         
            +
                    unless self.api_user_email && self.api_user_password
         
     | 
| 
      
 191 
     | 
    
         
            +
                      raise RailLocatorApi::RailLocatorApiError, "You must set an api_user_email and an api_user_password prior to making a call #{self.api_user_email} #{self.api_user_password} #{self.api_endpoint}"
         
     | 
| 
      
 192 
     | 
    
         
            +
                    end
         
     | 
| 
      
 193 
     | 
    
         
            +
                  else
         
     | 
| 
      
 194 
     | 
    
         
            +
                    raise RailLocatorApi::RailLocatorApiError, "You must set an api_auth_method prior to making a call #{self.api_auth_method} #{self.api_endpoint}"
         
     | 
| 
      
 195 
     | 
    
         
            +
                  end
         
     | 
| 
      
 196 
     | 
    
         
            +
                end
         
     | 
| 
      
 197 
     | 
    
         
            +
             
     | 
| 
      
 198 
     | 
    
         
            +
                def api_url
         
     | 
| 
      
 199 
     | 
    
         
            +
                  base_api_url + @request_builder.path
         
     | 
| 
      
 200 
     | 
    
         
            +
                end
         
     | 
| 
      
 201 
     | 
    
         
            +
             
     | 
| 
      
 202 
     | 
    
         
            +
                def base_api_url
         
     | 
| 
      
 203 
     | 
    
         
            +
                  "#{RailLocatorApi.api_endpoint}/api/v2.0/"
         
     | 
| 
      
 204 
     | 
    
         
            +
                end
         
     | 
| 
      
 205 
     | 
    
         
            +
              end
         
     | 
| 
      
 206 
     | 
    
         
            +
            end
         
     | 
| 
         @@ -0,0 +1,29 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            module RailLocatorApi
         
     | 
| 
      
 2 
     | 
    
         
            +
              class RailLocatorApiError < StandardError
         
     | 
| 
      
 3 
     | 
    
         
            +
                attr_reader :title, :detail, :body, :raw_body, :status_code
         
     | 
| 
      
 4 
     | 
    
         
            +
             
     | 
| 
      
 5 
     | 
    
         
            +
                def initialize(message = "", params = {})
         
     | 
| 
      
 6 
     | 
    
         
            +
                  @title       = params[:title]
         
     | 
| 
      
 7 
     | 
    
         
            +
                  @detail      = params[:detail]
         
     | 
| 
      
 8 
     | 
    
         
            +
                  @body        = params[:body]
         
     | 
| 
      
 9 
     | 
    
         
            +
                  @raw_body    = params[:raw_body]
         
     | 
| 
      
 10 
     | 
    
         
            +
                  @status_code = params[:status_code]
         
     | 
| 
      
 11 
     | 
    
         
            +
             
     | 
| 
      
 12 
     | 
    
         
            +
                  super(message)
         
     | 
| 
      
 13 
     | 
    
         
            +
                end
         
     | 
| 
      
 14 
     | 
    
         
            +
             
     | 
| 
      
 15 
     | 
    
         
            +
                def to_s
         
     | 
| 
      
 16 
     | 
    
         
            +
                  super + " " + instance_variables_to_s
         
     | 
| 
      
 17 
     | 
    
         
            +
                end
         
     | 
| 
      
 18 
     | 
    
         
            +
             
     | 
| 
      
 19 
     | 
    
         
            +
                private
         
     | 
| 
      
 20 
     | 
    
         
            +
             
     | 
| 
      
 21 
     | 
    
         
            +
                def instance_variables_to_s
         
     | 
| 
      
 22 
     | 
    
         
            +
                  [:title, :detail, :body, :raw_body, :status_code].map do |attr|
         
     | 
| 
      
 23 
     | 
    
         
            +
                    attr_value = send(attr)
         
     | 
| 
      
 24 
     | 
    
         
            +
             
     | 
| 
      
 25 
     | 
    
         
            +
                    "@#{attr}=#{attr_value.inspect}"
         
     | 
| 
      
 26 
     | 
    
         
            +
                  end.join(", ")
         
     | 
| 
      
 27 
     | 
    
         
            +
                end
         
     | 
| 
      
 28 
     | 
    
         
            +
              end
         
     | 
| 
      
 29 
     | 
    
         
            +
            end
         
     | 
| 
         @@ -0,0 +1,111 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            module RailLocatorApi
         
     | 
| 
      
 2 
     | 
    
         
            +
              class Request
         
     | 
| 
      
 3 
     | 
    
         
            +
                attr_accessor :access_token, :api_key, :api_user_email, :api_user_password, :api_auth_method, :api_endpoint,
         
     | 
| 
      
 4 
     | 
    
         
            +
                              :timeout, :open_timeout, :proxy, :ssl_options, :faraday_adapter, :symbolize_keys, :debug, :logger, :test
         
     | 
| 
      
 5 
     | 
    
         
            +
             
     | 
| 
      
 6 
     | 
    
         
            +
                AUTH_METHODS = [:keycloak, :api_key, :base64]
         
     | 
| 
      
 7 
     | 
    
         
            +
                DEFAULT_AUTH_METHOD = :keycloak
         
     | 
| 
      
 8 
     | 
    
         
            +
                DEFAULT_TIMEOUT = 60
         
     | 
| 
      
 9 
     | 
    
         
            +
                DEFAULT_OPEN_TIMEOUT = 60
         
     | 
| 
      
 10 
     | 
    
         
            +
             
     | 
| 
      
 11 
     | 
    
         
            +
                def initialize(access_token: nil, api_key: nil, api_user_email: nil, api_user_password: nil,
         
     | 
| 
      
 12 
     | 
    
         
            +
                               api_endpoint: nil, api_auth_method: nil, timeout: nil, open_timeout: nil, proxy: nil, ssl_options: nil,
         
     | 
| 
      
 13 
     | 
    
         
            +
                               faraday_adapter: nil, symbolize_keys: false, debug: false, logger: nil, test: false)
         
     | 
| 
      
 14 
     | 
    
         
            +
             
     | 
| 
      
 15 
     | 
    
         
            +
                  @path_parts = []
         
     | 
| 
      
 16 
     | 
    
         
            +
                  @api_key = api_key || self.class.api_key || ENV['RAIL_LOCATOR_API_KEY']
         
     | 
| 
      
 17 
     | 
    
         
            +
                  @api_key = @api_key.strip if @api_key
         
     | 
| 
      
 18 
     | 
    
         
            +
                  @api_key = RailLocatorApi::api_key if @api_key.nil?
         
     | 
| 
      
 19 
     | 
    
         
            +
             
     | 
| 
      
 20 
     | 
    
         
            +
                  @access_token = access_token || self.class.access_token || RailLocatorApi.generate_access_token.try(:dig, "access_token")
         
     | 
| 
      
 21 
     | 
    
         
            +
                  @access_token = @access_token.strip if @access_token
         
     | 
| 
      
 22 
     | 
    
         
            +
                  @access_token = RailLocatorApi::generate_access_token if @access_token.nil?
         
     | 
| 
      
 23 
     | 
    
         
            +
             
     | 
| 
      
 24 
     | 
    
         
            +
                  @api_user_email = api_user_email || RailLocatorApi::api_user_email || ENV['API_USER_EMAIL']
         
     | 
| 
      
 25 
     | 
    
         
            +
                  @api_user_password = api_user_password || RailLocatorApi::api_user_password || ENV['API_USER_PASSWORD']
         
     | 
| 
      
 26 
     | 
    
         
            +
                  @api_endpoint = api_endpoint || self.class.api_endpoint
         
     | 
| 
      
 27 
     | 
    
         
            +
                  @api_endpoint = RailLocatorApi::api_endpoint if @api_endpoint.nil?
         
     | 
| 
      
 28 
     | 
    
         
            +
                  @api_auth_method = api_auth_method || DEFAULT_AUTH_METHOD
         
     | 
| 
      
 29 
     | 
    
         
            +
                  @timeout = timeout || self.class.timeout || DEFAULT_TIMEOUT
         
     | 
| 
      
 30 
     | 
    
         
            +
                  @open_timeout = open_timeout || self.class.open_timeout || DEFAULT_OPEN_TIMEOUT
         
     | 
| 
      
 31 
     | 
    
         
            +
                  @proxy = proxy || self.class.proxy || ENV['RAIL_LOCATOR_API_PROXY']
         
     | 
| 
      
 32 
     | 
    
         
            +
                  @ssl_options = ssl_options || self.class.ssl_options || { version: "TLSv1_2" }
         
     | 
| 
      
 33 
     | 
    
         
            +
                  @faraday_adapter = faraday_adapter || self.class.faraday_adapter || Faraday.default_adapter
         
     | 
| 
      
 34 
     | 
    
         
            +
                  @symbolize_keys = symbolize_keys || self.class.symbolize_keys || false
         
     | 
| 
      
 35 
     | 
    
         
            +
                  @debug = debug || self.class.debug || false
         
     | 
| 
      
 36 
     | 
    
         
            +
                  @test = test || self.class.test || false
         
     | 
| 
      
 37 
     | 
    
         
            +
                  @logger = logger || self.class.logger || ::Logger.new(STDOUT)
         
     | 
| 
      
 38 
     | 
    
         
            +
                end
         
     | 
| 
      
 39 
     | 
    
         
            +
             
     | 
| 
      
 40 
     | 
    
         
            +
                def method_missing(method, *args)
         
     | 
| 
      
 41 
     | 
    
         
            +
                  @path_parts << method.to_s.gsub("_", "-").downcase
         
     | 
| 
      
 42 
     | 
    
         
            +
                  @path_parts << args if args.length > 0
         
     | 
| 
      
 43 
     | 
    
         
            +
                  @path_parts.flatten!
         
     | 
| 
      
 44 
     | 
    
         
            +
                  self
         
     | 
| 
      
 45 
     | 
    
         
            +
                end
         
     | 
| 
      
 46 
     | 
    
         
            +
             
     | 
| 
      
 47 
     | 
    
         
            +
                def respond_to_missing?(method_name, include_private = false)
         
     | 
| 
      
 48 
     | 
    
         
            +
                  true
         
     | 
| 
      
 49 
     | 
    
         
            +
                end
         
     | 
| 
      
 50 
     | 
    
         
            +
             
     | 
| 
      
 51 
     | 
    
         
            +
                def send(*args)
         
     | 
| 
      
 52 
     | 
    
         
            +
                  if args.length == 0
         
     | 
| 
      
 53 
     | 
    
         
            +
                    method_missing(:send, args)
         
     | 
| 
      
 54 
     | 
    
         
            +
                  else
         
     | 
| 
      
 55 
     | 
    
         
            +
                    __send__(*args)
         
     | 
| 
      
 56 
     | 
    
         
            +
                  end
         
     | 
| 
      
 57 
     | 
    
         
            +
                end
         
     | 
| 
      
 58 
     | 
    
         
            +
             
     | 
| 
      
 59 
     | 
    
         
            +
                def path
         
     | 
| 
      
 60 
     | 
    
         
            +
                  @path_parts.join('/')
         
     | 
| 
      
 61 
     | 
    
         
            +
                end
         
     | 
| 
      
 62 
     | 
    
         
            +
             
     | 
| 
      
 63 
     | 
    
         
            +
                def create(params: nil, headers: nil, body: {}, suffix: 'create')
         
     | 
| 
      
 64 
     | 
    
         
            +
                  APIRequest.new(builder: self).post(params: params, headers: headers, suffix: suffix, body: body)
         
     | 
| 
      
 65 
     | 
    
         
            +
                ensure
         
     | 
| 
      
 66 
     | 
    
         
            +
                  reset
         
     | 
| 
      
 67 
     | 
    
         
            +
                end
         
     | 
| 
      
 68 
     | 
    
         
            +
             
     | 
| 
      
 69 
     | 
    
         
            +
                def update(params: nil, headers: nil, body: {})
         
     | 
| 
      
 70 
     | 
    
         
            +
                  APIRequest.new(builder: self).post(params: params, headers: headers, suffix: 'edit', body: body)
         
     | 
| 
      
 71 
     | 
    
         
            +
                ensure
         
     | 
| 
      
 72 
     | 
    
         
            +
                  reset
         
     | 
| 
      
 73 
     | 
    
         
            +
                end
         
     | 
| 
      
 74 
     | 
    
         
            +
             
     | 
| 
      
 75 
     | 
    
         
            +
                def retrieve(params: nil, headers: nil, body: {})
         
     | 
| 
      
 76 
     | 
    
         
            +
                  APIRequest.new(builder: self).get(params: params, headers: headers, body: body)
         
     | 
| 
      
 77 
     | 
    
         
            +
                ensure
         
     | 
| 
      
 78 
     | 
    
         
            +
                  reset
         
     | 
| 
      
 79 
     | 
    
         
            +
                end
         
     | 
| 
      
 80 
     | 
    
         
            +
             
     | 
| 
      
 81 
     | 
    
         
            +
                def delete(params: nil, headers: nil, body: {})
         
     | 
| 
      
 82 
     | 
    
         
            +
                  APIRequest.new(builder: self).post(params: params, headers: headers, body: {}, suffix: 'delete')
         
     | 
| 
      
 83 
     | 
    
         
            +
                ensure
         
     | 
| 
      
 84 
     | 
    
         
            +
                  reset
         
     | 
| 
      
 85 
     | 
    
         
            +
                end
         
     | 
| 
      
 86 
     | 
    
         
            +
             
     | 
| 
      
 87 
     | 
    
         
            +
                protected
         
     | 
| 
      
 88 
     | 
    
         
            +
             
     | 
| 
      
 89 
     | 
    
         
            +
                def reset
         
     | 
| 
      
 90 
     | 
    
         
            +
                  @path_parts = []
         
     | 
| 
      
 91 
     | 
    
         
            +
                end
         
     | 
| 
      
 92 
     | 
    
         
            +
             
     | 
| 
      
 93 
     | 
    
         
            +
                class << self
         
     | 
| 
      
 94 
     | 
    
         
            +
                  attr_accessor :access_token, :api_key, :api_user_email, :api_user_password, :api_auth_method, :timeout, :open_timeout,
         
     | 
| 
      
 95 
     | 
    
         
            +
                                :api_endpoint, :proxy, :ssl_options, :faraday_adapter, :symbolize_keys, :debug, :logger, :test
         
     | 
| 
      
 96 
     | 
    
         
            +
             
     | 
| 
      
 97 
     | 
    
         
            +
                  def method_missing(sym, *args, &block)
         
     | 
| 
      
 98 
     | 
    
         
            +
                    new(access_token: self.access_token, api_key: self.api_key, api_user_email: self.api_user_email,
         
     | 
| 
      
 99 
     | 
    
         
            +
                        api_user_password: self.api_user_email,
         
     | 
| 
      
 100 
     | 
    
         
            +
                        api_auth_method: self.api_auth_method, api_endpoint: self.api_endpoint,
         
     | 
| 
      
 101 
     | 
    
         
            +
                        timeout: self.timeout, open_timeout: self.open_timeout, faraday_adapter: self.faraday_adapter,
         
     | 
| 
      
 102 
     | 
    
         
            +
                        symbolize_keys: self.symbolize_keys, debug: self.debug, proxy: self.proxy, ssl_options: self.ssl_options, logger: self.logger,
         
     | 
| 
      
 103 
     | 
    
         
            +
                        test: self.test).send(sym, *args, &block)
         
     | 
| 
      
 104 
     | 
    
         
            +
                  end
         
     | 
| 
      
 105 
     | 
    
         
            +
             
     | 
| 
      
 106 
     | 
    
         
            +
                  def respond_to_missing?(method_name, include_private = false)
         
     | 
| 
      
 107 
     | 
    
         
            +
                    true
         
     | 
| 
      
 108 
     | 
    
         
            +
                  end
         
     | 
| 
      
 109 
     | 
    
         
            +
                end
         
     | 
| 
      
 110 
     | 
    
         
            +
              end
         
     | 
| 
      
 111 
     | 
    
         
            +
            end
         
     | 
| 
         @@ -0,0 +1,61 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            require 'faraday'
         
     | 
| 
      
 2 
     | 
    
         
            +
            require 'rail-locator-api/version'
         
     | 
| 
      
 3 
     | 
    
         
            +
            require 'rail-locator-api/rail_locator_api_error'
         
     | 
| 
      
 4 
     | 
    
         
            +
            require 'rail-locator-api/request'
         
     | 
| 
      
 5 
     | 
    
         
            +
            require 'rail-locator-api/api_request'
         
     | 
| 
      
 6 
     | 
    
         
            +
            require 'rail-locator-api/response'
         
     | 
| 
      
 7 
     | 
    
         
            +
             
     | 
| 
      
 8 
     | 
    
         
            +
            module RailLocatorApi
         
     | 
| 
      
 9 
     | 
    
         
            +
              class << self
         
     | 
| 
      
 10 
     | 
    
         
            +
                def generate_access_token
         
     | 
| 
      
 11 
     | 
    
         
            +
                  response = Faraday.post(
         
     | 
| 
      
 12 
     | 
    
         
            +
                    RailLocatorApi.keycloak_token_url,
         
     | 
| 
      
 13 
     | 
    
         
            +
                    "grant_type=#{RailLocatorApi.keycloak_grant_type}&client_id=#{RailLocatorApi.keycloak_client_id}&username=#{RailLocatorApi.api_user_email}&password=#{RailLocatorApi.api_user_password}")
         
     | 
| 
      
 14 
     | 
    
         
            +
                  JSON.parse(response.body)
         
     | 
| 
      
 15 
     | 
    
         
            +
                end
         
     | 
| 
      
 16 
     | 
    
         
            +
             
     | 
| 
      
 17 
     | 
    
         
            +
                def setup
         
     | 
| 
      
 18 
     | 
    
         
            +
                  yield self
         
     | 
| 
      
 19 
     | 
    
         
            +
                end
         
     | 
| 
      
 20 
     | 
    
         
            +
             
     | 
| 
      
 21 
     | 
    
         
            +
                def register(name, value, type = nil)
         
     | 
| 
      
 22 
     | 
    
         
            +
                  cattr_accessor "#{name}_setting".to_sym
         
     | 
| 
      
 23 
     | 
    
         
            +
             
     | 
| 
      
 24 
     | 
    
         
            +
                  add_reader(name)
         
     | 
| 
      
 25 
     | 
    
         
            +
                  add_writer(name, type)
         
     | 
| 
      
 26 
     | 
    
         
            +
                  send "#{name}=", value
         
     | 
| 
      
 27 
     | 
    
         
            +
                end
         
     | 
| 
      
 28 
     | 
    
         
            +
             
     | 
| 
      
 29 
     | 
    
         
            +
                def add_reader(name)
         
     | 
| 
      
 30 
     | 
    
         
            +
                  define_singleton_method(name) do |*args|
         
     | 
| 
      
 31 
     | 
    
         
            +
                    send("#{name}_setting").value(*args)
         
     | 
| 
      
 32 
     | 
    
         
            +
                  end
         
     | 
| 
      
 33 
     | 
    
         
            +
                end
         
     | 
| 
      
 34 
     | 
    
         
            +
             
     | 
| 
      
 35 
     | 
    
         
            +
                def add_writer(name, type)
         
     | 
| 
      
 36 
     | 
    
         
            +
                  define_singleton_method("#{name}=") do |value|
         
     | 
| 
      
 37 
     | 
    
         
            +
                    send("#{name}_setting=", DynamicSetting.build(value, type))
         
     | 
| 
      
 38 
     | 
    
         
            +
                  end
         
     | 
| 
      
 39 
     | 
    
         
            +
                end
         
     | 
| 
      
 40 
     | 
    
         
            +
              end
         
     | 
| 
      
 41 
     | 
    
         
            +
             
     | 
| 
      
 42 
     | 
    
         
            +
              class DynamicSetting
         
     | 
| 
      
 43 
     | 
    
         
            +
                def self.build(setting, type)
         
     | 
| 
      
 44 
     | 
    
         
            +
                  (type ? klass(type) : self).new(setting)
         
     | 
| 
      
 45 
     | 
    
         
            +
                end
         
     | 
| 
      
 46 
     | 
    
         
            +
             
     | 
| 
      
 47 
     | 
    
         
            +
                def self.klass(type)
         
     | 
| 
      
 48 
     | 
    
         
            +
                  klass = "#{type.to_s.camelcase}Setting"
         
     | 
| 
      
 49 
     | 
    
         
            +
                  raise ArgumentError, "Unknown type: #{type}" unless RailLocatorApi.const_defined?(klass)
         
     | 
| 
      
 50 
     | 
    
         
            +
                  RailLocatorApi.const_get(klass)
         
     | 
| 
      
 51 
     | 
    
         
            +
                end
         
     | 
| 
      
 52 
     | 
    
         
            +
             
     | 
| 
      
 53 
     | 
    
         
            +
                def initialize(setting)
         
     | 
| 
      
 54 
     | 
    
         
            +
                  @setting = setting
         
     | 
| 
      
 55 
     | 
    
         
            +
                end
         
     | 
| 
      
 56 
     | 
    
         
            +
             
     | 
| 
      
 57 
     | 
    
         
            +
                def value(*_args)
         
     | 
| 
      
 58 
     | 
    
         
            +
                  @setting
         
     | 
| 
      
 59 
     | 
    
         
            +
                end
         
     | 
| 
      
 60 
     | 
    
         
            +
              end
         
     | 
| 
      
 61 
     | 
    
         
            +
            end
         
     | 
    
        metadata
    ADDED
    
    | 
         @@ -0,0 +1,82 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            --- !ruby/object:Gem::Specification
         
     | 
| 
      
 2 
     | 
    
         
            +
            name: rail-locator-api
         
     | 
| 
      
 3 
     | 
    
         
            +
            version: !ruby/object:Gem::Version
         
     | 
| 
      
 4 
     | 
    
         
            +
              version: 0.0.16
         
     | 
| 
      
 5 
     | 
    
         
            +
            platform: ruby
         
     | 
| 
      
 6 
     | 
    
         
            +
            authors:
         
     | 
| 
      
 7 
     | 
    
         
            +
            - Pavel Osetrov
         
     | 
| 
      
 8 
     | 
    
         
            +
            autorequire:
         
     | 
| 
      
 9 
     | 
    
         
            +
            bindir: bin
         
     | 
| 
      
 10 
     | 
    
         
            +
            cert_chain: []
         
     | 
| 
      
 11 
     | 
    
         
            +
            date: 2022-03-24 00:00:00.000000000 Z
         
     | 
| 
      
 12 
     | 
    
         
            +
            dependencies:
         
     | 
| 
      
 13 
     | 
    
         
            +
            - !ruby/object:Gem::Dependency
         
     | 
| 
      
 14 
     | 
    
         
            +
              name: faraday
         
     | 
| 
      
 15 
     | 
    
         
            +
              requirement: !ruby/object:Gem::Requirement
         
     | 
| 
      
 16 
     | 
    
         
            +
                requirements:
         
     | 
| 
      
 17 
     | 
    
         
            +
                - - ">="
         
     | 
| 
      
 18 
     | 
    
         
            +
                  - !ruby/object:Gem::Version
         
     | 
| 
      
 19 
     | 
    
         
            +
                    version: 0.16.0
         
     | 
| 
      
 20 
     | 
    
         
            +
              type: :runtime
         
     | 
| 
      
 21 
     | 
    
         
            +
              prerelease: false
         
     | 
| 
      
 22 
     | 
    
         
            +
              version_requirements: !ruby/object:Gem::Requirement
         
     | 
| 
      
 23 
     | 
    
         
            +
                requirements:
         
     | 
| 
      
 24 
     | 
    
         
            +
                - - ">="
         
     | 
| 
      
 25 
     | 
    
         
            +
                  - !ruby/object:Gem::Version
         
     | 
| 
      
 26 
     | 
    
         
            +
                    version: 0.16.0
         
     | 
| 
      
 27 
     | 
    
         
            +
            - !ruby/object:Gem::Dependency
         
     | 
| 
      
 28 
     | 
    
         
            +
              name: multi_json
         
     | 
| 
      
 29 
     | 
    
         
            +
              requirement: !ruby/object:Gem::Requirement
         
     | 
| 
      
 30 
     | 
    
         
            +
                requirements:
         
     | 
| 
      
 31 
     | 
    
         
            +
                - - ">="
         
     | 
| 
      
 32 
     | 
    
         
            +
                  - !ruby/object:Gem::Version
         
     | 
| 
      
 33 
     | 
    
         
            +
                    version: 1.11.0
         
     | 
| 
      
 34 
     | 
    
         
            +
              type: :runtime
         
     | 
| 
      
 35 
     | 
    
         
            +
              prerelease: false
         
     | 
| 
      
 36 
     | 
    
         
            +
              version_requirements: !ruby/object:Gem::Requirement
         
     | 
| 
      
 37 
     | 
    
         
            +
                requirements:
         
     | 
| 
      
 38 
     | 
    
         
            +
                - - ">="
         
     | 
| 
      
 39 
     | 
    
         
            +
                  - !ruby/object:Gem::Version
         
     | 
| 
      
 40 
     | 
    
         
            +
                    version: 1.11.0
         
     | 
| 
      
 41 
     | 
    
         
            +
            description: ''
         
     | 
| 
      
 42 
     | 
    
         
            +
            email: pavel.osetrov@me.com
         
     | 
| 
      
 43 
     | 
    
         
            +
            executables: []
         
     | 
| 
      
 44 
     | 
    
         
            +
            extensions: []
         
     | 
| 
      
 45 
     | 
    
         
            +
            extra_rdoc_files: []
         
     | 
| 
      
 46 
     | 
    
         
            +
            files:
         
     | 
| 
      
 47 
     | 
    
         
            +
            - LICENSE
         
     | 
| 
      
 48 
     | 
    
         
            +
            - README.markdown
         
     | 
| 
      
 49 
     | 
    
         
            +
            - lib/generators/rail_locator_api/install/USAGE
         
     | 
| 
      
 50 
     | 
    
         
            +
            - lib/generators/rail_locator_api/install/install_generator.rb
         
     | 
| 
      
 51 
     | 
    
         
            +
            - lib/generators/rail_locator_api/install/templates/rail_locator_api.rb
         
     | 
| 
      
 52 
     | 
    
         
            +
            - lib/generators/rail_locator_api/install/templates/rail_locator_api.yml
         
     | 
| 
      
 53 
     | 
    
         
            +
            - lib/rail-locator-api.rb
         
     | 
| 
      
 54 
     | 
    
         
            +
            - lib/rail-locator-api/api_request.rb
         
     | 
| 
      
 55 
     | 
    
         
            +
            - lib/rail-locator-api/rail_locator_api_error.rb
         
     | 
| 
      
 56 
     | 
    
         
            +
            - lib/rail-locator-api/request.rb
         
     | 
| 
      
 57 
     | 
    
         
            +
            - lib/rail-locator-api/response.rb
         
     | 
| 
      
 58 
     | 
    
         
            +
            - lib/rail-locator-api/version.rb
         
     | 
| 
      
 59 
     | 
    
         
            +
            homepage: https://rail-locator.com
         
     | 
| 
      
 60 
     | 
    
         
            +
            licenses:
         
     | 
| 
      
 61 
     | 
    
         
            +
            - MIT
         
     | 
| 
      
 62 
     | 
    
         
            +
            metadata: {}
         
     | 
| 
      
 63 
     | 
    
         
            +
            post_install_message:
         
     | 
| 
      
 64 
     | 
    
         
            +
            rdoc_options: []
         
     | 
| 
      
 65 
     | 
    
         
            +
            require_paths:
         
     | 
| 
      
 66 
     | 
    
         
            +
            - lib
         
     | 
| 
      
 67 
     | 
    
         
            +
            required_ruby_version: !ruby/object:Gem::Requirement
         
     | 
| 
      
 68 
     | 
    
         
            +
              requirements:
         
     | 
| 
      
 69 
     | 
    
         
            +
              - - ">="
         
     | 
| 
      
 70 
     | 
    
         
            +
                - !ruby/object:Gem::Version
         
     | 
| 
      
 71 
     | 
    
         
            +
                  version: 2.3.8
         
     | 
| 
      
 72 
     | 
    
         
            +
            required_rubygems_version: !ruby/object:Gem::Requirement
         
     | 
| 
      
 73 
     | 
    
         
            +
              requirements:
         
     | 
| 
      
 74 
     | 
    
         
            +
              - - ">="
         
     | 
| 
      
 75 
     | 
    
         
            +
                - !ruby/object:Gem::Version
         
     | 
| 
      
 76 
     | 
    
         
            +
                  version: '0'
         
     | 
| 
      
 77 
     | 
    
         
            +
            requirements: []
         
     | 
| 
      
 78 
     | 
    
         
            +
            rubygems_version: 3.2.32
         
     | 
| 
      
 79 
     | 
    
         
            +
            signing_key:
         
     | 
| 
      
 80 
     | 
    
         
            +
            specification_version: 4
         
     | 
| 
      
 81 
     | 
    
         
            +
            summary: RailLocator API
         
     | 
| 
      
 82 
     | 
    
         
            +
            test_files: []
         
     |