river 0.0.1

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.
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
@@ -0,0 +1,5 @@
1
+ *.sw?
2
+ .DS_Store
3
+ coverage
4
+ rdoc
5
+ pkg
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Justin Ball
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.
@@ -0,0 +1,18 @@
1
+ = river
2
+
3
+ Description goes here.
4
+
5
+ == Note on Patches/Pull Requests
6
+
7
+ * Fork the project.
8
+ * Make your feature addition or bug fix.
9
+ * Add tests for it. This is important so I don't break it in a
10
+ future version unintentionally.
11
+ * Commit, do not mess with rakefile, version, or history.
12
+ (if you want to have your own version, that is fine but
13
+ bump version in a commit by itself I can ignore when I pull)
14
+ * Send me a pull request. Bonus points for topic branches.
15
+
16
+ == Copyright
17
+
18
+ Copyright (c) 2009 Justin Ball. See LICENSE for details.
@@ -0,0 +1,57 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "river"
8
+ gem.summary = "Talk to Amazon"
9
+ gem.description = "Code to interact with the AWS API"
10
+ gem.email = "justinball@gmail.com"
11
+ gem.homepage = "http://github.com/jbasdf/river"
12
+ gem.authors = ["Justin Ball"]
13
+ gem.add_development_dependency "thoughtbot-shoulda"
14
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
+ end
16
+ Jeweler::GemcutterTasks.new
17
+ rescue LoadError
18
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
19
+ end
20
+
21
+ require 'rake/testtask'
22
+ Rake::TestTask.new(:test) do |test|
23
+ test.libs << 'lib' << 'test'
24
+ test.pattern = 'test/**/*_test.rb'
25
+ test.verbose = true
26
+ end
27
+
28
+ begin
29
+ require 'rcov/rcovtask'
30
+ Rcov::RcovTask.new do |test|
31
+ test.libs << 'test'
32
+ test.pattern = 'test/**/*_test.rb'
33
+ test.verbose = true
34
+ end
35
+ rescue LoadError
36
+ task :rcov do
37
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
38
+ end
39
+ end
40
+
41
+ task :test => :check_dependencies
42
+
43
+ task :default => :test
44
+
45
+ require 'rake/rdoctask'
46
+ Rake::RDocTask.new do |rdoc|
47
+ if File.exist?('VERSION')
48
+ version = File.read('VERSION')
49
+ else
50
+ version = ""
51
+ end
52
+
53
+ rdoc.rdoc_dir = 'rdoc'
54
+ rdoc.title = "river #{version}"
55
+ rdoc.rdoc_files.include('README*')
56
+ rdoc.rdoc_files.include('lib/**/*.rb')
57
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
@@ -0,0 +1,5 @@
1
+ require 'httparty'
2
+ require 'openssl'
3
+
4
+ require 'river/amazon'
5
+ require 'river/amazon_request'
@@ -0,0 +1,56 @@
1
+ class Amazon
2
+
3
+ # Do we have support for the SHA-256 Secure Hash Algorithm?
4
+ # Note that Module#constants returns Strings in Ruby 1.8 and Symbols in 1.9.
5
+ DIGEST_SUPPORT = OpenSSL::Digest.constants.include?('SHA256') || OpenSSL::Digest.constants.include?(:SHA256)
6
+
7
+ # Requests are authenticated using the SHA-256 Secure Hash Algorithm.
8
+ DIGEST = OpenSSL::Digest::Digest.new('sha256') if DIGEST_SUPPORT
9
+
10
+ AMAZON_SITES = {
11
+ :ca => 'http://ecs.amazonaws.ca/onca/xml',
12
+ :de => 'http://ecs.amazonaws.de/onca/xml',
13
+ :fr => 'http://ecs.amazonaws.fr/onca/xml',
14
+ :jp => 'http://ecs.amazonaws.jp/onca/xml',
15
+ :uk => 'http://ecs.amazonaws.co.uk/onca/xml',
16
+ :us => 'http://ecs.amazonaws.com/onca/xml'
17
+ }
18
+
19
+ AMAZON_XSLT_SITES = {
20
+ :ca => 'http://xml-ca.amznxslt.com/onca/xml',
21
+ :de => 'http://xml-de.amznxslt.com/onca/xml',
22
+ :fr => 'http://xml-fr.amznxslt.com/onca/xml',
23
+ :jp => 'http://xml-jp.amznxslt.com/onca/xml',
24
+ :uk => 'http://xml-uk.amznxslt.com/onca/xml',
25
+ :us => 'http://xml-us.amznxslt.com/onca/xml'
26
+ }
27
+
28
+ class AmazonResultError < StandardError; end
29
+
30
+ # Sign an amazon query
31
+ # Based on ruby-aaws and documentation here
32
+ # http://www.caliban.org/ruby/ruby-aws/
33
+ # http://docs.amazonwebservices.com/AWSECommerceService/latest/DG/index.html?RequestAuthenticationArticle.html
34
+ # Parameters
35
+ # query: The query to be signed
36
+ def self.sign_query(uri, query, amazon_secret_access_key)
37
+ raise 'SHA-256 not available in this version of openssl. Cannot sign Amazon requests.' unless DIGEST_SUPPORT
38
+ query << "&Timestamp=#{Time.now.utc.strftime('%Y-%m-%dT%H:%M:%SZ')}"
39
+ new_query = query.split('&').collect{|param| "#{param.split('=')[0]}=#{url_encode(param.split('=')[1])}"}.sort.join('&')
40
+ to_sign = "GET\n%s\n%s\n%s" % [uri.host, uri.path, new_query]
41
+ hmac = OpenSSL::HMAC.digest(DIGEST, amazon_secret_access_key, to_sign)
42
+ base64_hmac = [hmac].pack('m').chomp
43
+ signature = url_encode(base64_hmac)
44
+ new_query << "&Signature=#{signature}"
45
+ end
46
+
47
+ # Encode a string, such that it is suitable for HTTP transmission.
48
+ def self.url_encode(string)
49
+ return '' if string.nil?
50
+ # Shamelessly plagiarised from Wakou Aoyama's cgi.rb, but then altered slightly to please AWS.
51
+ string.gsub( /([^a-zA-Z0-9_.~-]+)/ ) do
52
+ '%' + $1.unpack( 'H2' * $1.bytesize ).join( '%' ).upcase
53
+ end
54
+ end
55
+
56
+ end
@@ -0,0 +1,108 @@
1
+ class AmazonRequest
2
+ include HTTParty
3
+ format :xml
4
+
5
+ # Initialize Amazon Request. Obtain valid Amazon credentials from your developer account
6
+ # Parameters:
7
+ # amazon_access_key_id: Valid Amazon access key
8
+ # amazon_secret_access_key: Valid Amazon secret access key
9
+ # amazon_associate_tag: Valid Amazon associates tag (optional)
10
+ # ecs_to_rss_wishlist: url for ecs_to_rss_wishlist stylesheet ie http://www.example.com/ecs_to_rss-wishlist.xslt
11
+ # locale: Locale for the specific amazon site to use valid values are :ca, :de, :fr, :jp, :uk, :us (optional, default is us)
12
+ def initialize(amazon_access_key_id, amazon_secret_access_key, ecs_to_rss_wishlist, amazon_associate_tag = nil, locale = :us)
13
+ @amazon_access_key_id = amazon_access_key_id
14
+ @amazon_secret_access_key = amazon_secret_access_key
15
+ @amazon_associate_tag = amazon_associate_tag
16
+ @locale = locale
17
+ @ecs_to_rss_wishlist = ecs_to_rss_wishlist
18
+ end
19
+
20
+ # Generate rss feeds for the give email
21
+ # Parameters:
22
+ # email: email for which to find feeds.
23
+ def get_amazon_feeds(email)
24
+ wishlists = get_customer_wishlists(email)
25
+ if !wishlists.blank?
26
+ wishlists = [wishlists] unless wishlists.is_a?(Array)
27
+ wishlist_ids = wishlists.collect{|list| list['ListId']}
28
+ generate_wishlist_rss(wishlist_ids)
29
+ end
30
+ end
31
+
32
+ # Get matching id for the given email
33
+ # Parameters:
34
+ # email: customer's email.
35
+ def get_customer_id(email)
36
+ query = "Operation=CustomerContentSearch&Email=#{email}"
37
+ result = make_request(query)
38
+ if result['CustomerContentSearchResponse']['Customers']['TotalResults'].to_i > 0
39
+ result['CustomerContentSearchResponse']['Customers']['Customer'][0]
40
+ end
41
+ end
42
+
43
+ # Get information for the given customer id
44
+ def get_customer_information(customer_id)
45
+ query = "Operation=CustomerContentLookup&ResponseGroup=CustomerLists&CustomerId=#{customer_id}"
46
+ make_request(query)
47
+ end
48
+
49
+ # Get customer's wishlist ids
50
+ def get_customer_wishlists(email)
51
+ query = "Operation=ListSearch&ListType=WishList&Email=#{email}"
52
+ result = make_request(query)
53
+ check_errors(result)
54
+ result['ListSearchResponse']['Lists']['List']
55
+ end
56
+
57
+ def generate_wishlist_rss(wishlist_ids)
58
+ feeds = []
59
+ wishlist_ids.each do |wishlist_id|
60
+ query = "Operation=ListLookup&ListType=WishList&ListId=#{wishlist_id}&ResponseGroup=ItemAttributes,ListItems,ListInfo,Offers&Sort=DateAdded&Style=#{@ecs_to_rss_wishlist}"
61
+ feeds << make_xslt_request(query)
62
+ end
63
+ feeds
64
+ end
65
+
66
+ protected
67
+
68
+ def check_errors(result)
69
+ result.each_pair do |key, value|
70
+ if key == 'Errors'
71
+ if value['Error'].is_a?(Array)
72
+ raise Amazon::AmazonResultError, value['Error'].collect{|error| error['Message']}.join(' ')
73
+ else
74
+ raise Amazon::AmazonResultError, value['Error']['Message']
75
+ end
76
+ elsif value.is_a?(Hash)
77
+ check_errors(value)
78
+ elsif value.is_a?(Array)
79
+ value.each do |item|
80
+ if item.is_a?(Hash)
81
+ check_errors(item)
82
+ end
83
+ end
84
+ end
85
+ end
86
+ end
87
+
88
+ def make_request(query)
89
+ add_required_params(query)
90
+ uri = Amazon::AMAZON_SITES[@locale]
91
+ signed_query = Amazon.sign_query(URI.parse(uri), query, @amazon_secret_access_key)
92
+ AmazonRequest.get(uri, :query => signed_query)
93
+ end
94
+
95
+ def make_xslt_request(query)
96
+ add_required_params(query)
97
+ uri = Amazon::AMAZON_XSLT_SITES[@locale]
98
+ signed_query = Amazon.sign_query(URI.parse(uri), query, @amazon_secret_access_key)
99
+ "#{uri}?#{signed_query}"
100
+ end
101
+
102
+ def add_required_params(query)
103
+ query << "&Service=AWSECommerceService"
104
+ query << "&AWSAccessKeyId=#{@amazon_access_key_id}"
105
+ query << "&AssociateTag=#{@amazon_associate_tag}" if @amazon_associate_tag
106
+ query << "&Version=2009-07-01"
107
+ end
108
+ end
@@ -0,0 +1,21 @@
1
+ require 'rake'
2
+ require 'rake/tasklib'
3
+
4
+ module River
5
+ class Tasks < ::Rake::TaskLib
6
+ def initialize
7
+ define
8
+ end
9
+ private
10
+ def define
11
+ namespace :river do
12
+ desc "Sync files from river."
13
+ task :sync do
14
+ path = File.join(File.dirname(__FILE__), *%w[.. ..])
15
+ system "rsync -ruv #{path}/public ."
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
21
+ River::Tasks.new
@@ -0,0 +1,41 @@
1
+ <?xml version="1.0" encoding="UTF-8" ?>
2
+ <xsl:stylesheet version="1.0"
3
+ xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
4
+ xmlns:aws="http://webservices.amazon.com/AWSECommerceService/2009-07-01">
5
+
6
+ <xsl:output method="xml"/>
7
+
8
+ <xsl:template match="/">
9
+ <rss version="2.0">
10
+ <channel>
11
+ <xsl:apply-templates select="aws:ListLookupResponse/aws:Lists/aws:List" />
12
+ </channel>
13
+ </rss>
14
+ </xsl:template>
15
+
16
+ <xsl:template match="aws:List">
17
+ <title>WishList for <xsl:value-of select="aws:CustomerName" /></title>
18
+ <link><xsl:value-of select="aws:ListURL"/></link>
19
+ <xsl:apply-templates select="aws:ListItem"/>
20
+ </xsl:template>
21
+
22
+ <xsl:template match="aws:ListItem">
23
+ <item>
24
+ <title><xsl:value-of select="aws:Item/aws:ItemAttributes/aws:Title" /></title>
25
+ <link><xsl:value-of select="aws:Item/aws:DetailPageURL" /></link>
26
+ <description>
27
+ <p>Author: <xsl:value-of select="aws:Item/aws:ItemAttributes/aws:Author" /></p>
28
+ <p>ISBN: <xsl:value-of select="aws:Item/aws:ItemAttributes/aws:ISBN" /></p>
29
+ <p>List Price: <xsl:value-of select="aws:Item/aws:ItemAttributes/aws:ListPrice/aws:FormattedPrice" /></p>
30
+ <p>Price: <xsl:value-of select="aws:Item/aws:OfferSummary/aws:LowestNewPrice/aws:FormattedPrice"/></p>
31
+ <xsl:apply-templates select="aws:Item/aws:ItemLinks/aws:ItemLink"/>
32
+ </description>
33
+ <guid><xsl:value-of select="aws:Item/aws:DetailPageURL" /></guid>
34
+ </item>
35
+ </xsl:template>
36
+
37
+ <xsl:template match="aws:ItemLink">
38
+ <a href="{aws:URL}"><xsl:value-of select="aws:Description" /></a>
39
+ </xsl:template>
40
+
41
+ </xsl:stylesheet>
@@ -0,0 +1 @@
1
+ require 'river'
@@ -0,0 +1,59 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{river}
8
+ s.version = "0.0.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Justin Ball"]
12
+ s.date = %q{2009-10-29}
13
+ s.description = %q{Code to interact with the AWS API}
14
+ s.email = %q{justinball@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "lib/river.rb",
27
+ "lib/river/amazon.rb",
28
+ "lib/river/amazon_request.rb",
29
+ "lib/river/tasks.rb",
30
+ "public/ecs_to_rss-wishlist.xslt",
31
+ "rails/init.rb",
32
+ "river.gemspec",
33
+ "tasks/river_tasks.rake",
34
+ "test/river_test.rb",
35
+ "test/test_helper.rb"
36
+ ]
37
+ s.homepage = %q{http://github.com/jbasdf/river}
38
+ s.rdoc_options = ["--charset=UTF-8"]
39
+ s.require_paths = ["lib"]
40
+ s.rubygems_version = %q{1.3.5}
41
+ s.summary = %q{Talk to Amazon}
42
+ s.test_files = [
43
+ "test/river_test.rb",
44
+ "test/test_helper.rb"
45
+ ]
46
+
47
+ if s.respond_to? :specification_version then
48
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
49
+ s.specification_version = 3
50
+
51
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
52
+ s.add_development_dependency(%q<thoughtbot-shoulda>, [">= 0"])
53
+ else
54
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
55
+ end
56
+ else
57
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
58
+ end
59
+ end
@@ -0,0 +1 @@
1
+ require File.join(File.dirname(__FILE__), '/../lib/river/tasks')
@@ -0,0 +1,5 @@
1
+ require 'test_helper'
2
+
3
+ class RiverTest < Test::Unit::TestCase
4
+
5
+ end
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ require 'river'
8
+
9
+ class Test::Unit::TestCase
10
+ end
metadata ADDED
@@ -0,0 +1,81 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: river
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Justin Ball
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-10-29 00:00:00 -06:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: thoughtbot-shoulda
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ description: Code to interact with the AWS API
26
+ email: justinball@gmail.com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - LICENSE
33
+ - README.rdoc
34
+ files:
35
+ - .document
36
+ - .gitignore
37
+ - LICENSE
38
+ - README.rdoc
39
+ - Rakefile
40
+ - VERSION
41
+ - lib/river.rb
42
+ - lib/river/amazon.rb
43
+ - lib/river/amazon_request.rb
44
+ - lib/river/tasks.rb
45
+ - public/ecs_to_rss-wishlist.xslt
46
+ - rails/init.rb
47
+ - river.gemspec
48
+ - tasks/river_tasks.rake
49
+ - test/river_test.rb
50
+ - test/test_helper.rb
51
+ has_rdoc: true
52
+ homepage: http://github.com/jbasdf/river
53
+ licenses: []
54
+
55
+ post_install_message:
56
+ rdoc_options:
57
+ - --charset=UTF-8
58
+ require_paths:
59
+ - lib
60
+ required_ruby_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: "0"
65
+ version:
66
+ required_rubygems_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: "0"
71
+ version:
72
+ requirements: []
73
+
74
+ rubyforge_project:
75
+ rubygems_version: 1.3.5
76
+ signing_key:
77
+ specification_version: 3
78
+ summary: Talk to Amazon
79
+ test_files:
80
+ - test/river_test.rb
81
+ - test/test_helper.rb