Module: Heroku::API::Mock

Defined in:
lib/heroku/api/mock.rb,
lib/heroku/api/mock/apps.rb,
lib/heroku/api/mock/keys.rb,
lib/heroku/api/mock/logs.rb,
lib/heroku/api/mock/user.rb,
lib/heroku/api/mock/login.rb,
lib/heroku/api/mock/addons.rb,
lib/heroku/api/mock/stacks.rb,
lib/heroku/api/mock/domains.rb,
lib/heroku/api/mock/features.rb,
lib/heroku/api/mock/releases.rb,
lib/heroku/api/mock/processes.rb,
lib/heroku/api/mock/attachments.rb,
lib/heroku/api/mock/config_vars.rb,
lib/heroku/api/mock/collaborators.rb

Constant Summary collapse

APP_NOT_FOUND =
{ :body => 'App not found.',   :status => 404 }
USER_NOT_FOUND =
{ :body => 'User not found.',  :status => 404 }
STACKS =
[
  {
    "beta"      => false,
    "requested" => false,
    "current"   => false,
    "name"      => "aspen-mri-1.8.6"
  },
  {
    "beta"      => false,
    "requested" => false,
    "current"   => false,
    "name"      => "bamboo-mri-1.9.2"
  },
  {
    "beta"      => false,
    "requested" => false,
    "current"   => false,
    "name"      => "bamboo-ree-1.8.7"
  },
  {
    "beta"      => true,
    "requested" => false,
    "current"   => false,
    "name"      => "cedar"
  }
]

Class Method Summary collapse

Class Method Details

.add_mock_app_addon(mock_data, app, addon) ⇒ Object



43
44
45
46
47
# File 'lib/heroku/api/mock.rb', line 43

def self.add_mock_app_addon(mock_data, app, addon)
  addon_data = get_mock_addon(mock_data, addon)
  mock_data[:addons][app] << addon_data.reject {|key, value| !['attachable', 'beta', 'configured', 'consumes_dyno_hours', 'description', 'group_description', 'name', 'plan_description', 'price', 'slug', 'state', 'terms_of_service', 'url'].include?(key)}
  add_mock_release(mock_data, app, {'descr' => "Add-on add #{addon_data['name']}"})
end

.add_mock_release(mock_data, app, release_data) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/heroku/api/mock.rb', line 49

def self.add_mock_release(mock_data, app, release_data)
  env = if get_mock_app(mock_data, app)['stack'] == 'cedar'
    {
      'BUNDLE_WITHOUT'      => 'development:test',
      'DATABASE_URL'        => 'postgres://username:[email protected]/username',
      'LANG'                => 'en_US.UTF-8',
      'RACK_ENV'            => 'production'
    }
  else
    {}
  end
  version = mock_data[:releases][app].map {|release| release['name'][1..-1].to_i}.max || 0
  mock_data[:releases][app] << {
    'addons'      => mock_data[:addons][app].map {|addon| addon['name']},
    'commit'      => nil,
    'created_at'  => timestamp,
    'descr'       => "",
    'env'         => env,
    'name'        => "v#{version + 1}",
    'pstable'     => { 'web' => '' },
    'user'        => '[email protected]'
  }.merge(release_data)
end

.get_mock_addon(mock_data, addon) ⇒ Object



73
74
75
76
77
78
79
# File 'lib/heroku/api/mock.rb', line 73

def self.get_mock_addon(mock_data, addon)
  @addons ||= begin
    data = File.read("#{File.dirname(__FILE__)}/mock/cache/get_addons.json")
    Heroku::API::OkJson.decode(data)
  end
  @addons.detect {|addon_data| addon_data['name'] == addon}
end

.get_mock_addon_price(mock_data, addon) ⇒ Object



81
82
83
84
85
86
87
88
89
# File 'lib/heroku/api/mock.rb', line 81

def self.get_mock_addon_price(mock_data, addon)
  addon_data = get_mock_addon(mock_data, addon)
  price_cents = addon_data['price_cents'] || 0
  price = format("$%d/mo", price_cents / 100)
  if price == '$0/mo'
    price = 'free'
  end
  price
end

.get_mock_app(mock_data, app) ⇒ Object



