Class: Fog::Service

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

Defined Under Namespace

Modules: Collections Classes: Error, NotFound

Class Method Summary collapse

Class Method Details

.collection(new_collection) ⇒ Object



101
102
103
# File 'lib/fog/core/service.rb', line 101

def collection(new_collection)
  collections << new_collection
end

.collectionsObject



105
106
107
# File 'lib/fog/core/service.rb', line 105

def collections
  @collections ||= []
end

.inherited(child) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/fog/core/service.rb', line 25

def inherited(child)
  child.class_eval <<-EOS, __FILE__, __LINE__
    module Collections
      include Fog::Service::Collections

      def service
        #{child}
      end
    end

    def self.service
      #{child}
    end
  EOS
end

.mocked_requestsObject



109
110
111
# File 'lib/fog/core/service.rb', line 109

def mocked_requests
  @mocked_requests ||= []
end

.model(new_model) ⇒ Object



113
114
115
# File 'lib/fog/core/service.rb', line 113

def model(new_model)
  models << new_model
end

.model_path(new_path) ⇒ Object



97
98
99
# File 'lib/fog/core/service.rb', line 97

def model_path(new_path)
  @model_path = new_path
end

.modelsObject



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

def models
  @models ||= []
end

.new(options = {}) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/fog/core/service.rb', line 41

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

  validate_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.new(options)
  end
end

.recognizedObject



145
146
147
# File 'lib/fog/core/service.rb', line 145

def recognized
  @recognized ||= []
end

.recognizes(*args) ⇒ Object



141
142
143
# File 'lib/fog/core/service.rb', line 141

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

.request(new_request) ⇒ Object



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

def request(new_request)
  requests << new_request
end

.request_path(new_path) ⇒ Object



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

def request_path(new_path)
  @request_path = new_path
end

.requestsObject



129
130
131
# File 'lib/fog/core/service.rb', line 129

def requests
  @requests ||= []
end

.requirementsObject



137
138
139
# File 'lib/fog/core/service.rb', line 137

def requirements
  @requirements ||= []
end

.requires(*args) ⇒ Object



133
134
135
# File 'lib/fog/core/service.rb', line 133

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

.reset_data(keys = Mock.data.keys) ⇒ Object



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

def reset_data(keys=Mock.data.keys)
  Mock.reset_data(keys)
end

.setup_requirementsObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/fog/core/service.rb', line 62

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



157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/fog/core/service.rb', line 157

def validate_options(options)
  missing = requirements - options.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