Class: Fog::Service

Inherits:
Object
  • Object
show all
Defined in:
lib/rackspace-fog/core/service.rb

Defined Under Namespace

Modules: Collections, NoLeakInspector Classes: Error, NotFound

Class Method Summary collapse

Class Method Details

.coerce_options(options) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/rackspace-fog/core/service.rb', line 129

def coerce_options(options)
  options.each do |key, value|
    value_string = value.to_s.downcase
    if value.nil?
      options.delete(key)
    elsif value == value_string.to_i.to_s
      options[key] = value.to_i
    else
      options[key] = case value_string
      when 'false'
        false
      when 'true'
        true
      else
        value
      end
    end
  end
end

.collection(new_collection) ⇒ Object



121
122
123
# File 'lib/rackspace-fog/core/service.rb', line 121

def collection(new_collection)
  collections << new_collection
end

.collectionsObject



125
126
127
# File 'lib/rackspace-fog/core/service.rb', line 125

def collections
  @collections ||= []
end

.fetch_credentials(options) ⇒ Object



72
73
74
75
76
77
78
79
80
# File 'lib/rackspace-fog/core/service.rb', line 72

def fetch_credentials(options)
  # attempt to load credentials from config file
  begin
    default_credentials = Fog.credentials.reject {|key, value| !(recognized | requirements).include?(key)}
  rescue LoadError
    # if there are no configured credentials, do nothing
    {}
  end
end

.inherited(child) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/rackspace-fog/core/service.rb', line 36

def inherited(child)
  child.class_eval <<-EOS, __FILE__, __LINE__
    class Error < Fog::Service::Error; end
    class NotFound < Fog::Service::NotFound; end

    module Collections
      include Fog::Service::Collections

      def service
        #{child}
      end
    end

    def self.service
      #{child}
    end
  EOS
end

.mocked_requestsObject



149
150
151
# File 'lib/rackspace-fog/core/service.rb', line 149

def mocked_requests
  @mocked_requests ||= []
end

.model(new_model) ⇒ Object



153
154
155
# File 'lib/rackspace-fog/core/service.rb', line 153

def model(new_model)
  models << new_model
end

.model_path(new_path) ⇒ Object



117
118
119
# File 'lib/rackspace-fog/core/service.rb', line 117

def model_path(new_path)
  @model_path = new_path
end

.modelsObject



157
158
159
# File 'lib/rackspace-fog/core/service.rb', line 157

def models
  @models ||= []
end

.new(options = {}) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/rackspace-fog/core/service.rb', line 55

def new(options={})
  options = Fog.symbolize_credentials(options)
  options = fetch_credentials(options).merge(options)
  validate_options(options)
  coerce_options(options)
  setup_requirements

  if Fog.mocking?
    service::Mock.send(:include, service::Collections)
    service::Mock.new(options)
  else
    service::Real.send(:include, service::Collections)
    service::Real.send(:include, service::NoLeakInspector)
    service::Real.new(options)
  end
end

.recognizedObject



195
196
197
# File 'lib/rackspace-fog/core/service.rb', line 195

def recognized
  @recognized ||= [:connection_options]
end

.recognizes(*args) ⇒ Object



191
192
193
# File 'lib/rackspace-fog/core/service.rb', line 191

def recognizes(*args)
  recognized.concat(args)
end

.request(new_request) ⇒ Object



165
166
167
# File 'lib/rackspace-fog/core/service.rb', line 165

def request(new_request)
  requests << new_request
end

.request_path(new_path) ⇒ Object



161
162
163
# File 'lib/rackspace-fog/core/service.rb', line 161

def request_path(new_path)
  @request_path = new_path
end

.requestsObject



169
170
171
# File 'lib/rackspace-fog/core/service.rb', line 169

def requests
  @requests ||= []
end

.requirementsObject



187
188
189
# File 'lib/rackspace-fog/core/service.rb', line 187

def requirements
  @requirements ||= []
end

.requires(*args) ⇒ Object



183
184
185
# File 'lib/rackspace-fog/core/service.rb', line 183

def requires(*args)
  requirements.concat(args)
end

.secrets(*args) ⇒ Object



173
174
175
176
177
178
179
180
181
# File 'lib/rackspace-fog/core/service.rb', line 173

def secrets(*args)
  if args.empty?
    @secrets ||= []
  else
    args.inject(secrets) do |secrets, secret|
      secrets << "@#{secret}".to_sym
    end
  end
end

.setup_requirementsObject



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/rackspace-fog/core/service.rb', line 82

def setup_requirements
  if superclass.respond_to?(:setup_requirements)
    superclass.setup_requirements
  end

  @required ||= false
  unless @required
    for collection in collections
      require [@model_path, collection].join('/')
      constant = collection.to_s.split('_').map {|characters| characters[0...1].upcase << characters[1..-1]}.join('')
      service::Collections.module_eval <<-EOS, __FILE__, __LINE__
        def #{collection}(attributes = {})
          #{service}::#{constant}.new({:connection => self}.merge(attributes))
        end
      EOS
    end
    for model in models
      require [@model_path, model].join('/')
    end
    for request in requests
      require [@request_path, request].join('/')
      if service::Mock.method_defined?(request)
        mocked_requests << request
      else
        service::Mock.module_eval <<-EOS, __FILE__, __LINE__
          def #{request}(*args)
            Fog::Mock.not_implemented
          end
        EOS
      end
    end
    @required = true
  end
end

.validate_options(options) ⇒ Object



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/rackspace-fog/core/service.rb', line 199

def validate_options(options)
  keys = []
  for key, value in options
    unless value.nil?
      keys << key
    end
  end
  missing = requirements - keys
  unless missing.empty?
    raise ArgumentError, "Missing required arguments: #{missing.join(', ')}"
  end

  unless recognizes.empty?
    unrecognized = options.keys - requirements - recognized
    unless unrecognized.empty?
      raise ArgumentError, "Unrecognized arguments: #{unrecognized.join(', ')}"
    end
  end
end