91
92
93
# File 'lib/heroku/api/mock.rb', line 91

def self.get_mock_app(mock_data, app)
  mock_data[:apps].detect {|app_data| app_data['name'] == app}
end

.get_mock_app_addon(mock_data, app, addon) ⇒ Object



95
96
97
# File 'lib/heroku/api/mock.rb', line 95

def self.get_mock_app_addon(mock_data, app, addon)
  mock_data[:addons][app].detect {|addon_data| addon_data['name'] == addon}
end

.get_mock_app_domain(mock_data, app, domain) ⇒ Object



99
100
101
# File 'lib/heroku/api/mock.rb', line 99

def self.get_mock_app_domain(mock_data, app, domain)
  mock_data[:domains][app].detect {|domain_data| domain_data['domain'] == domain}
end

.get_mock_collaborator(mock_data, app, email) ⇒ Object



103
104
105
# File 'lib/heroku/api/mock.rb', line 103

def self.get_mock_collaborator(mock_data, app, email)
  mock_data[:collaborators][app].detect {|collaborator_data| collaborator_data['email'] == email}
end

.get_mock_feature(mock_data, feature) ⇒ Object



107
108
109
110
111
112
113
# File 'lib/heroku/api/mock.rb', line 107

def self.get_mock_feature(mock_data, feature)
  @features ||= begin
    data = File.read("#{File.dirname(__FILE__)}/mock/cache/get_features.json")
    Heroku::API::OkJson.decode(data)
  end
  @features.detect {|feature_data| feature_data['name'] == feature}
end

.get_mock_key(mock_data, key) ⇒ Object



115
116
117
# File 'lib/heroku/api/mock.rb', line 115

def self.get_mock_key(mock_data, key)
  mock_data[:keys].detect {|key_data| %r{ #{Regexp.escape(key)}$}.match(key_data['contents'])}
end

.get_mock_processes(mock_data, app) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/heroku/api/mock.rb', line 119

def self.get_mock_processes(mock_data, app)
  mock_data[:ps][app].map do |ps|

    elapsed = Time.now.to_i - Time.parse(ps['transitioned_at']).to_i
    ps['elapsed'] = elapsed

    pretty_elapsed = if elapsed < 60
      "#{elapsed}s"
    elsif elapsed < (60 * 60)
      "#{elapsed / 60}m"
    else
      "#{elapsed / 60 / 60}h"
    end
    ps['pretty_state'] = "#{ps['state']} for #{pretty_elapsed}"

    ps
  end
end

.parse_stub_params(params) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/heroku/api/mock.rb', line 138

def self.parse_stub_params(params)
  mock_data = nil

  if params[:headers].has_key?('Authorization')
    api_key = Base64.decode64(params[:headers]['Authorization']).split(':').last

    parsed = params.dup
    begin # try to JSON decode
      parsed[:body] &&= Heroku::API::OkJson.decode(parsed[:body])
    rescue # else leave as is
    end
    mock_data = @mock_data[api_key]
  end

  [parsed, mock_data]
end

.remove_mock_app_addon(mock_data, app, addon) ⇒ Object



155
156
157
158
159
# File 'lib/heroku/api/mock.rb', line 155

def self.remove_mock_app_addon(mock_data, app, addon)
  addon_data = mock_data[:addons][app].detect {|addon_data| addon_data['name'] == addon}
  mock_data[:addons][app].delete(addon_data)
  add_mock_release(mock_data, app, {'descr' => "Add-on remove #{addon_data['name']}"})
end

.timestampObject



173
174
175
# File 'lib/heroku/api/mock.rb', line 173

def self.timestamp
  Time.now.strftime("%G/%m/%d %H:%M:%S %z")
end

.unescape(string) ⇒ Object



161
162
163
# File 'lib/heroku/api/mock.rb', line 161

def self.unescape(string)
  CGI.unescape(string)
end

.with_mock_app(mock_data, app, &block) ⇒ Object



165
166
167
168
169
170
171
# File 'lib/heroku/api/mock.rb', line 165

def self.with_mock_app(mock_data, app, &block)
  if app_data = get_mock_app(mock_data, app)
    yield(app_data)
  else
    APP_NOT_FOUND
  end
end