Class: Photoinfo

Inherits:
Object
  • Object
show all
Defined in:
lib/photoinfo.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#collectionObject (readonly)

Returns the value of attribute collection.



6
7
8
# File 'lib/photoinfo.rb', line 6

def collection
  @collection
end

#contributorObject (readonly)

Returns the value of attribute contributor.



6
7
8
# File 'lib/photoinfo.rb', line 6

def contributor
  @contributor
end

#descriptionObject (readonly)

Returns the value of attribute description.



6
7
8
# File 'lib/photoinfo.rb', line 6

def description
  @description
end

#infoObject (readonly)

Returns the value of attribute info.



6
7
8
# File 'lib/photoinfo.rb', line 6

def info
  @info
end

#keywordsObject (readonly)

Returns the value of attribute keywords.



6
7
8
# File 'lib/photoinfo.rb', line 6

def keywords
  @keywords
end

#photoidObject (readonly)

Returns the value of attribute photoid.



6
7
8
# File 'lib/photoinfo.rb', line 6

def photoid
  @photoid
end

#titleObject (readonly)

Returns the value of attribute title.



6
7
8
# File 'lib/photoinfo.rb', line 6

def title
  @title
end

#uploadedonObject (readonly)

Returns the value of attribute uploadedon.



6
7
8
# File 'lib/photoinfo.rb', line 6

def uploadedon
  @uploadedon
end

#webpageObject (readonly)

Returns the value of attribute webpage.



6
7
8
# File 'lib/photoinfo.rb', line 6

def webpage
  @webpage
end

Instance Method Details

#isCollectionObject



36
37
38
# File 'lib/photoinfo.rb', line 36

def isCollection
	@collection = @webpage.xpath('//*[@id="file-subHeader"]//ul/li[1]').text.strip
end

#isContributorObject



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

#isDescriptionObject



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

#isKeywordsObject



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

#isTitleObject



17
18
19
# File 'lib/photoinfo.rb', line 17

def isTitle
	@title = @webpage.css('span#iSFileTitle').text.strip
end

#isUploadedOnObject



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

#prepareToSaveObject



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

#readObject

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

#saveObject



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