Module: ApiRecipes

Defined in:
lib/api_recipes.rb,
lib/api_recipes/api.rb,
lib/api_recipes/route.rb,
lib/api_recipes/version.rb,
lib/api_recipes/endpoint.rb,
lib/api_recipes/response.rb,
lib/api_recipes/settings.rb,
lib/api_recipes/exceptions.rb,
lib/api_recipes/configuration.rb

Overview

TODO: Sistema i default nelle config

Defined Under Namespace

Modules: Settings Classes: Api, ApiConfigIsNotAnHash, ApiNameClashError, Configuration, Endpoint, MissingRouteAttribute, NoConfigurationGivenForEndpoint, NoRouteExists, PathParamsMismatch, ProvidedObjectNotAsResponseData, Response, ResponseCodeNotAsExpected, Route, RouteAndResourceNamesClashError, RouteNameClashWithExistentMethod

Constant Summary collapse

VERSION =
'2.10.0'.freeze

Class Method Summary collapse

Class Method Details

._aprcps_define_global_apisObject



107
108
109
110
111
112
113
114
115
# File 'lib/api_recipes.rb', line 107

def self._aprcps_define_global_apis
  configuration.apis_configs.each do |api_name, api_configs|
    api_name = api_name.to_sym
    _aprcps_global_storage[api_name] = Api.new api_name, api_configs, self
    define_singleton_method api_name do
      _aprcps_global_storage[api_name]
    end
  end
end

._aprcps_global_storageObject



117
118
119
120
121
122
123
# File 'lib/api_recipes.rb', line 117

def self._aprcps_global_storage
  unless @storage
    @storage = {}
  end

  @storage
end

._aprcps_merge_apis_configs(api_name, configs = nil) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/api_recipes.rb', line 132

def self._aprcps_merge_apis_configs(api_name, configs = nil)
  unless api_name.is_a?(String) || api_name.is_a?(Symbol)
    raise ArgumentError, "no api_name provided. Given: #{api_name.inspect}"
  end
  global_api_configs = _aprcps_global_storage[api_name]&.configs || {}
  if configs
    global_api_configs.deep_merge(configs) do |_, old_val, new_val|
      if new_val.nil?
        old_val
      else
        new_val
      end
    end
  else
    global_api_configs
  end
end

._aprcps_thread_storageObject



125
126
127
128
129
130
# File 'lib/api_recipes.rb', line 125

def self._aprcps_thread_storage
  unless Thread.current[:api_recipes]
    Thread.current[:api_recipes] = {}
  end
  Thread.current[:api_recipes]
end

.configurationObject



57
58
59
60
61
62
63
# File 'lib/api_recipes.rb', line 57

def self.configuration
  unless @configuration
    @configuration = Configuration.new
  end

  @configuration
end

.configureObject



65
66
67
68
69
70
71
72
# File 'lib/api_recipes.rb', line 65

def self.configure
  if block_given?
    yield(configuration)
    configuration.setup
  else
    configuration
  end
end

.copy_global_authorizations_to_api(api) ⇒ Object



74
75
76
77
78
79
80
81
82
83
# File 'lib/api_recipes.rb', line 74

def self.copy_global_authorizations_to_api(api)
  if _aprcps_global_storage[api.name]
    if auth = _aprcps_global_storage[api.name].basic_auth
      api.basic_auth = auth
    end
    if auth = _aprcps_global_storage[api.name].authorization
      api.authorization = auth
    end
  end
end

.included(receiver) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/api_recipes.rb', line 16

def self.included(receiver)

  def receiver.api(api_name, configs = {})
    unless api_name.is_a?(String) || api_name.is_a?(Symbol)
      raise ArgumentError, "api name must be a Symbol or String"
    end

    if configs && !configs.is_a?(Hash)
      raise ApiRecipes::ApiConfigIsNotAnHash.new(api_name)
    end

    api_name = api_name.to_sym
    # configs = ApiRecipes._aprcps_merge_apis_configs(api_name, configs.deep_symbolize_keys)
    if self.respond_to? api_name
      raise ApiNameClashError.new(self, api_name)
    else

      define_method api_name do
        configs = ApiRecipes._aprcps_merge_apis_configs(api_name, configs.deep_symbolize_keys)
        api = Api.new(api_name, configs, self)
        ApiRecipes.copy_global_authorizations_to_api api
        api
      end
      define_singleton_method api_name do
        configs = ApiRecipes._aprcps_merge_apis_configs(api_name, configs.deep_symbolize_keys)
        api = Api.new(api_name, configs, self)
        ApiRecipes.copy_global_authorizations_to_api api
        api
      end
    end
  end
end


53
54
55
# File 'lib/api_recipes.rb', line 53

def self.print_urls
  @print_urls || false
end


49
50
51
# File 'lib/api_recipes.rb', line 49

def self.print_urls=(value)
  @print_urls = value
end

.set_authorization_for_api(authorization, api_name) ⇒ Object



85
86
87
88
89
90
91
92
93
94
# File 'lib/api_recipes.rb', line 85

def self.set_authorization_for_api(authorization, api_name)
  api_name = api_name.to_sym

  # Set authorization on thread storage
  if _aprcps_thread_storage[api_name]
    _aprcps_thread_storage[api_name].each do |_, api|
      api.authorization = authorization
    end
  end
end

.set_basic_auth_for_api(basic_auth, api_name) ⇒ Object



96
97
98
99
100
101
102
103
104
105
# File 'lib/api_recipes.rb', line 96

def self.set_basic_auth_for_api(basic_auth, api_name)
  api_name = api_name.to_sym

  # Set authorization on thread storage
  if _aprcps_thread_storage[api_name]
    _aprcps_thread_storage[api_name].each do |_, api|
      api.authorization = basic_auth
    end
  end
end