Class: PosterousApiImporter

Inherits:
Object
  • Object
show all
Includes:
IoTools::Importer
Defined in:
lib/server/importers/posterous_api_importer.rb

Constant Summary collapse

PER_PAGE_LIMIT =
50

Class Method Summary collapse

Instance Method Summary collapse

Methods included from IoTools::Importer

included

Class Method Details

.extract_comments(comments) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/server/importers/posterous_api_importer.rb', line 23

def self.extract_comments comments
  return [] if comments.nil?
  if comments.is_a? Array
    comments.inject(IoTools::ItemStruct.new) do |comment,cmt|
      comment.body    = cmt['body']
      comment.author  = cmt['author']
      comment.date    = cmt['date']
      comment
    end
  else
    []
  end
end

Instance Method Details

#current_pageObject



66
67
68
# File 'lib/server/importers/posterous_api_importer.rb', line 66

def current_page
  @current_page ||= 1
end

#current_page=(num) ⇒ Object



70
71
72
# File 'lib/server/importers/posterous_api_importer.rb', line 70

def current_page=(num)
  @current_page = num
end

#items_for_current_pageObject



50
51
52
# File 'lib/server/importers/posterous_api_importer.rb', line 50

def items_for_current_page
  XmlSimple.xml_in( self.response_for_current_page, 'ForceArray' => false )['post']
end

#paginated_resultsObject



37
38
39
40
41
42
43
44
# File 'lib/server/importers/posterous_api_importer.rb', line 37

def paginated_results
  items = [] 
  while current_items = items_for_current_page
    items += current_items
    self.current_page += 1
  end
  items
end

#requestObject



58
59
60
61
62
63
64
# File 'lib/server/importers/posterous_api_importer.rb', line 58

def request
  @username, @password = self.params.delete('username'), self.params.delete('password') unless @username.present?
  req = Net::HTTP::Get.new(url.path)
  req.basic_auth @username, @password   
  req.set_form_data self.params.merge('num_posts' => PER_PAGE_LIMIT, 'page' => current_page)
  req
end

#response_for_current_pageObject



54
55
56
# File 'lib/server/importers/posterous_api_importer.rb', line 54

def response_for_current_page
  Net::HTTP.new(url.host, url.port).start { |http| http.request(request) }.body
end

#urlObject



46
47
48
# File 'lib/server/importers/posterous_api_importer.rb', line 46

def url
  @url ||= URI.parse('http://posterous.com/api/readposts')
end