Class: Instagram::RssGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/instagram-rss_generator.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user_name) ⇒ RssGenerator

Returns a new instance of RssGenerator.



13
14
15
# File 'lib/instagram-rss_generator.rb', line 13

def initialize(user_name)
  @user_name = user_name
end

Instance Attribute Details

#user_nameObject (readonly)

Returns the value of attribute user_name.



7
8
9
# File 'lib/instagram-rss_generator.rb', line 7

def user_name
  @user_name
end

Class Method Details

.call(user_name) ⇒ Object



9
10
11
# File 'lib/instagram-rss_generator.rb', line 9

def self.call(user_name)
  new(user_name).call
end

Instance Method Details

#build_rss_feedObject



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
# File 'lib/instagram-rss_generator.rb', line 29

def build_rss_feed
  builder = Nokogiri::XML::Builder.new do |xml|
    xml.rss(
      "xmlns:blogChannel" => "http://backend.userland.com/blogChannelModule",
      "xmlns:media" => "http://search.yahoo.com/mrss/",
      "xmlns:dc" => "http://purl.org/dc/elements/1.1/",
      "xmlns:atom" => "http://www.w3.org/2005/Atom",
      "version" => "2.0"
    ) {
      xml.channel {
      xml.title "#{user_name}'s Instagram Feed"
      xml.link instagram_url
      xml.description "#{user_name}'s Instagram Feed"
      xml["atom"].link("href" => instagram_url, "rel" => "self")
      items.each do |item|
        xml.item {
          xml.title "#{user_name}'s photo on Instagram"
          xml.link get_photo_url(item)
          xml.guid get_photo_url(item)
          xml.pubDate get_photo_date(item)
        }
      end
    }
    }
  end
  builder.to_xml
end

#build_static_htmlObject



22
23
24
25
26
27
# File 'lib/instagram-rss_generator.rb', line 22

def build_static_html
  file_path = page_building_script
  html = Phantomjs.run(file_path)
  File.delete(file_path)
  html
end

#callObject



17
18
19
20
# File 'lib/instagram-rss_generator.rb', line 17

def call
  @html_content = build_static_html
  build_rss_feed
end

#get_photo_date(item) ⇒ Object



71
72
73
74
75
76
77
# File 'lib/instagram-rss_generator.rb', line 71

def get_photo_date(item)
  elements = item.css('time.photo-date span')
  day = elements[2].text
  month = elements[4].text
  year = elements[6].text
  Date.parse("#{month} #{day} #{year}").rfc2822.to_s
end

#get_photo_url(item) ⇒ Object



66
67
68
69
# File 'lib/instagram-rss_generator.rb', line 66

def get_photo_url(item)
  anchor = item.css('a.bg.bg')
  anchor.attr('href').value
end

#instagram_urlObject



57
58
59
# File 'lib/instagram-rss_generator.rb', line 57

def instagram_url
  "http://instagram.com/#{user_name}"
end

#itemsObject



61
62
63
64
# File 'lib/instagram-rss_generator.rb', line 61

def items
  document = Nokogiri::HTML(@html_content)
  document.css("li.photo")
end

#page_building_scriptObject



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/instagram-rss_generator.rb', line 79

def page_building_script
  file = File.new('build.#{user_name}.js', 'w')
  file.write(
    <<-EOS
var page = require('webpage').create();
page.open('#{instagram_url}', function (status) {
  if (status !== 'success') {
console.log('unable to access network');
  } else {
var p = page.evaluate(function () {
  return document.getElementsByTagName('html')[0].innerHTML;
});
console.log(p);
  }
  phantom.exit();
});
    EOS
  )
  file.close
  file.path
end