gitlab-sdk-0.3.1/0000755000175000017500000000000014576531503012231 5ustar raviravigitlab-sdk-0.3.1/Rakefile0000644000175000017500000000022114576531503013671 0ustar raviravi# frozen_string_literal: true require "bundler/gem_tasks" require "rspec/core/rake_task" RSpec::Core::RakeTask.new(:spec) task default: :spec gitlab-sdk-0.3.1/README.md0000644000175000017500000000710414576531503013512 0ustar raviravi# GitLab Application SDK - Ruby This SDK is for using GitLab Application Services with Ruby. ## How to use the SDK ### Using the gem Add the gem to your Gemfile: ```ruby gem 'gitlab-sdk' ``` ### Using the client Initialize the client: ```ruby client = GitlabSDK::Client.new(app_id: 'YOUR_APP_ID', host: 'YOUR_HOST') ``` ## Client initialization options | Option | Description | |:----------------|:---------------------------------------------------------------------------------------------------------------------------------------------------| | `app_id` | The ID specified in the GitLab Project Analytics setup guide. It ensures your data is sent to your analytics instance. | | `host` | The GitLab Project Analytics instance specified in the setup guide. When using a non-standard port, includde it here, e.g. `http://localhost:9091` | | `buffer_size` | Optional. Default `1`. How many events are sent in one request at a time. Setting more than `1` will change the HTTP method from `GET` to `POST`. | | `async` | Optional. Default `true`. Use `AsyncEmitter` instead of `Emitter` for non-blocking requests. | For more details see Snowplow Ruby Tracker [docs](https://snowplow.github.io/snowplow-ruby-tracker/). ## Methods ### `flush` Used to manually flush all events from Snowplow Ruby Tracker's emitters, defaults to synchronous. ```ruby # Flush events synchronously (default) client.flush_events # Flush events asynchronously client.flush_events(async: true) ``` | Property | Type | Description | |:-----------------|:----------|:--------------------------------------------------------------------------| | `async` | `Boolean` | Optional. Default `false`. Use `true` to flush all events asynchronously. | ### `identify` Used to associate a user and their attributes with the session and tracking events. ```ruby client.identify('123abc', { user_name: 'Matthew' }) ``` | Property | Type | Description | |:-----------------|:----------|:-------------------------------------------------------------------------| | `user_id` | `String` | The ID of the user. | | `user_attributes` | `Hash` | Optional. The user attributes to add to the session and tracking events. | ### `track` Used to trigger a custom event. ```ruby client.track(event_name, event_attributes) ``` | Property | Type | Description | |:------------------|:----------|:-----------------------------------------------------------------| | `event_name` | `String` | The name of the event. | | `event_attributes` | `Hash` | The event attributes to add to the tracked event. | ## Developing with the devkit To develop with a local Snowplow pipeline, use Analytics devkit's [Snowplow setup](https://gitlab.com/gitlab-org/analytics-section/product-analytics/devkit/-/tree/main#setup). To test the gem's functionality, run `bin/console`. ## Releasing the gem To release a new version of the gem, create an MR using the "Release" template. After merging the MR, the gem will be automatically uploaded to [RubyGems](https://rubygems.org/gems/gitlab-sdk). gitlab-sdk-0.3.1/Gemfile.lock0000644000175000017500000000466614576531503014467 0ustar raviraviPATH remote: . specs: gitlab-sdk (0.3.1) activesupport (>= 5.2.0) rake (~> 13.0) snowplow-tracker (~> 0.8.0) GEM remote: https://rubygems.org/ specs: activesupport (7.0.4.3) concurrent-ruby (~> 1.0, >= 1.0.2) i18n (>= 1.6, < 2) minitest (>= 5.1) tzinfo (~> 2.0) addressable (2.8.4) public_suffix (>= 2.0.2, < 6.0) ast (2.4.2) concurrent-ruby (1.2.2) crack (0.4.5) rexml diff-lcs (1.5.0) gitlab-styles (10.0.0) rubocop (~> 1.43.0) rubocop-graphql (~> 0.18) rubocop-performance (~> 1.15) rubocop-rails (~> 2.17) rubocop-rspec (~> 2.18) hashdiff (1.0.1) i18n (1.12.0) concurrent-ruby (~> 1.0) json (2.6.3) minitest (5.18.0) parallel (1.22.1) parser (3.2.2.0) ast (~> 2.4.1) public_suffix (5.0.1) rack (3.0.7) rainbow (3.1.1) rake (13.1.0) regexp_parser (2.7.0) rexml (3.2.5) rspec (3.12.0) rspec-core (~> 3.12.0) rspec-expectations (~> 3.12.0) rspec-mocks (~> 3.12.0) rspec-core (3.12.1) rspec-support (~> 3.12.0) rspec-expectations (3.12.2) diff-lcs (>= 1.2.0, < 2.0) rspec-support (~> 3.12.0) rspec-mocks (3.12.5) diff-lcs (>= 1.2.0, < 2.0) rspec-support (~> 3.12.0) rspec-support (3.12.0) rubocop (1.43.0) json (~> 2.3) parallel (~> 1.10) parser (>= 3.2.0.0) rainbow (>= 2.2.2, < 4.0) regexp_parser (>= 1.8, < 3.0) rexml (>= 3.2.5, < 4.0) rubocop-ast (>= 1.24.1, < 2.0) ruby-progressbar (~> 1.7) unicode-display_width (>= 2.4.0, < 3.0) rubocop-ast (1.28.0) parser (>= 3.2.1.0) rubocop-capybara (2.17.1) rubocop (~> 1.41) rubocop-graphql (0.19.0) rubocop (>= 0.87, < 2) rubocop-performance (1.17.1) rubocop (>= 1.7.0, < 2.0) rubocop-ast (>= 0.4.0) rubocop-rails (2.19.0) activesupport (>= 4.2.0) rack (>= 1.1) rubocop (>= 1.33.0, < 2.0) rubocop-rspec (2.19.0) rubocop (~> 1.33) rubocop-capybara (~> 2.17) ruby-progressbar (1.13.0) snowplow-tracker (0.8.0) tzinfo (2.0.6) concurrent-ruby (~> 1.0) unicode-display_width (2.4.2) webmock (3.18.1) addressable (>= 2.8.0) crack (>= 0.3.2) hashdiff (>= 0.4.0, < 2.0.0) PLATFORMS arm64-darwin-21 DEPENDENCIES gitlab-sdk! gitlab-styles (~> 10.0.0) rspec (~> 3.0) webmock BUNDLED WITH 2.3.22 gitlab-sdk-0.3.1/gitlab-sdk.gemspec0000644000175000017500000000413014576531503015615 0ustar raviravi######################################################### # This file has been automatically generated by gem2tgz # ######################################################### # -*- encoding: utf-8 -*- # stub: gitlab-sdk 0.3.1 ruby lib Gem::Specification.new do |s| s.name = "gitlab-sdk".freeze s.version = "0.3.1" s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version= s.metadata = { "allowed_push_host" => "https://rubygems.org", "changelog_uri" => "https://gitlab.com/gitlab-org/analytics-section/product-analytics/gl-application-sdk-rb/-/releases", "homepage_uri" => "https://gitlab.com/gitlab-org/analytics-section/product-analytics/gl-application-sdk-rb", "source_code_uri" => "https://gitlab.com/gitlab-org/analytics-section/product-analytics/gl-application-sdk-rb" } if s.respond_to? :metadata= s.require_paths = ["lib".freeze] s.authors = ["GitLab".freeze] s.bindir = "exe".freeze s.date = "2024-02-29" s.description = "Client side Ruby SDK for GitLab Application services".freeze s.email = ["gitlab_rubygems@gitlab.com".freeze] s.files = [".rspec".freeze, ".rubocop.yml".freeze, "Gemfile".freeze, "Gemfile.lock".freeze, "README.md".freeze, "Rakefile".freeze, "lib/gitlab-sdk.rb".freeze, "lib/gitlab-sdk/client.rb".freeze, "lib/gitlab-sdk/current_user.rb".freeze, "lib/gitlab-sdk/version.rb".freeze] s.homepage = "https://gitlab.com/gitlab-org/analytics-section/product-analytics/gl-application-sdk-rb".freeze s.licenses = ["MIT".freeze] s.required_ruby_version = Gem::Requirement.new(">= 2.6.0".freeze) s.rubygems_version = "3.4.20".freeze s.summary = "Client side Ruby SDK for GitLab Application services".freeze s.specification_version = 4 s.add_runtime_dependency(%q.freeze, [">= 5.2.0"]) s.add_development_dependency(%q.freeze, ["~> 10.0.0"]) s.add_runtime_dependency(%q.freeze, ["~> 13.0"]) s.add_development_dependency(%q.freeze, ["~> 3.0"]) s.add_runtime_dependency(%q.freeze, ["~> 0.8.0"]) s.add_development_dependency(%q.freeze, [">= 0"]) end gitlab-sdk-0.3.1/Gemfile0000644000175000017500000000017614576531503013530 0ustar raviravi# frozen_string_literal: true source "https://rubygems.org" # Specify your gem's dependencies in gitlab-sdk.gemspec gemspec gitlab-sdk-0.3.1/lib/0000755000175000017500000000000014576531503012777 5ustar raviravigitlab-sdk-0.3.1/lib/gitlab-sdk.rb0000644000175000017500000000010414576531503015340 0ustar raviravi# frozen_string_literal: true require_relative "gitlab-sdk/client" gitlab-sdk-0.3.1/lib/gitlab-sdk/0000755000175000017500000000000014576531503015020 5ustar raviravigitlab-sdk-0.3.1/lib/gitlab-sdk/client.rb0000644000175000017500000000526214576531503016630 0ustar raviravi# frozen_string_literal: true require_relative "version" require_relative "current_user" require 'snowplow-tracker' module GitlabSDK class Client SCHEMAS = { custom_event: 'iglu:com.gitlab/custom_event/jsonschema/1-0-0', user_context: 'iglu:com.gitlab/user_context/jsonschema/1-0-0' }.freeze DEFAULT_TRACKER_NAMESPACE = 'gitlab' USERAGENT = "GitLab Analytics Ruby SDK/#{GitlabSDK::VERSION}" HostHasNoSchemeError = Class.new(StandardError) def initialize(app_id:, host:, buffer_size: 1, async: true) emitter = build_emitter(host, buffer_size: buffer_size, async: async) @tracker = SnowplowTracker::Tracker.new( emitters: emitter, app_id: app_id, namespace: DEFAULT_TRACKER_NAMESPACE ) end def track(event_name, event_payload = {}) self_desc_json = SnowplowTracker::SelfDescribingJson.new( SCHEMAS[:custom_event], name: event_name, props: event_payload ) track_arguments = { event_json: self_desc_json } set_subject_data set_user_context(track_arguments) tracker.track_self_describing_event(**track_arguments) end def identify(user_id, user_attributes = {}) GitlabSDK::CurrentUser.user_id = user_id GitlabSDK::CurrentUser.user_attributes = user_attributes end def flush_events(async: false) tracker.flush(async: async) end private attr_reader :tracker def build_emitter(host, buffer_size:, async:) uri = URI(host) raise HostHasNoSchemeError unless uri.scheme raise ArgumentError, 'buffer_size has to be positive' unless buffer_size.positive? endpoint = "#{uri.hostname}:#{uri.port}#{uri.path}" method = buffer_size > 1 ? 'post' : 'get' emitter_class = async ? SnowplowTracker::AsyncEmitter : SnowplowTracker::Emitter emitter_class.new( endpoint: endpoint, options: { protocol: uri.scheme, method: method, buffer_size: buffer_size } ) end def set_subject_data subject = SnowplowTracker::Subject.new set_user_id_on_subject(subject) subject.set_useragent(USERAGENT) tracker.set_subject(subject) end def set_user_id_on_subject(subject) user_id = GitlabSDK::CurrentUser.user_id subject.set_user_id(user_id) if user_id end def set_user_context(track_arguments) user_attributes = GitlabSDK::CurrentUser.user_attributes || {} return if user_attributes.empty? user_context = SnowplowTracker::SelfDescribingJson.new( SCHEMAS[:user_context], user_attributes ) track_arguments[:context] = [user_context] end end end gitlab-sdk-0.3.1/lib/gitlab-sdk/current_user.rb0000644000175000017500000000026614576531503020071 0ustar raviravi# frozen_string_literal: true require 'active_support' module GitlabSDK class CurrentUser < ::ActiveSupport::CurrentAttributes attribute :user_id, :user_attributes end end gitlab-sdk-0.3.1/lib/gitlab-sdk/version.rb0000644000175000017500000000011014576531503017022 0ustar raviravi# frozen_string_literal: true module GitlabSDK VERSION = "0.3.1" end gitlab-sdk-0.3.1/.rubocop.yml0000644000175000017500000000057414576531503014511 0ustar raviraviinherit_gem: gitlab-styles: - rubocop-default.yml CodeReuse/ActiveRecord: Exclude: - spec/**/*.rb RSpec/FilePath: Exclude: - spec/gitlab-sdk/version_spec.rb CustomTransform: GitlabSDK: gitlab-sdk RSpec/MultipleMemoizedHelpers: Max: 10 Naming/FileName: Exclude: - lib/gitlab-sdk.rb RSpec/MultipleMemoizedHelpers: Max: 25 AllowSubject: true gitlab-sdk-0.3.1/.rspec0000644000175000017500000000006514576531503013347 0ustar raviravi--format documentation --color --require spec_helper