Class: Panoptes::Endpoints::BaseEndpoint

Inherits:
Object
  • Object
show all
Defined in:
lib/panoptes/endpoints/base_endpoint.rb

Direct Known Subclasses

JsonApiEndpoint, JsonEndpoint

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(auth: {}, url: nil, prefix: nil, params: nil) {|faraday| ... } ⇒ BaseEndpoint

Returns a new instance of BaseEndpoint.

Parameters:

  • auth (Hash<token: String, client_id: String, client_secret: String>) (defaults to: {})

    Authentication details

    • either nothing,

    • a hash with :token (an existing OAuth user token),

    • or a hash with :client_id and :client_secret (a keypair for an OAuth Application).

  • url (String) (defaults to: nil)

    API location to use.

  • prefix (String) (defaults to: nil)

    An optional API url prefix

Yields:

  • Allows an optional block to configure the faraday connection

Yield Parameters:

  • faraday (Faraday::Connection)

    The faraday connection



21
22
23
24
25
26
27
# File 'lib/panoptes/endpoints/base_endpoint.rb', line 21

def initialize(auth: {}, url: nil, prefix: nil, params: nil, &config)
  @auth = auth
  @url = url
  @prefix = prefix
  @config = config
  @params = params
end

Instance Attribute Details

#authObject (readonly)

Returns the value of attribute auth.



10
11
12
# File 'lib/panoptes/endpoints/base_endpoint.rb', line 10

def auth
  @auth
end

#paramsObject (readonly)

Returns the value of attribute params.



10
11
12
# File 'lib/panoptes/endpoints/base_endpoint.rb', line 10

def params
  @params
end

#prefixObject (readonly)

Returns the value of attribute prefix.



10
11
12
# File 'lib/panoptes/endpoints/base_endpoint.rb', line 10

def prefix
  @prefix
end

#urlObject (readonly)

Returns the value of attribute url.



10
11
12
# File 'lib/panoptes/endpoints/base_endpoint.rb', line 10

def url
  @url
end

Instance Method Details

#connectionObject



29
30
31
32
33
34
# File 'lib/panoptes/endpoints/base_endpoint.rb', line 29

def connection
  @connection ||= Faraday.new(url) do |faraday|
    auth_request faraday, auth
    configure faraday
  end
end

#delete(path, query = {}, etag: nil) ⇒ Object



52
53
54
# File 'lib/panoptes/endpoints/base_endpoint.rb', line 52

def delete(path, query = {}, etag: nil)
  request :delete, path, query, etag_header(etag)
end

#etag_header(etag) ⇒ Object



56
57
58
59
60
# File 'lib/panoptes/endpoints/base_endpoint.rb', line 56

def etag_header(etag)
  {}.tap do |headers|
    headers['If-Match'] = etag if etag
  end
end

#get(path, query = {}) ⇒ Object



36
37
38
# File 'lib/panoptes/endpoints/base_endpoint.rb', line 36

def get(path, query = {})
  request :get, path, query
end

#handle_response(response) ⇒ Object



70
71
72
73
74
75
76
77
78
79
# File 'lib/panoptes/endpoints/base_endpoint.rb', line 70

def handle_response(response)
  case response.status
  when 404
    raise Panoptes::Client::ResourceNotFound, status: response.status, body: response.body
  when 400..600
    raise Panoptes::Client::ServerError.new, response.body
  else
    response.body
  end
end

#patch(path, body = {}, etag: nil) ⇒ Object



48
49
50
# File 'lib/panoptes/endpoints/base_endpoint.rb', line 48

def patch(path, body = {}, etag: nil)
  request :patch, path, body, etag_header(etag)
end

#post(path, body = {}) ⇒ Object



40
41
42
# File 'lib/panoptes/endpoints/base_endpoint.rb', line 40

def post(path, body = {})
  request :post, path, body
end

#put(path, body = {}, etag: nil) ⇒ Object



44
45
46
# File 'lib/panoptes/endpoints/base_endpoint.rb', line 44

def put(path, body = {}, etag: nil)
  request :put, path, body, etag_header(etag)
end

#request(method, path, *args) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/panoptes/endpoints/base_endpoint.rb', line 62

def request(method, path, *args)
  if prefix
    sep = path[0] == '/' ? nil : '/'
    path = "#{prefix}#{sep}#{path}"
  end
  handle_response connection.send(method, path, *args)
end