diff --git a/app/models/service.rb b/app/models/service.rb new file mode 100644 index 00000000..5457d168 --- /dev/null +++ b/app/models/service.rb @@ -0,0 +1,38 @@ +class Service < ActiveRecord::Base + belongs_to :user + validates_uniqueness_of :uid, scope: :type + + class << self + + def first_from_omniauth(auth_hash) + @@auth = auth_hash + where(type: service_type, uid: options[:uid]).first + end + + def initialize_from_omniauth(auth_hash) + @@auth = auth_hash + service_type.constantize.new(options) + end + + private + + def auth + @@auth + end + + def service_type + "Services::#{options[:provider].camelize}" + end + + def options + { + nickname: auth['info']['nickname'], + access_token: auth['credentials']['token'], + access_secret: auth['credentials']['secret'], + uid: auth['uid'], + provider: auth['provider'], + info: auth['info'] + } + end + end +end diff --git a/app/models/services/twitter.rb b/app/models/services/twitter.rb new file mode 100644 index 00000000..bb47815c --- /dev/null +++ b/app/models/services/twitter.rb @@ -0,0 +1,40 @@ +class Services::Twitter < Service + include Rails.application.routes.url_helpers + + def provider + "twitter" + end + + def post(answer) + Rails.logger.debug "posting to Twitter {'answer' => #{answer.id}, 'user' => #{self.user_id}}" + post_tweet answer + end + + private + + def client + @client ||= Twitter::REST::Client.new( + consumer_key: APP_CONFIG['sharing']['twitter']['consumer_key'], + consumer_secret: APP_CONFIG['sharing']['twitter']['consumer_secret'], + access_token: self.access_token, + access_token_secret: self.access_secret + ) + end + + def post_tweet(answer) + client.update prepare_tweet(answer) + end + + def prepare_tweet(answer) + # TODO: improve this. + question_content = answer.question.content + answer_content = answer.content + answer_url = show_user_answer_url( + id: answer.id, + username: answer.user.screen_name + ) + Rails.logger.debug "answer_url => #{answer_url}" + "#{question_content[0..55]}#{'…' if question_content.length > 56}" \ + " — #{answer_content[0..55]}#{'…' if answercontent.length > 56} #{answer_url}" + end +end \ No newline at end of file diff --git a/app/models/user.rb b/app/models/user.rb index feb6240d..a34029fe 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -20,6 +20,7 @@ class User < ActiveRecord::Base has_many :friends, through: :active_relationships, source: :target has_many :followers, through: :passive_relationships, source: :source has_many :smiles + has_many :services SCREEN_NAME_REGEX = /\A[a-zA-Z0-9_]{1,16}\z/ WEBSITE_REGEX = /https?:\/\/([A-Za-z.\-]+)\/?(?:.*)/i diff --git a/db/migrate/20141212193625_create_services.rb b/db/migrate/20141212193625_create_services.rb new file mode 100644 index 00000000..0ee0db34 --- /dev/null +++ b/db/migrate/20141212193625_create_services.rb @@ -0,0 +1,14 @@ +class CreateServices < ActiveRecord::Migration + def change + create_table :services do |t| + t.string :type, null: false + t.integer :user_id, null: false + t.string :uid + t.string :access_token + t.string :access_secret + t.string :nickname + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 213bfe0a..2933058f 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20141208111714) do +ActiveRecord::Schema.define(version: 20141212193625) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -70,6 +70,17 @@ ActiveRecord::Schema.define(version: 20141208111714) do add_index "relationships", ["source_id"], name: "index_relationships_on_source_id", using: :btree add_index "relationships", ["target_id"], name: "index_relationships_on_target_id", using: :btree + create_table "services", force: true do |t| + t.string "type", null: false + t.integer "user_id", null: false + t.string "uid" + t.string "access_token" + t.string "access_secret" + t.string "nickname" + t.datetime "created_at" + t.datetime "updated_at" + end + create_table "smiles", force: true do |t| t.integer "user_id" t.integer "answer_id"