diff --git a/.github/.example/.bundle/config b/.github/.example/.bundle/config new file mode 100644 index 00000000..23692288 --- /dev/null +++ b/.github/.example/.bundle/config @@ -0,0 +1,2 @@ +--- +BUNDLE_PATH: "vendor/bundle" diff --git a/.github/.example/Gemfile b/.github/.example/Gemfile new file mode 100644 index 00000000..8e8f9406 --- /dev/null +++ b/.github/.example/Gemfile @@ -0,0 +1,6 @@ +source 'https://rubygems.org' + +gem "typhoeus" +gem "bundler" + +gem "talon_one", path: "../.." \ No newline at end of file diff --git a/.github/.example/example.rb b/.github/.example/example.rb new file mode 100644 index 00000000..5d8e8538 --- /dev/null +++ b/.github/.example/example.rb @@ -0,0 +1,85 @@ +# Load the gem +require 'talon_one' + +# Setup authorization +TalonOne.configure do |config| + # Configure the API host destination while explicitly using HTTPS as the default is HTTP. + config.scheme = 'http' + config.host = 'localhost:9000' + + # Configure API key authorization: api_key_v1 + config.api_key['Authorization'] = ENV["TALON_API_KEY"] + config.api_key_prefix['Authorization'] = 'ApiKey-v1' +end + +# Integration API example to send a session update +integration_api = TalonOne::IntegrationApi.new + +session_integration_id = '8fb7129e-68e6-4464-9631-09b588391619' # String | The integration identifier of the session + +# NewCustomerSessionV2 object +customer_session_v2 = TalonOne::NewCustomerSessionV2.new( + profile_id: 'Some_1', + state: 'open', + cart_items: [ + TalonOne::CartItem.new( + name: 'Nigiri Sake', + sku: 'sush1', + quantity: 2, + price: 3.7, + category: 'Sushi' + ), + TalonOne::CartItem.new( + name: 'Rainbow Roll I/O', + sku: 'sush2', + quantity: 1, + price: 6.5, + category: 'Sushi' + ), + TalonOne::CartItem.new( + name: 'Kirin', + sku: 'k1r', + quantity: 2, + price: 2.2, + category: 'Beverages' + ), + ], + coupon_codes: [ + 'Cool-Stuff!' + ] +) + +# Instantiating a new IntegrationRequest object +integration_request = TalonOne::IntegrationRequest.new( + customer_session: customer_session_v2, + # Optional list of requested information to be present on the response. + # See docs/IntegrationRequest.md for full list + # response_content: [ + # 'customerSession' + # ] +) + +begin + # Create/update a customer session using `update_customer_session_v2` function + result = integration_api.update_customer_session_v2(session_integration_id, integration_request) + + # Prints response to the console + puts result + + # Parsing the returned effects list, please consult https://developers.talon.one/Integration-API/handling-effects-v2 for the full list of effects and their corresponding properties + result.effects.each do |effect| + if effect.effect_type == 'setDiscount' + # Initiating right props instance according to the effect type + props = TalonOne::SetDiscountEffectProps.build_from_hash(effect.props) + + # Access the specific effect's properties + puts "Set a discount '#{props.name}' of #{props.value}" + elsif effect.effect_type == 'rejectCoupon' + # Initiating right props instance according to the effect type + props = TalonOne::RejectCouponEffectProps.build_from_hash(effect.props) + + # Work with AcceptCouponEffectProps' properties + # ... + end + end +end \ No newline at end of file diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 00000000..a57e9eb1 --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,75 @@ +name: run tests + +on: [push] + +jobs: + test: + runs-on: ubuntu-latest + permissions: + contents: 'read' + id-token: 'write' + + steps: + - uses: actions/checkout@v4 + - name: Authenticate to Google Cloud + id: auth + uses: google-github-actions/auth@v1 + with: + token_format: access_token + workload_identity_provider: projects/949875736540/locations/global/workloadIdentityPools/external-pool/providers/github-provider + service_account: artifact-pusher@talon-artifacts.iam.gserviceaccount.com + - name: Login to GAR + uses: docker/login-action@v3 + with: + registry: europe-west3-docker.pkg.dev + username: oauth2accesstoken + password: ${{ steps.auth.outputs.access_token }} + - uses: hoverkraft-tech/compose-action@v2.0.2 + - uses: ruby/setup-ruby@v1 + with: + ruby-version: '3' + - name: Install dependencies + working-directory: .github/.example + run: | + bundle config set local.talon_one ../.. + bundle install + sudo apt-get install jq curl + - name: Run example + working-directory: .github/.example + run: | + ACCOUNT_RESPONSE=$(curl -s --location "http://localhost:9000/v1/accounts" \ + --header "Content-Type: application/json" \ + --data-raw '{ + "companyName": "demo", + "email": "integrationtest@talon.one", + "password": "Password1234!" + }'); + export TALON_USER_ID=$(echo $ACCOUNT_RESPONSE | jq ".userId"); + export TALON_USER_TOKEN=$(echo $ACCOUNT_RESPONSE | jq ".token" | tr -d '"'); + USER_RESPONSE=$(curl -s --location "http://localhost:9000/v1/users/$TALON_USER_ID" \ + --header "Authorization: Bearer $TALON_USER_TOKEN"); + export TALON_ACCOUNT_ID=$(echo $USER_RESPONSE | jq ".accountId"); + echo "User with ID $TALON_USER_ID and Token $TALON_USER_TOKEN was created for application $TALON_ACCOUNT_ID"; + APPLICATION_RESPONSE=$(curl -s --location "http://localhost:9000/v1/applications" \ + --header "Content-Type: application/json" \ + --header "Authorization: Bearer $TALON_USER_TOKEN" \ + --data-raw '{ + "name": "demo", + "currency": "EUR", + "timezone": "Europe/Berlin", + "enableFlattenedCartItems": false + }'); + export TALON_APPLICATION_ID=$(echo $USER_RESPONSE | jq ".id"); + echo "Application with ID $TALON_APPLICATION_ID was created" + API_KEY_RESPONSE=$(curl -s -v --location "http://localhost:9000/v1/applications/$TALON_APPLICATION_ID/apikeys" \ + --header "Content-Type: application/json" \ + --header "Authorization: Bearer $TALON_USER_TOKEN" \ + --data-raw '{ + "title": "Application HIT KEY", + "expires": "2099-01-01T0:00:00Z" + }'); + echo "Api-Key-Response: $API_KEY_RESPONSE"; + export TALON_API_KEY=$(echo $API_KEY_RESPONSE | jq ".key" | tr -d '"'); + echo "Api-Key $TALON_API_KEY created" + + ruby -Ilib example.rb; diff --git a/.gitignore b/.gitignore index ab93602d..c6bde232 100644 --- a/.gitignore +++ b/.gitignore @@ -39,4 +39,7 @@ build/ .rvmrc .idea -.DS_Store \ No newline at end of file +.DS_Store + +.github/.example/vendor +.github/.example/Gemfile.lock \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 00000000..3030a59c --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,47 @@ +services: + api-service: + image: europe-west3-docker.pkg.dev/talon-artifacts/talon-images/talon-service:master + depends_on: + - database-service + ports: + - "9000:9000" + environment: + - TALON_DB_NAME=talon + - TALON_DB_USER=talon + - TALON_DB_PASSWORD=talon.one.9000 + - TALON_DB_HOST=database-service + - TALON_DB_PORT=5432 + - TALON_ENABLE_WEBHOOK_WORKER_POOL=false + - TZ=UTC + - RELEASE_STAGE=ci + - TALON_CH_ENABLED=false + - TALON_DISABLE_PROFILER=true + - USE_REPLICA_DB=false + command: + - /talon/talon + healthcheck: + test: ["CMD", "curl", "-f", "http://localhost:9000/v1/status/health"] + interval: 10s + timeout: 5s + retries: 10 + restart: "on-failure:10" + + database-service: + image: docker.io/bitnami/postgresql:15 + volumes: + - 'postgresql_master_data:/bitnami/postgresql' + ports: + - "5433:5432" + environment: + - POSTGRESQL_DATABASE=talon + - POSTGRESQL_USERNAME=talon + - POSTGRESQL_PASSWORD=talon.one.9000 + healthcheck: + test: ["CMD-SHELL", "pg_isready -U talon -d talon"] + interval: 10s + timeout: 5s + retries: 5 + restart: "on-failure:10" + +volumes: + postgresql_master_data: