Class: NewEden::Connection

Inherits:
Object
  • Object
show all
Includes:
Account, Api, Character, Corporation, Errors, Eve, Image, Map, Server
Defined in:
lib/neweden/connection.rb

Constant Summary collapse

REQUEST_TIMEOUT =

60 seconds

60000
CACHE_TIMEOUT =

5 minutes

300

Constants included from Map

Map::MAP_ENDPOINTS

Constants included from Image

Image::IMAGE_ENDPOINTS

Constants included from Eve

Eve::EVE_ENDPOINTS

Constants included from Corporation

NewEden::Corporation::CORPORATION_ENDPOINTS

Constants included from Character

NewEden::Character::CHARACTER_ENDPOINTS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Server

#server_status

Methods included from Eve

#character_ids, #character_info, #character_names

Methods included from Character

#all_notification_texts, #calendar_event_attendees, #notification_texts, #upcoming_calendar_events

Methods included from Api

#call_list

Methods included from Account

#account_status, #api_key_info, #characters

Constructor Details

#initialize(key_id, vcode, use_test = false) ⇒ Connection

Returns a new instance of Connection.



18
19
20
21
22
23
24
25
# File 'lib/neweden/connection.rb', line 18

def initialize(key_id, vcode, use_test = false)
  @key_id         = key_id
  @vcode          = vcode
  @game_server    = "api.eveonline.com".freeze
  @test_server    = "apitest.eveonline.com".freeze
  @server = use_test ? @test_server : @game_server
  @hydra = Typhoeus::Hydra.new
end

Instance Attribute Details

#game_serverObject (readonly)

Returns the value of attribute game_server.



16
17
18
# File 'lib/neweden/connection.rb', line 16

def game_server
  @game_server
end

#hydraObject (readonly)

Returns the value of attribute hydra.



16
17
18
# File 'lib/neweden/connection.rb', line 16

def hydra
  @hydra
end

#key_idObject (readonly)

Returns the value of attribute key_id.



16
17
18
# File 'lib/neweden/connection.rb', line 16

def key_id
  @key_id
end

#serverObject (readonly)

Returns the value of attribute server.



16
17
18
# File 'lib/neweden/connection.rb', line 16

def server
  @server
end

#test_serverObject (readonly)

Returns the value of attribute test_server.



16
17
18
# File 'lib/neweden/connection.rb', line 16

def test_server
  @test_server
end

#vcodeObject (readonly)

Returns the value of attribute vcode.



16
17
18
# File 'lib/neweden/connection.rb', line 16

def vcode
  @vcode
end

Instance Method Details

#handle_response(response) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/neweden/connection.rb', line 50

def handle_response(response)
  if response.timed_out?
    raise TimeoutError, "Response timed out."
  elsif response.code == 0
    raise NoResponseError, "No response received."
  elsif response.code == 404
    raise NotFoundError, "API endpoint not found."
  elsif !response.success?
    raise UnsuccessfulResponseError, "Received HTTP response: #{response.code.to_s}"
  end

  xml = Nokogiri::XML.parse(response.body)

  unless (xml/:eveapi/:error).empty?
    error_code = (xml/:error).attribute('code').value.to_i
    case error_code
    when 203
      raise AuthenticationError, "Invalid key id or vcode."
    when 124, 125
      raise NotInvolvedInFactionalWarfare, "Not involved in factional warfare."
    else
      raise ApiError, (xml/:eveapi/:error).children.map { |e| e.to_s.gsub(/\.$/, '') }.join(", ")
    end
  end

  xml
end

#raw_request(endpoint, method = :post, params = {}) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/neweden/connection.rb', line 36

def raw_request(endpoint, method = :post, params = {})
  url = "https://#{@server}/#{sanitize_endpoint(endpoint)}"
  case method
  when :get
    Typhoeus::Request.get(url, request_params(params))
  when :put
    Typhoeus::Request.put(url, request_params(params))
  when :delete
    Typhoeus::Request.delete(url, request_params(params))
  else
    Typhoeus::Request.post(url, request_params(params))
  end
end

#request(endpoint, method = :post, params = {}) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/neweden/connection.rb', line 27

def request(endpoint, method = :post, params = {})
  xml = handle_response(raw_request(endpoint, method, params))
  if xml
    Hash.from_xml((xml/:eveapi/:result).to_s)[:result] # rescue raise XMLParsingError, "No result in set"
  else
    raise XMLParsingError, "No XML parsed successfully"
  end
end

#request_params(params = {}) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/neweden/connection.rb', line 90

def request_params(params = {})
  if @key_id.nil? || @vcode.nil?
    {
      :headers => { :Accept => "application/xml" },
      :timeout => REQUEST_TIMEOUT
    }
  else
    {
      :headers => { :Accept => "application/xml" },
      :timeout => REQUEST_TIMEOUT,
      :params => params.merge({ :keyID => @key_id, :vCode => @vcode })
    }
  end
end

#request_urlObject



86
87
88
# File 'lib/neweden/connection.rb', line 86

def request_url
  "https://#{@server}/#{sanitize_endpoint(endpoint)}"
end

#use_game_serverObject



78
79
80
# File 'lib/neweden/connection.rb', line 78

def use_game_server
  @server = @game_server
end

#use_test_serverObject



82
83
84
# File 'lib/neweden/connection.rb', line 82

def use_test_server
  @server = @test_server
end