Class: EveApp::EveCentral

Inherits:
Object
  • Object
show all
Defined in:
lib/eve_app/eve_central.rb

Defined Under Namespace

Classes: PriceResult

Constant Summary collapse

ENDPOINT =
'http://api.eve-central.com/api/marketstat/json'
MAX_TRIES =
3

Class Method Summary collapse

Class Method Details

.fetch(type_ids, system_id = EveApp::SolarSystem::JITA) ⇒ Object



9
10
11
12
13
14
15
16
# File 'lib/eve_app/eve_central.rb', line 9

def self.fetch(type_ids, system_id=EveApp::SolarSystem::JITA)
  type_ids = Array[type_ids].flatten
  results = []
  type_ids.in_groups_of(100, false) do |group|
    results += fetch_prices(group, system_id)
  end
  results
end

.fetch_prices(type_ids, system_id = EveApp::SolarSystem::JITA) ⇒ Object



18
19
20
21
22
# File 'lib/eve_app/eve_central.rb', line 18

def self.fetch_prices(type_ids, system_id=EveApp::SolarSystem::JITA)
  type_ids = Array[type_ids].flatten
  json = request(generate_url(ENDPOINT, type_ids, system_id))
  parse_response(json)
end

.generate_url(endpoint, type_ids, system_id, extra = {}) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'lib/eve_app/eve_central.rb', line 24

def self.generate_url(endpoint, type_ids, system_id, extra={})
  type_ids = Array[type_ids].flatten
  params = type_ids.map { |id| "typeid=#{id}" }
  params.push("usesystem=#{system_id}")
  extra.each do |key,val|
    params.push("#{key}=#{val}")
  end
  "#{endpoint}?#{params.join('&')}"
end

.parse_response(json) ⇒ Object



53
54
55
56
57
58
59
60
61
62
# File 'lib/eve_app/eve_central.rb', line 53

def self.parse_response(json)
  json.map { |row|
    OpenStruct.new(
      type_id:         row[:all][:forQuery][:types].first,
      solar_system_id: row[:all][:forQuery][:systems].first,
      buy:             PriceResult.new(:buy, row[:buy]),
      sell:            PriceResult.new(:sell, row[:sell])
    )
  }
end

.request(url) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/eve_app/eve_central.rb', line 34

def self.request(url)
  response = nil

  1.upto(MAX_TRIES) do |try|
    begin
      response = RestClient.get(url, user_agent: 'EveBuddy 0.1')
      break if response
    rescue StandardError => e
      puts "==================================================="
      puts "Error received #{e.class}: #{e.message}"
      puts url.to_s
      puts "==================================================="
      break
    end
  end

  MultiJson.load(response.body, symbolize_keys: true) rescue {}
end