dougsko-apod 0.0.3 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.3
1
+ 0.1.0
data/apod.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{apod}
8
- s.version = "0.0.3"
8
+ s.version = "0.1.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["dougsko"]
data/bin/apod-download CHANGED
@@ -29,4 +29,4 @@ opts.parse(ARGV)
29
29
  save_path = options["o"]
30
30
 
31
31
  apod = Apod.new
32
- apod.download(0, save_path)
32
+ apod.pictures.first.download(save_path)
data/lib/apod.rb CHANGED
@@ -5,6 +5,7 @@
5
5
 
6
6
  require 'rubygems'
7
7
  require 'open-uri'
8
+ require 'httpclient'
8
9
  require 'hpricot'
9
10
  require 'lib/pic'
10
11
 
@@ -23,16 +24,21 @@ class Apod
23
24
  @pictures.collect{ |pic| pic.title }
24
25
  end
25
26
 
26
- def download(index, save_path)
27
- doc = Hpricot(open(@pictures[index].website))
28
- link = "http://apod.nasa.gov/" + doc.at("//center/p[2]/a")["href"]
29
-
30
- url = URI.parse(link)
31
-
32
- url.open do |pic|
33
- File.open(save_path, "w") do |file|
34
- file << pic.read
27
+ def search(query)
28
+ if query.size > 60
29
+ return "ERROR: Query must be less than 60 characters"
30
+ end
31
+ @pictures = []
32
+ clnt = HTTPClient.new("http://antwrp.gsfc.nasa.gov/cgi-bin/apod/apod_search")
33
+ results = clnt.post('http://antwrp.gsfc.nasa.gov/cgi-bin/apod/apod_search', {'tquery' => query}).content
34
+ doc = Hpricot(results)
35
+ doc.search("//p").each do |para|
36
+ title = para.at("a[2]")
37
+ link = para.at("a")
38
+ if title != nil and link != nil
39
+ @pictures << Pic.new(title.innerHTML, link["href"])
35
40
  end
36
41
  end
42
+ @pictures.pop
37
43
  end
38
44
  end
data/lib/pic.rb CHANGED
@@ -10,4 +10,16 @@ class Pic
10
10
  @title = title
11
11
  @website = website
12
12
  end
13
+
14
+ def download(save_path)
15
+ doc = Hpricot(open(self.website))
16
+ link = "http://apod.nasa.gov/" + doc.at("//center/p[2]/a")["href"]
17
+ url = URI.parse(link)
18
+
19
+ url.open do |pic|
20
+ File.open(save_path, "w") do |file|
21
+ file << pic.read
22
+ end
23
+ end
24
+ end
13
25
  end
data/spec/apod_spec.rb CHANGED
@@ -13,10 +13,16 @@ describe "Apod" do
13
13
  @apod.titles.class.to_s.should == "Array"
14
14
  end
15
15
 
16
- it "should download today's pic" do
17
- @apod.download(0, "/tmp/apod.jpg")
16
+ it "search and download the first picture" do
17
+ @apod.search("earth")
18
+ @apod.pictures.first.download("/tmp/apod.jpg")
18
19
  `file /tmp/apod.jpg`.should match(/image data/)
19
20
  `rm /tmp/apod.jpg`
20
21
  end
21
22
 
23
+ it "tries to search with a query that is too big" do
24
+ @apod.search("A"*61).should match(/ERROR/)
25
+ end
26
+
27
+
22
28
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dougsko-apod
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - dougsko
@@ -57,6 +57,7 @@ files:
57
57
  - spec/spec_helper.rb
58
58
  has_rdoc: false
59
59
  homepage: http://github.com/dougsko/apod
60
+ licenses:
60
61
  post_install_message:
61
62
  rdoc_options:
62
63
  - --charset=UTF-8
@@ -77,7 +78,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
77
78
  requirements: []
78
79
 
79
80
  rubyforge_project:
80
- rubygems_version: 1.2.0
81
+ rubygems_version: 1.3.5
81
82
  signing_key:
82
83
  specification_version: 3
83
84
  summary: A library to work with NASA's astronomy pictures of the day.