diff --git a/Gemfile b/Gemfile index 4751dd7b7b..9ec37ddfd0 100644 --- a/Gemfile +++ b/Gemfile @@ -13,6 +13,9 @@ gem "active_model_serializers", "~> 0.10.0" # ActiveRecord Session store for server side storage of session data gem 'activerecord-session_store' +# Allowing for URI templates, for HTTP clients +gem 'addressable', '~> 2.6' + # Pagination gem "api-pagination" diff --git a/Gemfile.lock b/Gemfile.lock index 1afffc6108..c7a62645c4 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -522,6 +522,7 @@ DEPENDENCIES active_model_serializers (~> 0.10.0) activejob-traffic_control activerecord-session_store + addressable (~> 2.6) api-pagination attr_encrypted (~> 3.1.0) autometal-piwik! diff --git a/app/jobs/publisher_removal_job.rb b/app/jobs/publisher_removal_job.rb index fc3e16c8cf..d61757daef 100644 --- a/app/jobs/publisher_removal_job.rb +++ b/app/jobs/publisher_removal_job.rb @@ -5,6 +5,7 @@ def perform(publisher_id:) publisher = Publisher.find_by(id: publisher_id) return if publisher.suspended? publisher.status_updates.create(status: PublisherStatusUpdate::DELETED) + publisher.reload ActiveRecord::Base.transaction do publisher.update(email: nil) publisher.update(pending_email: nil) diff --git a/app/models/json_builders/channels_json_builder_v2.rb b/app/models/json_builders/channels_json_builder_v2.rb index 5009c420dc..7496adc7a0 100644 --- a/app/models/json_builders/channels_json_builder_v2.rb +++ b/app/models/json_builders/channels_json_builder_v2.rb @@ -73,9 +73,10 @@ def site_banner_details(channel) end end + # (Albert Wang): Note: Ordering is different from v1 def append_excluded @excluded_channel_ids.each do |excluded_channel_id| - @channels.push([excluded_channel_id, false, true, {}, nil]) + @channels.push([excluded_channel_id, false, true, "", {}]) end end end diff --git a/app/services/base_api_client.rb b/app/services/base_api_client.rb index 781fbec3a3..8260cf48da 100644 --- a/app/services/base_api_client.rb +++ b/app/services/base_api_client.rb @@ -1,10 +1,68 @@ class BaseApiClient < BaseService + # Make a GET request. + # + # path - [String] the path relative to the endpoint + # options - [Hash] the parameters to supply + # + # returns the response + def get(path, options = {}) + request(:get, path, params: options) + end + + # Make a POST request. + # + # path - [String] the path relative to the endpoint + # options - [Hash] the parameters to supply + # + # returns - [Response] the response + def post(path, options = {}) + request(:post, path, form: options) + end + + # Make a PUT request. + # + # path - [String] the path relative to the endpoint + # options - [Hash] the parameters to supply + # + # returns [Response] the response + def put(path, options = {}) + request(:put, path, form: options) + end + + # Make a PATCH request. + # + # path - [String] the path relative to the endpoint + # options - [Hash] the parameters to supply + # + # returns [Response] the response + def patch(path, options = {}) + request(:patch, path, form: options) + end + + # Make a DELETE request. + # path - [String] the path relative to the endpoint + # options - [Hash] the parameters to supply + # returns [Response] the response + def delete(path, options = {}) + request(:delete, path, form: options) + end + private def api_base_uri raise "specify me" end + def request(method, path, payload = {}) + @connection.send(method) do |req| + req.url [api_base_uri, path].join('') + req.headers["Authorization"] = api_authorization_header + req.headers['Content-Type'] = 'application/json' + req.body = payload.to_json if method.to_sym.eql?(:post) + req.params = payload if method.to_sym.eql?(:get) + end + end + def connection @connection ||= begin require "faraday" diff --git a/app/services/promo/client.rb b/app/services/promo/client.rb new file mode 100644 index 0000000000..ac3a3df7bc --- /dev/null +++ b/app/services/promo/client.rb @@ -0,0 +1,25 @@ +module Promo + class Client < BaseApiClient + def initialize(connection = nil, options = {}) + @connection = connection + end + + def owner_state + Promo::Models::OwnerState.new(connection) + end + + private + + def perform_offline + true + end + + def api_base_uri + Rails.application.secrets[:api_promo_base_uri] + end + + def api_authorization_header + "Bearer #{Rails.application.secrets[:api_promo_key]}" + end + end +end diff --git a/app/services/promo/models/owner_state.rb b/app/services/promo/models/owner_state.rb new file mode 100644 index 0000000000..5b90c9fc0a --- /dev/null +++ b/app/services/promo/models/owner_state.rb @@ -0,0 +1,59 @@ +require 'addressable/template' +require 'json' + +module Promo + module Models + class OwnerState < Client + # For more information about how these URI templates are structured read the explaination in the RFC + # https://www.rfc-editor.org/rfc/rfc6570.txt + PATH = Addressable::Template.new("/api/2/promo/owners/{id}/states{/state}") + + class State + SUSPEND = "suspend".freeze + NO_UGP = "no_ugp".freeze + end + + def initialize(connection, params = {}) + super(connection, params) + end + + # Finds the current state of the owner on the promo server + # + # @param [String] id The publisher id to find on the promo server. + # + # @return [array] of owner states + def find(id:) + response = get(PATH.expand(id: id)) + + JSON.parse(response.body) + end + + # Creates a new state for the specified owner + # + # @param [String] id The publisher id + # @state [Promo::Models::OwnerState::State] state The state to put the owner into. + # + # @return [true] if create was a success + def create(id:, state:) + response = put(PATH.expand(id: id, state: state)) + + # response.body returns an array of owner states + # ["no_ugp", "suspend"] + JSON.parse(response.body).include? state + end + + # Removes the state for the specified owner + # + # @param [String] id The publisher id + # @state [Promo::Models::OwnerState::State] state The state to remove from the owner. + # + # @return [true] if destroy was a success + def destroy(id:, state:) + response = delete(PATH.expand(id: id, state: state)) + + # response.body returns an array of owner states + JSON.parse(response.body).exclude? state + end + end + end +end diff --git a/app/views/publishers/_choose_channel_type.html.slim b/app/views/publishers/_choose_channel_type.html.slim index 4541a4be80..5caaa61d20 100644 --- a/app/views/publishers/_choose_channel_type.html.slim +++ b/app/views/publishers/_choose_channel_type.html.slim @@ -27,7 +27,6 @@ = t "publishers.choose_new_channel_type.twitch.description" = link_to(publisher_register_twitter_channel_omniauth_authorize_path, method: :post, class: 'card block-link--card', :"data-piwik-action" => "AddTwitterChannelClicked", :"data-piwik-name" => "Clicked", :"data-piwik-value" => "AddChannelFlow") do - .new-tag .card-body .icon-and-text--icon = image_tag "choose-channel/twitter.svg", class: "choose-channel--icon" @@ -35,15 +34,14 @@ h6= t "publishers.choose_new_channel_type.twitter.heading" = t "publishers.choose_new_channel_type.twitter.description" -- if params[:vimeo] - = link_to(publisher_register_vimeo_channel_omniauth_authorize_path, method: :post, class: 'card block-link--card', :"data-piwik-action" => "AddVimeoChannelClicked", :"data-piwik-name" => "Clicked", :"data-piwik-value" => "AddChannelFlow") do - .new-tag - .card-body - .icon-and-text--icon - = image_tag "choose-channel/vimeo.svg", class: "choose-channel--icon" - .icon-and-text--text - h6= t "publishers.choose_new_channel_type.vimeo.heading" - = t "publishers.choose_new_channel_type.vimeo.description" += link_to(publisher_register_vimeo_channel_omniauth_authorize_path, method: :post, class: 'card block-link--card', :"data-piwik-action" => "AddVimeoChannelClicked", :"data-piwik-name" => "Clicked", :"data-piwik-value" => "AddChannelFlow") do + .new-tag + .card-body + .icon-and-text--icon + = image_tag "choose-channel/vimeo.svg", class: "choose-channel--icon" + .icon-and-text--text + h6= t "publishers.choose_new_channel_type.vimeo.heading" + = t "publishers.choose_new_channel_type.vimeo.description" = link_to(publisher_register_reddit_channel_omniauth_authorize_path, method: :post, class: 'card block-link--card', :"data-piwik-action" => "AddRedditChannelClicked", :"data-piwik-name" => "Clicked", :"data-piwik-value" => "AddChannelFlow") do .new-tag