ruby-bare-esi 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 51d7424d3b3a6fff9715f55c174a88c5e3390e80929d733bca75adc5e82ee445
4
+ data.tar.gz: ea08d2ce56f66861f3d1a80b08079a7b117c223bd38910ccd774e8ffcf82fdee
5
+ SHA512:
6
+ metadata.gz: d5ffd99af5f2870b2af3bbe786f3b4560405c9b607293e6758e94f02e9ef1d5515278937810273dff50cb63a2a198cbda502e031cd6e567a852e404161205869
7
+ data.tar.gz: f13bcc881b65183579d05e2af4852572d0596d7c26fd2e4e1badaf3d1dfa0d475ea0614c43c76ef0a275e3c7d862a1caa4e70bc493a512f3bc1a8da375d9c8af
data/.gitignore ADDED
@@ -0,0 +1,58 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /spec/examples.txt
9
+ /test/tmp/
10
+ /test/version_tmp/
11
+ /tmp/
12
+
13
+ # Used by dotenv library to load environment variables.
14
+ # .env
15
+
16
+ # Ignore Byebug command history file.
17
+ .byebug_history
18
+
19
+ ## Specific to RubyMotion:
20
+ .dat*
21
+ .repl_history
22
+ build/
23
+ *.bridgesupport
24
+ build-iPhoneOS/
25
+ build-iPhoneSimulator/
26
+
27
+ ## Specific to RubyMotion (use of CocoaPods):
28
+ #
29
+ # We recommend against adding the Pods directory to your .gitignore. However
30
+ # you should judge for yourself, the pros and cons are mentioned at:
31
+ # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
32
+ #
33
+ # vendor/Pods/
34
+
35
+ ## Documentation cache and generated files:
36
+ /.yardoc/
37
+ /_yardoc/
38
+ /doc/
39
+ /rdoc/
40
+
41
+ ## Environment normalization:
42
+ /.bundle/
43
+ /vendor/bundle
44
+ /lib/bundler/man/
45
+
46
+ # for a library or gem, you might want to ignore these files since the code is
47
+ # intended to run in multiple environments; otherwise, check them in:
48
+ # Gemfile.lock
49
+ # .ruby-version
50
+ # .ruby-gemset
51
+
52
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
53
+ .rvmrc
54
+
55
+ # Used by RuboCop. Remote config files pulled in from inherit_from directive.
56
+ # .rubocop-https?--*
57
+
58
+ .idea
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.6.5
data/.travis.yml ADDED
@@ -0,0 +1,24 @@
1
+ #sudo: false
2
+
3
+ #before_install: gem install bundler -v 1.13.6
4
+
5
+ env:
6
+ global:
7
+ - CC_TEST_REPORTER_ID=53cb04febdb8d11846e501c5613f375017c0165929f26315da1b72be364d1c61
8
+
9
+ language: ruby
10
+ rvm:
11
+ - 2.6
12
+ - 2.5
13
+ - 2.4
14
+
15
+ before_script:
16
+ - curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
17
+ - chmod +x ./cc-test-reporter
18
+ - ./cc-test-reporter before-build
19
+
20
+ script:
21
+ - bundle exec rake test
22
+
23
+ after_script:
24
+ - ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT
data/Gemfile ADDED
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
6
+
7
+ # gem "rails"
8
+
9
+ gemspec
10
+
11
+ group :test do
12
+ gem 'codeclimate-test-reporter', '~> 1.0.0'
13
+ gem 'mocha'
14
+ gem 'simplecov'
15
+ gem 'minitest'
16
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,35 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ ruby-bare-esi (0.0.1)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ codeclimate-test-reporter (1.0.9)
10
+ simplecov (<= 0.13)
11
+ docile (1.1.5)
12
+ json (2.3.0)
13
+ minitest (5.14.0)
14
+ mocha (1.11.2)
15
+ rake (10.5.0)
16
+ simplecov (0.13.0)
17
+ docile (~> 1.1.0)
18
+ json (>= 1.8, < 3)
19
+ simplecov-html (~> 0.10.0)
20
+ simplecov-html (0.10.2)
21
+
22
+ PLATFORMS
23
+ ruby
24
+
25
+ DEPENDENCIES
26
+ bundler (~> 2.0.2)
27
+ codeclimate-test-reporter (~> 1.0.0)
28
+ minitest
29
+ mocha
30
+ rake (~> 10.0)
31
+ ruby-bare-esi!
32
+ simplecov
33
+
34
+ BUNDLED WITH
35
+ 2.0.2
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2020 Zuger Cédric
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.md ADDED
@@ -0,0 +1,6 @@
1
+ [![Build Status](https://travis-ci.org/czuger/ruby_bare_esi.svg?branch=master)](https://travis-ci.org/czuger/ruby_bare_esi)
2
+ [![Maintainability](https://api.codeclimate.com/v1/badges/aafe9a7f8a15c7c03746/maintainability)](https://codeclimate.com/github/czuger/ruby_bare_esi/maintainability)
3
+ [![Test Coverage](https://api.codeclimate.com/v1/badges/aafe9a7f8a15c7c03746/test_coverage)](https://codeclimate.com/github/czuger/ruby_bare_esi/test_coverage)
4
+
5
+ # Ruby_bare_esi
6
+ A really low level ruby ESI library
data/Rakefile ADDED
@@ -0,0 +1,5 @@
1
+ require 'rake/testtask'
2
+
3
+ Rake::TestTask.new do |t|
4
+ t.pattern = 'test/*_test.rb'
5
+ end
@@ -0,0 +1,6 @@
1
+ module EsiErrors
2
+ class BadGateway < Base
3
+ PAUSE_DURATION_VALUE=10
4
+ RETRY=true
5
+ end
6
+ end
@@ -0,0 +1,73 @@
1
+ require 'pp'
2
+
3
+ module EsiErrors
4
+
5
+ class EsiErrors::Base < RuntimeError
6
+
7
+ PAUSE_DURATION_VALUE=60
8
+ RETRY=false
9
+
10
+ def self.dispatch( exception, debug_mode: false )
11
+
12
+ # return EsiErrors::SocketError.new if exception.message =~ /SocketError/
13
+
14
+ puts "EsiErrors::Base got exception : #{exception.inspect}" if debug_mode
15
+
16
+ case exception.message
17
+
18
+ when '500 Internal Server Error'
19
+ error = EsiErrors::GatewayTimeout.new
20
+ when '504 Gateway Timeout', '504 Gateway Time-out'
21
+ error = EsiErrors::GatewayTimeout.new
22
+ when '502 Bad Gateway'
23
+ error = EsiErrors::BadGateway.new
24
+ when '403 Forbidden'
25
+ error = EsiErrors::Forbidden.new
26
+ when '420 status code 420'
27
+ error = EsiErrors::ErrorLimited.new
28
+ when '404 Not Found'
29
+ error = EsiErrors::NotFound.new
30
+ when 'Net::OpenTimeout'
31
+ error = EsiErrors::OpenTimeout.new
32
+ when 'SocketError'
33
+ error = EsiErrors::SocketError.new
34
+ when '503 Service Unavailable'
35
+ error = EsiErrors::ServiceUnavailable.new
36
+ when '520 status code 520'
37
+ error = EsiErrors::UnknownError.new
38
+ else
39
+ puts exception.full_message
40
+ # pp exception.backtrace_locations
41
+ raise 'Unhandled error'
42
+ end
43
+
44
+ if debug_mode
45
+ puts "EsiErrors::Base about to return : #{error.inspect}"
46
+ puts "EsiErrors::Base : retry = #{error.retry?}"
47
+ end
48
+
49
+ error
50
+ end
51
+
52
+ def retry?
53
+ self.class::RETRY
54
+ end
55
+
56
+ def pause( test_mode: false )
57
+ sleep( self.class::PAUSE_DURATION_VALUE ) unless test_mode
58
+ end
59
+
60
+ end
61
+
62
+ end
63
+
64
+ require_relative 'bad_gateway'
65
+ require_relative 'error_limited'
66
+ require_relative 'forbidden'
67
+ require_relative 'gateway_timeout'
68
+ require_relative 'internal_server_error'
69
+ require_relative 'not_found'
70
+ require_relative 'open_timeout'
71
+ require_relative 'service_unavailable'
72
+ require_relative 'socket_error'
73
+ require_relative 'unknown_error'
@@ -0,0 +1,4 @@
1
+ module EsiErrors
2
+ class ErrorLimited < Base
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module EsiErrors
2
+ class Forbidden < Base
3
+ end
4
+ end
@@ -0,0 +1,5 @@
1
+ module EsiErrors
2
+ class GatewayTimeout < Base
3
+ RETRY=true
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ module EsiErrors
2
+ class InternalServerError < Base
3
+ RETRY=true
4
+ end
5
+ end
@@ -0,0 +1,4 @@
1
+ module EsiErrors
2
+ class NotFound < Base
3
+ end
4
+ end
@@ -0,0 +1,5 @@
1
+ module EsiErrors
2
+ class OpenTimeout < Base
3
+ RETRY=true
4
+ end
5
+ end
@@ -0,0 +1,6 @@
1
+ module EsiErrors
2
+ class ServiceUnavailable < Base
3
+ PAUSE_DURATION_VALUE=120
4
+ RETRY=true
5
+ end
6
+ end
@@ -0,0 +1,5 @@
1
+ module EsiErrors
2
+ class SocketError < Base
3
+ RETRY=true
4
+ end
5
+ end
@@ -0,0 +1,6 @@
1
+ module EsiErrors
2
+ class UnknownError < Base
3
+ PAUSE_DURATION_VALUE=60*3
4
+ RETRY=true
5
+ end
6
+ end
@@ -0,0 +1,44 @@
1
+ module RubyBareEsiGetPages
2
+ module GetAllPages
3
+
4
+ # Get a single page. Doesn't check for remaining pages, in case of error fail.
5
+ #
6
+ # @param expect [String] used only for tests.
7
+ #
8
+ # @return [Hash] a hash containing the data you are requested. For data content see API.
9
+ def get_all_pages( expect: nil )
10
+ result = []
11
+ @params[:page] = 1
12
+
13
+ loop do
14
+ puts "RubyBareEsi.get_all_pages : requesting page #{@params[:page]}/#{@pages_count}" if @debug_mode
15
+
16
+ pages = get_page
17
+
18
+ unless pages.empty?
19
+ result += pages if pages.is_a? Array
20
+ result << pages if pages.is_a? Hash
21
+ else
22
+ puts 'RubyBareEsi.get_all_pages : page is empty' if @debug_mode
23
+ end
24
+
25
+ if @pages_count == 0 || @pages_count == 1
26
+ puts 'RubyBareEsi.get_all_pages : no other pages to download - breaking out' if @debug_mode
27
+ break
28
+ else
29
+ puts "RubyBareEsi.get_all_pages : More pages to download : #{@pages_count}" if @debug_mode
30
+ @params[:page] += 1
31
+ end
32
+
33
+ if @params[:page] && @params[:page] > @pages_count
34
+ puts 'RubyBareEsi.get_all_pages : No more pages to download - breaking out' if @debug_mode
35
+ @params.delete(:page)
36
+ break
37
+ end
38
+ end
39
+
40
+ result
41
+ end
42
+
43
+ end
44
+ end
@@ -0,0 +1,54 @@
1
+ module RubyBareEsiGetPages
2
+ module GetPage
3
+
4
+ # Get a single page. Doesn't check for remaining pages, in case of error fail.
5
+ #
6
+ # @param page_number [Int] the number of the pages you are requesting, if there are more pages you need to get (default the first).
7
+ #
8
+ # @return [Hash] a hash containing the data you are requested. For data content see API.
9
+ def get_page( page_number=nil )
10
+ @params[:page] = page_number if page_number
11
+ url = self.build_url
12
+ puts "Fetching : #{url}" if @debug_mode
13
+
14
+ parsed_result = nil
15
+
16
+ begin
17
+ @request = open( url )
18
+
19
+ set_headers
20
+
21
+ json_result = @request.read
22
+ parsed_result = JSON.parse( json_result )
23
+
24
+ rescue JSON::ParserError => parse_error
25
+ warn 'Got parse error !!!' unless @test_mode
26
+ raise parse_error
27
+
28
+ rescue => e
29
+ error = EsiErrors::Base.dispatch( e, debug_mode: @debug_mode )
30
+ error_print( error )
31
+
32
+ raise error
33
+ end
34
+
35
+ parsed_result
36
+ end
37
+
38
+ protected
39
+
40
+ def set_headers
41
+ puts "RubyBareEsi.set_headers : request = #{@request}" if @debug_mode
42
+
43
+ @pages_count = @request.meta['x-pages'].to_i
44
+ @errors_limit_remain = @request.meta['x-esi-error-limit-remain']
45
+ @errors_limit_reset = @request.meta['x-esi-error-limit-reset']
46
+ end
47
+
48
+ def build_url
49
+ url = ( @rest_url + '?' + @params.map{ |k, v| "#{k}=#{v}" }.join( '&' ) ).gsub( '//', '/' )
50
+ self.class::ESI_BASE_URL + url
51
+ end
52
+
53
+ end
54
+ end
@@ -0,0 +1,40 @@
1
+ module RubyBareEsiGetPages
2
+ module GetPageRetryOnError
3
+
4
+ # Get a single page. Doesn't check for remaining pages, in case of error retry according to Error class retry parameters.
5
+ #
6
+ # @param page_number [Int] the number of the pages you are requesting, if there are more pages you need to get (default the first).
7
+ #
8
+ # @return [Hash] a hash containing the data you are requested. For data content see API.
9
+ def get_page_retry_on_error( page_number=nil )
10
+ parsed_result = nil
11
+ retry_count = 0
12
+
13
+ loop do
14
+ begin
15
+ parsed_result = get_page( page_number )
16
+ break
17
+
18
+ rescue EsiErrors::Base => error
19
+ puts "RubyBareEsiGetPages::GetPageRetryOnError.get_page_retry_on_error : retry = #{error.retry?}" if @debug_mode
20
+
21
+ if error.retry?
22
+
23
+ retry_count += 1
24
+ if retry_count >= 20
25
+ raise 'Retry count exceeded.'
26
+ end
27
+
28
+ error.pause( test_mode: @test_mode )
29
+ next
30
+ else
31
+ raise error
32
+ end
33
+ end
34
+ end
35
+
36
+ parsed_result
37
+ end
38
+
39
+ end
40
+ end
@@ -0,0 +1,24 @@
1
+ require 'ostruct'
2
+
3
+ module Esi
4
+
5
+ class GetCharacterInfo < Download
6
+
7
+ def initialize
8
+ super
9
+ @character_infos = {}
10
+ end
11
+
12
+ def get( character_id )
13
+ unless @character_infos[ character_id ]
14
+ @rest_url = "characters/#{character_id}/"
15
+ character_data = get_page_retry_on_error
16
+ @character_infos[character_id] = OpenStruct.new( character_data )
17
+ end
18
+
19
+ @character_infos[character_id]
20
+ end
21
+
22
+ end
23
+
24
+ end
@@ -0,0 +1,24 @@
1
+ require 'ostruct'
2
+
3
+ module Esi
4
+
5
+ class GetCorporationInfo < Download
6
+
7
+ def initialize
8
+ super
9
+ @corporation_infos = {}
10
+ end
11
+
12
+ def get( corporation_id )
13
+ unless @corporation_infos[ corporation_id ]
14
+ @rest_url = "corporations/#{corporation_id}/"
15
+ corporation_data = get_page_retry_on_error
16
+ @corporation_infos[corporation_id] = OpenStruct.new( corporation_data )
17
+ end
18
+
19
+ @corporation_infos[corporation_id]
20
+ end
21
+
22
+ end
23
+
24
+ end
@@ -0,0 +1,15 @@
1
+ module Esi
2
+
3
+ class GetFactions < Download
4
+
5
+ def initialize
6
+ super( 'universe/factions/', {} )
7
+ end
8
+
9
+ def get_ids
10
+ get_page.map{ |f| f['faction_id'] }
11
+ end
12
+
13
+ end
14
+
15
+ end
@@ -0,0 +1,15 @@
1
+ module Esi
2
+
3
+ class GetKillmail < Download
4
+
5
+ def initialize( killmail_id, killmail_hash )
6
+ super( "killmails/#{killmail_id}/#{killmail_hash}/", {} )
7
+ end
8
+
9
+ def get
10
+ get_page_retry_on_error
11
+ end
12
+
13
+ end
14
+
15
+ end
@@ -0,0 +1,28 @@
1
+ require 'ostruct'
2
+
3
+ module Esi
4
+
5
+ class GetSystemInfo < Download
6
+
7
+ def initialize
8
+ super
9
+ @system_infos = {}
10
+ end
11
+
12
+ def get( system_id )
13
+ unless @system_infos[ system_id ]
14
+ @rest_url = "universe/systems/#{system_id}/"
15
+
16
+ # p @rest_url
17
+ system_data = get_page_retry_on_error
18
+
19
+ # p system_data
20
+ @system_infos[system_id] = OpenStruct.new( system_data )
21
+ end
22
+
23
+ @system_infos[system_id]
24
+ end
25
+
26
+ end
27
+
28
+ end
@@ -0,0 +1,99 @@
1
+ require 'open-uri'
2
+ require 'json'
3
+
4
+ require_relative 'errors/base'
5
+
6
+ require_relative 'get_pages/get_page'
7
+ require_relative 'get_pages/get_page_retry_on_error'
8
+ require_relative 'get_pages/get_all_pages'
9
+
10
+ # This class is the entry point for all method allowing data retrieval from the ESI API
11
+ #
12
+ # @author Cédric ZUGER - 2020
13
+ class RubyBareEsi
14
+
15
+ include RubyBareEsiGetPages::GetPage
16
+ include RubyBareEsiGetPages::GetPageRetryOnError
17
+ include RubyBareEsiGetPages::GetAllPages
18
+
19
+ # This is the default address of the ESI API
20
+ ESI_BASE_URL='https://esi.evetech.net/latest/'
21
+ # And the default server used for the requests
22
+ ESI_DATA_SOURCE={ datasource: :tranquility }
23
+
24
+ # This initialize an RubyBareEsi download object.
25
+ #
26
+ # @param rest_url [String] the path of the method to access ESI (the exact path you would send to the API).
27
+ # @param params [Hash] the params if required.
28
+ # @param test_mode [Boolean] turns on or off test_mode. test_mode is off by default. It is turned on only during
29
+ # tests to turn off some errors warnings and to reduce sleep time in case of automatic retry.
30
+ # @param debug_mode [Boolean] turns on debugging if required. This also turn on verbose_mode.
31
+ def initialize( rest_url = nil, params = {}, test_mode: false, debug_mode: false )
32
+
33
+ raise "RubyBareEsi.initialize : rest_url can't be nil" unless rest_url
34
+
35
+ @debug_mode = debug_mode || ENV['EBS_DEBUG_MODE'] == 'true'
36
+ @test_mode = test_mode
37
+
38
+ puts 'RubyBareEsi.initialize : debug mode on' if @debug_mode
39
+
40
+ @rest_url = rest_url
41
+ @params = params.merge( ESI_DATA_SOURCE )
42
+ @forbidden_count = 0
43
+ end
44
+
45
+ def set_auth_token( user=nil )
46
+
47
+ # p user
48
+
49
+ return false unless user.expires_on && user.token && user.renew_token
50
+
51
+ unless user
52
+ user_id = File.open( 'config/character_id.txt' ).read.to_i
53
+ user = User.find_by_uid( user_id )
54
+ end
55
+
56
+ if user.expires_on < Time.now().utc
57
+ puts "Token expired - #{user.expires_on} < #{Time.now().utc}"
58
+ renew_token( user )
59
+ end
60
+
61
+ @params[:token] = user.token
62
+
63
+ true
64
+ end
65
+
66
+ private
67
+
68
+ def renew_token( user )
69
+ client_id = secret_key = nil
70
+ if File.exists?( 'config/omniauth.yaml' )
71
+ results = YAML.load( File.open( 'config/omniauth.yaml' ).read )
72
+
73
+ if results && results[:esi]
74
+ client_id, secret_key = results[:esi]
75
+ end
76
+ end
77
+
78
+ auth64 = Base64.strict_encode64( "#{client_id}:#{secret_key}" )
79
+ auth_string = "Basic #{auth64}"
80
+
81
+ RestClient.log = 'stdout' if @debug_mode
82
+
83
+ c = RestClient.post 'https://login.eveonline.com/oauth/token',
84
+ { grant_type: :refresh_token, refresh_token: user.renew_token },
85
+ { 'Authorization' => auth_string }
86
+ response = JSON.parse( c.body )
87
+
88
+ user.update!( token: response['access_token'], expires_on: Time.now() + response['expires_in'] )
89
+ end
90
+
91
+ def error_print( e )
92
+ unless @test_mode
93
+ warn "#{Time.now} - Requesting #{@rest_url}, #{@params.inspect} got '#{e}', limit_remains = #{@errors_limit_remain}, limit_reset = #{@errors_limit_reset}"
94
+ end
95
+
96
+ STDOUT.flush
97
+ end
98
+
99
+ end
@@ -0,0 +1,3 @@
1
+ class RubyBareEsi
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,34 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'ruby_bare_esi/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+
8
+ spec.name = 'ruby-bare-esi'
9
+ spec.version = RubyBareEsi::VERSION
10
+ spec.authors = ['Cédric ZUGER']
11
+ spec.email = ['zuger.cedric@gmail.com']
12
+
13
+ spec.summary = 'Very low level ESI access library for ruby'
14
+ spec.description = 'This library is designed to provide a bare ESI access in ruby. No classes encapsulation,
15
+ just a way to'
16
+ spec.homepage = 'https://github.com/czuger/ruby-bare-esi'
17
+ spec.license = 'MIT'
18
+
19
+ spec.files = `git ls-files`.split($/).reject{ |e| e =~ /bin/ }
20
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
21
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
22
+ spec.require_paths = ['lib']
23
+
24
+ spec.bindir = 'exe'
25
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
26
+ spec.require_paths = ['lib']
27
+
28
+ spec.add_development_dependency 'bundler', '~> 2.0.2'
29
+ spec.add_development_dependency 'rake', '~> 10.0'
30
+ spec.add_development_dependency 'minitest', '~> 5.0'
31
+
32
+ spec.required_ruby_version = '>= 2.4'
33
+
34
+ end
@@ -0,0 +1,38 @@
1
+ require_relative 'test_helper'
2
+
3
+ class ErrorsTest < Minitest::Test
4
+
5
+ TESTS_TO_DO = [
6
+ [ 20, '500 Internal Server Error', RuntimeError, 'Retry count exceeded.'],
7
+ [ 20, '504 Gateway Timeout', RuntimeError, 'Retry count exceeded.'],
8
+ [ 20, '504 Gateway Time-out', RuntimeError, 'Retry count exceeded.'],
9
+ [ 20, '502 Bad Gateway', RuntimeError, 'Retry count exceeded.'],
10
+ [ 1, '403 Forbidden', EsiErrors::Forbidden, nil],
11
+ [ 1, '420 status code 420', EsiErrors::ErrorLimited, nil],
12
+ [ 1, '404 Not Found', EsiErrors::NotFound, nil],
13
+ [ 20, 'Net::OpenTimeout', RuntimeError, 'Retry count exceeded.'],
14
+ [ 20, 'SocketError', RuntimeError, 'Retry count exceeded.'],
15
+ [ 20, '503 Service Unavailable', RuntimeError, 'Retry count exceeded.'],
16
+ [ 20, '520 status code 520', RuntimeError, 'Retry count exceeded.'],
17
+ ]
18
+
19
+ def test_all_errors
20
+ TESTS_TO_DO.each do |test|
21
+ # p test
22
+
23
+ expect_times, error_message, error_klass, exception_message = test
24
+
25
+ re = RubyBareEsi.new( 'characters/1/', test_mode: true, debug_mode: false )
26
+
27
+ re.expects(:open).times(expect_times).returns(
28
+ EsiFakeRequest.new( error_message: error_message ) )
29
+
30
+ exception = assert_raises error_klass do
31
+ re.get_page_retry_on_error
32
+ end
33
+
34
+ assert_equal(exception_message, exception.message) if exception_message
35
+ end
36
+ end
37
+
38
+ end
@@ -0,0 +1,30 @@
1
+ require_relative 'test_helper'
2
+
3
+ class GetAllPagesTest < Minitest::Test
4
+
5
+ def setup
6
+ @page = EsiFakeRequest.new
7
+ @re = RubyBareEsi.new( 'characters/1/', test_mode: true, debug_mode: false )
8
+ end
9
+
10
+ def test_get_all_pages_with_hash_input
11
+ @re.expects(:open).returns( EsiFakeRequest.new( read_data: '{ "foo": "bar" }') )
12
+ assert_equal([{ 'foo' => 'bar' }], @re.get_all_pages )
13
+ end
14
+
15
+ def test_get_all_pages_with_array_input
16
+ @re.expects(:open).returns( EsiFakeRequest.new( read_data: '[ "foo", "bar" ]') )
17
+ assert_equal(%w( foo bar ), @re.get_all_pages )
18
+ end
19
+
20
+ def test_get_all_pages_with_hash_input_with_multiple_pages
21
+ @re = RubyBareEsi.new( 'characters/1/', test_mode: true, debug_mode: false )
22
+
23
+ @re.expects(:open).times(2).returns(
24
+ EsiFakeRequest.new( meta: { 'x-pages' => 2 }, read_data: '{ "foo": "bar" }') )
25
+
26
+ assert_equal([{ 'foo' => 'bar' }, { 'foo' => 'bar' }], @re.get_all_pages )
27
+ end
28
+
29
+
30
+ end
@@ -0,0 +1,36 @@
1
+ require_relative 'test_helper'
2
+
3
+ class GetPageRetryOnErrorTest < Minitest::Test
4
+
5
+ def setup
6
+ @page = EsiFakeRequest.new
7
+ @re = RubyBareEsi.new( 'characters/1/', test_mode: true, debug_mode: false )
8
+ end
9
+
10
+ def test_get_page_retry_on_error_single_step
11
+ @re.expects(:open).returns( @page )
12
+ assert_equal( {}, @re.get_page_retry_on_error )
13
+ end
14
+
15
+ def test_get_page_with_json_parse_error
16
+ @re.expects(:open).returns( EsiFakeRequest.new( read_data: '{:}') )
17
+ assert_raises JSON::ParserError do
18
+ @re.get_page_retry_on_error
19
+ end
20
+ end
21
+
22
+ def test_get_page_with_internal_server_error
23
+ @re.expects(:open).times(20).returns( EsiFakeRequest.new( error_message: '500 Internal Server Error' ) )
24
+ assert_raises RuntimeError do
25
+ @re.get_page_retry_on_error
26
+ end
27
+ end
28
+
29
+ def test_get_page_with_not_found_error
30
+ @re.expects(:open).returns( EsiFakeRequest.new( error_message: '404 Not Found' ) )
31
+ assert_raises EsiErrors::NotFound do
32
+ @re.get_page_retry_on_error
33
+ end
34
+ end
35
+
36
+ end
@@ -0,0 +1,35 @@
1
+ require_relative 'test_helper'
2
+
3
+ class GetPageTest < Minitest::Test
4
+
5
+ def setup
6
+ @page = EsiFakeRequest.new
7
+ @re = RubyBareEsi.new( 'characters/1/', test_mode: true )
8
+ end
9
+
10
+ def test_rest_url_mandatory
11
+ assert_raises do
12
+ RubyBareEsi.new
13
+ end
14
+ end
15
+
16
+ def test_get_page
17
+ @re.expects(:open).returns( @page )
18
+ assert_equal( {}, @re.get_page )
19
+ end
20
+
21
+ def test_get_page_with_json_parse_error
22
+ @re.expects(:open).returns( EsiFakeRequest.new( read_data: '{:}') )
23
+ assert_raises JSON::ParserError do
24
+ @re.get_page
25
+ end
26
+ end
27
+
28
+ def test_get_page_with_internal_server_error
29
+ @re.expects(:open).returns( EsiFakeRequest.new( error_message: '500 Internal Server Error' ) )
30
+ assert_raises EsiErrors::GatewayTimeout do
31
+ @re.get_page
32
+ end
33
+ end
34
+
35
+ end
@@ -0,0 +1,32 @@
1
+ require 'simplecov'
2
+ SimpleCov.start
3
+
4
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
5
+
6
+ require_relative '../lib/ruby_bare_esi'
7
+
8
+ require 'minitest/autorun'
9
+ require 'mocha/minitest'
10
+ require 'pp'
11
+ require 'ostruct'
12
+ require 'json'
13
+
14
+ class EsiFakeRequest
15
+
16
+ DEFAULT_META = { 'x-pages' => 1, 'x-esi-error-limit-remain' => 100, 'x-esi-error-limit-reset' => 100 }
17
+
18
+ attr_reader :meta
19
+
20
+ def initialize( read_data: '{}', meta: {}, error_message: nil )
21
+ @read_data = read_data
22
+ @meta = DEFAULT_META.clone
23
+ @meta.merge!( meta )
24
+ @error_message = error_message
25
+ end
26
+
27
+ def read
28
+ raise @error_message if @error_message
29
+ @read_data
30
+ end
31
+
32
+ end
metadata ADDED
@@ -0,0 +1,127 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby-bare-esi
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Cédric ZUGER
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-01-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 2.0.2
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 2.0.2
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ description: |-
56
+ This library is designed to provide a bare ESI access in ruby. No classes encapsulation,
57
+ just a way to
58
+ email:
59
+ - zuger.cedric@gmail.com
60
+ executables: []
61
+ extensions: []
62
+ extra_rdoc_files: []
63
+ files:
64
+ - ".gitignore"
65
+ - ".ruby-version"
66
+ - ".travis.yml"
67
+ - Gemfile
68
+ - Gemfile.lock
69
+ - LICENSE
70
+ - README.md
71
+ - Rakefile
72
+ - lib/errors/bad_gateway.rb
73
+ - lib/errors/base.rb
74
+ - lib/errors/error_limited.rb
75
+ - lib/errors/forbidden.rb
76
+ - lib/errors/gateway_timeout.rb
77
+ - lib/errors/internal_server_error.rb
78
+ - lib/errors/not_found.rb
79
+ - lib/errors/open_timeout.rb
80
+ - lib/errors/service_unavailable.rb
81
+ - lib/errors/socket_error.rb
82
+ - lib/errors/unknown_error.rb
83
+ - lib/get_pages/get_all_pages.rb
84
+ - lib/get_pages/get_page.rb
85
+ - lib/get_pages/get_page_retry_on_error.rb
86
+ - lib/gets/get_character_info.rb
87
+ - lib/gets/get_corporation_info.rb
88
+ - lib/gets/get_factions.rb
89
+ - lib/gets/get_killmail.rb
90
+ - lib/gets/get_system_info.rb
91
+ - lib/ruby_bare_esi.rb
92
+ - lib/ruby_bare_esi/version.rb
93
+ - ruby_bare_esi.gemspec
94
+ - test/errors_test.rb
95
+ - test/get_all_pages_test.rb
96
+ - test/get_page_retry_on_error_test.rb
97
+ - test/get_page_test.rb
98
+ - test/test_helper.rb
99
+ homepage: https://github.com/czuger/ruby-bare-esi
100
+ licenses:
101
+ - MIT
102
+ metadata: {}
103
+ post_install_message:
104
+ rdoc_options: []
105
+ require_paths:
106
+ - lib
107
+ required_ruby_version: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ version: '2.4'
112
+ required_rubygems_version: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ requirements: []
118
+ rubygems_version: 3.0.3
119
+ signing_key:
120
+ specification_version: 4
121
+ summary: Very low level ESI access library for ruby
122
+ test_files:
123
+ - test/errors_test.rb
124
+ - test/get_all_pages_test.rb
125
+ - test/get_page_retry_on_error_test.rb
126
+ - test/get_page_test.rb
127
+ - test/test_helper.rb