Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

working for new feature: service

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

0 notes on commit 1130301

Please sign in to comment.
Something went wrong with that request. Please try again.