-
-
Save jeffutter/adc79f6fec0c36c3f9cd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Refinery | |
module ArtistEvent | |
class Activity < Refinery::Core::BaseModel | |
attr_accessible :name, :date_time, :duration, :public | |
attr_accessible :twitter_posted, :twitter_updated, :facebook_event_id, :facebook_posted, :facebook_updated | |
acts_as_indexed :fields => [:name, :date_time] | |
belongs_to :event | |
validates :name, :presence => true | |
validates :date_time, :presence => true | |
validates :duration, :presence => true | |
# attr_accessor :fb_event | |
after_save :auto_update_facebook_event | |
before_destroy :destroy_facebook_event | |
attr_accessor :facebook_obj | |
attr_accessor :twitter_obj | |
# def initialize(title) | |
# @title = title | |
# end | |
def facebook | |
@facebook_obj = Refinery::ArtistEvent::FacebookService.new(self) unless @facebook_obj | |
return @facebook_obj | |
end | |
def twitter | |
@twitter_obj = Refinery::ArtistEvent::TwitterService.new(self) unless @twitter_obj | |
return @twitter_obj | |
end | |
def end_time | |
ActiveSupport::TimeWithZone.new(date_time.to_datetime+Rational(duration,1440), ActiveSupport::TimeZone.new('UTC')) | |
end | |
def timezone | |
ActiveSupport::TimeZone.new(venue.timezone) | |
end | |
def end_in_utc | |
timezone.local_to_utc(end_time, timezone) | |
end | |
def start_in_utc | |
timezone.local_to_utc(date_time, timezone) | |
end | |
def venue | |
event.venue | |
end | |
def group | |
event.group | |
end | |
def add_to_ical(ical) | |
timestr = "%Y%m%dT%H%M%SZ" | |
dtstart = start_in_utc.strftime(timestr) | |
dtend = end_in_utc.strftime(timestr) | |
dtstamp = created_at.strftime(timestr) | |
summary = "#{event.title} - #{name}" | |
description = event.details | |
# ip_class = public ? 'PUBLIC' : 'PRIVATE' | |
location = "#{event.venue.address1} #{event.venue.address2} #{event.venue.city} #{event.venue.state}, #{event.venue.zip}" | |
url = venue.url.present? ? venue.url : false | |
ical.event do | |
dtstart dtstart | |
dtend dtend | |
dtstamp dtstamp | |
summary summary | |
description description | |
status 'CONFIRMED' | |
# ip_class ip_class | |
ip_class 'PUBLIC' # or it doesn't show the title or any details in google calendar | |
location location | |
if url | |
url url | |
end | |
end | |
end | |
def title | |
event.activities.public.count > 1 ? "#{event.title} - #{name} at #{venue.name} in #{venue.city}, #{venue.state}" : "#{event.title} at #{venue.name} in #{venue.city}, #{venue.state}" | |
end | |
def event_link | |
Refinery::ArtistEvent.posting_host.to_s+Refinery::Core::Engine.routes.url_helpers.artist_event_event_path(event) | |
end | |
def auto_update_facebook_event(force = false) | |
if self.facebook_event_id.present? | |
facebook.update_event if ( self.name_changed? || self.date_time_changed? || self.duration_changed? ) || force | |
end | |
end | |
class << self | |
def public | |
where(:public => true) | |
end | |
#This is getting stupid. Really need to store all times in UTC | |
def upcoming | |
where('refinery_artist_event_activities.date_time >= ?', Time.zone.local_to_utc(Time.now, Time.zone)) | |
end | |
#This is getting stupid. Really need to store all times in UTC | |
def today | |
where(["refinery_artist_event_activities.date_time BETWEEN ? AND ?", Time.parse("#{Date.today} 00:00:00"), Time.parse("#{Date.today} 23:59:59")]) | |
end | |
#This is getting stupid. Really need to store all times in UTC | |
def archived | |
where('refinery_artist_event_activities.date_time < ?', Time.zone.local_to_utc(Time.now, Time.zone)) | |
end | |
def posted_to(network = nil) | |
where("refinery_artist_event_events.#{network}_posted IS NOT NULL") | |
end | |
def not_posted_to(network = nil) | |
where("refinery_artist_event_events.#{network}_posted IS NULL") | |
end | |
def updated_to(network = nil) | |
where("refinery_artist_event_events.#{network}_updated IS NOT NULL") | |
end | |
def not_updated_to(network = nil) | |
where("refinery_artist_event_events.#{network}_updated IS NULL") | |
end | |
def not_posted_to_any | |
self.not_posted_to(:facebook).not_posted_to(:twitter) | |
end | |
def not_updated_to_any | |
self.not_posted_to(:facebook).not_posted_to(:twitter) | |
end | |
end | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Refinery | |
module ArtistEvent | |
class FacebookService | |
attr_accessor :activity | |
attr_accessor :fb_event | |
def initialize(activity) | |
@activity = activity | |
end | |
def facebook_hash | |
hash = { | |
:name => @activity.title, | |
:start_time => @activity.start_in_utc.iso8601, | |
:end_time => @activity.end_in_utc.iso8601, | |
:venue => { | |
:street => @activity.venue.address1, | |
:city => @activity.venue.city, | |
:state => @activity.venue.state, | |
:zip => @activity.venue.zip, | |
:latitude => @activity.venue.latitude, | |
:longitude => @activity.venue.longitude | |
}, | |
:privacy => 'OPEN', | |
:description => "Come join me with #{@activity.group.name} at #{@activity.venue.name}. Support live music!", | |
:updated_time => @activity.updated_at.iso8601 | |
} | |
hash[:location_id] = @activity.venue.facebook_location.location if @activity.venue.facebook_location_id.present? | |
return hash | |
end | |
def diff_hashes | |
fb_hash = facebook_event.raw_attributes.reject{ |key,val| ["id", "owner", "is_date_only", "access_token"].include?(key) } | |
this_hash = facebook_hash.stringify_keys | |
this_hash["venue"] = facebook_hash[:venue].stringify_keys | |
updated_hash = this_hash.diff(fb_hash) | |
# Need to do this as facebook returns the same time in a different timezone and therefore they do not match even if they are the same | |
updated_hash.delete("start_time") if Time.parse(fb_hash["start_time"]) == Time.parse(facebook_hash[:start_time]) | |
updated_hash.delete("end_time") if Time.parse(fb_hash["end_time"]) == Time.parse(facebook_hash[:end_time]) | |
updated_hash.delete("updated_time") if Time.parse(fb_hash["updated_time"]) == Time.parse(facebook_hash[:updated_time]) | |
# Facebook also rewrites the venue... don't update it for now | |
updated_hash.delete("venue") | |
# This is also weird | |
updated_hash.delete("location") | |
updated_hash | |
end | |
def facebook_event | |
if @activity.facebook_event_id | |
@fb_event = FbGraph::Event.new(@activity.facebook_event_id, :access_token => Refinery::User.first.facebook_access_token).fetch unless @fb_event | |
return @fb_event | |
else | |
nil | |
end | |
end | |
def create_event | |
me = FbGraph::User.me(Refinery::User.first.facebook_access_token) | |
event = me.event!(facebook_hash) | |
if event | |
@activity.update_attribute(:facebook_event_id, event.identifier) | |
@activity.update_attribute(:facebook_posted, Time.now) | |
return true | |
else | |
return false | |
end | |
end | |
def destroy_event | |
facebook_event.destroy() | |
@activity.update_attribute(:facebook_event_id, nil) | |
end | |
def day_of | |
message = "#{Refinery::ArtistEvent.events_name.capitalize} with #{@activity.event.group.name} today at #{@activity.event.date.strftime("%l:%M%P")}. #{@activity.event.venue.name} in #{@activity.event.venue.city}. Come support local music." | |
post = FbGraph::User.me(Refinery::User.first.facebook_access_token).feed!( | |
:message => message, | |
:link => @activity.event_link, | |
:name => 'Jeffery Utter - Events', | |
:description => "#{Refinery::ArtistEvent.events_name.capitalize} today at #{@activity.event.venue.name} in #{@activity.event.venue.city}", | |
:picture => Refinery::ArtistEvent.facebook_image_link | |
) | |
if post | |
@activity.update_attribute(:facebook_updated, Time.now) | |
return true | |
else | |
return false | |
end | |
end | |
def facebook_event_url | |
@activity.facebook_event_id ? "http://facebook.com/events/"+@activity.facebook_event_id : nil | |
end | |
def update_facebook_event | |
facebook_event.update(diff_hashes) | |
end | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Refinery | |
module ArtistEvent | |
class TwitterService | |
attr_accessor :activity | |
def initialize(activity) | |
@activity = activity | |
end | |
def twitter_text(message, link) | |
if [message, link].join(" ").length <= 140 | |
message = [message, link].join(" ") | |
else | |
message = [message[0..(139-link.length-4)], link].join("... ") | |
end | |
return message | |
end | |
def post | |
base_message = "New #{Refinery::ArtistEvent.events_name.downcase} with #{@activity.event.group.name} at #{@activity.event.venue.name} in #{@activity.event.venue.city} at #{@activity.event.date.strftime("%l:%M%P")} on #{@activity.event.date.strftime("%b %e")}." | |
message = twitter_text(base_message, @activity.event_link) | |
if Twitter.update(message) | |
@activity.update_attribute(:twitter_posted, Time.now) | |
return true | |
else | |
return false | |
end | |
end | |
def day_of | |
base_message = "#{Refinery::ArtistEvent.events_name.capitalize} with #{@activity.event.group.name} today at #{@activity.event.date.strftime("%l:%M%P")}. #{@activity.event.venue.name} in #{@activity.event.venue.city}." | |
message = twitter_text(base_message, @activity.event_link) | |
if Twitter.update(message) | |
@activity.update_attribute(:twitter_updated, Time.now) | |
return true | |
else | |
return false | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment