Permalink
Browse files

initial commit

  • Loading branch information...
0 parents commit 1e6d0eaf5ee150a3414b21837cea50fe9c5493c5 @adamzaninovich committed May 13, 2011
Showing with 245 additions and 0 deletions.
  1. +6 −0 .gitignore
  2. +14 −0 Gemfile
  3. +47 −0 README.md
  4. +17 −0 Rakefile
  5. +1 −0 config.yml.example
  6. +16 −0 demo.rb
  7. +9 −0 lib/snoother.rb
  8. +79 −0 lib/snoother/search.rb
  9. +3 −0 lib/snoother/version.rb
  10. +21 −0 snoother.gemspec
  11. +2 −0 test/test_helper.rb
  12. +11 −0 test/test_search.rb
  13. +19 −0 test/test_snoother.rb
6 .gitignore
@@ -0,0 +1,6 @@
+*.gem
+.bundle
+Gemfile.lock
+pkg/*
+config.yml
+.rvmrc
14 Gemfile
@@ -0,0 +1,14 @@
+# using bundler simply to keep track of the dependencies
+# you MUST explicitly require any gems in the files they are needed
+
+source "http://rubygems.org"
+
+# Specify your gem's dependencies in snoother.gemspec
+# gemspec
+
+# the following should eventually be moved into the gemspec file
+group :test do
+ # minitest is included in ruby 1.9, but the gem pulls in the latest changes
+ gem 'minitest', '~> 2', :require => false
+ # gem 'mocha', '~> 0.9', :require => false
+end
47 README.md
@@ -0,0 +1,47 @@
+# Snoother
+
+## Summary
+
+Snoother is a gem for accessing the Snooth database and is modeled after their own PHP library.
+
+[Snooth](http://snooth.com) is a social-based wine shopping website. They claim to have the world's largest wine database. That database is available through the [Snooth API](http://api.snooth.com) and this gem exposes the api for use in your Ruby project.
+
+_Note: This is not yet ready for use._
+
+## Setup
+
+0. Get a Snooth API key [here](http://api.snooth.com/register/)
+1. Rename `config.yml.example` to `config.yml`
+2. Add your API key to config.yml
+3. Run `bundle`
+4. Checkout `demo.rb` included
+
+## Development Setup
+
+0. Get a Snooth API key [here](http://api.snooth.com/register/)
+1. Rename `config.yml.example` to `config.yml`
+2. Add your API key to config.yml
+3. Run `bundle`
+4. Run `rake` to run the tests and make sure everything is working properly
+
+## License
+
+Snoother is released under the MIT license.
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
17 Rakefile
@@ -0,0 +1,17 @@
+require 'bundler'
+require 'rake/testtask'
+require_relative 'lib/snoother'
+Bundler::GemHelper.install_tasks
+
+Rake::TestTask.new do |t|
+ t.libs << "test"
+ t.test_files = FileList['test/test_*.rb']
+ t.verbose = true
+end
+
+# desc "Load stuff in IRB."
+# task :console do
+# exec "irb -Ilib -rsmpl"
+# end
+
+task :default => :test
1 config.yml.example
@@ -0,0 +1 @@
+api_key: your-snooth-api-key
16 demo.rb
@@ -0,0 +1,16 @@
+#!/usr/bin/env ruby -wKU
+
+require 'yaml'
+
+require_relative 'lib/snoother'
+
+api_key = YAML.load(File.read(Dir.pwd+'/config.yml'))['api_key']
+
+search = Snoother::Search.new(api_key)
+search.set_parameter(
+ a: 0, # search all wines, not just in stock
+ n: 5, # return 5 results
+ t: :wine, # return only wine, no spirits
+ q: "Castoro Tempranillo"
+)
+p search.execute(:wines)
9 lib/snoother.rb
@@ -0,0 +1,9 @@
+require "net/http"
+require 'json'
+
+module Snoother
+ SNOOTH_BASE_URL = 'http://api.snooth.com'
+ MY_API_KEY = ''
+end
+
+require_relative 'snoother/search'
79 lib/snoother/search.rb
@@ -0,0 +1,79 @@
+module Snoother
+
+ class Search
+ attr_reader :response
+
+ def initialize(akey=MY_API_KEY, ip='0.0.0.0', format=:json)
+ @akey = akey
+ @ip = ip
+ @format = format
+ @parameters = Hash.new
+ @username = ''
+ @password = ''
+ end
+
+ def set_parameter params
+ # Search
+
+ # method: wine (id is required)
+ # id - The unique code for the requested wine.
+ # i - Inventory. Set this to 1 to return price information for the requested wine.
+ # c - Country. Limits price data to wines available in this country.
+ # z - Zip Code. When combined with certain countries this will return local results.
+ # lat - Latitude. When combined with longitude this will return local results.
+ # lng - Longitude. When combined with latitude this will return local results.
+ # lang - Language. Return winery supplied content (Winemaker's Notes, Vintage Notes, etc) in the specified language. Possible values: en, el, fr, de, it, pt, es. (Beta)
+
+ # method: wines
+ # q - Your search.
+ # f - The first result to return. Default: 1
+ # n - Number of results. Possible values are 1-1000. Default: 10.
+ # t - Product type. Possible values are 'wine' or 'spirits'. Leave blank to include results for both wines and spirits.
+ # m - Store ID. Limits results to wines available from this store. Will cause location-based parameters to be ignored.
+ # c - Country. Limits results to wines available in this country. (List of supported countries)
+ # z - Zip Code. When combined with certain countries this will return local results.
+ # lat - Latitude. When combined with a longitude value, will return local results. If a Country parameter is included, this parameter will be ignored.
+ # lng - Longitude. When combined with a latitude value, will return local results. If a Country parameter is included, this parameter will be ignored.
+ # s - Sort. Possible values are sr (SnoothRank), price+asc, price+desc, qpr (Quality Price Ratio), vintage+asc, vintage+desc. Leave blank for recommended sort.
+ # mp - Minimum price. Possible values are in full US dollars.
+ # xp - Maximum price. Possible values are in full US dollars.
+ # a - Available. Set this to 0 to return all wines, or 1 to return wines in stock. Default: 1.
+ # lang - Language. Only return wines with content in the specified language. Possible values: en, el, fr, de, it, pt, es. (Beta)
+
+ @parameters.merge(params)
+ end
+
+ def set_authentication username, password
+ @username = username
+ @password = password
+ end
+
+ def reset_parameters
+ @parameters = Hash.new
+ end
+
+ def execute method
+ unless method.empty?
+ return send(SNOOTH_BASE_URL + '/' + method.to_s + '/')
+ end
+ return false
+ end
+
+ private
+ def send url
+ parameters = {akey: @akey, ip: @ip, format: @format}.merge(@parameters)
+ uri = URI.parse(url)
+ http = Net::HTTP.new(uri.host,uri.port)
+ request = Net::HTTP::Post.new(uri.request_uri)
+ request.set_form_data(parameters) unless parameters.empty?
+ begin
+ response = http.request(request)
+ return JSON.parse(response.body)
+ rescue Net::HTTPError => e
+ return false
+ end
+ end
+
+ end
+
+end
3 lib/snoother/version.rb
@@ -0,0 +1,3 @@
+module Snoother
+ VERSION = "0.0.1"
+end
21 snoother.gemspec
@@ -0,0 +1,21 @@
+# -*- encoding: utf-8 -*-
+$:.push File.expand_path("../lib", __FILE__)
+require "snoother/version"
+
+Gem::Specification.new do |s|
+ s.name = "snoother"
+ s.version = Snoother::VERSION
+ s.platform = Gem::Platform::RUBY
+ s.authors = ["TODO: Write your name"]
+ s.email = ["TODO: Write your email address"]
+ s.homepage = ""
+ s.summary = %q{TODO: Write a gem summary}
+ s.description = %q{TODO: Write a gem description}
+
+ s.rubyforge_project = "snoother"
+
+ s.files = `git ls-files`.split("\n")
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
+ s.require_paths = ["lib"]
+end
2 test/test_helper.rb
@@ -0,0 +1,2 @@
+require 'minitest/autorun'
+require 'snoother'
11 test/test_search.rb
@@ -0,0 +1,11 @@
+require_relative 'test_helper'
+
+class TestSnootherSearch < MiniTest::Unit::TestCase
+
+ # def setup
+ # # This gets run before every test
+ # end
+
+
+
+end
19 test/test_snoother.rb
@@ -0,0 +1,19 @@
+require_relative 'test_helper'
+
+# High-level Integration tests go here
+
+class TestSnoother < MiniTest::Unit::TestCase
+
+ # def setup
+ # # This gets run before every test
+ # end
+
+ def test_the_truth
+ assert true
+ end
+
+ def test_the_untruth
+ refute false
+ end
+
+end

0 comments on commit 1e6d0ea

Please sign in to comment.