class Appfirst::Client::Mock

Public Class Methods

data() click to toggle source
# File lib/appfirst/client.rb, line 70
def self.data
  @data ||= Hash.new do |h,k|
    {
      :servers     => {},
      :server_tags => {},
    }
  end
end
new(options={}) click to toggle source
# File lib/appfirst/client.rb, line 88
def initialize(options={})
  setup(options)
end
reset!() click to toggle source
# File lib/appfirst/client.rb, line 83
def self.reset!
  @data = nil
  @serial_id = 0
end

Public Instance Methods

create_server_tag(params={}) click to toggle source
# File lib/appfirst/requests/create_server_tag.rb, line 9
def create_server_tag(params={})
  identity = self.serial_id

  name    = require_parameter(params, :name, code: 5, message: "Invalid server tag name, name must be provided") # {"errors"=>[{"message"=>"Invalid server tag name, name must be provided", "code"=>5}]}
  servers = params[:servers] || []

  response_hash = {
    :id           => identity,
    :name         => name,
    :servers      => servers,
    :resource_uri => resource_uri("/server_tags/#{identity}")
  }

  self.data[:server_tags][identity] = response_hash

  response(
    :body    => response_hash,
    :status  => 200,
    :headers => {
      "Content-Type" => "application/json; charset=utf8"
    }
  )
end
data() click to toggle source
# File lib/appfirst/client.rb, line 79
def data
  self.class.data[self.url]
end
require_parameter(params, param, options={}) click to toggle source
# File lib/appfirst/client.rb, line 114
def require_parameter(params, param, options={})
  if value = params[param]
    value
  else
    errors = [{"message" => (options[:message] || "Missing parameter: #{param}"), "code" => (options[:code] || -1)}]

    response(
      :body   => {"errors" => errors},
      :status => 400,
    )
  end
end
resource_uri(fragment) click to toggle source
# File lib/appfirst/client.rb, line 110
def resource_uri(fragment)
  File.join(path, fragment)
end
response(options={}) click to toggle source
# File lib/appfirst/client.rb, line 92
def response(options={})
  status  = options[:status] || 200
  body    = options[:body]
  headers = {
    "Content-Type"  => "application/json; charset=utf-8"
  }.merge(options[:headers] || {})

  Appfirst::Response.new(status, headers, body).raise!
end
serial_id() click to toggle source
# File lib/appfirst/client.rb, line 127
def serial_id
  @@serial_id ||= 0
  @@serial_id += 1
end
stringify_keys(hash) click to toggle source
# File lib/appfirst/client.rb, line 106
def stringify_keys(hash)
  hash.is_a?(Hash) ? hash.inject({}){|r,(k,v)| r.merge(k.to_s => stringify_keys(v))} : hash
end
url_for(path) click to toggle source
# File lib/appfirst/client.rb, line 102
def url_for(path)
  File.join(@url.to_s, path.to_s)
end