Class: GW2API::Endpoint

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

Direct Known Subclasses

BuildEndpoint, ItemsEndpoint, WorldsEndpoint

Instance Method Summary collapse

Constructor Details

#initializeEndpoint

Returns a new instance of Endpoint.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/endpoint.rb', line 7

def initialize
	# The URL of the API endpoint
	@url = GW2API::BASE_URL
	# Does this endpoint support ?id= and ?ids=?
	@bulk_expandable = false
	# Does this endpoint support ?ids=all?
	@bulk_expandable_all = false
	# Does this endpoint support ?page= and ?page_size=?
	@paginated = false
	# Does this endpoint support ?language=?
	@localized = false
	# Does this endpoint require an API key?
	@authenticated = false
	# Does this endpoint optionally allow an API key?
	@authenticated_optional = false
end

Instance Method Details

#allObject



38
39
40
41
# File 'lib/endpoint.rb', line 38

def all
	return nil unless @bulk_expandable_all
	api_call("#{@url}?ids=all")
end

#api_call(url) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/endpoint.rb', line 43

def api_call(url)
	request = Typhoeus::Request.new(url)
	request.on_complete do |resp|
		if resp.success?
			return JSON.parse(resp.body, object_class: OpenStruct)
		else
			return nil
		end
	end
	request.run
end

#get(id = nil) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/endpoint.rb', line 29

def get(id = nil)
	return nil if id.nil? && @bulk_expandable
	if id.kind_of?(Array)
		api_call("#{@url}?ids=#{id.join(',')}")
	else
		api_call("#{@url}/#{id}")
	end
end

#idsObject



24
25
26
27
# File 'lib/endpoint.rb', line 24

def ids
	return nil unless @bulk_expandable
	api_call "#{@url}?ids=all"
end