Class: Fog::Google::Pubsub::Mock

Inherits:
Object
  • Object
show all
Includes:
Shared
Defined in:
lib/fog/google/pubsub/mock.rb,
lib/fog/google/requests/pubsub/get_topic.rb,
lib/fog/google/requests/pubsub/list_topics.rb,
lib/fog/google/requests/pubsub/create_topic.rb,
lib/fog/google/requests/pubsub/delete_topic.rb,
lib/fog/google/requests/pubsub/publish_topic.rb,
lib/fog/google/requests/pubsub/get_subscription.rb,
lib/fog/google/requests/pubsub/pull_subscription.rb,
lib/fog/google/requests/pubsub/list_subscriptions.rb,
lib/fog/google/requests/pubsub/create_subscription.rb,
lib/fog/google/requests/pubsub/delete_subscription.rb,
lib/fog/google/requests/pubsub/acknowledge_subscription.rb

Instance Attribute Summary

Attributes included from Shared

#api_url, #api_version, #project

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Shared

#build_excon_response, #create_signing_key, #initialize_google_client, #new_pk12_google_client, #request, #shared_initialize

Constructor Details

#initialize(options) ⇒ Mock

Returns a new instance of Mock.



7
8
9
# File 'lib/fog/google/pubsub/mock.rb', line 7

def initialize(options)
  shared_initialize(options[:google_project], GOOGLE_PUBSUB_API_VERSION, GOOGLE_PUBSUB_BASE_URL)
end

Class Method Details

.dataObject



11
12
13
14
15
16
17
18
# File 'lib/fog/google/pubsub/mock.rb', line 11

def self.data
  @data ||= Hash.new do |hash, key|
    hash[key] = {
      :topics => {},
      :subscriptions => {}
    }
  end
end

.resetObject



20
21
22
# File 'lib/fog/google/pubsub/mock.rb', line 20

def self.reset
  @data = nil
end

Instance Method Details

#acknowledge_subscription(subscription, ack_ids) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/fog/google/requests/pubsub/acknowledge_subscription.rb', line 24

def acknowledge_subscription(subscription, ack_ids)
  unless data[:subscriptions].key?(subscription)
    subscription_resource = subscription.to_s.split("/")[-1]
    body = {
      "error" => {
        "code"    => 404,
        "message" => "Resource not found (resource=#{subscription_resource}).",
        "status"  => "NOT_FOUND"
      }
    }
    status = 404
    return build_excon_response(body, status)
  end

  sub = data[:subscriptions][subscription]
  sub[:messages].delete_if { |msg| ack_ids.member?(msg["messageId"]) }

  build_excon_response(nil, 200)
end

#create_subscription(subscription_name, topic, push_config = {}, ack_deadline_seconds = nil) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/fog/google/requests/pubsub/create_subscription.rb', line 41

def create_subscription(subscription_name, topic, push_config = {}, ack_deadline_seconds = nil)
  subscription = {
    "name"               => subscription_name,
    "topic"              => topic,
    "pushConfig"         => push_config,
    "ackDeadlineSeconds" => ack_deadline_seconds
  }

  # We also track pending messages
  data[:subscriptions][subscription_name] = subscription.merge(:messages => [])

  build_excon_response(subscription, 200)
end

#create_topic(topic_name) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/fog/google/requests/pubsub/create_topic.rb', line 22

def create_topic(topic_name)
  data = {
    "name" => topic_name
  }
  self.data[:topics][topic_name] = data

  body = data.clone
  status = 200

  build_excon_response(body, status)
end

#dataObject



24
25
26
# File 'lib/fog/google/pubsub/mock.rb', line 24

def data
  self.class.data[project]
end

#delete_subscription(subscription_name) ⇒ Object



20
21
22
23
24
# File 'lib/fog/google/requests/pubsub/delete_subscription.rb', line 20

def delete_subscription(subscription_name)
  data[:subscriptions].delete(subscription_name)

  build_excon_response(nil, 200)
end

#delete_topic(topic_name) ⇒ Object



20
21
22
23
24
25
# File 'lib/fog/google/requests/pubsub/delete_topic.rb', line 20

def delete_topic(topic_name)
  data[:topics].delete(topic_name)

  status = 200
  build_excon_response(nil, status)
end

#get_subscription(subscription_name) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/fog/google/requests/pubsub/get_subscription.rb', line 20

