Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't alter data unless needed #738

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 33 additions & 19 deletions app/models/apple/show.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,21 +63,32 @@ def category_data
[{"type" => "categories", "id" => "1301"}]
end

def show_data
def update_attributes
create_attributes.except(:releaseFrequency)
end

def create_attributes
{
data: {
type: "shows",
relationships: {
allowedCountriesAndRegions: {data: api.countries_and_regions}
},
attributes: {
kind: "RSS",
rssUrl: feed_published_url,
releaseFrequency: "OPTOUT",
thirdPartyRights: "HAS_RIGHTS_TO_THIRD_PARTY_CONTENT"
kind: "RSS",
rssUrl: feed_published_url,
releaseFrequency: "WEEKLY",
thirdPartyRights: "HAS_RIGHTS_TO_THIRD_PARTY_CONTENT"
}
end

def show_data(attributes, id: nil)
res =
{
data: {
type: "shows",
relationships: {
allowedCountriesAndRegions: {data: api.countries_and_regions}
},
attributes: attributes
}
}
}
res[:data][:id] = id if id.present?
res
end

def apple_id
Expand Down Expand Up @@ -106,20 +117,23 @@ def sync!
end

def create_show!
data = show_data
data = show_data(create_attributes)
Rails.logger.info("Creating show", show_data: data)
resp = api.post("shows", data)

api.response(resp)
end

def update_show!(sync)
show_data_with_id = show_data
show_data_with_id[:data][:id] = sync.external_id
Rails.logger.info("Updating show", show_data: show_data_with_id)
resp = api.patch("shows/#{sync.external_id}", show_data_with_id)

api.response(resp)
Rails.logger.warn("Skipping update for existing show!")
nil

# TODO, map out the cases where we'd actually need to update a show
# data = show_data(update_attributes, id: apple_id)
# Rails.logger.info("Updating show", show_data: data)
# resp = api.patch("shows/#{sync.external_id}", data)
#
# api.response(resp)
end

def create_or_update_show(sync)
Expand Down
2 changes: 1 addition & 1 deletion test/models/apple/show_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@

describe "#show_data" do
it "returns a hash" do
assert_equal apple_show.show_data.class, Hash
assert_equal apple_show.show_data({}).class, Hash
end
end

Expand Down