acts_as_tumblr 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1 +1,71 @@
1
- TBD
1
+ ActsAsTumblr
2
+ ================
3
+
4
+ ActsAsTumblr works like a sponge absorbing all your Tumblr posts. It will retrieve posts as well as set up your routes for basic authentication. Currently, this works in one direction to retrieve posts. Posting to Tumblr will be build in future releases. This gem uses tumblr_client, acts-as-taggable-on, omniauth-tumblr, exportr, paperclip, and hashie. Very beta.
5
+
6
+
7
+ Requirements
8
+ ------------
9
+
10
+ 1. You are using Rails 3.1 or higher.
11
+
12
+
13
+ Installation
14
+ ------------
15
+
16
+ Add this to your gemfile:
17
+
18
+ gem 'acts_as_tumblr'
19
+
20
+ Run <code>bundle install</code>.
21
+
22
+ Run `rails generate acts_as_tumblr MODEL`
23
+
24
+ The generator creates all migrations, controllers, and models.
25
+
26
+ Run `rake db:migrate`
27
+
28
+
29
+ Usage
30
+ -----------
31
+
32
+ ActsAsTumblr can be used to retrieve posts as well as capture them.
33
+
34
+ ### Retrieve
35
+
36
+ Post.fetch_all(["array", "of", "tags"], limit.to_i)
37
+
38
+ "fetch" can be used to define posts types.
39
+
40
+ Post.fetch_photos
41
+ Post.fetch_quotes
42
+
43
+ Tags and limit are completely optional.
44
+
45
+
46
+
47
+ ### Capture
48
+
49
+ posts = Post.fetch_all("nyc", "alldayeveryday")
50
+ posts.each do |p|
51
+ Post.capture(p, :options => {:photos => false})
52
+ end
53
+
54
+ "capture" will save the post. "options" is optional (ha!) but will allow you to control what is or isn't saved.
55
+
56
+ Post.capture(p, :options => {:stats => true})
57
+
58
+ <code>:stats => true</code> will only save IDs and Note Count
59
+
60
+
61
+
62
+ ### Updating Note Counts
63
+
64
+ post.update_notes
65
+
66
+ "update_notes" will check to see if the post has attained new likes and reblogs. If if has, it will update on it's own.
67
+
68
+
69
+ Questions?
70
+ ----------
71
+ Write to info@alldayeveryday.com.
@@ -0,0 +1,9 @@
1
+ require 'rails'
2
+ require 'omniauth'
3
+ require 'omniauth-tumblr'
4
+
5
+ module ActsAsTumblr
6
+ class Engine < ::Rails::Engine
7
+ isolate_namespace ActsAsTumblr
8
+ end
9
+ end
@@ -1,3 +1,3 @@
1
1
  module ActsAsTumblr
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -61,6 +61,31 @@ module ActsAsTumblr
61
61
 
62
62
  if tags.count == 0
63
63
  t = ""
64
+ elsif tags.count > 1
65
+ big_tag = tags.first
66
+ t = "&tag=" + URI.escape(big_tag)
67
+ test_address = api_address + "/posts" + type + "?api_key=" + ENV['TUMBLR_API_KEY'] + t
68
+ json = open(test_address).read.to_s rescue false
69
+ if json != false
70
+ data = JSON.parse(json)
71
+ total_posts = data["response"]["total_posts"]
72
+ puts "compare to " + total_posts.to_s
73
+ end
74
+ tags.each do |tag|
75
+ t = "&tag=" + URI.escape(tag)
76
+ test_address = api_address + "/posts" + type + "?api_key=" + ENV['TUMBLR_API_KEY'] + t
77
+ json = open(test_address).read.to_s rescue false
78
+ if json != false
79
+ data = JSON.parse(json)
80
+ new_posts = data["response"]["total_posts"]
81
+ if new_posts > total_posts
82
+ total_posts = new_posts
83
+ big_tag = tag
84
+ puts "New total is " + total_posts.to_s + " for tag: " + big_tag
85
+ t = "&tag=" + URI.escape(big_tag)
86
+ end
87
+ end
88
+ end
64
89
  else
65
90
  t = "&tag=" + URI.escape(tags.first)
66
91
  end
@@ -71,13 +96,38 @@ module ActsAsTumblr
71
96
  json = open(address).read.to_s rescue false
72
97
  if json != false
73
98
  posts = []
74
- json_posts = JSON.parse(json)["response"]["posts"]
99
+ data = JSON.parse(json)
100
+ total_posts = data["response"]["total_posts"]
101
+ json_posts = data["response"]["posts"]
75
102
  json_posts.each do |p|
76
103
  post = Hashie::Mash.new(p)
77
104
  posts << post
78
105
  end
106
+ if total_posts > 20
107
+ offset = 20
108
+ posts_to_go = total_posts - offset
109
+ while posts_to_go != false
110
+ o = "&offset=" + offset.to_s
111
+ address = api_address + "/posts" + type + "?api_key=" + ENV['TUMBLR_API_KEY'] + t + o
112
+ puts "Connecting to... " + address
113
+ json = open(address).read.to_s rescue false
114
+ if json != false
115
+ data = JSON.parse(json)
116
+ json_posts = data["response"]["posts"]
117
+ if data["response"]["posts"].count != 0
118
+ json_posts.each do |p|
119
+ post = Hashie::Mash.new(p)
120
+ posts << post
121
+ end
122
+ posts.flatten
123
+ offset += 20
124
+ else
125
+ posts_to_go = false
126
+ end
127
+ end
128
+ end
129
+ end
79
130
  if tags.count > 1
80
- tags.shift
81
131
  first_posts = posts
82
132
  remove_posts = []
83
133
  tags.each do |t|
@@ -100,26 +150,28 @@ module ActsAsTumblr
100
150
  end
101
151
 
102
152
  def self.capture(post, options={:save => "all"})
103
- if options[:save] == "all"
104
- existing_post = self.find_by_tumblr_id(post.id.to_s)
105
- if !existing_post
106
- url = post.post_url
107
- date = post.date
108
- reblog_key = post.reblog_key
109
- tumblr_id = post.id.to_s
110
- type = post.type
111
- source_url = nil
112
- title = nil
113
- body = nil
114
- embed = nil
115
- asking_name = nil
116
- photos = nil
153
+ existing_post = self.find_by_tumblr_id(post.id.to_s)
154
+ if !existing_post
155
+ url = post.post_url
156
+ date = post.date
157
+ reblog_key = post.reblog_key
158
+ tumblr_id = post.id.to_s
159
+ type = post.type
160
+ source_url = nil
161
+ title = nil
162
+ body = nil
163
+ embed = nil
164
+ asking_name = nil
165
+ photos = nil
166
+ if options[:stats] != true
117
167
  if type == "text"
118
168
  title = post.title
119
169
  body = post.body
120
170
  elsif type == "photo"
121
171
  body = post.caption
122
- photos = post.photos
172
+ if options[:save] == "all" || options[:photos] != false
173
+ photos = post.photos
174
+ end
123
175
  elsif type == "quote"
124
176
  title = post.source_title
125
177
  body = post.text
@@ -147,29 +199,33 @@ module ActsAsTumblr
147
199
  asking_name = post.asking_name
148
200
  source_url = post.asking_url
149
201
  end
202
+ end
203
+ if options[:save] == "all" || options[:note_count] != false
150
204
  note_count = post.note_count
151
- tags = post.tags
152
- new_post = Post.create!(
153
- :url => url,
154
- :date => date,
155
- :reblog_key => reblog_key,
156
- :tumblr_id => tumblr_id,
157
- :post_type => type,
158
- :note_count => note_count,
159
- :title => title,
160
- :body => body,
161
- :source_url => source_url,
162
- :embed => embed,
163
- :asking_name => asking_name
164
- )
165
- if tags.count > 0
166
- new_post.tag_list = tags
167
- new_post.save
168
- end
169
- if !photos.nil?
170
- photos.each do |p|
171
- Post.get_photo(p.original_size.url, new_post)
172
- end
205
+ else
206
+ note_count = nil
207
+ end
208
+ tags = post.tags
209
+ new_post = Post.create!(
210
+ :url => url,
211
+ :date => date,
212
+ :reblog_key => reblog_key,
213
+ :tumblr_id => tumblr_id,
214
+ :post_type => type,
215
+ :note_count => note_count,
216
+ :title => title,
217
+ :body => body,
218
+ :source_url => source_url,
219
+ :embed => embed,
220
+ :asking_name => asking_name
221
+ )
222
+ if tags.count > 0
223
+ new_post.tag_list = tags
224
+ new_post.save
225
+ end
226
+ if !photos.nil?
227
+ photos.each do |p|
228
+ Post.get_photo(p.original_size.url, new_post)
173
229
  end
174
230
  end
175
231
  end
@@ -255,5 +311,5 @@ module ActsAsTumblr
255
311
  end
256
312
 
257
313
  end
258
-
314
+ require "acts_as_tumblr/engine"
259
315
  ActiveRecord::Base.send :include, ActsAsTumblr
@@ -71,6 +71,10 @@ end
71
71
  copy_file "tumblr_user.rb", "app/models/tumblr_user.rb"
72
72
  end
73
73
 
74
+ def make_initializer
75
+ copy_file "acts_as_tumblr.rb", "config/initializers/acts_as_tumblr.rb"
76
+ end
77
+
74
78
  protected
75
79
 
76
80
  def migration_name
@@ -0,0 +1,4 @@
1
+ Rails.application.config.middleware.use OmniAuth::Builder do
2
+ provider :developer unless Rails.env.production?
3
+ provider :tumblr, ENV['TUMBLR_API_KEY'], ENV['TUMBLR_SECRET_KEY']
4
+ end
@@ -1,14 +1,16 @@
1
1
  class SessionsController < ApplicationController
2
+ def new
3
+ logger.debug "params: #{params.inspect}"
4
+ services = ['tumblr'] # TODO extract from OmniAuth.config
5
+ links = services.sort.map{|service| "<li style='margin: 15px;'><a href='/auth/#{service}'>#{service}</a></li>" }
6
+ render :text => "Authenticate with: <ul style='font-size: 20pt;'>#{links.join}</ul>", :layout => true
7
+ end
8
+
2
9
  def create
