diff --git a/app/models/megaphone/api.rb b/app/models/megaphone/api.rb index b694bdb7e..0dafbba3e 100644 --- a/app/models/megaphone/api.rb +++ b/app/models/megaphone/api.rb @@ -4,8 +4,8 @@ class Api DEFAULT_ENDPOINT = "https://cms.megaphone.fm/api" - PAGINATION_HEADERS = %w(link x-per-page x-page x-total) - PAGINATION_LINKS = %w(first last next previous) + PAGINATION_HEADERS = %w[link x-per-page x-page x-total] + PAGINATION_LINKS = %w[first last next previous] def initialize(token:, network_id:, endpoint_url: nil) @token = token @@ -13,11 +13,11 @@ def initialize(token:, network_id:, endpoint_url: nil) @endpoint_url = endpoint_url end - def get(path, params={}, headers={}) + def get(path, params = {}, headers = {}) request = {url: join_url(path), headers: headers, params: params} response = get_url(request) data = incoming_body_filter(response.body) - if data.kind_of?(Array) + if data.is_a?(Array) pagination = pagination_from_headers(response.env.response_headers) {items: data, pagination: pagination, request: request, response: response} else @@ -25,15 +25,15 @@ def get(path, params={}, headers={}) end end - def post(path, body, headers={}) - response = connection({url: join_url(path), headers: headers}).post() do |req| + def post(path, body, headers = {}) + response = connection({url: join_url(path), headers: headers}).post do |req| req.body = outgoing_body_filter(body) end incoming_body_filter(response.body) end - def put(path, body, headers={}) - connection({url: join_url(path), headers: headers}).put() do |req| + def put(path, body, headers = {}) + connection({url: join_url(path), headers: headers}).put do |req| req.body = outgoing_body_filter(body) end incoming_body_filter(response.body) @@ -47,7 +47,7 @@ def api_base def pagination_from_headers(headers) paging = (headers || {}).slice(*PAGINATION_HEADERS).transform_keys do |h| - h.sub(/^x-/, "").gsub(/-/, "_").to_sym + h.sub(/^x-/, "").tr("-", "_").to_sym end [:page, :per_page, :total].each do |k| @@ -63,16 +63,15 @@ def parse_links(link_headers) return {} unless link_headers.present? collection = LinkHeaderParser.parse(link_headers, base: mp.api.api_base) links = collection.group_by_relation_type - PAGINATION_LINKS.inject({}) do |map, key| - if link = (links[key] || []).first + PAGINATION_LINKS.each_with_object({}) do |key, map| + if (link = (links[key] || []).first) map[key] = link.target_uri end - map end end def get_url(options) - response = connection(options).get() + connection(options).get end def join_url(*path) @@ -85,7 +84,7 @@ def incoming_body_filter(str) end def transform_keys(result) - if result.kind_of?(Array) + if result.is_a?(Array) result.map { |r| transform_keys(r) } elsif result.respond_to?(:deep_transform_keys) result.deep_transform_keys { |key| key.to_s.underscore.to_sym } diff --git a/app/models/megaphone/podcast.rb b/app/models/megaphone/podcast.rb index 325f2eed4..be9211f7d 100644 --- a/app/models/megaphone/podcast.rb +++ b/app/models/megaphone/podcast.rb @@ -1,33 +1,34 @@ module Megaphone class Podcast include ActiveModel::Model - attr_accessor :feed, :api + attr_accessor :feed + attr_writer :api # Required attributes for a create # external_id is not required by megaphone, but we need it to be set! - CREATE_REQUIRED = %w(title subtitle summary itunes_categories language external_id) + CREATE_REQUIRED = %w[title subtitle summary itunes_categories language external_id] # Other attributes available on create - CREATE_ATTRIBUTES = CREATE_REQUIRED + %w(link copyright author background_image_file_url + CREATE_ATTRIBUTES = CREATE_REQUIRED + %w[link copyright author background_image_file_url explicit owner_name owner_email slug original_rss_url itunes_identifier podtrac_enabled - google_play_identifier episode_limit podcast_type advertising_tags excluded_categories) + google_play_identifier episode_limit podcast_type advertising_tags excluded_categories] # Update also allows the span opt in - UPDATE_ATTRIBUTES = CREATE_ATTRIBUTES + %w(span_opt_in) + UPDATE_ATTRIBUTES = CREATE_ATTRIBUTES + %w[span_opt_in] # Deprecated, so we shouldn't rely on these, but they show up as attributes - DEPRECATED = %w(category redirect_url itunes_active redirected_at itunes_rating - google_podcasts_identifier stitcher_identifier) + DEPRECATED = %w[category redirect_url itunes_active redirected_at itunes_rating + google_podcasts_identifier stitcher_identifier] # All other attributes we might expect back from the Megaphone API # (some documented, others not so much) - OTHER_ATTRIBUTES = %w(id created_at updated_at image_file uid network_id recurring_import + OTHER_ATTRIBUTES = %w[id created_at updated_at image_file uid network_id recurring_import episodes_count spotify_identifier default_ad_settings iheart_identifier feed_url - default_pre_count default_post_count cloned_feed_urls ad_free_feed_urls main_feed ad_free) + default_pre_count default_post_count cloned_feed_urls ad_free_feed_urls main_feed ad_free] ALL_ATTRIBUTES = (UPDATE_ATTRIBUTES + DEPRECATED + OTHER_ATTRIBUTES) - attr_accessor *ALL_ATTRIBUTES + attr_accessor(*ALL_ATTRIBUTES) validates_presence_of CREATE_REQUIRED @@ -36,7 +37,7 @@ class Podcast validates_absence_of :id, on: :create # initialize from attributes - def initialize(attributes={}) + def initialize(attributes = {}) super end @@ -69,7 +70,7 @@ def self.attributes_from_feed(feed) episode_limit: feed.display_episodes_count, external_id: podcast.guid, podcast_type: podcast.itunes_type, - advertising_tags: podcast.categories, + advertising_tags: podcast.categories # set in augury, can we get it here? # excluded_categories: ????? TBD, } @@ -85,7 +86,7 @@ def find_by_guid Megaphone::PagedCollection.new(Megaphone::Podcast, result) end - def find_by_megaphone_id(mpid = self.id) + def find_by_megaphone_id(mpid = id) result = api.get("podcasts/#{mpid}") (result[:items] || []).first end diff --git a/config/application.rb b/config/application.rb index 0dadf0e2a..b70d9a9a1 100644 --- a/config/application.rb +++ b/config/application.rb @@ -152,8 +152,8 @@ def fmt.call(data) config.log_tags = [:request_id] config.active_record.schema_format = :ruby - config.active_record.encryption.primary_key = ENV['ACTIVE_RECORD_ENCRYPTION_PRIMARY_KEY'] - config.active_record.encryption.deterministic_key = ENV['ACTIVE_RECORD_ENCRYPTION_DETERMINISTIC_KEY'] - config.active_record.encryption.key_derivation_salt = ENV['ACTIVE_RECORD_ENCRYPTION_KEY_DERIVATION_SALT'] + config.active_record.encryption.primary_key = ENV["ACTIVE_RECORD_ENCRYPTION_PRIMARY_KEY"] + config.active_record.encryption.deterministic_key = ENV["ACTIVE_RECORD_ENCRYPTION_DETERMINISTIC_KEY"] + config.active_record.encryption.key_derivation_salt = ENV["ACTIVE_RECORD_ENCRYPTION_KEY_DERIVATION_SALT"] end end diff --git a/test/models/megaphone/api_test.rb b/test/models/megaphone/api_test.rb index fef6095c9..e2ec20643 100644 --- a/test/models/megaphone/api_test.rb +++ b/test/models/megaphone/api_test.rb @@ -2,7 +2,6 @@ require "securerandom" describe Megaphone::Api do - let(:token) { SecureRandom.uuid } let(:network_id) { "this-is-a-network-id" } let(:api) { Megaphone::Api.new(token: token, network_id: network_id) }