Class: Grepolis::Api::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/grepolis/api/connection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Connection

Returns a new instance of Connection.



8
9
10
11
# File 'lib/grepolis/api/connection.rb', line 8

def initialize(options = {})
  @world = options[:world]
  @protocol = options[:protocol] || 'http'
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args) ⇒ Object



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

def method_missing(meth, *args)
  begin
    "Grepolis::Api::#{meth.to_s.camelize}".constantize.new(self)
  rescue
    super
  end
end

Instance Attribute Details

#protocolObject

Returns the value of attribute protocol.



6
7
8
# File 'lib/grepolis/api/connection.rb', line 6

def protocol
  @protocol
end

#worldObject

Returns the value of attribute world.



6
7
8
# File 'lib/grepolis/api/connection.rb', line 6

def world
  @world
end

Instance Method Details

#request(type, mapping, response_handler = method(:default_response_handler)) ⇒ Object



17
18
19
20
21
22
23
24
25
26
# File 'lib/grepolis/api/connection.rb', line 17

def request(type, mapping, response_handler = method(:default_response_handler))
  conn = Faraday.new(:url => uri) do |faraday|
    faraday.response :logger                  # log requests to STDOUT
    faraday.adapter  Faraday.default_adapter  # make requests with Net::HTTP
  end

  response = conn.get("/data/#{type}.txt")

  response_handler.call response, mapping
end

#uriObject



13
14
15
# File 'lib/grepolis/api/connection.rb', line 13

def uri
  "#{protocol}://#{world}.grepolis.com"
end