Sha256: 611f630fca0c4dc2205c4483d77f994f507ea6793f01d3713c26b589579bde3d

Contents?: true

Size: 1.93 KB

Versions: 2

Compression:

Stored size: 1.93 KB

Contents

module HackerOne
  module Client
    module Activities
      class Activity
        delegate :message, :created_at, :updated_at, to: :attributes
        delegate :actor, to: :relationships

        def initialize(activity)
          @activity = OpenStruct.new activity
        end

        def internal?
          attributes.internal
        end

        private

        def relationships
          OpenStruct.new(activity.relationships)
        end

        def attributes
          OpenStruct.new(activity.attributes)
        end

        attr_reader :activity
      end

      class BountyAwarded < Activity
        def bounty_amount
          formatted_bounty_amount = attributes.bounty_amount || "0"
          formatted_bounty_amount.gsub(/[^\d]/, "").to_i
        end

        def bonus_amount
          formatted_bonus_amount = attributes.bonus_amount || "0"
          formatted_bonus_amount.gsub(/[^\d]/, "").to_i
        end
      end

      class SwagAwarded < Activity
        delegate :swag, to: :relationships
      end

      class UserAssignedToBug < Activity
        delegate :assigned_user, to: :relationships
      end

      class BugTriaged < Activity
      end

      class ReferenceIdAdded < Activity
        delegate :reference, :reference_url, to: :attributes
      end

      class CommentAdded < Activity
        delegate :message, :internal, to: :attributes
      end

      ACTIVITY_TYPE_CLASS_MAPPING = {
        'activity-bounty-awarded' => BountyAwarded,
        'activity-swag-awarded' => SwagAwarded,
        'activity-user-assigned-to-bug' => UserAssignedToBug,
        'activity-bug-triaged' => BugTriaged,
        'activity-reference-id-added' => ReferenceIdAdded,
        'activity-comment' => CommentAdded
      }.freeze

      def self.build(activity_data)
        activity_type_class = ACTIVITY_TYPE_CLASS_MAPPING.fetch \
          activity_data[:type], Activity

        activity_type_class.new activity_data
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
hackerone-client-0.7.0 lib/hackerone/client/activity.rb
hackerone-client-0.6.0 lib/hackerone/client/activity.rb