vast_api 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,18 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+ gem "activesupport", ">= 2.3.5"
6
+ gem "nokogiri", ">= 0"
7
+
8
+ # Add dependencies to develop your gem here.
9
+ # Include everything needed to run rake, tests, features, etc.
10
+ group :development do
11
+ gem "minitest", ">= 0"
12
+ gem "yard", "~> 0.6.0"
13
+ gem "bundler", "~> 1.0.0"
14
+ gem "jeweler", "~> 1.5.2"
15
+ gem "rcov", ">= 0"
16
+ gem "webmock", ">= 0"
17
+ gem "vcr", ">= 0"
18
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,34 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ activesupport (3.0.5)
5
+ addressable (2.2.4)
6
+ crack (0.1.8)
7
+ git (1.2.5)
8
+ jeweler (1.5.2)
9
+ bundler (~> 1.0.0)
10
+ git (>= 1.2.5)
11
+ rake
12
+ minitest (2.0.2)
13
+ nokogiri (1.4.4)
14
+ rake (0.8.7)
15
+ rcov (0.9.9)
16
+ vcr (1.7.0)
17
+ webmock (1.6.2)
18
+ addressable (>= 2.2.2)
19
+ crack (>= 0.1.7)
20
+ yard (0.6.5)
21
+
22
+ PLATFORMS
23
+ ruby
24
+
25
+ DEPENDENCIES
26
+ activesupport (>= 2.3.5)
27
+ bundler (~> 1.0.0)
28
+ jeweler (~> 1.5.2)
29
+ minitest
30
+ nokogiri
31
+ rcov
32
+ vcr
33
+ webmock
34
+ yard (~> 0.6.0)
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Ryan Ong
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,47 @@
1
+ = vast_api
2
+
3
+ A ruby wrapper for the vast.com listings api.
4
+
5
+ == Install
6
+ gem install vast_api
7
+
8
+ == Usage
9
+ # initialize vast
10
+ vast = @Vast.new('apikey','category') # both settings are optional
11
+
12
+ # Set Category
13
+ vast.category('item_vehicle/bike') #chain able
14
+ vast.category #retrives category
15
+
16
+ # set params
17
+ vast.where("price"=>"1001-2000")
18
+
19
+ # Get listings
20
+ vast.listing
21
+
22
+ # Get attributes
23
+ vast.attributes
24
+
25
+ # Get attributes of listings and visa versa
26
+ vast.listing.attributes
27
+ vast.attributes.listing
28
+
29
+ # Get Categories
30
+ vast.categories
31
+
32
+
33
+ == Contributing to vast_api
34
+
35
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
36
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
37
+ * Fork the project
38
+ * Start a feature/bugfix branch
39
+ * Commit and push until you are happy with your contribution
40
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
41
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
42
+
43
+ == Copyright
44
+
45
+ Copyright (c) 2011 Ryan Ong. See LICENSE.txt for
46
+ further details.
47
+
data/Rakefile ADDED
@@ -0,0 +1,46 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'rake'
11
+
12
+ require 'jeweler'
13
+ Jeweler::Tasks.new do |gem|
14
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
15
+ gem.name = "vast_api"
16
+ gem.homepage = "http://github.com/ryanong/vast_api"
17
+ gem.license = "MIT"
18
+ gem.summary = "A ruby wrapper for the http://www.vast.com/ api"
19
+ gem.description = "A ruby wrapper for the http://www.vast.com/ api"
20
+ gem.email = "ryanong@gmail.com"
21
+ gem.authors = ["Ryan Ong", "Mikhail Knyazev"]
22
+ # Include your dependencies below. Runtime dependencies are required when using your gem,
23
+ # and development dependencies are only needed for development (ie running rake tasks, tests, etc)
24
+ # gem.add_runtime_dependency 'jabber4r', '> 0.1'
25
+ # gem.add_development_dependency 'rspec', '> 1.2.3'
26
+ end
27
+ Jeweler::RubygemsDotOrgTasks.new
28
+
29
+ require 'rake/testtask'
30
+ Rake::TestTask.new(:test) do |test|
31
+ test.libs << 'lib' << 'test'
32
+ test.pattern = 'test/**/test_*.rb'
33
+ test.verbose = true
34
+ end
35
+
36
+ require 'rcov/rcovtask'
37
+ Rcov::RcovTask.new do |test|
38
+ test.libs << 'test'
39
+ test.pattern = 'test/**/test_*.rb'
40
+ test.verbose = true
41
+ end
42
+
43
+ task :default => :test
44
+
45
+ require 'yard'
46
+ YARD::Rake::YardocTask.new
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
data/lib/vast_api.rb ADDED
@@ -0,0 +1,87 @@
1
+ require 'active_support'
2
+ begin
3
+ require 'active_support/core_ext/object/to_query'
4
+ rescue
5
+ end
6
+
7
+ require File.expand_path('../vast_api/base.rb', __FILE__)
8
+ require File.expand_path('../vast_api/attributes.rb', __FILE__)
9
+ require File.expand_path('../vast_api/attributes/entry.rb', __FILE__)
10
+ require File.expand_path('../vast_api/categories.rb', __FILE__)
11
+ require File.expand_path('../vast_api/categories/category.rb', __FILE__)
12
+ require File.expand_path('../vast_api/listings.rb', __FILE__)
13
+ require File.expand_path('../vast_api/listings/entry.rb', __FILE__)
14
+
15
+ class VastApi
16
+ API_URL = 'http://data.vast.com/'
17
+
18
+ def initialize(apikey=nil, cat=nil)
19
+ @category = cat
20
+ @api_key = apikey
21
+ @query = { }
22
+ @categories = {}
23
+ @attributes = {}
24
+ @listings = {}
25
+ end
26
+
27
+ def category(cat=nil)
28
+ if cat.nil?
29
+ @category
30
+ else
31
+ @category = cat
32
+ self
33
+ end
34
+ end
35
+
36
+ def api_key(api_key=nil)
37
+ if api_key.nil?
38
+ @api_key
39
+ else
40
+ @api_key = api_key
41
+ self
42
+ end
43
+ end
44
+
45
+ def where(options)
46
+ @query.merge!(options)
47
+ self
48
+ end
49
+
50
+ def query
51
+ query = @query.dup
52
+ query['apikey'] = @api_key if @api_key
53
+ query.to_query
54
+ end
55
+
56
+ def listings
57
+ raise ArgumentError, "No category was entered" if @category.nil? || @category.empty?
58
+ @listings[opt_hash] ||= Listings.new(self)
59
+ end
60
+
61
+ def attributes
62
+ raise ArgumentError, "No category was entered" if @category.nil? || @category.empty?
63
+ @attributes[opt_hash] ||= Attributes.new(self)
64
+ end
65
+
66
+ def categories
67
+ @categories[@api_key] ||= Categories.new(self)
68
+ end
69
+
70
+ def find(id, cat)
71
+ Listings.get("#{API_URL}listings/#{id}/-/#{cat}")
72
+ end
73
+
74
+ def self.find_by_url(url)
75
+ Listings.get(url)
76
+ end
77
+
78
+ private
79
+
80
+ def opt_hash
81
+ str = ''
82
+ str << @category if @category
83
+ str << @api_key if @api_key
84
+ str << @query.hash.to_s if @query
85
+ str.hash
86
+ end
87
+ end
@@ -0,0 +1,44 @@
1
+ class VastApi
2
+ class Attributes < Hash
3
+ attr_reader :xml, :url, :vast
4
+
5
+ def initialize(vast)
6
+ @vast = vast
7
+ @url = "#{API_URL}attributes/-/#{@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
+ private
36
+
37
+ def populate
38
+ @xml.xpath("//xmlns:entry").each do |entry|
39
+ self[entry.at_xpath('v:attribute')['id']] = Entry.new(entry)
40
+ end
41
+ end
42
+
43
+ end
44
+ end
@@ -0,0 +1,52 @@
1
+ class VastApi
2
+ class Attributes
3
+ class Entry < Hash
4
+
5
+ def initialize(doc)
6
+ @attributes = {}
7
+ @doc = doc
8
+ @doc.xpath("v:attribute/v:value").each do |value|
9
+ self[value["id"]] = { :name => value.content, :count => value["count"] }
10
+ end
11
+ end
12
+
13
+ def author
14
+ @attributes['author'] ||= get_children(@doc.at('author'))
15
+ end
16
+
17
+ def attribute(var_name)
18
+ @attributes[var_name] ||=
19
+ if %W{author updated}.index(var_name)
20
+ self.send(var_name)
21
+ elsif %W{ title id content }.index(var_name)
22
+ get_var(var_name)
23
+ else
24
+ get_var("//v:#{var_name}")
25
+ end
26
+ end
27
+
28
+ def updated
29
+ @attributes["updated"] ||= DateTime.strptime(@doc.at("updated").content)
30
+ end
31
+
32
+ private
33
+
34
+ def get_var(var_name)
35
+ if var = @doc.at(var_name)
36
+ var.text
37
+ else
38
+ nil
39
+ end
40
+ end
41
+
42
+ def get_children(var)
43
+ hash = {}
44
+ var.children.each do |item|
45
+ hash[item.name] = item.content
46
+ end
47
+ hash
48
+ end
49
+
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,43 @@
1
+ require 'open-uri'
2
+ require 'nokogiri'
3
+
4
+ class VastApi
5
+ class Base < Array
6
+ attr_reader :xml, :url, :vast
7
+
8
+ def initialize(vast)
9
+ @vast = vast
10
+ @url = "#{API_URL}#{self.class.to_s.split('::')[1].downcase}/-/#{@vast.category}?#{@vast.query}"
11
+ @xml = Nokogiri::XML(open(@url))
12
+ populate
13
+ end
14
+
15
+ def attr(var_name)
16
+ if var = @xml.at(var_name)
17
+ var.text
18
+ elsif var = @xml.at('//v:'+var_name)
19
+ var.text
20
+ elsif var = @xml.at('//o:'+var_name)
21
+ var.text
22
+ else
23
+ nil
24
+ end
25
+ end
26
+
27
+ def attributes
28
+ @vast.attributes
29
+ end
30
+
31
+ def listings
32
+ @vast.listings
33
+ end
34
+
35
+ def categories
36
+ @vast.categories
37
+ end
38
+ private
39
+
40
+ def populate
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,51 @@
1
+ class VastApi
2
+ class Categories < Hash
3
+ attr_reader :url
4
+ def initialize(vast)
5
+ @vast = vast
6
+ query = (@vast.api_key) ? "?apikey=#{@vast.api_key}" : ''
7
+ @url = "#{API_URL}categories#{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
+
36
+ private
37
+
38
+ def populate
39
+ @xml.xpath('//xmlns:entry/v:category').each do |node|
40
+ node.extend(Category)
41
+ self[node['id']] = {
42
+ :name => node['name'],
43
+ :count => node['count']
44
+ }
45
+ if node.element_children.length > 0
46
+ self[node['id']][:children] = node.categories
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end