eve_online 0.34.0 → 0.38.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,8 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "net/http"
4
- require "openssl"
5
- require "json"
3
+ require "faraday"
4
+ require "faraday_middleware"
6
5
  require "active_support/time"
7
6
 
8
7
  module EveOnline
@@ -10,23 +9,20 @@ module EveOnline
10
9
  class Base
11
10
  API_HOST = "esi.evetech.net"
12
11
 
13
- attr_reader :token, :parser, :_read_timeout, :_open_timeout, :_etag,
14
- :language
12
+ attr_reader :token, :_read_timeout, :_open_timeout, :_write_timeout,
13
+ :_etag, :language, :adapter, :middlewares
15
14
 
16
- if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("2.6.0")
17
- attr_reader :_write_timeout
18
- end
15
+ attr_writer :token
19
16
 
20
17
  def initialize(options = {})
21
18
  @token = options.fetch(:token, nil)
22
- @parser = options.fetch(:parser, JSON)
23
19
  @_read_timeout = options.fetch(:read_timeout, 60)
24
20
  @_open_timeout = options.fetch(:open_timeout, 60)
25
- if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("2.6.0")
26
- @_write_timeout = options.fetch(:write_timeout, 60)
27
- end
21
+ @_write_timeout = options.fetch(:write_timeout, 60)
28
22
  @_etag = options.fetch(:etag, nil)
29
23
  @language = options.fetch(:language, "en-us")
24
+ @adapter = options.fetch(:adapter, Faraday.default_adapter)
25
+ @middlewares = options.fetch(:middlewares, [])
30
26
  end
31
27
 
32
28
  def url
@@ -42,33 +38,31 @@ module EveOnline
42
38
  end
43
39
 
44
40
  def http_method
45
- "Get"
41
+ :get
46
42
  end
47
43
 
48
44
  def read_timeout
49
- client.read_timeout
45
+ connection.options.read_timeout
50
46
  end
51
47
 
52
48
  def read_timeout=(value)
53
- client.read_timeout = value
49
+ connection.options.read_timeout = value
54
50
  end
55
51
 
56
52
  def open_timeout
57
- client.open_timeout
53
+ connection.options.open_timeout
58
54
  end
59
55
 
60
56
  def open_timeout=(value)
61
- client.open_timeout = value
57
+ connection.options.open_timeout = value
62
58
  end
63
59
 
64
- if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("2.6.0")
65
- def write_timeout
66
- client.write_timeout
67
- end
60
+ def write_timeout
61
+ connection.options.write_timeout
62
+ end
68
63
 
69
- def write_timeout=(value)
70
- client.write_timeout = value
71
- end
64
+ def write_timeout=(value)
65
+ connection.options.write_timeout = value
72
66
  end
73
67
 
74
68
  def etag=(value)
@@ -76,56 +70,53 @@ module EveOnline
76
70
  end
77
71
 
78
72
  def etag
79
- resource.header["Etag"]&.gsub("W/", "")&.gsub('"', "")
73
+ resource.headers["etag"]&.gsub("W/", "")&.gsub('"', "")
80
74
  end
81
75
 
82
76
  def page
83
77
  end
84
78
 
85
79
  def total_pages
86
- resource.header["X-Pages"]&.to_i
80
+ resource.headers["x-pages"]&.to_i
87
81
  end
88
82
 
89
83
  def error_limit_remain
90
- resource.header["X-ESI-Error-Limit-Remain"]&.to_i
84
+ resource.headers["x-esi-error-limit-remain"]&.to_i
91
85
  end
92
86
 
93
87
  def error_limit_reset
94
- resource.header["X-ESI-Error-Limit-Reset"]&.to_i
88
+ resource.headers["x-esi-error-limit-reset"]&.to_i
95
89
  end
96
90
 
97
- def client
98
- @client ||= begin
99
- http = Net::HTTP.new(uri.host, uri.port)
100
- http.read_timeout = _read_timeout
101
- http.open_timeout = _open_timeout
102
- if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("2.6.0")
103
- http.write_timeout = _write_timeout
104
- end
105
- http.use_ssl = true
106
- http.verify_mode = OpenSSL::SSL::VERIFY_PEER
107
- # http.set_debug_output($stdout)
108
- http
109
- end
91
+ def request_id
92
+ resource.headers["x-esi-request-id"]
110
93
  end
111
94
 
112
- def request
113
- @request ||= begin
114
- request = "Net::HTTP::#{http_method}".constantize.new(uri.request_uri)
115
-
116
- request["User-Agent"] = user_agent
117
- request["Accept"] = "application/json"
118
- request["Authorization"] = "Bearer #{token}" if token
119
- request["If-None-Match"] = _etag if _etag
120
- request["Content-Type"] = "application/json" if http_method == "Post"
121
- request.body = payload if http_method == "Post"
122
-
123
- request
124
- end
95
+ def add_middleware(middleware)
96
+ @middlewares << middleware
125
97
  end
