8. Форматирование
def write_stats(publication, total, data = {})
activity = count_total_from_publication_data(publication)
update_activity(publication, activity)
total = activity.values.sum
if override_stat_date?(publication, 'created_at')
data[:date] = Time.parse(publication.data['created_at'])
end
unless dont_write_stats
if plays_total = publication.data.try(:[], 'playback_count').to_i
stat =
Stat.record_total(
"soundcloud_plays",
{ project: publication.project, resource: publication }.merge(data),
plays_total
)
end
end
super
end
9. Форматирование
def write_stats(publication, total, data = {})
activity = count_total_from_publication_data(publication)
total = activity.values.sum
update_activity(publication, activity)
if override_stat_date?(publication, 'created_at')
data[:date] = Time.parse(publication.data['created_at'])
end
unless dont_write_stats
if plays_total = publication.data.try(:[], 'playback_count').to_i
stat =
Stat.record_total(
"soundcloud_plays",
{ project: publication.project, resource: publication }.merge(data),
plays_total
)
end
end
super
end
10. Форматирование
def write_stats(publication, total, data = {})
activity = count_total_from_publication_data(publication)
total = activity.values.sum
update_activity(publication, activity)
if override_stat_date?(publication, 'created_at')
data[:date] = Time.parse(publication.data['created_at'])
end
unless dont_write_stats
if plays_total = publication.data.try(:[], 'playback_count').to_i
stat =
Stat.record_total(
"soundcloud_plays",
{ project: publication.project, resource: publication }.merge(data),
plays_total
)
end
end
super
end
11. Форматирование
def write_stats(publication, total, data = {})
activity = count_total_from_publication_data(publication)
total = activity.values.sum
update_activity(publication, activity)
if override_stat_date?(publication, 'created_at')
data[:date] = Time.parse(publication.data['created_at'])
end
unless dont_write_stats
if plays_total = publication.data.try(:[], 'playback_count').to_i
stat =
Stat.record_total(
"soundcloud_plays",
{ project: publication.project, resource: publication }.merge(data),
plays_total
)
end
end
super
end
12. Форматирование
def write_stats(publication, total, data = {})
activity = count_total_from_publication_data(publication)
total = activity.values.sum
update_activity(publication, activity)
if override_stat_date?(publication, 'created_at')
data[:date] = Time.parse(publication.data['created_at'])
end
unless dont_write_stats
if plays_total = publication.data.try(:[], 'playback_count').to_i
stat =
Stat.record_total(
"soundcloud_plays",
{ project: publication.project, resource: publication }.merge(data),
plays_total
)
end
end
super
end
13. Форматирование
def write_stats(publication, total, data = {})
activity = count_total_from_publication_data(publication)
total = activity.values.sum
update_activity(publication, activity)
if override_stat_date?(publication, 'created_at')
data[:date] = Time.parse(publication.data['created_at'])
end
unless dont_write_stats
if plays_total = publication.data.try(:[], 'playback_count').to_i
stat =
Stat.record_total(
"soundcloud_plays",
{ project: publication.project, resource: publication }.merge(data),
plays_total
)
end
end
super
end
14. Форматирование
def write_stats(publication, total, data = {})
activity = count_total_from_publication_data(publication)
total = activity.values.sum
update_activity(publication, activity)
if override_stat_date?(publication, 'created_at')
data[:date] = Time.parse(publication.data['created_at'])
end
unless dont_write_stats
if plays_total = publication.data.try(:[], 'playback_count').to_i
stat =
Stat.record_total(
"soundcloud_plays",
{ project: publication.project, resource: publication }.merge(data),
plays_total
)
end
end
super
end
23. Неправильные имена
def access_token
# We store only refresh token and for each request
# just fetching new access token
api = Odnoklassniki::Client.new(
access_token: @account.token,
client_id: Settings.odnoklassniki.app_id,
client_secret: Settings.odnoklassniki.app_secret,
)
new_token = api.refresh_token!
raise Social::API::Unauthorized if new_token.blank?
api
end
24. Неправильные имена
def access_token
# We store only refresh token and for each request
# just fetching new access token
api = Odnoklassniki::Client.new(
access_token: @account.token,
client_id: Settings.odnoklassniki.app_id,
client_secret: Settings.odnoklassniki.app_secret,
)
new_token = api.refresh_token!
raise Social::API::Unauthorized if new_token.blank?
api
end
25. Неправильные имена
def access_token
# We store only refresh token and for each request
# just fetching new access token
api = Odnoklassniki::Client.new(
access_token: @account.token,
client_id: Settings.odnoklassniki.app_id,
client_secret: Settings.odnoklassniki.app_secret,
)
new_token = api.refresh_token!
if new_token.blank?
@account.disable!
raise Social::API::Unauthorized
end
api
end
26. Неправильные имена
def rest_client_with_refreshed_token
client = Odnoklassniki::Client.new(
access_token: @account.token,
client_id: Settings.odnoklassniki.app_id,
client_secret: Settings.odnoklassniki.app_secret,
)
refreshed_token = client.refresh_token!
raise Social::API::Unauthorized if refreshed_token.blank?
client
end
29. module Social
module API
class Vkontakte
def initialize(access_token)
@access_token = access_token
end
# ...
end
end
end
module Social
module API
class Twitter
def initialize(access_token)
@access_token = access_token
end
# ...
end
end
end
Дублирование и DRY
30. module Social
module API
class Base
def initialize(access_token)
@access_token = access_token
end
end
end
end
module Social
module API
class Vkontakte < Base
# ...
end
end
end
Дублирование и DRY
31. Дублирование и DRY
module Social
module API
class Base
def initialize(access_token)
@access_token = access_token
end
def rescue_api_errors(error, args)
# ..
end
def send_notification
# ...
end
end
end
end