docbrown/app/models/buffer_update.rb
rhymes 6553f08d94 Rubocop cleanups (#1415)
* Update rubocop-todo.yml with new violations

* Fix Layout/EmptyLine* rules

* Fix Layout/Indentation* rules

* Fix remaining Layout/* rules

* Fix Lint/DuplicateMethods by removing unused accessor

* Fix Lint/IneffectiveAccessModifier

* Fix Lint/MissingCopEnableDirective

* Re-run rubocop auto gen config

* Fix Layout/RescueEnsureAlignment

* Fix Naming/* rules

* Fix some RSpec/* rules

* Fix typos

* Fix RSpec/LetBeforeExamples

* Series should only be an attr_writer, not an attr_accessor

* Fix RSpec/InstanceVariable

* Fix RSpec/InstanceVariable

* Fix RSpec/RepeatedDescription and RSpec/RepeatedExample

* Fix Style/ClassAndModuleChildren

* Fix Style/ConditionalAssignment

* Fix some Style/* rules

* Trigger Travis CI build because failing tests are not failing locally

* Revert "Fix Style/ClassAndModuleChildren"

This reverts commit 1686801d8a1516ba1894f79e24401a20dea65f99.
2019-01-02 11:20:02 -05:00

39 lines
1.1 KiB
Ruby

class BufferUpdate < ApplicationRecord
belongs_to :article
validate :validate_body_text_recent_uniqueness
def self.buff!(article_id, text, buffer_profile_id_code, social_service_name = "twitter", tag_id = nil)
buffer_response = send_to_buffer(text, buffer_profile_id_code)
create(
article_id: article_id,
tag_id: tag_id,
body_text: text,
buffer_profile_id_code: buffer_profile_id_code,
social_service_name: social_service_name,
buffer_response: buffer_response,
)
end
def self.send_to_buffer(text, buffer_profile_id_code)
client = Buffer::Client.new(ApplicationConfig["BUFFER_ACCESS_TOKEN"])
client.create_update(
body: {
text:
text,
profile_ids: [
buffer_profile_id_code,
]
},
)
end
private
def validate_body_text_recent_uniqueness
if BufferUpdate.where(body_text: body_text, article_id: article_id, tag_id: tag_id, social_service_name: social_service_name).
where("created_at > ?", 2.minutes.ago).any?
errors.add(:body_text, "\"#{body_text}\" has already been submitted very recently")
end
end
end