3
- @user = TumblrUser.find_or_create_from_auth_hash(auth_hash)
4
- redirect_to '/'
10
+ render :text => "<pre>"+request.env["omniauth.auth"].to_yaml+"</pre>"
5
11
  end
6
-
7
- def sign_in; end
8
-
9
- protected
10
12
 
11
- def auth_hash
12
- request.env['omniauth.auth']
13
- end
13
+ def failure
14
+ render :text => "FAILURE :-("
15
+ end
14
16
  end
@@ -33,6 +33,7 @@ class <%= migration_class_name %> < ActiveRecord::Migration
33
33
  t.string "oauth_token"
34
34
  t.string "oauth_secret"
35
35
  t.string "tumblr_id"
36
+ t.string "tumblr_url"
36
37
  t.timestamps
37
38
  end
38
39
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: acts_as_tumblr
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2012-07-12 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
16
- requirement: &70291306200860 !ruby/object:Gem::Requirement
16
+ requirement: &70335904113100 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '3.1'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70291306200860
24
+ version_requirements: *70335904113100
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: paperclip
27
- requirement: &70291306186020 !ruby/object:Gem::Requirement
27
+ requirement: &70335904112280 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: 3.1.2
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *70291306186020
35
+ version_requirements: *70335904112280
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: aws-sdk
38
- requirement: &70291306184260 !ruby/object:Gem::Requirement
38
+ requirement: &70335904111480 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ~>
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: 1.3.4
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *70291306184260
46
+ version_requirements: *70335904111480
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: acts-as-taggable-on
49
- requirement: &70291306182860 !ruby/object:Gem::Requirement
49
+ requirement: &70335904110880 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ~>
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: 2.3.1
55
55
  type: :runtime
56
56
  prerelease: false
57
- version_requirements: *70291306182860
57
+ version_requirements: *70335904110880
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: exportr
60
- requirement: &70291306180400 !ruby/object:Gem::Requirement
60
+ requirement: &70335904109740 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ~>
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: 0.2.6
66
66
  type: :runtime
67
67
  prerelease: false
68
- version_requirements: *70291306180400
68
+ version_requirements: *70335904109740
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: omniauth
71
- requirement: &70291306173360 !ruby/object:Gem::Requirement
71
+ requirement: &70335904107700 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ! '>='
@@ -76,10 +76,10 @@ dependencies:
76
76
  version: '0'
77
77
  type: :runtime
78
78
  prerelease: false
79
- version_requirements: *70291306173360
79
+ version_requirements: *70335904107700
80
80
  - !ruby/object:Gem::Dependency
81
81
  name: omniauth-tumblr
82
- requirement: &70291306171440 !ruby/object:Gem::Requirement
82
+ requirement: &70335904107040 !ruby/object:Gem::Requirement
83
83
  none: false
84
84
  requirements:
85
85
  - - ! '>='
@@ -87,10 +87,10 @@ dependencies:
87
87
  version: '0'
88
88
  type: :runtime
89
89
  prerelease: false
90
- version_requirements: *70291306171440
90
+ version_requirements: *70335904107040
91
91
  - !ruby/object:Gem::Dependency
92
92
  name: tumblr_client
93
- requirement: &70291306169140 !ruby/object:Gem::Requirement
93
+ requirement: &70335904106360 !ruby/object:Gem::Requirement
94
94
  none: false
95
95
  requirements:
96
96
  - - ! '>='
@@ -98,10 +98,10 @@ dependencies:
98
98
  version: '0'
99
99
  type: :runtime
100
100
  prerelease: false
101
- version_requirements: *70291306169140
101
+ version_requirements: *70335904106360
102
102
  - !ruby/object:Gem::Dependency
103
103
  name: hashie
104
- requirement: &70291306167020 !ruby/object:Gem::Requirement
104
+ requirement: &70335904105320 !ruby/object:Gem::Requirement
105
105
  none: false
106
106
  requirements:
107
107
  - - ! '>='
@@ -109,16 +109,18 @@ dependencies:
109
109
  version: '0'
110
110
  type: :runtime
111
111
  prerelease: false
112
- version_requirements: *70291306167020
112
+ version_requirements: *70335904105320
113
113
  description: Tumblr Essentials
114
114
  email: keith@alldayeveryday.com
115
115
  executables: []
116
116
  extensions: []
117
117
  extra_rdoc_files: []
118
118
  files:
119
+ - lib/acts_as_tumblr/engine.rb
119
120
  - lib/acts_as_tumblr/version.rb
120
121
  - lib/acts_as_tumblr.rb
121
122
  - lib/generators/acts_as_tumblr/acts_as_tumblr_generator.rb
123
+ - lib/generators/acts_as_tumblr/templates/acts_as_tumblr.rb
122
124
  - lib/generators/acts_as_tumblr/templates/application_controller.rb
123
125
  - lib/generators/acts_as_tumblr/templates/exportr.yml
124
126
  - lib/generators/acts_as_tumblr/templates/sessions_controller.rb