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.



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

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



36
37
38
39
# File 'lib/endpoint.rb', line 36

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

#api_call(url) ⇒ Object



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

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



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

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



22
23
24
25
# File 'lib/endpoint.rb', line 22

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