Class: Apidiesel::Api

Inherits:
Object
  • Object
show all
Extended by:
Handlers
Defined in:
lib/apidiesel/api.rb

Overview

This is the abstract main interface class for the Apidiesel gem. It is meant to be inherited from:

module MyApi class Api < Apidiesel::Api end end

Apidiesel expects there to be an Actions namespace alongside the same scope, in which it can find the individual endpoint definitions for this API:

module MyApi class Api < Apidiesel::Api end

module Actions
  class Action1; end
  class Action2; end
end

end

# Registers endpoints Action1 and Action2 MyApi::Api.register_actions

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Handlers

exception_handlers, request_handlers, response_handlers, use

Constructor Details

#initialize(*args) ⇒ Api

Returns a new instance of Api.

Parameters:

  • *args (Hash)

101
102
103
# File 'lib/apidiesel/api.rb', line 101

def initialize(*args)
  @config = args.extract_options!.reverse_merge(self.class.config)
end

Class Method Details

.config(key = nil, value = nil) ⇒ Object


31
32
33
34
35
36
37
38
39
# File 'lib/apidiesel/api.rb', line 31

def config(key = nil, value = nil)
  @config ||= {}

  if key && value
    @config[key] = value
  else
    @config
  end
end

.http_basic_auth(username = nil, password = nil) ⇒ Object

Combined getter/setter for the HTTP Basic Auth

Falls back to the Api setting if blank.

Parameters:

  • username (String) (defaults to: nil)
  • password (String) (defaults to: nil)

73
74
75
76
77
78
79
80
# File 'lib/apidiesel/api.rb', line 73

def http_basic_auth(username = nil, password = nil)
  if username && password
    config[:http_basic_username] = username
    config[:http_basic_password] = password
  else
    return config[:http_basic_username], config[:http_basic_password]
  end
end

.http_method(value = nil) ⇒ Object

Combined getter/setter for the HTTP method used

Falls back to the Api setting if blank.

Parameters:

  • value (String) (defaults to: nil)

59
60
61
62
63
64
65
# File 'lib/apidiesel/api.rb', line 59

def http_method(value = nil)
  if value
    config[:http_method] = value
  else
    config[:http_method]
  end
end

.logger(logger = nil) ⇒ Object


91
92
93
94
95
96
97
# File 'lib/apidiesel/api.rb', line 91

def logger(logger = nil)
  if logger
    @logger = logger
  else
    @logger
  end
end

.register_actionsObject

Registers the individual API endpoint definitions


83
84
85
86
87
88
89
# File 'lib/apidiesel/api.rb', line 83

def register_actions
  namespace = "#{self.name.deconstantize}::Actions".safe_constantize

  namespace.constants.each do |action|
    namespace.const_get(action).register(self)
  end
end

.url(base_url = nil) ⇒ Object

Combined getter/setter for this actions URL

Falls back to the Api setting if blank.

Parameters:

  • base_url (String) (defaults to: nil)

46
47
48
49
50
51
52
# File 'lib/apidiesel/api.rb', line 46

def url(base_url = nil)
  if base_url
    config[:url] = URI.parse(base_url)
  else
    config[:url]
  end
end

Instance Method Details

#loggerObject


105
106
107
# File 'lib/apidiesel/api.rb', line 105

def logger
  self.class.logger
end