Permalink
Browse files

working for new feature: service

  • Loading branch information...
1 parent 25044b5 commit 11303018611e3e583300b87b86ef433106f9c16c @bighorse committed Feb 11, 2011
View
6 db/migrate/002_create_services.rb
@@ -2,15 +2,13 @@ class CreateServices < ActiveRecord::Migration
def self.up
create_table :services do |t|
t.string :name
- t.string :email
- t.string :password
- t.string :bio
+ t.string :type
t.timestamps
end
end
def self.down
- drop_table :users
+ drop_table :services
end
end
View
14 db/migrate/003_create_user_service_relationships.rb
@@ -0,0 +1,14 @@
+class CreateUserServiceRelationships < ActiveRecord::Migration
+ def self.up
+ create_table :user_service_relationships do |t|
+ t.integer :user_id
+ t.integer :service_id
+
+ t.timestamps
+ end
+ end
+
+ def self.down
+ drop_table :user_service_relationships
+ end
+end
View
33 models/service.rb
@@ -0,0 +1,33 @@
+require 'rubygems'
+require 'typhoeus'
+require 'json'
+require 'logger'
+require 'rexml/document'
+
+class Service < ActiveRecord::Base
+ validates_uniqueness_of :name
+
+class FlickrService < Service
+ def get_tags(hydra, &block)
+ tags = []
+
+ request = Typhoeus::Request.new(
+ "http://api.flickr.com/services/rest/?method=flickr.tags.getListUser&api_key=f0de31f98fe2a16cbd81959d5144e525&user_id=29435289@N00&per_page=9999&format=json&nojsoncallback=1")
+
+ request.on_complete do |response|
+ if response.code == 200
+ results = JSON.parse(response.body)["who"]["tags"]["tag"]
+ results.each { |result| tags << Tag.new(result["_content"])}
+ elsif response.code == 404
+ nil
+ else
+ raise response.body
+ end
+ block.call(tags)
+ end
+
+ hydra.queue(request)
+ end
+end
+
+end
View
27 models/tag.rb
@@ -1,8 +1,6 @@
require 'rubygems'
-require 'typhoeus'
-require 'json'
require 'logger'
-require 'rexml/document'
+
class Tag
include ActiveModel::Serializers::JSON
@@ -20,22 +18,13 @@ def attributes
def self.find_by_user(user)
#logger = Logger.new("log.txt")
hydra = Typhoeus::Hydra.new
- tags = []
+ all_tags = []
+
+ user.regist_services.each do |regist_service|
+ regist_service.get_tags(hydra) { |tags| all_tags << tags}
+ end
- flickr_request = Typhoeus::Request.new(
- "http://api.flickr.com/services/rest/?method=flickr.tags.getListUser&api_key=f0de31f98fe2a16cbd81959d5144e525&user_id=29435289@N00&per_page=9999&format=json&nojsoncallback=1")
- flickr_request.on_complete do |response|
- if response.code == 200
- tags_array = JSON.parse(response.body)["who"]["tags"]["tag"]
- tags_array.each { |item| tags << new(item["_content"])}
- elsif response.code == 404
- nil
- else
- raise response.body
- end
- end
- hydra.queue(flickr_request)
picasa_request = Typhoeus::Request.new(
"http://picasaweb.google.com/data/feed/api/user/maguangjun?kind=tag")
@@ -54,7 +43,7 @@ def self.find_by_user(user)
hydra.queue(picasa_request)
hydra.run
- #logger.info("model/tag:#{tags}")
- tags.uniq {|tag| tag.name}
+ #logger.info("model/tag:#{all_tags}")
+ all_tags.uniq {|tag| tag.name}
end
end
View
9 models/user.rb
@@ -1,12 +1,9 @@
-require File.dirname(__FILE__) + '/tag'
-
class User < ActiveRecord::Base
+ has_many :user_service_relationships
+ has_many :regist_services, :through => :user_service_relationships, :source => :Service
validates_uniqueness_of :name, :email
def to_json
super(:except => :password)
end
-
- def tags
- Tag.find_by_user(self)
- end
+
end
View
4 models/user_service_relationship.rb
@@ -0,0 +1,4 @@
+class UserServiceRelationship < ActiveRecord::Base
+ belongs_to :User
+ belongs_to :Service
+end
View
8 service.rb
@@ -4,6 +4,7 @@
require 'sinatra'
require 'logger'
require "#{File.dirname(__FILE__)}/models/user"
+require "#{File.dirname(__FILE__)}/models/tag"
# setting up our environment
# logger = Logger.new("log.txt")
@@ -19,8 +20,8 @@
# TIP: You can get you database information
# from ENV['DATABASE_URI'] (see /env route below)
- #databases = YAML.load_file("config/database.yml")
- #ActiveRecord::Base.establish_connection(databases[ENV["RACK_ENV"]])
+ databases = YAML.load_file("config/database.yml")
+ ActiveRecord::Base.establish_connection(databases[ENV["RACK_ENV"]])
#logger = Logger.new("log.txt")
#logger.info("#{ENV["RACK_ENV"]}")
end
@@ -34,7 +35,8 @@
user = User.find_by_name(params[:name])
if user
#logger.info("service:#{user.tags.to_json}")
- user.tags.to_json
+ #user.tags.to_json
+ Tag.find_by_user(user).to_json
else
error 404, "user not found".to_json
end

0 comments on commit 1130301

Please sign in to comment.