126
98
 
127
- if Gem::Version.new(RUBY_VERSION) < Gem::Version.new("2.6.0")
128
- class Net::WriteTimeout < StandardError; end
99
+ def connection
100
+ @connection ||= Faraday.new { |f|
101
+ f.headers["User-Agent"] = user_agent
102
+ f.headers["If-None-Match"] = _etag if _etag
103
+ f.authorization :Bearer, token if token
104
+ f.options.read_timeout = _read_timeout
105
+ f.options.open_timeout = _open_timeout
106
+ f.options.write_timeout = _write_timeout
107
+ f.use FaradayMiddlewares::RaiseErrors
108
+ middlewares.each do |middleware|
109
+ if middleware[:esi].present?
110
+ f.use middleware[:class], esi: middleware[:esi]
111
+ else
112
+ f.use middleware[:class]
113
+ end
114
+ end
115
+ # f.use Faraday::Response::Logger
116
+ # f.use FaradayMiddleware::FollowRedirects, limit: 5
117
+ f.response :json, content_type: "application/json"
118
+ f.adapter adapter
119
+ }
129
120
  end
130
121
 
131
122
  def uri
@@ -159,49 +150,25 @@ module EveOnline
159
150
  end
160
151
 
161
152
  def resource
162
- @resource ||= client.request(request)
163
- rescue Net::OpenTimeout, Net::ReadTimeout, Net::WriteTimeout
153
+ @resource ||= connection.public_send(http_method, uri)
154
+ rescue Faraday::ConnectionFailed, Faraday::TimeoutError
164
155
  raise EveOnline::Exceptions::Timeout
165
156
  end
166
157
 
167
158
  def not_modified?
168
- resource.is_a?(Net::HTTPNotModified)
159
+ resource.status == 304
169
160
  end
170
161
 
171
162
  def content
172
- case resource
173
- when Net::HTTPOK
174
- # TODO: memoize resource.body as @content
175
- resource.body
176
- when Net::HTTPCreated
177
- # TODO: write
178
- raise NotImplementedError
179
- when Net::HTTPNoContent
180
- raise EveOnline::Exceptions::NoContent
181
- when Net::HTTPNotModified
163
+ if not_modified?
182
164
  raise EveOnline::Exceptions::NotModified
183
- when Net::HTTPBadRequest
184
- raise EveOnline::Exceptions::BadRequest
185
- when Net::HTTPUnauthorized
186
- raise EveOnline::Exceptions::Unauthorized
187
- when Net::HTTPForbidden
188
- raise EveOnline::Exceptions::Forbidden
189
- when Net::HTTPNotFound
190
- raise EveOnline::Exceptions::ResourceNotFound
191
- when Net::HTTPInternalServerError
192
- raise EveOnline::Exceptions::InternalServerError
193
- when Net::HTTPBadGateway
194
- raise EveOnline::Exceptions::BadGateway
195
- when Net::HTTPServiceUnavailable
196
- raise EveOnline::Exceptions::ServiceUnavailable
197
165
  else
198
- # raise EveOnline::Exceptions::UnknownStatus
199
- raise NotImplementedError
166
+ resource.body
200
167
  end
201
168
  end
202
169
 
203
170
  def response
204
- @response ||= parser.parse(content)
171
+ @response ||= content
205
172
  end
206
173
 
207
174
  private
@@ -26,7 +26,7 @@ module EveOnline
26
26
  end
27
27
 
28
28
  def http_method
29
- "Post"
29
+ :post
30
30
  end
31
31
 
32
32
  def payload
@@ -26,7 +26,7 @@ module EveOnline
26
26
  end
27
27
 
28
28
  def http_method
29
- "Post"
29
+ :post
30
30
  end
31
31
 
32
32
  def payload
@@ -26,7 +26,7 @@ module EveOnline
26
26
  end
27
27
 
28
28
  def http_method
29
- "Post"
29
+ :post
30
30
  end
31
31
 
32
32
  def payload
@@ -26,7 +26,7 @@ module EveOnline
26
26
  end
27
27
 
28
28
  def http_method
29
- "Post"
29
+ :post
30
30
  end
31
31
 
