Class: Appfirst::Client::Mock
- Inherits:
-
Object
- Object
- Appfirst::Client::Mock
- Includes:
- Shared
- Defined in:
- lib/appfirst/client.rb,
lib/appfirst/requests/create_server_tag.rb
Overview
Real
Instance Attribute Summary
Attributes included from Shared
Class Method Summary collapse
Instance Method Summary collapse
- #create_server_tag(params = {}) ⇒ Object
- #data ⇒ Object
-
#initialize(options = {}) ⇒ Mock
constructor
A new instance of Mock.
- #require_parameter(params, param, options = {}) ⇒ Object
- #resource_uri(fragment) ⇒ Object
- #response(options = {}) ⇒ Object
- #serial_id ⇒ Object
- #stringify_keys(hash) ⇒ Object
- #url_for(path) ⇒ Object
Methods included from Shared
Constructor Details
#initialize(options = {}) ⇒ Mock
Returns a new instance of Mock.
88 89 90 |
# File 'lib/appfirst/client.rb', line 88 def initialize(={}) setup() end |
Class Method Details
.data ⇒ Object
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 |
#data ⇒ Object
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, ={}) if value = params[param] value else errors = [{"message" => ([:message] || "Missing parameter: #{param}"), "code" => ([: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(={}) status = [:status] || 200 body = [:body] headers = { "Content-Type" => "application/json; charset=utf-8" }.merge([:headers] || {}) Appfirst::Response.new(status, headers, body).raise! end |
#serial_id ⇒ Object
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 |