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

#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 <<-KRP
  
    require 'exportr'
    require 'paperclip'
    require 'aws-sdk'
    include Paperclip::Glue
    belongs_to :#{belongs}
    before_save :get_orientation
    
    attr_accessible :asset, :url
    
    def get_orientation
      dimensions = Paperclip::Geometry.from_file(self.asset(:original))
      if dimensions.height > dimensions.width
        self.orientation = "portrait"
      else
        self.orientation = "landscape"
      end
    end
    
  KRP
  
end

#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 <<-KRP
  
    require 'oauth'
    require 'exportr'
    require 'omniauth'
  
    def self.find_or_create_from_auth_hash(auth_hash)
      puts auth_hash
    end
  KRP
end