Module: Xplenty::API::Mock

Defined in:
lib/xplenty/api/mock.rb,
lib/xplenty/api/mock/jobs.rb,
lib/xplenty/api/mock/clusters.rb

Constant Summary collapse

CLUSTER_NOT_FOUND =
{ :body => 'Cluster not found.',   :status => 404 }
JOB_NOT_FOUND =
{ :body => 'Job not found.',  :status => 404 }

Class Method Summary collapse

Class Method Details

.get_mock_cluster(mock_data, cluster_id) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/xplenty/api/mock.rb', line 19

def self.get_mock_cluster(mock_data, cluster_id)
  @clusters ||= begin
    data = File.read("#{File.dirname(__FILE__)}/mock/cache/clusters.json")
    Xplenty::API::OkJson.decode(data)
  end
  mock_data[:clusters] = @clusters

  mock_data[:clusters].detect {|cluster_data| cluster_data['id'].to_s == cluster_id.to_s}
end

.get_mock_job(mock_data, job_id) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/xplenty/api/mock.rb', line 29

def self.get_mock_job(mock_data, job_id)
  @jobs ||= begin
    data = File.read("#{File.dirname(__FILE__)}/mock/cache/jobs.json")
    Xplenty::API::OkJson.decode(data)
  end
  mock_data[:jobs] = @jobs

  mock_data[:jobs].detect {|job_data| job_data['id'].to_s == job_id.to_s}
end

.parse_stub_params(params) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/xplenty/api/mock.rb', line 39

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] &&= Xplenty::API::OkJson.decode(parsed[:body])
    rescue # else leave as is
    end
    mock_data = @mock_data[api_key]
  end

  [parsed, mock_data]
end

.timestampObject



81
82
83
# File 'lib/xplenty/api/mock.rb', line 81

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

.with_mock_cluster(mock_data, cluster_id, &block) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/xplenty/api/mock.rb', line 56

def self.with_mock_cluster(mock_data, cluster_id, &block)
  if cluster_data = get_mock_cluster(mock_data, cluster_id)
    yield(cluster_data)
  else
    CLUSTER_NOT_FOUND
  end
end

.with_mock_clusters(mock_data) {|mock_data| ... } ⇒ Object

Yields:

  • (mock_data)


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

def self.with_mock_clusters(mock_data, &block)
  @clusters ||= begin
    data = File.read("#{File.dirname(__FILE__)}/mock/cache/clusters.json")
    Xplenty::API::OkJson.decode(data)
  end
  mock_data[:clusters] = @clusters
  yield(mock_data)
end

.with_mock_job(mock_data, job_id, &block) ⇒ Object



64
65
66
67
68
69
70
# File 'lib/xplenty/api/mock.rb', line 64

def self.with_mock_job(mock_data, job_id, &block)
  if job_data = get_mock_job(mock_data, job_id)
    yield(job_data)
  else
    JOB_NOT_FOUND
  end
end