Class: Photoinfo
- Inherits:
-
Object
- Object
- Photoinfo
- Defined in:
- lib/photoinfo.rb
Instance Attribute Summary collapse
-
#collection ⇒ Object
readonly
Returns the value of attribute collection.
-
#contributor ⇒ Object
readonly
Returns the value of attribute contributor.
-
#description ⇒ Object
readonly
Returns the value of attribute description.
-
#info ⇒ Object
readonly
Returns the value of attribute info.
-
#keywords ⇒ Object
readonly
Returns the value of attribute keywords.
-
#photoid ⇒ Object
readonly
Returns the value of attribute photoid.
-
#title ⇒ Object
readonly
Returns the value of attribute title.
-
#uploadedon ⇒ Object
readonly
Returns the value of attribute uploadedon.
-
#webpage ⇒ Object
readonly
Returns the value of attribute webpage.
Instance Method Summary collapse
-
#initialize(photoid) ⇒ Photoinfo
constructor
A new instance of Photoinfo.
- #isCollection ⇒ Object
- #isContributor ⇒ Object
- #isDescription ⇒ Object
- #isKeywords ⇒ Object
- #isTitle ⇒ Object
- #isUploadedOn ⇒ Object
- #prepareToSave ⇒ Object
-
#read ⇒ Object
puts “Please input istockphoto id: ” sample istockphoto id “21846754”.
- #save ⇒ Object
Constructor Details
#initialize(photoid) ⇒ Photoinfo
Returns a new instance of Photoinfo.
7 8 9 10 11 12 |
# File 'lib/photoinfo.rb', line 7 def initialize(photoid) @photoid = photoid.to_i # puts "Please input istockphoto id: " # @photoid = gets.chomp!.to_i # sample istockphoto id "21846754" end |
Instance Attribute Details
#collection ⇒ Object (readonly)
Returns the value of attribute collection.
6 7 8 |
# File 'lib/photoinfo.rb', line 6 def collection @collection end |
#contributor ⇒ Object (readonly)
Returns the value of attribute contributor.
6 7 8 |
# File 'lib/photoinfo.rb', line 6 def contributor @contributor end |
#description ⇒ Object (readonly)
Returns the value of attribute description.
6 7 8 |
# File 'lib/photoinfo.rb', line 6 def description @description end |
#info ⇒ Object (readonly)
Returns the value of attribute info.
6 7 8 |
# File 'lib/photoinfo.rb', line 6 def info @info end |
#keywords ⇒ Object (readonly)
Returns the value of attribute keywords.
6 7 8 |
# File 'lib/photoinfo.rb', line 6 def keywords @keywords end |
#photoid ⇒ Object (readonly)
Returns the value of attribute photoid.
6 7 8 |
# File 'lib/photoinfo.rb', line 6 def photoid @photoid end |
#title ⇒ Object (readonly)
Returns the value of attribute title.
6 7 8 |
# File 'lib/photoinfo.rb', line 6 def title @title end |
#uploadedon ⇒ Object (readonly)
Returns the value of attribute uploadedon.
6 7 8 |
# File 'lib/photoinfo.rb', line 6 def uploadedon @uploadedon end |
#webpage ⇒ Object (readonly)
Returns the value of attribute webpage.
6 7 8 |
# File 'lib/photoinfo.rb', line 6 def webpage @webpage end |
Instance Method Details
#isCollection ⇒ Object
36 37 38 |
# File 'lib/photoinfo.rb', line 36 def isCollection @collection = @webpage.xpath('//*[@id="file-subHeader"]//ul/li[1]').text.strip end |
#isContributor ⇒ Object
26 27 28 |
# File 'lib/photoinfo.rb', line 26 def isContributor @contributor = @webpage.xpath('//*[@id="file-details-table"]//td[2]/ul/li/a[2]').text.strip end |
#isDescription ⇒ Object
20 21 22 23 24 25 |
# File 'lib/photoinfo.rb', line 20 def isDescription a = @webpage.xpath('//*[@id="artist-description-container"]').text b = a.match (/(\n)(.+)(\n)(.+)/) @description = b[4].sub(/\s+/, "") "Description: #@description" end |
#isKeywords ⇒ Object
29 30 31 32 33 34 35 |
# File 'lib/photoinfo.rb', line 29 def isKeywords @keywords = [] @webpage.xpath('//*[@id="file-details-table"]//tr[2]/td[2]/a').each do |m| @keywords += [m.text] end @keywords = @keywords.join(", ") end |
#isTitle ⇒ Object
17 18 19 |
# File 'lib/photoinfo.rb', line 17 def isTitle @title = @webpage.css('span#iSFileTitle').text.strip end |
#isUploadedOn ⇒ Object
39 40 41 |
# File 'lib/photoinfo.rb', line 39 def isUploadedOn @uploadedon = @webpage.xpath('//*[@id="file-details-table"]//tr[6]/td[2]').text.strip end |
#prepareToSave ⇒ Object
42 43 44 45 46 47 48 49 50 |
# File 'lib/photoinfo.rb', line 42 def prepareToSave read isTitle isDescription isContributor isKeywords isCollection isUploadedOn end |
#read ⇒ Object
puts “Please input istockphoto id: ” sample istockphoto id “21846754”
13 14 15 16 |
# File 'lib/photoinfo.rb', line 13 def read @webpage = Nokogiri::HTML(open("http://www.istockphoto.com/search/text/#@photoid").read) @webpage.encoding = 'utf-8' end |
#save ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/photoinfo.rb', line 51 def save self.prepareToSave @info = { :title => @title, :description => @description, :contributor => @contributor, :keywords => @keywords.to_s, :collection => @collection, :uploadedon => @uploadedon } output = File.new('istockphoto.yml', 'a+') output.puts YAML.dump(@info) output.close end |