added service model

This commit is contained in:
nilsding 2014-12-12 21:42:34 +01:00
parent 704d40cadd
commit 6d23ed5dcd
5 changed files with 105 additions and 1 deletions

38
app/models/service.rb Normal file
View File

@ -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

View File

@ -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

View File

@ -20,6 +20,7 @@ class User < ActiveRecord::Base
has_many :friends, through: :active_relationships, source: :target has_many :friends, through: :active_relationships, source: :target
has_many :followers, through: :passive_relationships, source: :source has_many :followers, through: :passive_relationships, source: :source
has_many :smiles has_many :smiles
has_many :services
SCREEN_NAME_REGEX = /\A[a-zA-Z0-9_]{1,16}\z/ SCREEN_NAME_REGEX = /\A[a-zA-Z0-9_]{1,16}\z/
WEBSITE_REGEX = /https?:\/\/([A-Za-z.\-]+)\/?(?:.*)/i WEBSITE_REGEX = /https?:\/\/([A-Za-z.\-]+)\/?(?:.*)/i

View File

@ -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

View File

@ -11,7 +11,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # 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 # These are extensions that must be enabled in order to support this database
enable_extension "plpgsql" 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", ["source_id"], name: "index_relationships_on_source_id", using: :btree
add_index "relationships", ["target_id"], name: "index_relationships_on_target_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| create_table "smiles", force: true do |t|
t.integer "user_id" t.integer "user_id"
t.integer "answer_id" t.integer "answer_id"