vast_api 0.0.1 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -13,7 +13,7 @@ require 'jeweler'
13
13
  Jeweler::Tasks.new do |gem|
14
14
  # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
15
15
  gem.name = "vast_api"
16
- gem.homepage = "http://github.com/ryanong/vast_api"
16
+ gem.homepage = "http://github.com/carzen/vast_api"
17
17
  gem.license = "MIT"
18
18
  gem.summary = "A ruby wrapper for the http://www.vast.com/ api"
19
19
  gem.description = "A ruby wrapper for the http://www.vast.com/ api"
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.1
1
+ 0.0.4
@@ -10,29 +10,29 @@ class VastApi
10
10
  end
11
11
  end
12
12
 
13
- def author
14
- @attributes['author'] ||= get_children(@doc.at('author'))
15
- end
16
-
17
13
  def attribute(var_name)
18
14
  @attributes[var_name] ||=
19
15
  if %W{author updated}.index(var_name)
20
16
  self.send(var_name)
21
17
  elsif %W{ title id content }.index(var_name)
22
- get_var(var_name)
18
+ get_var('xmlns:'+var_name)
23
19
  else
24
- get_var("//v:#{var_name}")
20
+ get_var("v:#{var_name}")
25
21
  end
26
22
  end
27
23
 
24
+ def author
25
+ @attributes['author'] ||= get_children(@doc.at_xpath('xmlns:author'))
26
+ end
27
+
28
28
  def updated
29
- @attributes["updated"] ||= DateTime.strptime(@doc.at("updated").content)
29
+ @attributes["updated"] ||= DateTime.strptime(@doc.at_xpath('xmlns:updated').content)
30
30
  end
31
31
 
32
32
  private
33
33
 
34
34
  def get_var(var_name)
35
- if var = @doc.at(var_name)
35
+ if var = @doc.at_xpath(var_name)
36
36
  var.text
37
37
  else
38
38
  nil
@@ -1,6 +1,7 @@
1
1
  class VastApi
2
2
  class Listings
3
3
  class Entry
4
+ attr_reader :doc
4
5
  def initialize(doc)
5
6
  @doc = doc
6
7
  @attributes = {}
@@ -11,9 +12,9 @@ class VastApi
11
12
  if %W{id author link feeds updated published}.index(var_name)
12
13
  self.send(var_name)
13
14
  elsif %W{ title }.index(var_name)
14
- get_var(var_name)
15
+ get_var('xmlns:'+var_name)
15
16
  else
16
- get_var("//v:#{var_name}")
17
+ get_var("v:#{var_name}")
17
18
  end
18
19
  end
19
20
 
@@ -31,34 +32,39 @@ class VastApi
31
32
  end
32
33
 
33
34
  def id
34
- @attributes['id'] ||= get_var('//v:item_id')
35
+ @attributes['id'] ||= get_var('v:item_id')
35
36
  end
36
37
 
37
38
  def link
38
- @attributes['link'] ||= get_var('id')
39
+ @attributes['link'] ||= get_var('xmlns:id')
39
40
  end
40
41
 
41
42
  def author
42
- @attributes['author'] ||= get_children(@doc.at('author'))
43
+ @attributes['author'] ||= get_children(@doc.at_xpath('xmlns:author'))
43
44
  end
44
45
 
45
46
  def updated
46
- @attributes["updated"] ||= DateTime.strptime(@doc.at("updated").content)
47
+ @attributes["updated"] ||= DateTime.strptime(@doc.at_xpath('xmlns:updated').content)
47
48
  end
48
49
 
49
50
  def published
50
- @attributes["published"] ||= DateTime.strptime(@doc.at("published").content)
51
+ @attributes["published"] ||= DateTime.strptime(@doc.at_xpath('xmlns:published').content)
51
52
  end
52
53
 
53
54
  def vast_links
54
- @attributes["vast_links"] = @doc.css('link').map do |ln|
55
- ln.attributes
55
+ if @attributes["vast_links"].nil?
56
+ @attributes["vast_links"] = {}
57
+ @doc.css('link').each do |ln|
58
+ attributes = ln.attributes
59
+ @attributes["vast_links"][attributes['rel']] = attributes
60
+ end
56
61
  end
62
+ @attributes["vast_links"]
57
63
  end
58
64
  private
59
65
 
60
66
  def get_var(var_name)
61
- if var = @doc.at(var_name)
67
+ if var = @doc.at_xpath(var_name)
62
68
  var.text
63
69
  else
64
70
  nil
@@ -1,5 +1,38 @@
1
1
  class VastApi
2
- class Listings < VastApi::Base
2
+ class Listings < Array
3
+ attr_reader :xml, :url, :vast
4
+
5
+ def initialize(vast)
6
+ @vast = vast
7
+ @url = "#{API_URL}#{self.class.to_s.split('::')[1].downcase}/-/#{@vast.category}?#{@vast.query}"
8
+ @xml = Nokogiri::XML(open(@url))
9
+ populate
10
+ end
11
+
12
+ def attr(var_name)
13
+ if var = @xml.at(var_name)
14
+ var.text
15
+ elsif var = @xml.at('//v:'+var_name)
16
+ var.text
17
+ elsif var = @xml.at('//o:'+var_name)
18
+ var.text
19
+ else
20
+ nil
21
+ end
22
+ end
23
+
24
+ def attributes
25
+ @vast.attributes
26
+ end
27
+
28
+ def listings
29
+ @vast.listings
30
+ end
31
+
32
+ def categories
33
+ @vast.categories
34
+ end
35
+
3
36
  def query
4
37
  if @query.nil?
5
38
  @query = {}
data/lib/vast_api.rb CHANGED
@@ -1,10 +1,11 @@
1
+ require 'open-uri'
2
+ require 'nokogiri'
1
3
  require 'active_support'
2
4
  begin
3
5
  require 'active_support/core_ext/object/to_query'
4
6
  rescue
5
7
  end
6
8
 
7
- require File.expand_path('../vast_api/base.rb', __FILE__)
8
9
  require File.expand_path('../vast_api/attributes.rb', __FILE__)
9
10
  require File.expand_path('../vast_api/attributes/entry.rb', __FILE__)
10
11
  require File.expand_path('../vast_api/categories.rb', __FILE__)
@@ -67,7 +68,21 @@ class VastApi
67
68
  @categories[@api_key] ||= Categories.new(self)
68
69
  end
69
70
 
70
- def find(id, cat)
71
+ def find(id)
72
+ raise ArgumentError, "No category was entered" if @category.nil? || @category.empty?
73
+ if id.nil? || id.empty?
74
+ nil
75
+ else
76
+ Listings.get("#{API_URL}listings/#{id}/-/#{@category}")
77
+ end
78
+ end
79
+
80
+ def find!(id)
81
+ raise ArgumentError, "No id was entered" if id.nil? || id.empty?
82
+ find(id)
83
+ end
84
+
85
+ def self.find(id, cat)
71
86
  Listings.get("#{API_URL}listings/#{id}/-/#{cat}")
72
87
  end
73
88