<commit_before>#--
# Copyright 2015 Google Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

require "gcloud/gce"
require "gcloud/pubsub/connection"
require "gcloud/pubsub/credentials"
require "gcloud/pubsub/errors"
require "gcloud/pubsub/topic"

module Gcloud
  module Pubsub
    ##
    # = Project
    #
    # Represents the project that pubsub messages are pushed to and pulled from.
    # Topic is a named resource to which messages are sent by publishers.
    # Subscription is a named resource representing the stream of messages from
    # a single, specific topic, to be delivered to the subscribing application.
    # Message is a combination of data and attributes that a publisher sends to
    # a topic and is eventually delivered to subscribers.
    #
    #   require "gcloud"
    #
    #   gcloud = Gcloud.new
    #   pubsub = gcloud.pubsub
    #
    #   topic = pubsub.topic "my-topic"
    #   topic.publish "task completed"
    #
    # See Gcloud#pubsub
    class Project
      ##
      # The Connection object.
      attr_accessor :connection #:nodoc:

      ##
      # Creates a new Connection instance.
      def initialize project, credentials #:nodoc:
        project = project.to_s # Always cast to a string
        fail ArgumentError, "project is missing" if project.empty?
        @connection = Connection.new project, credentials
      end

      # The Pub/Sub project connected to.
      #
      # === Example
      #
      #   require "gcloud"
      #
      #   gcloud = Gcloud.new "my-todo-project",
      #                       "/path/to/keyfile.json"
      #   pubsub = gcloud.pubsub
      #
      #   pubsub.project #=> "my-todo-project"
      #
      def project
        connection.project
      end

      ##
      # Default project.
      def self.default_project #:nodoc:
        ENV["PUBSUB_PROJECT"] ||
          ENV["GCLOUD_PROJECT"] ||
          ENV["GOOGLE_CLOUD_PROJECT"] ||
          Gcloud::GCE.project_id
      end

      # rubocop:disable Metrics/AbcSize
      # Disabled rubocop because there isn't much benefit to adding indirection

      ##
      # Retrieves topic by name.
      #
      # === Parameters
      #
      # +topic_name+::
      #   Name of a topic. (+String+)
      # +options+::
      #   An optional Hash for controlling additional behavior. (+Hash+)
      # <code>options[:autocreate]</code>::
      #   Flag to control whether the requested topic will be created if it does
      #   not exist. Ignored if +skip_lookup+ is +true+. The default value is
      #   +false+. (+Boolean+)
      # <code>options[:project]</code>::
      #   If the topic belongs to a project other than the one currently
      #   connected to, the alternate project ID can be specified here.
      #   (+String+)
      # <code>options[:skip_lookup]</code>::
      #   Optionally create a Topic object without verifying the topic resource
      #   exists on the Pub/Sub service. Calls made on this object will raise
      #   errors if the topic resource does not exist. Default is +false+.
      #   (+Boolean+)
      #
      # === Returns
      #
      # Gcloud::Pubsub::Topic or nil if topic does not exist. Will return a
      # newly created Gcloud::Pubsub::Topic if the topic does not exist and
      # +autocreate+ is set to +true+.
      #
      # === Examples
      #
      #   require "gcloud"
      #
      #   gcloud = Gcloud.new
      #   pubsub = gcloud.pubsub
      #   topic = pubsub.topic "existing-topic"
      #
      # By default +nil+ will be returned if the topic does not exist.
      # the topic will be created in Pub/Sub when needed.
      #
      #   require "gcloud"
      #
      #   gcloud = Gcloud.new
      #   pubsub = gcloud.pubsub
      #   topic = pubsub.topic "non-existing-topic" #=> nil
      #
      # The topic will be created if the topic does not exist and the
      # +autocreate+ option is set to true.
      #
      #   require "gcloud"
      #
      #   gcloud = Gcloud.new
      #   pubsub = gcloud.pubsub
      #   topic = pubsub.topic "non-existing-topic", autocreate: true
      #
      # A topic in a different project can be created using the +project+ flag.
      #
      #   require "gcloud"
      #
      #   gcloud = Gcloud.new
      #   pubsub = gcloud.pubsub
      #   topic = pubsub.topic "another-topic", project: "another-project"
      #
      # The lookup against the Pub/Sub service can be skipped using the
      # +skip_lookup+ option:
      #
      #   require "gcloud"
      #
      #   gcloud = Gcloud.new
      #   pubsub = gcloud.pubsub
      #   topic = pubsub.topic "another-topic", skip_lookup: true
      #
      def topic topic_name, options = {}
        ensure_connection!
        if options[:skip_lookup]
          return Topic.new_lazy(topic_name, connection, options)
        end
        resp = connection.get_topic topic_name
        return Topic.from_gapi(resp.data, connection) if resp.success?
        if resp.status == 404
          return create_topic(topic_name) if options[:autocreate]
          return nil
        end
        fail ApiError.from_response(resp)
      end
      alias_method :get_topic, :topic
      alias_method :find_topic, :topic

      # rubocop:enable Metrics/AbcSize

      ##
      # Creates a new topic.
      #
      # === Parameters
      #
      # +topic_name+::
      #   Name of a topic. (+String+)
      #
      # === Returns
      #
      # Gcloud::Pubsub::Topic
      #
      # === Examples
      #
      #   require "gcloud"
      #
      #   gcloud = Gcloud.new
      #   pubsub = gcloud.pubsub
      #   topic = pubsub.create_topic "my-topic"
      #
      def create_topic topic_name
        ensure_connection!
        resp = connection.create_topic topic_name
        if resp.success?
          Topic.from_gapi resp.data, connection<commit_msg>Disable method length check<commit_after>#--
