Skip to content

Commit

Permalink
Merge branch 'master' of github.com:adzerk/adzerk-management-sdk-ruby
Browse files Browse the repository at this point in the history
  • Loading branch information
CrshOverride committed Apr 14, 2021
2 parents 59aecef + 2d8b476 commit fe16045
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 51 deletions.
80 changes: 40 additions & 40 deletions .github/workflows/ruby.yml
Original file line number Diff line number Diff line change
@@ -1,46 +1,46 @@
name: Ruby
# name: Ruby

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
schedule:
- cron: "0 9 * * 4"
# on:
# push:
# branches: [ master ]
# pull_request:
# branches: [ master ]
# schedule:
# - cron: "0 9 * * 4"

jobs:
test:
# jobs:
# test:

runs-on: ubuntu-latest
# runs-on: ubuntu-latest

strategy:
max-parallel: 1
matrix:
ruby-version: [2.5.7, 2.6, 2.7.0, 3.0.0]
# strategy:
# max-parallel: 1
# matrix:
# ruby-version: [2.5.7, 2.6, 2.7.0, 3.0.0]

steps:
- uses: actions/checkout@v2
- name: Set up Ruby
# To automatically get bug fixes and new Ruby versions for ruby/setup-ruby,
# change this to (see https://github.com/ruby/setup-ruby#versioning):
uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby-version }}
- name: Install dependencies
run: bundle install
- name: Run tests
env:
ADZERK_API_KEY: ${{ secrets.ADZERK_API_KEY }}
run: bundle exec rake
# steps:
# - uses: actions/checkout@v2
# - name: Set up Ruby
# # To automatically get bug fixes and new Ruby versions for ruby/setup-ruby,
# # change this to (see https://github.com/ruby/setup-ruby#versioning):
# uses: ruby/setup-ruby@v1
# with:
# ruby-version: ${{ matrix.ruby-version }}
# - name: Install dependencies
# run: bundle install
# - name: Run tests
# env:
# ADZERK_API_KEY: ${{ secrets.ADZERK_API_KEY }}
# run: bundle exec rake

slack:
needs: test
runs-on: ubuntu-latest
if: always()
steps:
- uses: technote-space/workflow-conclusion-action@v2
- uses: 8398a7/action-slack@v3
with:
status: ${{ env.WORKFLOW_CONCLUSION }}
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
# slack:
# needs: test
# runs-on: ubuntu-latest
# if: always()
# steps:
# - uses: technote-space/workflow-conclusion-action@v2
# - uses: 8398a7/action-slack@v3
# with:
# status: ${{ env.WORKFLOW_CONCLUSION }}
# env:
# SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
52 changes: 41 additions & 11 deletions lib/adzerk/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ class Client
VERSION = Gem.loaded_specs['adzerk'].version.to_s
SDK_HEADER_NAME = 'X-Adzerk-Sdk-Version'
SDK_HEADER_VALUE = "adzerk-management-sdk-ruby:#{VERSION}"
BASE_SLEEP = 0.25
MAX_SLEEP = 5
MAX_ATTEMPTS = 10

DEFAULTS = {
:host => ENV["ADZERK_API_HOST"] || 'https://api.adzerk.net/',
Expand Down Expand Up @@ -90,35 +93,62 @@ def put_json_request(url, data, version: 'v1')
end

def create_creative(data={}, image_path='', version: 'v1')
response = RestClient.post(@config[:host] + version + '/creative',
{:creative => camelize_data(data).to_json},
:X_Adzerk_ApiKey => @api_key,
:X_Adzerk_Sdk_Version => SDK_HEADER_VALUE,
:accept => :json)
response = nil
attempt = 0

loop do
response = RestClient.post(@config[:host] + version + '/creative',
{:creative => camelize_data(data).to_json},
:X_Adzerk_ApiKey => @api_key,
:X_Adzerk_Sdk_Version => SDK_HEADER_VALUE,
:accept => :json)
break if response.code != 429 or attempt >= (@config[:max_attempts] || MAX_ATTEMPTS)
sleep(rand(0.0..[MAX_SLEEP, BASE_SLEEP * 2 ** attempt].min()))
attempt += 1
end
response = upload_creative(JSON.parse(response)["Id"], image_path) unless image_path.empty?
response
end

def upload_creative(id, image_path, size_override: false, version: 'v1')
response = nil
attempt = 0
image = File.new(image_path, 'rb')
url = @config[:host] + version + '/creative/' + id.to_s + '/upload'
url += '?sizeOverride=true' if size_override
RestClient.post(url,
{:image => image},
"X-Adzerk-ApiKey" => @api_key,
SDK_HEADER_NAME => SDK_HEADER_VALUE,
:accept => :mime)
loop do
response = RestClient.post(url,
{:image => image},
"X-Adzerk-ApiKey" => @api_key,
SDK_HEADER_NAME => SDK_HEADER_VALUE,
:accept => :mime)

break if response.code != 429 or attempt >= (@config[:max_attempts] || MAX_ATTEMPTS)
sleep(rand(0.0..[MAX_SLEEP, BASE_SLEEP * 2 ** attempt].min()))
attempt += 1
end
response
end

def send_request(request, uri)
response = nil
attempt = 0
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = uri.scheme == 'https'
response = http.request(request)

loop do
response = http.request(request)
break if response.code != "429" or attempt >= (@config[:max_attempts] || MAX_ATTEMPTS)
sleep(rand(0.0..[MAX_SLEEP, BASE_SLEEP * 2 ** attempt].min()))
attempt += 1
end

if response.kind_of? Net::HTTPClientError or response.kind_of? Net::HTTPServerError
error_response = JSON.parse(response.body)
msg = error_response["message"] || error_response["Error"] || response.body
raise Adzerk::ApiError.new(msg)
end

response
end

Expand Down
4 changes: 4 additions & 0 deletions test/creative_template_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@
type: 'String',
variable: 'ctTitle',
required: true,
ad_query: false
}, {
name: 'Thumbnail',
description: 'The URL of a Thumbnail Image',
type: 'String',
variable: 'ctThumbnailUrl',
required: false,
ad_query: true
}],
contents: [{
type: 'Raw',
Expand Down Expand Up @@ -60,12 +62,14 @@
type: 'String',
variable: 'ctTitle',
required: true,
ad_query: false,
}, {
name: 'Thumbnail',
description: 'The URL of a Thumbnail Image',
type: 'String',
variable: 'ctThumbnailUrl',
required: false,
ad_query: true,
}],
contents: [{
type: 'Raw',
Expand Down

0 comments on commit fe16045

Please sign in to comment.