Skip to content

Commit

Permalink
Linting fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
kookster committed Oct 1, 2024
1 parent 4c76d44 commit 9ca679c
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 31 deletions.
27 changes: 13 additions & 14 deletions app/models/megaphone/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,36 @@ 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
@network_id = network_id
@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
{items: [data], pagination: {}, request: request, response: response}
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)
Expand All @@ -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|
Expand All @@ -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)
Expand All @@ -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 }
Expand Down
27 changes: 14 additions & 13 deletions app/models/megaphone/podcast.rb
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -36,7 +37,7 @@ class Podcast
validates_absence_of :id, on: :create

# initialize from attributes
def initialize(attributes={})
def initialize(attributes = {})
super
end

Expand Down Expand Up @@ -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,
}
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 0 additions & 1 deletion test/models/megaphone/api_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
Expand Down

0 comments on commit 9ca679c

Please sign in to comment.