# Copyright 2015 Google Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

require "gcloud/gce"
require "gcloud/pubsub/connection"
require "gcloud/pubsub/credentials"
require "gcloud/pubsub/errors"
require "gcloud/pubsub/topic"

module Gcloud
  module Pubsub
    ##
    # = Project
    #
    # Represents the project that pubsub messages are pushed to and pulled from.
    # Topic is a named resource to which messages are sent by publishers.
    # Subscription is a named resource representing the stream of messages from
    # a single, specific topic, to be delivered to the subscribing application.
    # Message is a combination of data and attributes that a publisher sends to
    # a topic and is eventually delivered to subscribers.
    #
    #   require "gcloud"
    #
    #   gcloud = Gcloud.new
    #   pubsub = gcloud.pubsub
    #
    #   topic = pubsub.topic "my-topic"
    #   topic.publish "task completed"
    #
    # See Gcloud#pubsub
    class Project
      ##
      # The Connection object.
      attr_accessor :connection #:nodoc:

      ##
      # Creates a new Connection instance.
      def initialize project, credentials #:nodoc:
        project = project.to_s # Always cast to a string
        fail ArgumentError, "project is missing" if project.empty?
        @connection = Connection.new project, credentials
      end

      # The Pub/Sub project connected to.
      #
      # === Example
      #
      #   require "gcloud"
      #
      #   gcloud = Gcloud.new "my-todo-project",
      #                       "/path/to/keyfile.json"
      #   pubsub = gcloud.pubsub
      #
      #   pubsub.project #=> "my-todo-project"
      #
      def project
        connection.project
      end

      ##
      # Default project.
      def self.default_project #:nodoc:
        ENV["PUBSUB_PROJECT"] ||
          ENV["GCLOUD_PROJECT"] ||
          ENV["GOOGLE_CLOUD_PROJECT"] ||
          Gcloud::GCE.project_id
      end

      # rubocop:disable Metrics/AbcSize
      # rubocop:disable Metrics/MethodLength
      # Disabled rubocop because there isn't much benefit to adding indirection

      ##
      # Retrieves topic by name.
      #
      # === Parameters
      #
      # +topic_name+::
      #   Name of a topic. (+String+)
      # +options+::
      #   An optional Hash for controlling additional behavior. (+Hash+)
      # <code>options[:autocreate]</code>::
      #   Flag to control whether the requested topic will be created if it does
      #   not exist. Ignored if +skip_lookup+ is +true+. The default value is
      #   +false+. (+Boolean+)
      # <code>options[:project]</code>::
      #   If the topic belongs to a project other than the one currently
      #   connected to, the alternate project ID can be specified here.
      #   (+String+)
      # <code>options[:skip_lookup]</code>::
      #   Optionally create a Topic object without verifying the topic resource
      #   exists on the Pub/Sub service. Calls made on this object will raise
      #   errors if the topic resource does not exist. Default is +false+.
      #   (+Boolean+)
      #
      # === Returns
      #
      # Gcloud::Pubsub::Topic or nil if topic does not exist. Will return a
      # newly created Gcloud::Pubsub::Topic if the topic does not exist and
      # +autocreate+ is set to +true+.
      #
      # === Examples
      #
      #   require "gcloud"
      #
      #   gcloud = Gcloud.new
      #   pubsub = gcloud.pubsub
      #   topic = pubsub.topic "existing-topic"
      #
      # By default +nil+ will be returned if the topic does not exist.
      # the topic will be created in Pub/Sub when needed.
      #
      #   require "gcloud"
      #
      #   gcloud = Gcloud.new
      #   pubsub = gcloud.pubsub
      #   topic = pubsub.topic "non-existing-topic" #=> nil
      #
      # The topic will be created if the topic does not exist and the
      # +autocreate+ option is set to true.
      #
      #   require "gcloud"
      #
      #   gcloud = Gcloud.new
      #   pubsub = gcloud.pubsub
      #   topic = pubsub.topic "non-existing-topic", autocreate: true
      #
      # A topic in a different project can be created using the +project+ flag.
      #
      #   require "gcloud"
      #
      #   gcloud = Gcloud.new
      #   pubsub = gcloud.pubsub
      #   topic = pubsub.topic "another-topic", project: "another-project"
      #
      # The lookup against the Pub/Sub service can be skipped using the
      # +skip_lookup+ option:
      #
      #   require "gcloud"
      #
      #   gcloud = Gcloud.new
      #   pubsub = gcloud.pubsub
      #   topic = pubsub.topic "another-topic", skip_lookup: true
      #
      def topic topic_name, options = {}
        ensure_connection!
        if options[:skip_lookup]
          return Topic.new_lazy(topic_name, connection, options)
        end
        resp = connection.get_topic topic_name
        return Topic.from_gapi(resp.data, connection) if resp.success?
        if resp.status == 404
          return create_topic(topic_name) if options[:autocreate]
          return nil
        end
        fail ApiError.from_response(resp)
      end
      alias_method :get_topic, :topic
      alias_method :find_topic, :topic

      # rubocop:enable Metrics/AbcSize
      # rubocop:enable Metrics/MethodLength

      ##
      # Creates a new topic.
      #
      # === Parameters
      #
      # +topic_name+::
      #   Name of a topic. (+String+)
      #
      # === Returns
      #
      # Gcloud::Pubsub::Topic
      #
      # === Examples
      #
      #   require "gcloud"
      #
      #   gcloud = Gcloud.new
      #   pubsub = gcloud.pubsub
      #   topic = pubsub.create_topic "my-topic"
      #
      def create_topic topic_name
        ensure_connection!
        resp = connection.create_topic topic_name
        if resp.success?
          Topic.from_gapi resp.data, connection
{'fraction_non_alphanumeric': 0.09031854112859043, 'fraction_numerical': 0.0017218439383266807, 'mean_word_length': 2.0924491771539206, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1754941', 'n_tokens_mistral': 3732, 'n_tokens_neox': 3653, 'n_words': 1184}