Class: FleetAPI::Client::Mock

Inherits:
Object
  • Object
show all
Defined in:
lib/fleet_api/client.rb,
lib/fleet_api/requests/get_unit.rb,
lib/fleet_api/requests/get_units.rb,
lib/fleet_api/requests/create_unit.rb,
lib/fleet_api/requests/update_unit.rb,
lib/fleet_api/requests/get_machines.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Mock

Returns a new instance of Mock.



67
68
69
# File 'lib/fleet_api/client.rb', line 67

def initialize(options={})
  @url = options[:url] || "http://fleet-api.localhost"
end

Class Method Details

.dataObject



71
72
73
74
75
76
# File 'lib/fleet_api/client.rb', line 71

def self.data
  @data ||= {
    :units => {},
    :machines => {},
   }
end

.reset!Object



78
79
80
# File 'lib/fleet_api/client.rb', line 78

def self.reset!
  @data = nil
end

Instance Method Details

#create_unit(params = {}) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/fleet_api/requests/create_unit.rb', line 15

def create_unit(params={})
  params['machineID'] = FleetAPI::Mock.random_id

  self.data[:units][params['name']] = params

  response(
    :body   => {"unit" => self.data[:units][params['name']]},
    :status => 200,
  )
end

#dataObject



82
83
84
# File 'lib/fleet_api/client.rb', line 82

def data
  self.class.data
end

#get_machines(params = {}) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/fleet_api/requests/get_machines.rb', line 13

def get_machines(params={})
  units = self.data['machines']

  response(
    :body    => {"machines" => machines},
    :status  => 200,
    :headers => {
      "Content-Type" => "application/json; charset=utf8",
    }
  )
end

#get_unit(params = {}) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/fleet_api/requests/get_unit.rb', line 15

def get_unit(params={})
  name = params['name']

  if unit = self.data[:units][name]
    response(
      :body   => {'unit' => unit},
      :status => 200,
    )
  else
    response(
      :body   => {"error" => "Couldn't find unit with name #{name}"},
      :status => 404,
    )
  end
end

#get_units(params = {}) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/fleet_api/requests/get_units.rb', line 12

def get_units(params={})
  units = page(params, :units, "unit")

  response(
    :body    => {"units" => units},
    :status  => 200,
    :headers => {
      "Content-Type" => "application/json; charset=utf8",
    }
  )
end

#page(params, collection, object_root, options = {}) ⇒ Object



108
109
110
111
112
113
114
115
116
117
# File 'lib/fleet_api/client.rb', line 108

def page(params, collection, object_root, options={})
  resources   = options[:resources] || self.data[collection]
  page_size   = (params["per_page"] || 20).to_i
  page_index  = (params["page"] || 1).to_i
  offset      = (page_index - 1) * page_size

  resource_page = resources.values.reverse.slice(offset, page_size)

  resource_page
end

#response(options = {}) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/fleet_api/client.rb', line 86

def response(options={})
  url     = options[:url] || File.join(@url.to_s, options[:path] || "/")
  method  = (options[:method] || :get).to_s.to_sym
  status  = options[:status] || 200
  body    = options[:body]
  headers = {
    "Content-Type" => "application/json; charset=utf-8"
  }.merge(options[:headers] || {})

  FleetAPI::Response.new(
    :status  => status,
    :headers => headers,
    :body    => body,
    :request => {
      :method  => method,
      :url     => url,
      :body    => body,
      :headers => headers,
    }
  ).raise!
end

#update_unit(params = {}) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/fleet_api/requests/update_unit.rb', line 14

def update_unit(params={})
  name = params['name']

  unit = self.data[:units][name]
  unless unit
    response(
      :body   => {"error" => "Couldn't find Unit with name #{name}"},
      :status => 404,
    )
  end

  update = params.delete("unit") || {}

  updated_unit = unit.merge(update)

  self.data[:units][unit['name']] = updated_unit

  response(
    :body   => {"unit" => updated_unit},
    :status => 204,
  )
end

#url_for_page(collection, token) ⇒ Object



119
120
121
# File 'lib/fleet_api/client.rb', line 119

def url_for_page(collection, token)
  "<#{File.join(@url, collection.to_s)}??nextPageToken=#{token}"
end