Class: Appfirst::Client::Mock

Inherits:
Object
  • Object
show all
Includes:
Shared
Defined in:
lib/appfirst/client.rb,
lib/appfirst/requests/create_server_tag.rb

Overview

Real

Instance Attribute Summary

Attributes included from Shared

#path, #url, #user

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Shared

#setup

Constructor Details

#initialize(options = {}) ⇒ Mock

Returns a new instance of Mock.



88
89
90
# File 'lib/appfirst/client.rb', line 88

def initialize(options={})
  setup(options)
end

Class Method Details

.dataObject



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

def self.data
  @data ||= Hash.new do |h,k|
    {
      :servers     => {},
      :server_tags => {},
    }
  end
end

.reset!Object



83
84
85
86
# File 'lib/appfirst/client.rb', line 83

def self.reset!
  @data = nil
  @serial_id = 0
end

Instance Method Details

#create_server_tag(params = {}) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# 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

#dataObject



79
80
81
# File 'lib/appfirst/client.rb', line 79

def data
  self.class.data[self.url]
end

#require_parameter(params, param, options = {}) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
# 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) ⇒ Object



110
111
112
# File 'lib/appfirst/client.rb', line 110

def resource_uri(fragment)
  File.join(path, fragment)
end

#response(options = {}) ⇒ Object



92
93
94
95
96
97
98
99
100
# 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_idObject



127
128
129
130
# File 'lib/appfirst/client.rb', line 127

def serial_id
  @@serial_id ||= 0
  @@serial_id += 1
end

#stringify_keys(hash) ⇒ Object



106
107
108
# 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) ⇒ Object



102
103
104
# File 'lib/appfirst/client.rb', line 102

def url_for(path)
  File.join(@url.to_s, path.to_s)
end