acts_as_tumblr 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,20 @@
1
+ Copyright 2012 ALLDAYEVERYDAY
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1 @@
1
+ TBD
@@ -0,0 +1,259 @@
1
+ module ActsAsTumblr
2
+
3
+ def self.included(base)
4
+ base.extend(ClassMethods)
5
+ end
6
+
7
+ module ClassMethods
8
+
9
+ def acts_as_tumblr
10
+
11
+ class_eval <<-KRP
12
+
13
+ has_many :images, :dependent => :destroy
14
+
15
+ include ActsAsTumblr::InstanceMethods
16
+ require 'open-uri'
17
+ require 'acts-as-taggable-on'
18
+ require 'exportr'
19
+ require 'hashie'
20
+
21
+ default_scope order("date desc")
22
+
23
+ acts_as_taggable
24
+ acts_as_taggable_on :tags
25
+
26
+ def self.method_missing(meth, *args, &block)
27
+ if meth.to_s =~ /^fetch_(.+)$/
28
+ post_type = meth.to_s.split("fetch_")[1]
29
+ if args.count == 1
30
+ if args[0].is_a?(Integer)
31
+ limit = args[0]
32
+ tags = [""]
33
+ else
34
+ limit = 0
35
+ array = []
36
+ array << args[0]
37
+ tags = array.flatten
38
+ end
39
+ elsif args.count == 2
40
+ limit = args[1]
41
+ array = []
42
+ array << args[0]
43
+ tags = array.flatten
44
+ else
45
+ tags = []
46
+ limit = 0
47
+ end
48
+ Post.get_tumblr_posts(post_type, tags, limit)
49
+ else
50
+ super
51
+ end
52
+ end
53
+
54
+ def self.get_tumblr_posts(post_type = "all", tags = [], limit = 0)
55
+ api_address = "http://api.tumblr.com/v2/blog/" + ENV['TUMBLR_ADDRESS']
56
+ if post_type == "all"
57
+ type = ""
58
+ else
59
+ type = "/" + post_type.singularize
60
+ end
61
+
62
+ if tags.count == 0
63
+ t = ""
64
+ else
65
+ t = "&tag=" + URI.escape(tags.first)
66
+ end
67
+
68
+ address = api_address + "/posts" + type + "?api_key=" + ENV['TUMBLR_API_KEY'] + t
69
+ puts "Connecting to... " + address
70
+
71
+ json = open(address).read.to_s rescue false
72
+ if json != false
73
+ posts = []
74
+ json_posts = JSON.parse(json)["response"]["posts"]
75
+ json_posts.each do |p|
76
+ post = Hashie::Mash.new(p)
77
+ posts << post
78
+ end
79
+ if tags.count > 1
80
+ tags.shift
81
+ first_posts = posts
82
+ remove_posts = []
83
+ tags.each do |t|
84
+ posts.each do |p|
85
+ if !p.tags.any?{|tag| tag.casecmp(t) == 0}
86
+ remove_posts << p
87
+ end
88
+ end
89
+ end
90
+ posts = first_posts - remove_posts
91
+ end
92
+ if limit == 0
93
+ return posts
94
+ else
95
+ return posts.first(limit)
96
+ end
97
+ else
98
+ return "'" + post_type + "' is not a valid post type. Try again!"
99
+ end
100
+ end
101
+
102
+ 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
117
+ if type == "text"
118
+ title = post.title
119
+ body = post.body
120
+ elsif type == "photo"
121
+ body = post.caption
122
+ photos = post.photos
123
+ elsif type == "quote"
124
+ title = post.source_title
125
+ body = post.text
126
+ source_url = post.source_url
127
+ elsif type == "link"
128
+ title = post.title
129
+ body = post.description
130
+ source_url = post.url
131
+ elsif type == "chat"
132
+ title = post.title
133
+ body = post.body
134
+ elsif type == "audio"
135
+ title = post.source_title
136
+ body = post.caption
137
+ source_url = post.source_url
138
+ embed = post.player
139
+ elsif type == "video"
140
+ title = post.source_title
141
+ body = post.caption
142
+ source_url = post.source_url
143
+ embed = post.player.last.embed_code
144
+ elsif type == "answer"
145
+ title = post.question
146
+ body = post.answer
147
+ asking_name = post.asking_name
148
+ source_url = post.asking_url
149
+ end
150
+ 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
173
+ end
174
+ end
175
+ end
176
+ end
177
+
178
+ def self.get_photo(photo, post)
179
+ img = open(URI.parse(photo))
180
+ post.images.create!(:url => photo, :asset => img)
181
+ if !post.public?
182
+ post.update_attributes!(:public => true)
183
+ end
184
+ end
185
+
186
+ def update_notes
187
+ if self.tumblr_id?
188
+ api_address = "http://api.tumblr.com/v2/blog/" + ENV['TUMBLR_ADDRESS']
189
+ address = api_address + "/posts?api_key=" + ENV['TUMBLR_API_KEY'] + "&id=" + self.tumblr_id
190
+ puts "Connecting to... " + address
191
+ json = open(address).read.to_s rescue false
192
+ if json != false
193
+ post = Hashie::Mash.new(JSON.parse(json)["response"]).posts.first
194
+ if post.note_count != self.note_count
195
+ self.update_attributes!(:note_count => post.note_count)
196
+ puts "Notes Updated"
197
+ else
198
+ puts "No New Notes"
199
+ end
200
+ else
201
+ puts "Couldn't Find Post With Tumblr ID: " + self.tumblr_id
202
+ end
203
+ end
204
+ end
205
+
206
+ KRP
207
+
208
+ end
209
+
210
+ def acts_as_tumblr_media(opts={})
211
+
212
+ belongs = opts[:for].nil? ? "post" : "#{opts[:for].to_s}" rescue "post"
213
+ class_eval <<-KRP
214
+
215
+ require 'exportr'
216
+ require 'paperclip'
217
+ require 'aws-sdk'
218
+ include Paperclip::Glue
219
+ belongs_to :#{belongs}
220
+ before_save :get_orientation
221
+
222
+ attr_accessible :asset, :url
223
+
224
+ def get_orientation
225
+ dimensions = Paperclip::Geometry.from_file(self.asset(:original))
226
+ if dimensions.height > dimensions.width
227
+ self.orientation = "portrait"
228
+ else
229
+ self.orientation = "landscape"
230
+ end
231
+ end
232
+
233
+ KRP
234
+
235
+ end
236
+
237
+ def acts_as_tumblr_user
238
+ class_eval <<-KRP
239
+
240
+ require 'oauth'
241
+ require 'exportr'
242
+ require 'omniauth'
243
+
244
+ def self.find_or_create_from_auth_hash(auth_hash)
245
+ puts auth_hash
246
+ end
247
+ KRP
248
+ end
249
+
250
+ end
251
+
252
+
253
+ module InstanceMethods
254
+
255
+ end
256
+
257
+ end
258
+
259
+ ActiveRecord::Base.send :include, ActsAsTumblr
@@ -0,0 +1,3 @@
1
+ module ActsAsTumblr
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,88 @@
1
+ require 'rails/generators'
2
+ require 'rails/generators/migration'
3
+
4
+ class ActsAsTumblrGenerator < Rails::Generators::NamedBase
5
+
6
+ include Rails::Generators::Migration
7
+
8
+ source_root File.expand_path("../templates", __FILE__)
9
+
10
+ def self.next_migration_number(path)
11
+ Time.now.utc.strftime("%Y%m%d%H%M%S")
12
+ end
13
+
14
+ def setup_routes
15
+ route("match '/auth/:provider/callback', to: 'sessions#create'")
16
+ route("match '/auth/:provider/', to: 'sessions#sign_in'")
17
+ end
18
+
19
+ def create_tag_migrations
20
+ generate("acts_as_taggable_on:migration")
21
+ end
22
+
23
+ def generate_migration
24
+ migration_template "tumblr_migrations.rb.erb", "db/migrate/#{migration_file_name}"
25
+ end
26
+
27
+ def make_models
28
+
29
+ create_file "app/models/#{file_name}.rb",
30
+ <<-FILE
31
+ class #{class_name} < ActiveRecord::Base
32
+ acts_as_tumblr
33
+ end
34
+ FILE
35
+
36
+ create_file "app/models/image.rb",
37
+ <<-FILE
38
+ class Image < ActiveRecord::Base
39
+ acts_as_tumblr_media :for => :#{plural_name.singularize}
40
+
41
+ has_attached_file :asset, :styles => {
42
+ :thumb => ["50x50#", :jpg],
43
+ :small => ["150x150#", :jpg],
44
+ :medium => ["300x210#", :jpg],
45
+ :full => ["700x", :jpg]
46
+ },
47
+ :processors => [:thumbnail],
48
+ :storage => :s3,
49
+ :s3_credentials => {
50
+ :access_key_id => ENV['S3_ACCESS_KEY_ID'],
51
+ :secret_access_key => ENV['S3_SECRET_ACCESS_KEY'],
52
+ :bucket => ENV['S3_BUCKET']
53
+ },
54
+ :path => ":attachment/:id/:style.:content_type_extension",
55
+ :default_url => "/assets/missing.png"
56
+ end
57
+ FILE
58
+
59
+ end
60
+
61
+ def make_exportr_file
62
+ copy_file "exportr.yml", "config/exportr.yml"
63
+ end
64
+
65
+ def make_controllers
66
+ copy_file "sessions_controller.rb", "app/controllers/sessions_controller.rb"
67
+ copy_file "application_controller.rb", "app/controllers/application_controller.rb"
68
+ end
69
+
70
+ def make_tumblr_user_file
71
+ copy_file "tumblr_user.rb", "app/models/tumblr_user.rb"
72
+ end
73
+
74
+ protected
75
+
76
+ def migration_name
77
+ "add_tumblr_images_to_#{name.underscore.pluralize}"
78
+ end
79
+
80
+ def migration_file_name
81
+ "#{migration_name}.rb"
82
+ end
83
+
84
+ def migration_class_name
85
+ migration_name.camelize
86
+ end
87
+
88
+ end
@@ -0,0 +1,3 @@
1
+ class ApplicationController < ActionController::Base
2
+ protect_from_forgery
3
+ end
@@ -0,0 +1,14 @@
1
+ # This file is loaded at app initialization to set app specific environment variables
2
+ # set environment variables in the format [KEY]: [VALUE], each separated by a newline
3
+
4
+ # Examples:
5
+ # FACEBOOK_APP_KEY: '234443366312034'
6
+ # S3_BUCKET: 'myapp-production'
7
+
8
+ ---
9
+ S3_ACCESS_KEY_ID: 'Your Amazon S3 Access Key ID'
10
+ S3_SECRET_ACCESS_KEY: 'Your Amazon S3 Secret Access Key'
11
+ S3_BUCKET: 'Your Amazon S3 Bucket'
12
+ TUMBLR_API_KEY: 'Tumblr API Key'
13
+ TUMBLR_SECRET_KEY: 'Tumblr Secret Key'
14
+ TUMBLR_ADDRESS: 'blog.alldayeveryday.com'
@@ -0,0 +1,14 @@
1
+ class SessionsController < ApplicationController
2
+ def create
3
+ @user = TumblrUser.find_or_create_from_auth_hash(auth_hash)
4
+ redirect_to '/'
5
+ end
6
+
7
+ def sign_in; end
8
+
9
+ protected
10
+
11
+ def auth_hash
12
+ request.env['omniauth.auth']
13
+ end
14
+ end
@@ -0,0 +1 @@
1
+ <a href="http://www.tumblr.com/oauth/authorize?oauth_token=<%= ENV['TUMBLR_API_KEY'] %>">Sign In With Tumblr</a>
@@ -0,0 +1,46 @@
1
+ class <%= migration_class_name %> < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :<%= table_name %> do |t|
4
+ t.string "title"
5
+ t.string "tumblr_id"
6
+ t.text "body"
7
+ t.text "embed"
8
+ t.string "post_type"
9
+ t.string "url"
10
+ t.string "source_url"
11
+ t.string "asking_name"
12
+ t.timestamp "date"
13
+ t.string "reblog_key"
14
+ t.boolean "public", :default => false
15
+ t.integer "note_count", :default => 0
16
+ t.timestamps
17
+ end
18
+
19
+ create_table :images do |t|
20
+ t.integer "<%= table_name.singularize %>_id"
21
+ t.text "caption"
22
+ t.string "url"
23
+ t.string "asset_file_name"
24
+ t.string "asset_content_type"
25
+ t.integer "asset_file_size"
26
+ t.timestamp "asset_updated_at"
27
+ t.string "orientation"
28
+ t.timestamps
29
+ end
30
+
31
+ create_table :tumblr_users do |t|
32
+ t.string "user"
33
+ t.string "oauth_token"
34
+ t.string "oauth_secret"
35
+ t.string "tumblr_id"
36
+ t.timestamps
37
+ end
38
+
39
+ end
40
+
41
+ def self.down
42
+ drop_table :<%= table_name %>
43
+ drop_table :images
44
+ drop_table :tumblr_users
45
+ end
46
+ end
@@ -0,0 +1,3 @@
1
+ class TumblrUser < ::ActiveRecord::Base
2
+ acts_as_tumblr_user
3
+ end
metadata ADDED
@@ -0,0 +1,154 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: acts_as_tumblr
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Keith Poplawski
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-07-12 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rails
16
+ requirement: &70291306200860 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '3.1'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70291306200860
25
+ - !ruby/object:Gem::Dependency
26
+ name: paperclip
27
+ requirement: &70291306186020 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: 3.1.2
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *70291306186020
36
+ - !ruby/object:Gem::Dependency
37
+ name: aws-sdk
38
+ requirement: &70291306184260 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: 1.3.4
44
+ type: :runtime
45
+ prerelease: false
46
+ version_requirements: *70291306184260
47
+ - !ruby/object:Gem::Dependency
48
+ name: acts-as-taggable-on
49
+ requirement: &70291306182860 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 2.3.1
55
+ type: :runtime
56
+ prerelease: false
57
+ version_requirements: *70291306182860
58
+ - !ruby/object:Gem::Dependency
59
+ name: exportr
60
+ requirement: &70291306180400 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ~>
64
+ - !ruby/object:Gem::Version
65
+ version: 0.2.6
66
+ type: :runtime
67
+ prerelease: false
68
+ version_requirements: *70291306180400
69
+ - !ruby/object:Gem::Dependency
70
+ name: omniauth
71
+ requirement: &70291306173360 !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ type: :runtime
78
+ prerelease: false
79
+ version_requirements: *70291306173360
80
+ - !ruby/object:Gem::Dependency
81
+ name: omniauth-tumblr
82
+ requirement: &70291306171440 !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ! '>='
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ type: :runtime
89
+ prerelease: false
90
+ version_requirements: *70291306171440
91
+ - !ruby/object:Gem::Dependency
92
+ name: tumblr_client
93
+ requirement: &70291306169140 !ruby/object:Gem::Requirement
94
+ none: false
95
+ requirements:
96
+ - - ! '>='
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ type: :runtime
100
+ prerelease: false
101
+ version_requirements: *70291306169140
102
+ - !ruby/object:Gem::Dependency
103
+ name: hashie
104
+ requirement: &70291306167020 !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ type: :runtime
111
+ prerelease: false
112
+ version_requirements: *70291306167020
113
+ description: Tumblr Essentials
114
+ email: keith@alldayeveryday.com
115
+ executables: []
116
+ extensions: []
117
+ extra_rdoc_files: []
118
+ files:
119
+ - lib/acts_as_tumblr/version.rb
120
+ - lib/acts_as_tumblr.rb
121
+ - lib/generators/acts_as_tumblr/acts_as_tumblr_generator.rb
122
+ - lib/generators/acts_as_tumblr/templates/application_controller.rb
123
+ - lib/generators/acts_as_tumblr/templates/exportr.yml
124
+ - lib/generators/acts_as_tumblr/templates/sessions_controller.rb
125
+ - lib/generators/acts_as_tumblr/templates/sign_in.html.erb
126
+ - lib/generators/acts_as_tumblr/templates/tumblr_migrations.rb.erb
127
+ - lib/generators/acts_as_tumblr/templates/tumblr_user.rb
128
+ - MIT-LICENSE
129
+ - README.md
130
+ homepage: http://alldayeveryday.com
131
+ licenses: []
132
+ post_install_message:
133
+ rdoc_options: []
134
+ require_paths:
135
+ - lib
136
+ required_ruby_version: !ruby/object:Gem::Requirement
137
+ none: false
138
+ requirements:
139
+ - - ! '>='
140
+ - !ruby/object:Gem::Version
141
+ version: '0'
142
+ required_rubygems_version: !ruby/object:Gem::Requirement
143
+ none: false
144
+ requirements:
145
+ - - ! '>='
146
+ - !ruby/object:Gem::Version
147
+ version: '0'
148
+ requirements: []
149
+ rubyforge_project:
150
+ rubygems_version: 1.8.10
151
+ signing_key:
152
+ specification_version: 3
153
+ summary: Errday I'm Tumblin'
154
+ test_files: []