Module: ActsAsTumblr::ClassMethods

Defined in:
lib/acts_as_tumblr.rb

Instance Method Summary collapse

Instance Method Details

#acts_as_tumblrObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# File 'lib/acts_as_tumblr.rb', line 9

def acts_as_tumblr
  
  class_eval "  \n    has_many :images, :dependent => :destroy\n  \n    include ActsAsTumblr::InstanceMethods\n    require 'open-uri'\n    require 'acts-as-taggable-on'\n    require 'exportr'\n    require 'hashie'\n    \n    default_scope order(\"date desc\")\n    \n    acts_as_taggable\n    acts_as_taggable_on :tags\n    \n    def self.method_missing(meth, *args, &block)\n      if meth.to_s =~ /^fetch_(.+)$/\n        post_type = meth.to_s.split(\"fetch_\")[1]\n        if args.count == 1\n          if args[0].is_a?(Integer)\n            limit = args[0]\n            tags = [\"\"]\n            tumblr_address = ENV['TUMBLR_ADDRESS']\n          else\n            limit = 0\n            if args[0].include?(\".\")\n              tumblr_address = args[0]\n              tags = []\n            else\n              array = []\n              array << args[0]\n              tags = array.flatten\n              tumblr_address = ENV['TUMBLR_ADDRESS']\n            end\n          end\n        elsif args.count == 2\n          if args[0].is_a?(Integer)\n            limit = args[0]\n            tumblr_address = args[1]\n            tags = []\n          else\n            array = []\n            array << args[0]\n            tags = array.flatten\n            if args[1].is_a?(Integer)\n              tumblr_address = ENV['TUMBLR_ADDRESS']\n              limit = args[1]\n            else\n              tumblr_address = args[1]\n              limit = 0\n            end\n          end\n        elsif args.count == 3\n          array = []\n          array << args[0]\n          tags = array.flatten\n          limit = args[1]\n          tumblr_address = args[2]\n        else\n          tags = []\n          limit = 0\n          tumblr_address = ENV['TUMBLR_ADDRESS']\n        end\n        Post.get_tumblr_posts(post_type, tags, limit, tumblr_address)\n      else\n        super\n      end\n    end\n    \n    def self.get_tumblr_posts(post_type = \"all\", tags = [], limit = 0, tumblr_address = ENV['TUMBLR_ADDRESS'])\n      api_address = \"http://api.tumblr.com/v2/blog/\" + tumblr_address\n      if post_type == \"all\"\n        type = \"\"\n      else\n        type = \"/\" + post_type.singularize\n      end\n      \n      if tags.count == 0\n        t = \"\"\n      elsif tags.count > 1\n        big_tag = tags.first\n        t = \"&tag=\" + URI.escape(big_tag)\n        test_address = api_address + \"/posts\" + type + \"?api_key=\" + tumblr_address + t\n        json = open(test_address).read.to_s rescue false\n        if json != false\n          data = JSON.parse(json)\n          total_posts = data[\"response\"][\"total_posts\"]\n        end\n        tags.each do |tag|\n          t = \"&tag=\" + URI.escape(tag)\n          test_address = api_address + \"/posts\" + type + \"?api_key=\" + tumblr_address + t\n          json = open(test_address).read.to_s rescue false\n          if json != false\n            data = JSON.parse(json)\n            new_posts = data[\"response\"][\"total_posts\"]\n            if new_posts > total_posts\n              total_posts = new_posts\n              big_tag = tag\n              t = \"&tag=\" + URI.escape(big_tag)\n            end\n          end\n        end\n      else\n        t = \"&tag=\" + URI.escape(tags.first)\n      end\n      \n      address = api_address + \"/posts\" + type + \"?api_key=\" + ENV['TUMBLR_API_KEY'] + t\n      puts \"Connecting to... \" + address\n      \n      json = open(address).read.to_s rescue false\n      if json != false\n        posts = []\n        data = JSON.parse(json)\n        total_posts = data[\"response\"][\"total_posts\"]\n        json_posts = data[\"response\"][\"posts\"]\n        json_posts.each do |p|\n          post = Hashie::Mash.new(p)\n          posts << post\n        end\n        if total_posts > 20\n          offset = 20\n          posts_to_go = total_posts - offset\n          while posts_to_go != false\n            o = \"&offset=\" + offset.to_s\n            address = api_address + \"/posts\" + type + \"?api_key=\" + ENV['TUMBLR_API_KEY'] + t + o\n            puts \"Connecting to... \" + address\n            json = open(address).read.to_s rescue false\n            if json != false\n              data = JSON.parse(json)\n              json_posts = data[\"response\"][\"posts\"]\n              if data[\"response\"][\"posts\"].count != 0\n                json_posts.each do |p|\n                  post = Hashie::Mash.new(p)\n                  posts << post\n                end\n                posts.flatten\n                offset += 20\n              else\n                posts_to_go = false\n              end\n            end\n          end\n        end\n        if tags.count > 1\n          first_posts = posts\n          remove_posts = []\n          tags.each do |t|\n            posts.each do |p|\n              if !p.tags.any?{|tag| tag.casecmp(t) == 0}\n                remove_posts << p\n              end\n            end\n          end\n          posts = first_posts - remove_posts\n        end\n        if limit == 0\n          return posts\n        else\n          return posts.first(limit)\n        end\n      else\n        return \"'\" + post_type + \"' is not a valid post type. Try again!\"\n      end\n    end\n    \n    def self.capture(post, options={:save => \"all\"})\n      existing_post = self.find_by_tumblr_id(post.id.to_s)\n      if !existing_post\n        url = post.post_url\n        date = post.date\n        reblog_key = post.reblog_key\n        tumblr_id = post.id.to_s\n        type = post.type\n        source_url = nil\n        title = nil\n        body = nil\n        embed = nil\n        asking_name = nil\n        photos = nil\n        if options[:stats] != true\n          if type == \"text\"\n            title = post.title\n            body = post.body\n          elsif type == \"photo\"\n            body = post.caption\n            if options[:save] == \"all\" || options[:photos] != false\n              photos = post.photos\n            end\n          elsif type == \"quote\"\n            title = post.source_title\n            body = post.text\n            source_url = post.source_url\n          elsif type == \"link\"\n            title = post.title\n            body = post.description\n            source_url = post.url\n          elsif type == \"chat\"\n            title = post.title\n            body = post.body\n          elsif type == \"audio\"\n            title = post.source_title\n            body = post.caption\n            source_url = post.source_url\n            embed = post.player\n          elsif type == \"video\"\n            title = post.source_title\n            body = post.caption\n            source_url = post.source_url\n            embed = post.player.last.embed_code\n          elsif type == \"answer\"\n            title = post.question\n            body = post.answer\n            asking_name = post.asking_name\n            source_url = post.asking_url\n          end\n        end\n        if options[:save] == \"all\" || options[:note_count] != false\n          note_count = post.note_count\n        else\n          note_count = nil\n        end\n        tags = post.tags\n        new_post = Post.create!(\n          :url => url,\n          :date => date,\n          :reblog_key => reblog_key,\n          :tumblr_id => tumblr_id,\n          :post_type => type,\n          :note_count => note_count,\n          :title => title,\n          :body => body,\n          :source_url => source_url,\n          :embed => embed,\n          :asking_name => asking_name\n        )\n        if tags.count > 0\n          new_post.tag_list = tags\n          new_post.save\n        end\n        if !photos.nil?\n          photos.each do |p|\n            Post.get_photo(p.original_size.url, new_post)\n          end\n        end\n        return new_post\n      end\n    end\n    \n    def self.get_photo(photo, post)\n      img = open(URI.parse(photo))\n      post.images.create!(:url => photo, :asset => img)\n      if !post.public?\n        post.update_attributes!(:public => true)\n      end\n    end\n    \n    def update_notes(tumblr_address = ENV['TUMBLR_ADDRESS'])\n      if self.tumblr_id?\n        api_address = \"http://api.tumblr.com/v2/blog/\" + tumblr_address\n        address = api_address + \"/posts?api_key=\" + ENV['TUMBLR_API_KEY'] + \"&id=\" + self.tumblr_id\n        puts \"Connecting to... \" + address\n        json = open(address).read.to_s rescue false\n        if json != false\n          post = Hashie::Mash.new(JSON.parse(json)[\"response\"]).posts.first\n          if post.note_count != self.note_count\n            self.update_attributes!(:note_count => post.note_count)\n            puts \"Notes Updated\"\n          else\n            puts \"No New Notes\"\n          end\n        else\n          puts \"Couldn't Find Post With Tumblr ID: \" + self.tumblr_id\n        end\n      end\n    end\n    \n  KRP\n  \nend\n"

#acts_as_tumblr_media(opts = {}) ⇒ Object



291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# File 'lib/acts_as_tumblr.rb', line 291

def acts_as_tumblr_media(opts={})
  
  belongs = opts[:for].nil? ? "post" : "#{opts[:for].to_s}" rescue "post"
  class_eval "  \n    require 'exportr'\n    require 'paperclip'\n    require 'aws-sdk'\n    include Paperclip::Glue\n    belongs_to :\#{belongs}\n    before_save :get_orientation\n    \n    attr_accessible :asset, :url\n    \n    def get_orientation\n      dimensions = Paperclip::Geometry.from_file(self.asset(:original))\n      if dimensions.height > dimensions.width\n        self.orientation = \"portrait\"\n      else\n        self.orientation = \"landscape\"\n      end\n    end\n    \n  KRP\n  \nend\n"

#acts_as_tumblr_userObject



318
319
320
321
322
323
324
325
326
327
328
329
# File 'lib/acts_as_tumblr.rb', line 318

def acts_as_tumblr_user
  class_eval "  \n    require 'oauth'\n    require 'exportr'\n    require 'omniauth'\n  \n    def self.find_or_create_from_auth_hash(auth_hash)\n      puts auth_hash\n    end\n  KRP\nend\n"