def get_subscription(subscription_name)
  sub = data[:subscriptions][subscription_name]
  if sub.nil?
    subscription_resource = subscription_name.split("/")[-1]
    body = {
      "error" => {
        "code"    => 404,
        "message" => "Resource not found (resource=#{subscription_resource}).",
        "status"  => "NOT_FOUND"
      }
    }
    return build_excon_response(body, 404)
  end

  body = sub.select do |k, _|
    %w(name topic pushConfig ackDeadlineSeconds).include?(k)
  end
  status = 200

  build_excon_response(body, status)
end

#get_topic(topic_name) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/fog/google/requests/pubsub/get_topic.rb', line 20

def get_topic(topic_name)
  if !data[:topics].key?(topic_name)
    topic_resource = topic_name.split("/")[-1]
    body = {
      "error" => {
        "code"    => 404,
        "message" => "Resource not found (resource=#{topic_resource}).",
        "status"  => "NOT_FOUND"
      }
    }
    status = 404
  else
    body = data[:topics][topic_name].clone
    status = 200
  end

  build_excon_response(body, status)
end

#list_subscriptions(_project = nil) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/fog/google/requests/pubsub/list_subscriptions.rb', line 22

def list_subscriptions(_project = nil)
  subs = data[:subscriptions].values.map do |sub|
    # Filter out any keys that aren't part of the response object
    sub.select do |k, _|
      %w(name topic pushConfig ackDeadlineSeconds).include?(k)
    end
  end

  body = {
    "subscriptions" => subs
  }
  status = 200
  build_excon_response(body, status)
end

#list_topics(_project = nil) ⇒ Object



22
23
24
25
26
27
28
29
# File 'lib/fog/google/requests/pubsub/list_topics.rb', line 22

def list_topics(_project = nil)
  body = {
    "topics" => data[:topics].values
  }
  status = 200

  build_excon_response(body, status)
end

#publish_topic(topic, messages) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/fog/google/requests/pubsub/publish_topic.rb', line 28

def publish_topic(topic, messages)
  if data[:topics].key?(topic)
    published_messages = messages.map do |msg|
      msg.merge("messageId" => Fog::Mock.random_letters(16), "publishTime" => Time.now.iso8601)
    end

    # Gather the subscriptions and publish
    data[:subscriptions].values.each do |sub|
      next unless sub["topic"] == topic
      sub[:messages] += published_messages
    end

    body = {
      "messageIds" => published_messages.map { |msg| msg["messageId"] }
    }
    status = 200
  else
    topic_resource = topic_name.split("/")[-1]
    body = {
      "error" => {
        "code"    => 404,
        "message" => "Resource not found (resource=#{topic_resource}).",
        "status"  => "NOT_FOUND"
      }
    }
    status = 404
  end

  build_excon_response(body, status)
end

#pull_subscription(subscription, options = { :return_immediately => true, :max_messages => 10 }) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/fog/google/requests/pubsub/pull_subscription.rb', line 36

def pull_subscription(subscription, options = { :return_immediately => true, :max_messages => 10 })
  # We're going to ignore return_immediately; feel free to add support
  # if you need it for testing
  subscription_name = Fog::Google::Pubsub.subscription_name(subscription)
  sub = data[:subscriptions][subscription_name]

  if sub.nil?
    subscription_resource = subscription_name.split("/")[-1]
    body = {
      "error" => {
        "code"    => 404,
        "message" => "Resource not found (resource=#{subscription_resource}).",
        "status"  => "NOT_FOUND"
      }
    }
    return build_excon_response(body, 404)
  end

  # This implementation is a bit weak; instead of "hiding" messages for
  # some period of time after they are pulled, instead we always return
  # them until acknowledged. This might cause issues with clients that
  # refuse to acknowledge.
  #
  # Also, note that here we use the message id as the ack id - again,
  # this might cause problems with some strange-behaving clients.
  msgs = sub[:messages].take(options[:max_messages]).map do |msg|
    {
      "ackId"   => msg["messageId"],
      "message" => msg
    }
  end

  body = {
    "receivedMessages" => msgs
  }
  status = 200
  build_excon_response(body, status)
end

#reset_dataObject



28
29
30
# File 'lib/fog/google/pubsub/mock.rb', line 28

def reset_data
  self.class.data.delete(project)
end