32
32
  def payload
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module EveOnline
4
+ module ESI
5
+ class CorporationMembers < Base
6
+ API_PATH = "/v3/corporations/%<corporation_id>s/members/"
7
+
8
+ attr_reader :corporation_id
9
+
10
+ def initialize(options = {})
11
+ super
12
+
13
+ @corporation_id = options.fetch(:corporation_id)
14
+ end
15
+
16
+ def character_ids
17
+ response
18
+ end
19
+
20
+ def scope
21
+ "esi-corporations.read_corporation_membership.v1"
22
+ end
23
+
24
+ def path
25
+ format(API_PATH, corporation_id: corporation_id)
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "faraday"
4
+
5
+ module EveOnline
6
+ module ESI
7
+ module FaradayMiddlewares
8
+ class RaiseErrors < Faraday::Middleware
9
+ def call(env)
10
+ @app.call(env).on_complete do |environment|
11
+ on_complete(environment)
12
+ end
13
+ end
14
+
15
+ def on_complete(env)
16
+ case env[:status]
17
+ when 204
18
+ raise EveOnline::Exceptions::NoContent
19
+ when 400
20
+ raise EveOnline::Exceptions::BadRequest
21
+ when 401
22
+ raise EveOnline::Exceptions::Unauthorized
23
+ when 403
24
+ raise EveOnline::Exceptions::Forbidden
25
+ when 404
26
+ raise EveOnline::Exceptions::ResourceNotFound
27
+ when 420
28
+ raise EveOnline::Exceptions::ErrorLimited
29
+ when 500
30
+ raise EveOnline::Exceptions::InternalServerError
31
+ when 502
32
+ raise EveOnline::Exceptions::BadGateway
33
+ when 503
34
+ raise EveOnline::Exceptions::ServiceUnavailable
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,98 @@
1
+ # frozen_string_literal: true
2
+
3
+ module EveOnline
4
+ module ESI
5
+ module Models
6
+ class PublicContract < Base
7
+ def as_json
8
+ {
9
+ buyout: buyout,
10
+ collateral: collateral,
11
+ contract_id: contract_id,
12
+ date_expired: date_expired,
13
+ date_issued: date_issued,
14
+ days_to_complete: days_to_complete,
15
+ end_location_id: end_location_id,
16
+ for_corporation: for_corporation,
17
+ issuer_corporation_id: issuer_corporation_id,
18
+ issuer_id: issuer_id,
19
+ price: price,
20
+ reward: reward,
21
+ start_location_id: start_location_id,
22
+ title: title,
23
+ kind: kind,
24
+ volume: volume
25
+ }
26
+ end
27
+
28
+ def buyout
29
+ options["buyout"]
30
+ end
31
+
32
+ def collateral
33
+ options["collateral"]
34
+ end
35
+
36
+ def contract_id
37
+ options["contract_id"]
38
+ end
39
+
40
+ def date_expired
41
+ date_expired = options["date_expired"]
42
+
43
+ parse_datetime_with_timezone(date_expired) if date_expired
44
+ end
45
+
46
+ def date_issued
47
+ date_issued = options["date_issued"]
48
+
49
+ parse_datetime_with_timezone(date_issued) if date_issued
50
+ end
51
+
52
+ def days_to_complete
53
+ options["days_to_complete"]
54
+ end
55
+
56
+ def end_location_id
57
+ options["end_location_id"]
58
+ end
59
+
60
+ def for_corporation
61
+ options["for_corporation"]
62
+ end
63
+
64
+ def issuer_corporation_id
65
+ options["issuer_corporation_id"]
66
+ end
67
+
68
+ def issuer_id
69
+ options["issuer_id"]
70
+ end
71
+
72
+ def price
73
+ options["price"]
74
+ end
75
+
76
+ def reward
77
+ options["reward"]
78
+ end
79
+
80
+ def start_location_id
81
+ options["start_location_id"]
82
+ end
83
+
84
+ def title
85
+ options["title"]
86
+ end
87
+
88
+ def kind
89
+ options["type"]
90
+ end
91
+
92
+ def volume
93
+ options["volume"]
94
+ end
95
+ end
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,59 @@
1
+ # frozen_string_literal: true
2
+
3
+ module EveOnline
4
+ module ESI
5
+ module Models
6
+ class PublicContractItem < Base
7
+ def as_json
8
+ {
9
+ is_blueprint_copy: is_blueprint_copy,
10
+ is_included: is_included,
11
+ item_id: item_id,
12
+ material_efficiency: material_efficiency,
13
+ quantity: quantity,
14
+ record_id: record_id,
15
+ runs: runs,
16
+ time_efficiency: time_efficiency,
17
+ type_id: type_id
18
+ }
19
+ end
20
+
21
+ def is_blueprint_copy
22
+ options["is_blueprint_copy"]
23
+ end
24
+
25
+ def is_included
26
+ options["is_included"]
27
+ end
28
+
29
+ def item_id
30
+ options["item_id"]
31
+ end
32
+
33
+ def material_efficiency
34
+ options["material_efficiency"]
35
+ end
36
+
37
+ def quantity
38
+ options["quantity"]
39
+ end
40
+
41
+ def record_id
42
+ options["record_id"]
43
+ end
44
+
45
+ def runs
46
+ options["runs"]
47
+ end
48
+
49
+ def time_efficiency
50
+ options["time_efficiency"]
51
+ end
52
+
53
+ def type_id
54
+ options["type_id"]
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end