From 7e2e5b5ed54b096f8f4a3622c0f61b0b51af8eb4 Mon Sep 17 00:00:00 2001 From: David Martinez Date: Wed, 10 Nov 2010 16:08:10 -0800 Subject: [PATCH 1/9] A very green, very preliminary implementation for alexa. It's missing a bunch of stuff but at least it does requests and returns hashes. Good to start with. --- Rakefile | 7 ++ lib/alexa/alexa.rb | 239 ++++++++++++++++++++++++++++++++++++++ lib/aws.rb | 2 +- lib/awsbase/support.rb | 34 ++++++ test/alexa/test_alexa.rb | 53 +++++++++ test/alexa/test_helper.rb | 2 + 6 files changed, 336 insertions(+), 1 deletion(-) create mode 100644 lib/alexa/alexa.rb create mode 100644 test/alexa/test_alexa.rb create mode 100644 test/alexa/test_helper.rb diff --git a/Rakefile b/Rakefile index b4031d7..19c7fd8 100644 --- a/Rakefile +++ b/Rakefile @@ -94,4 +94,11 @@ task :testacf do require 'test/acf/test_acf.rb' end +desc "Test Alexa interface" +task :testalexa do + require 'test/test_credentials' + TestCredentials.get_credentials + require 'test/alexa/test_alexa.rb' +end + # vim: syntax=Ruby diff --git a/lib/alexa/alexa.rb b/lib/alexa/alexa.rb new file mode 100644 index 0000000..11dd1a7 --- /dev/null +++ b/lib/alexa/alexa.rb @@ -0,0 +1,239 @@ +module Aws +class Alexa + include AwsBaseInterface + DEFAULT_HOST = "awis.amazonaws.com" + DEFAULT_PATH = "/" + API_VERSION = "2005-07-11" + DEFAULT_PROTOCOL = 'http' + DEFAULT_PORT = 80 + + VALID_URLINFO_RESPONSE_GROUPS = [ + :related_links, + :categories, + :rank, + :rank_by_country, + :rank_by_city, + :usage_stats, + :contact_info, + :adult_content, + :speed, + :language, + :keywords, + :owned_domains, + :links_in_count, + :site_data + ] + # TODO - the docs mentions :popups on the meta groups, but it's not on the response group. Oversight? Check to see if that kind of info is being passed + + VALID_CATEGORY_BROWSE_RESPONSE_GROUPS = [ :categories, + :related_categories, + :language_categories, + :letter_bars + ] + + META_GROUPS = { + :related=>[:related_links, :categories], + :traffic_data=>[:rank, :usage_stats], + :content_data=>[:site_data, :adult_content, :popups, :speed, :language] + } + @@bench = AwsBenchmarkingBlock.new + def self.bench_xml + @@bench.xml + end + def self.bench_ec2 + @@bench.service + end + + @@api = ENV['ALEXA_API_VERSION'] || API_VERSION + def initialize(aws_access_key_id=nil, aws_secret_access_key=nil, params={}) + init({ :name => 'ALEXA', + :default_host => ENV['ALEXA_URL'] ? URI.parse(ENV['ALEXA_URL']).host : DEFAULT_HOST, + :default_port => ENV['ALEXA_URL'] ? URI.parse(ENV['ALEXA_URL']).port : DEFAULT_PORT, + :default_service => ENV['ALEXA_URL'] ? URI.parse(ENV['ALEXA_URL']).path : DEFAULT_PATH, + :default_protocol => ENV['ALEXA_URL'] ? URI.parse(ENV['ALEXA_URL']).scheme : DEFAULT_PROTOCOL, + :api_version => API_VERSION }, + aws_access_key_id || ENV['AWS_ACCESS_KEY_ID'] , + aws_secret_access_key|| ENV['AWS_SECRET_ACCESS_KEY'], + params) + # EC2 doesn't really define any transient errors to retry, and in fact, + # when they return a 503 it is usually for 'request limit exceeded' which + # we most certainly should not retry. So let's pare down the list of + # retryable errors to InternalError only (see AwsBase for the default + # list) + amazon_problems = ['InternalError'] + end + + def generate_request(action, params={}) #:nodoc: + service_hash = {"Action" => action, + "AWSAccessKeyId" => @aws_access_key_id, + "Version" => @@api } + service_hash.update(params) + service_params = signed_service_params(@aws_secret_access_key, service_hash, :get, @params[:server], @params[:service]) + + # use POST method if the length of the query string is too large + if service_params.size > 2000 + if signature_version == '2' + # resign the request because HTTP verb is included into signature + service_params = signed_service_params(@aws_secret_access_key, service_hash, :post, @params[:server], @params[:service]) + end + request = Net::HTTP::Post.new(@params[:service]) + request.body = service_params + request['Content-Type'] = 'application/x-www-form-urlencoded' + else + request = Net::HTTP::Get.new("#{@params[:service]}?#{service_params}") + end + # prepare output hash + { :request => request, + :server => @params[:server], + :port => @params[:port], + :protocol => @params[:protocol] } + end + + def request_info(request, parser) #:nodoc: + thread = @params[:multi_thread] ? Thread.current : Thread.main + thread[:ec2_connection] ||= Rightscale::HttpConnection.new(:exception => AwsError, :logger => @logger) + request_info_impl(thread[:ec2_connection], @@bench, request, parser) + end + + # Quickly return the alexa URL info, (rank only) + # + # Example: + # alexa_rank("http://www.yahoo.com")[:rank][:text] # 4 + def alexa_rank(url, params={}, cache_for = nil) + result = alexa_url_info(url, {:response_groups=>:rank}) + return result[:url_info_response][:response][:url_info_result][:alexa][:traffic_data] + end + + # Retrieves the Alexa URL info for a URL. + # By default it returns every response group. + # Override response_groups to set your own. + # + # Example: + # alexa_url_info("http://www.google.com")[:url_info_response].. + def alexa_url_info(url, opts={}, cache_for=nil, parser = QAlexaUrlInfoParser) + options = {:response_groups=>VALID_URLINFO_RESPONSE_GROUPS}.merge(opts) + request_hash = {} + request_hash['Url'] = url + request_hash['ResponseGroup'] = response_groups_to_param(options[:response_groups]) + params.each do |key, val| + request_hash.merge! hash_params(key, (val.is_a?(Array) ? val : [val])) + end + link = generate_request('UrlInfo', request_hash) + request_cache_or_info(cache_for, link, parser, @@bench, cache_for) + rescue Exception + on_exception + end + + # Returns a list of subcategories inside a category + # + def alexa_category_browse(path, opts={}, cache_for=nil, parser = QAlexaUrlInfoParser) + options = {:response_groups=>VALID_CATEGORY_BROWSE_RESPONSE_GROUPS}.merge(opts) + request_hash = {} + request_hash['Path'] = path + request_hash['ResponseGroup'] = response_groups_to_param(options[:response_groups]) + params.each do |key, val| + request_hash.merge! hash_params(key, (val.is_a?(Array) ? val : [val])) + end + link = generate_request('CategoryBrowse', request_hash) + request_cache_or_info(cache_for, link, parser, @@bench, cache_for) + rescue Exception + on_exception + end + + # Returns a list of category listings + def alexa_category_listings(path, cache_for=nil, parser = QAlexaUrlInfoParser) + request_hash = {} + request_hash['Path'] = path + request_hash['ResponseGroup'] = "Listings" + params.each do |key, val| + request_hash.merge! hash_params(key, (val.is_a?(Array) ? val : [val])) + end + link = generate_request('CategoryListings', request_hash) + request_cache_or_info(cache_for, link, parser, @@bench, cache_for) + rescue Exception + on_exception + end + + def alexa_sites_linking_in(path, cache_for, parser = QAlexaUrlInfoParser) + request_hash = {} + request_hash['Path'] = path + request_hash['ResponseGroup'] = "SitesLinkingIn" + params.each do |key, val| + request_hash.merge! hash_params(key, (val.is_a?(Array) ? val : [val])) + end + link = generate_request('CategoryListings', request_hash) + request_cache_or_info(cache_for, link, parser, @@bench, cache_for) + rescue Exception + on_exception + end + + def alexa_traffic_history + throw ArgumentError.new("Not Implemented. Sorry!") + end + + private + + def response_groups_to_param(groups) + actual_groups = groups.is_a?(Array) ? groups : [groups] + actual_groups.collect{|g| String.camelize(g.to_s) }.join(",") + end + +end + + class QAlexaElementHash < Hash + attr_accessor :finalized + def last + self + end + def to_s + return has_key?(:text) ? self[:text] : super.to_s + end + end + + class QAlexaUrlInfoParser < AwsParser + + def initialize(args) + super(args) + @result = {} + end + + def tagstart(name, attr) + item = current_item(name) + _attr = attr.dup + _attr.delete("xmlns:aws") + item.merge!(_attr) + end + + def tagend(name) + element = current_item(name) + element[:text] = @text.strip + element.finalized = true + end + + def current_item(name) + outer = @result + inner = nil + path_array = @xmlpath.split('/') + path_array << name + path_array.collect{|s| s.sub('aws:','')}.each{|_xpath_element| + name_sym = symbol_for(_xpath_element) + inner = outer[name_sym] || QAlexaElementHash.new + inner = inner.last # Sometimes we may get an array. + if inner.finalized + old_inner = inner + inner = QAlexaElementHash.new + outer[name_sym] = [old_inner, inner] + end + outer[name_sym] = inner if ! outer.has_key?(name_sym) + outer = inner + } + return inner.nil? ? outer : inner + end + + def symbol_for(name) + sym_name = name.sub('aws:','').underscore.to_sym + end + + end + +end diff --git a/lib/aws.rb b/lib/aws.rb index 1aa798e..888c4b1 100644 --- a/lib/aws.rb +++ b/lib/aws.rb @@ -27,4 +27,4 @@ require 'acf/right_acf_interface' require 'elb/elb_interface' require 'rds/rds' - +require 'alexa/alexa' diff --git a/lib/awsbase/support.rb b/lib/awsbase/support.rb index e0d409e..bfa552f 100644 --- a/lib/awsbase/support.rb +++ b/lib/awsbase/support.rb @@ -51,6 +51,40 @@ def constantize() constant end + # By default, +camelize+ converts strings to UpperCamelCase. If the argument to +camelize+ + # is set to :lower then +camelize+ produces lowerCamelCase. + # + # +camelize+ will also convert '/' to '::' which is useful for converting paths to namespaces. + # + # Examples: + # "active_record".camelize # => "ActiveRecord" + # "active_record".camelize(:lower) # => "activeRecord" + # "active_record/errors".camelize # => "ActiveRecord::Errors" + # "active_record/errors".camelize(:lower) # => "activeRecord::Errors" + def self.camelize(lower_case_and_underscored_word, first_letter_in_uppercase = true) + if first_letter_in_uppercase + lower_case_and_underscored_word.to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase } + else + lower_case_and_underscored_word.first.downcase + camelize(lower_case_and_underscored_word)[1..-1] + end + end + + # The reverse of +camelize+. Makes an underscored, lowercase form from the expression in the string. + # + # Changes '::' to '/' to convert namespaces to paths. + # + # Examples: + # "ActiveRecord".underscore # => "active_record" + # "ActiveRecord::Errors".underscore # => active_record/errors + def underscore + self.to_s.gsub(/::/, '/'). + gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2'). + gsub(/([a-z\d])([A-Z])/,'\1_\2'). + tr("-", "_"). + downcase + end + + end diff --git a/test/alexa/test_alexa.rb b/test/alexa/test_alexa.rb new file mode 100644 index 0000000..8622bac --- /dev/null +++ b/test/alexa/test_alexa.rb @@ -0,0 +1,53 @@ +require File.dirname(__FILE__) + '/test_helper.rb' +require 'pp' +require File.dirname(__FILE__) + '/../test_credentials.rb' + +require 'ruby-debug' +Debugger.start + +# Tests the Alexa AWS implementation +# Note this is very preliminary code. The code just spits back hashes +# and it has no concept of types just yet. The parser can +# be overriden on the calls if you need stronger types and must +# have something right away. +class TestAlexa < Test::Unit::TestCase + + def setup + TestCredentials.get_credentials + @alexa = Aws::Alexa.new(TestCredentials.aws_access_key_id, + TestCredentials.aws_secret_access_key) + end + + # Quick rank request + def test_rank + return + TestCredentials.get_credentials + rank = @alexa.alexa_rank("http://www.youtube.com") + assert ! rank.empty? + assert ! rank[:rank][:text].blank? + assert rank[:rank][:text].to_i > 0 + assert ! rank[:data_url][:text].blank? + end + + def test_alexa_urlinfo + TestCredentials.get_credentials + result = @alexa.alexa_url_info("http://www.yahoo.com") + assert result[:url_info_response][:response][:url_info_result][:alexa][:contact_info][:company_stock_ticker][:text] == "YHOO" + end + + def test_alexa_category_browse + TestCredentials.get_credentials + category_browse = @alexa.alexa_category_browse("Top/Computers/Software/Operating_Systems") + assert ! category_browse.empty? + assert ! category_browse[:category_browse_response][:response][:category_browse_result][:alexa][:category_browse][:categories][:category].first[:path][:text].blank? + + end + + def test_alexa_category_listings + TestCredentials.get_credentials + category_browse = @alexa.alexa_category_listings("Top/Computers/Software/Operating_Systems") + assert ! category_browse.empty? + assert category_browse[:category_listings_response][:response][:category_listings_result][:alexa][:category_listings][:listings][:listing].length > 0 + end + +end diff --git a/test/alexa/test_helper.rb b/test/alexa/test_helper.rb new file mode 100644 index 0000000..afb6844 --- /dev/null +++ b/test/alexa/test_helper.rb @@ -0,0 +1,2 @@ +require 'test/unit' +require File.dirname(__FILE__) + '/../../lib/aws' From d0b546c8117f5e59a0a67d7741aed404f248ceb2 Mon Sep 17 00:00:00 2001 From: David Martinez Date: Mon, 15 Nov 2010 10:34:12 -0800 Subject: [PATCH 2/9] Camelize mini-refactor to keep it compatible with rails. --- aws.gemspec | 10 ++++++---- lib/alexa/alexa.rb | 2 +- lib/awsbase/support.rb | 2 +- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/aws.gemspec b/aws.gemspec index 45124a6..67eaf6c 100644 --- a/aws.gemspec +++ b/aws.gemspec @@ -1,6 +1,6 @@ # Generated by jeweler -# DO NOT EDIT THIS FILE DIRECTLY -# Instead, edit Jeweler::Tasks in rakefile, and run the gemspec command +# DO NOT EDIT THIS FILE +# Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec` # -*- encoding: utf-8 -*- Gem::Specification.new do |s| @@ -9,7 +9,7 @@ Gem::Specification.new do |s| s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version= s.authors = ["Travis Reeder", "Chad Arimura", "RightScale"] - s.date = %q{2010-11-04} + s.date = %q{2010-11-11} s.description = %q{AWS Ruby Library for interfacing with Amazon Web Services.} s.email = %q{travis@appoxy.com} s.extra_rdoc_files = [ @@ -17,6 +17,7 @@ Gem::Specification.new do |s| ] s.files = [ "lib/acf/right_acf_interface.rb", + "lib/alexa/alexa.rb", "lib/aws.rb", "lib/awsbase/aws_response_array.rb", "lib/awsbase/benchmark_fix.rb", @@ -42,6 +43,8 @@ Gem::Specification.new do |s| s.test_files = [ "test/acf/test_acf.rb", "test/acf/test_helper.rb", + "test/alexa/test_alexa.rb", + "test/alexa/test_helper.rb", "test/ec2/test_ec2.rb", "test/ec2/test_helper.rb", "test/ec2/test_mon.rb", @@ -82,4 +85,3 @@ Gem::Specification.new do |s| s.add_dependency(%q, [">= 0"]) end end - diff --git a/lib/alexa/alexa.rb b/lib/alexa/alexa.rb index 11dd1a7..bf3215a 100644 --- a/lib/alexa/alexa.rb +++ b/lib/alexa/alexa.rb @@ -175,7 +175,7 @@ def alexa_traffic_history def response_groups_to_param(groups) actual_groups = groups.is_a?(Array) ? groups : [groups] - actual_groups.collect{|g| String.camelize(g.to_s) }.join(",") + actual_groups.collect{|g| g.to_s.camelize }.join(",") end end diff --git a/lib/awsbase/support.rb b/lib/awsbase/support.rb index bfa552f..5380ca6 100644 --- a/lib/awsbase/support.rb +++ b/lib/awsbase/support.rb @@ -61,7 +61,7 @@ def constantize() # "active_record".camelize(:lower) # => "activeRecord" # "active_record/errors".camelize # => "ActiveRecord::Errors" # "active_record/errors".camelize(:lower) # => "activeRecord::Errors" - def self.camelize(lower_case_and_underscored_word, first_letter_in_uppercase = true) + def camelize(lower_case_and_underscored_word=self, first_letter_in_uppercase = true) if first_letter_in_uppercase lower_case_and_underscored_word.to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase } else From c4da2678f995b5253118f3a0acbf30d93892f082 Mon Sep 17 00:00:00 2001 From: David Martinez Date: Wed, 17 Nov 2010 09:16:50 -0800 Subject: [PATCH 3/9] Commiting the rakefile because jeweler insists on it apparently --- Rakefile | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Rakefile b/Rakefile index 19c7fd8..a35362b 100644 --- a/Rakefile +++ b/Rakefile @@ -11,12 +11,12 @@ testglobs = ["test/ts_right_aws.rb"] begin require 'jeweler' Jeweler::Tasks.new do |gem| - gem.name = "aws" - gem.summary = "AWS Ruby Library for interfacing with Amazon Web Services." + gem.name = "hackerdude-aws" + gem.summary = "AWS Ruby Library for interfacing with Amazon Web Services. Hackerdude branch including Alexa (Experimental)." gem.email = "travis@appoxy.com" - gem.homepage = "http://github.com/appoxy/aws/" - gem.description = "AWS Ruby Library for interfacing with Amazon Web Services." - gem.authors = ["Travis Reeder", "Chad Arimura", "RightScale"] + gem.homepage = "http://github.com/hackerdude/aws/" + gem.description = "AWS Ruby Library for interfacing with Amazon Web Services. Alexa-experimental branch." + gem.authors = ["Travis Reeder", "Chad Arimura", "RightScale", "David Martinez"] gem.files = FileList['lib/**/*.rb'] gem.add_dependency 'uuidtools' gem.add_dependency 'http_connection' From 634290b6f9231eb4fed39b7a57ada5099f76b679 Mon Sep 17 00:00:00 2001 From: Alex Soto Date: Mon, 13 Dec 2010 14:55:05 -0800 Subject: [PATCH 4/9] added more parsed fields for describe_snapshots --- lib/ec2/right_ec2.rb | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/lib/ec2/right_ec2.rb b/lib/ec2/right_ec2.rb index aa54623..4814663 100644 --- a/lib/ec2/right_ec2.rb +++ b/lib/ec2/right_ec2.rb @@ -1817,12 +1817,15 @@ def tagstart(name, attributes) end def tagend(name) case name - when 'volumeId' then @snapshot[:aws_volume_id] = @text - when 'snapshotId' then @snapshot[:aws_id] = @text - when 'status' then @snapshot[:aws_status] = @text - when 'startTime' then @snapshot[:aws_started_at] = Time.parse(@text) - when 'progress' then @snapshot[:aws_progress] = @text - when 'item' then @result << @snapshot + when 'volumeId' then @snapshot[:aws_volume_id] = @text + when 'snapshotId' then @snapshot[:aws_id] = @text + when 'status' then @snapshot[:aws_status] = @text + when 'startTime' then @snapshot[:aws_started_at] = Time.parse(@text) + when 'progress' then @snapshot[:aws_progress] = @text + when 'description' then @snapshot[:aws_description] = @text + when 'ownerId' then @snapshot[:aws_owner] = @text + when 'volumeSize' then @snapshot[:aws_volume_size] = @text.to_i + when 'item' then @result << @snapshot end end def reset From 0bf76c785c978fa59716795c84cb44477c1c12a0 Mon Sep 17 00:00:00 2001 From: David Martinez Date: Mon, 13 Dec 2010 15:39:58 -0800 Subject: [PATCH 5/9] Regenerate gemspec for version 2.3.26 --- hackerdude-aws.gemspec | 84 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 hackerdude-aws.gemspec diff --git a/hackerdude-aws.gemspec b/hackerdude-aws.gemspec new file mode 100644 index 0000000..19e9c99 --- /dev/null +++ b/hackerdude-aws.gemspec @@ -0,0 +1,84 @@ +# Generated by jeweler +# DO NOT EDIT THIS FILE DIRECTLY +# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec' +# -*- encoding: utf-8 -*- + +Gem::Specification.new do |s| + s.name = %q{hackerdude-aws} + s.version = "2.3.26" + + s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version= + s.authors = ["Travis Reeder", "Chad Arimura", "RightScale", "David Martinez"] + s.date = %q{2010-12-13} + s.description = %q{AWS Ruby Library for interfacing with Amazon Web Services. Alexa-experimental branch.} + s.email = %q{travis@appoxy.com} + s.extra_rdoc_files = [ + "README.markdown" + ] + s.files = [ + "lib/acf/right_acf_interface.rb", + "lib/aws.rb", + "lib/awsbase/aws_response_array.rb", + "lib/awsbase/benchmark_fix.rb", + "lib/awsbase/right_awsbase.rb", + "lib/awsbase/support.rb", + "lib/ec2/right_ec2.rb", + "lib/ec2/right_mon_interface.rb", + "lib/elb/elb_interface.rb", + "lib/rds/rds.rb", + "lib/right_aws.rb", + "lib/s3/right_s3.rb", + "lib/s3/right_s3_interface.rb", + "lib/sdb/active_sdb.rb", + "lib/sdb/right_sdb_interface.rb", + "lib/sqs/right_sqs.rb", + "lib/sqs/right_sqs_interface.rb" + ] + s.homepage = %q{http://github.com/hackerdude/aws/} + s.require_paths = ["lib"] + s.rubygems_version = %q{1.3.7} + s.summary = %q{AWS Ruby Library for interfacing with Amazon Web Services. Hackerdude branch including Alexa (Experimental).} + s.test_files = [ + "test/acf/test_acf.rb", + "test/acf/test_helper.rb", + "test/ec2/test_ec2.rb", + "test/ec2/test_helper.rb", + "test/ec2/test_mon.rb", + "test/elb/test_elb.rb", + "test/http_connection.rb", + "test/rds/test_rds.rb", + "test/s3/test_helper.rb", + "test/s3/test_s3.rb", + "test/s3/test_s3_stubbed.rb", + "test/sdb/test_active_sdb.rb", + "test/sdb/test_helper.rb", + "test/sdb/test_sdb.rb", + "test/sqs/test_helper.rb", + "test/sqs/test_sqs.rb", + "test/test_credentials.rb", + "test/ts_right_aws.rb" + ] + + if s.respond_to? :specification_version then + current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION + s.specification_version = 3 + + if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then + s.add_runtime_dependency(%q, [">= 0"]) + s.add_runtime_dependency(%q, [">= 0"]) + s.add_runtime_dependency(%q, [">= 0"]) + s.add_runtime_dependency(%q, [">= 0"]) + else + s.add_dependency(%q, [">= 0"]) + s.add_dependency(%q, [">= 0"]) + s.add_dependency(%q, [">= 0"]) + s.add_dependency(%q, [">= 0"]) + end + else + s.add_dependency(%q, [">= 0"]) + s.add_dependency(%q, [">= 0"]) + s.add_dependency(%q, [">= 0"]) + s.add_dependency(%q, [">= 0"]) + end +end + From 95f8c076f0949e4f315869ff6d258c43d144520f Mon Sep 17 00:00:00 2001 From: David Martinez Date: Mon, 9 May 2011 08:21:21 -0700 Subject: [PATCH 6/9] 1.8.7 needs the require-relative if we're going to be using rake with it. --- Rakefile | 1 + 1 file changed, 1 insertion(+) diff --git a/Rakefile b/Rakefile index c67dc82..260005b 100644 --- a/Rakefile +++ b/Rakefile @@ -5,6 +5,7 @@ require 'rubygems' #require "rake/testtask" #require 'rcov/rcovtask' $: << File.dirname(__FILE__) +require 'lib/awsbase/require_relative.rb' require 'lib/right_aws.rb' testglobs = ["test/ts_right_aws.rb"] From 9f448cddd46adf225e3a0b549be9973f59d50208 Mon Sep 17 00:00:00 2001 From: David Martinez Date: Mon, 9 May 2011 08:45:58 -0700 Subject: [PATCH 7/9] Fix camelcase for our purposes. We don't need all that stuff. --- lib/alexa/alexa.rb | 10 +++------- test/alexa/test_alexa.rb | 6 +++--- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/lib/alexa/alexa.rb b/lib/alexa/alexa.rb index 5d42d7a..028a72c 100644 --- a/lib/alexa/alexa.rb +++ b/lib/alexa/alexa.rb @@ -179,12 +179,8 @@ def response_groups_to_param(groups) end # This is ye olde ActiveSupport camelize, just without the dependency - def camelize(strign, lower_case_and_underscored_word=self, first_letter_in_uppercase = true) - if first_letter_in_uppercase - lower_case_and_underscored_word.to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase } - else - lower_case_and_underscored_word.first.downcase + camelize(lower_case_and_underscored_word)[1..-1] - end + def camelize(string) + string.split('_').map {|w| w.capitalize}.join end end @@ -240,7 +236,7 @@ def current_item(name) end def symbol_for(name) - sym_name = name.sub('aws:','').underscore.to_sym + sym_name = Aws::Utils.underscore(name.sub('aws:','')).to_sym end end diff --git a/test/alexa/test_alexa.rb b/test/alexa/test_alexa.rb index 8622bac..f7399fa 100644 --- a/test/alexa/test_alexa.rb +++ b/test/alexa/test_alexa.rb @@ -24,9 +24,9 @@ def test_rank TestCredentials.get_credentials rank = @alexa.alexa_rank("http://www.youtube.com") assert ! rank.empty? - assert ! rank[:rank][:text].blank? + assert ! rank[:rank][:text].empty? assert rank[:rank][:text].to_i > 0 - assert ! rank[:data_url][:text].blank? + assert ! rank[:data_url][:text].empty? end def test_alexa_urlinfo @@ -39,7 +39,7 @@ def test_alexa_category_browse TestCredentials.get_credentials category_browse = @alexa.alexa_category_browse("Top/Computers/Software/Operating_Systems") assert ! category_browse.empty? - assert ! category_browse[:category_browse_response][:response][:category_browse_result][:alexa][:category_browse][:categories][:category].first[:path][:text].blank? + assert ! category_browse[:category_browse_response][:response][:category_browse_result][:alexa][:category_browse][:categories][:category].first[:path][:text].empty? end From d372432c0cde0c613706a52903a3c47c04d6414c Mon Sep 17 00:00:00 2001 From: David Martinez Date: Mon, 9 May 2011 08:47:40 -0700 Subject: [PATCH 8/9] Tie in the alexa implementation on right_aws --- lib/right_aws.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/right_aws.rb b/lib/right_aws.rb index 75867d7..4c6913a 100644 --- a/lib/right_aws.rb +++ b/lib/right_aws.rb @@ -47,6 +47,7 @@ require 'sdb/sdb_interface' require 'acf/acf_interface' require 'elb/elb_interface' +require 'alexa/alexa' # backwards compatible. From fe61debaa40de345fd5fa06af5c939c0db496742 Mon Sep 17 00:00:00 2001 From: David Martinez Date: Mon, 9 May 2011 08:53:10 -0700 Subject: [PATCH 9/9] Removing alexa_, so it doesn't read so silly. --- lib/alexa/alexa.rb | 18 +++++++++--------- test/alexa/test_alexa.rb | 14 +++++++------- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/lib/alexa/alexa.rb b/lib/alexa/alexa.rb index 028a72c..a0eaaa8 100644 --- a/lib/alexa/alexa.rb +++ b/lib/alexa/alexa.rb @@ -98,9 +98,9 @@ def request_info(request, parser) #:nodoc: # Quickly return the alexa URL info, (rank only) # # Example: - # alexa_rank("http://www.yahoo.com")[:rank][:text] # 4 - def alexa_rank(url, params={}, cache_for = nil) - result = alexa_url_info(url, {:response_groups=>:rank}) + # rank("http://www.yahoo.com")[:rank][:text] # 4 + def rank(url, params={}, cache_for = nil) + result = url_info(url, {:response_groups=>:rank}) return result[:url_info_response][:response][:url_info_result][:alexa][:traffic_data] end @@ -109,8 +109,8 @@ def alexa_rank(url, params={}, cache_for = nil) # Override response_groups to set your own. # # Example: - # alexa_url_info("http://www.google.com")[:url_info_response].. - def alexa_url_info(url, opts={}, cache_for=nil, parser = QAlexaUrlInfoParser) + # url_info("http://www.google.com")[:url_info_response].. + def url_info(url, opts={}, cache_for=nil, parser = QAlexaUrlInfoParser) options = {:response_groups=>VALID_URLINFO_RESPONSE_GROUPS}.merge(opts) request_hash = {} request_hash['Url'] = url @@ -126,7 +126,7 @@ def alexa_url_info(url, opts={}, cache_for=nil, parser = QAlexaUrlInfoParser) # Returns a list of subcategories inside a category # - def alexa_category_browse(path, opts={}, cache_for=nil, parser = QAlexaUrlInfoParser) + def category_browse(path, opts={}, cache_for=nil, parser = QAlexaUrlInfoParser) options = {:response_groups=>VALID_CATEGORY_BROWSE_RESPONSE_GROUPS}.merge(opts) request_hash = {} request_hash['Path'] = path @@ -141,7 +141,7 @@ def alexa_category_browse(path, opts={}, cache_for=nil, parser = QAlexaUrlInfoPa end # Returns a list of category listings - def alexa_category_listings(path, cache_for=nil, parser = QAlexaUrlInfoParser) + def category_listings(path, cache_for=nil, parser = QAlexaUrlInfoParser) request_hash = {} request_hash['Path'] = path request_hash['ResponseGroup'] = "Listings" @@ -154,7 +154,7 @@ def alexa_category_listings(path, cache_for=nil, parser = QAlexaUrlInfoParser) on_exception end - def alexa_sites_linking_in(path, cache_for, parser = QAlexaUrlInfoParser) + def sites_linking_in(path, cache_for, parser = QAlexaUrlInfoParser) request_hash = {} request_hash['Path'] = path request_hash['ResponseGroup'] = "SitesLinkingIn" @@ -167,7 +167,7 @@ def alexa_sites_linking_in(path, cache_for, parser = QAlexaUrlInfoParser) on_exception end - def alexa_traffic_history + def traffic_history throw ArgumentError.new("Not Implemented. Sorry!") end diff --git a/test/alexa/test_alexa.rb b/test/alexa/test_alexa.rb index f7399fa..17892fd 100644 --- a/test/alexa/test_alexa.rb +++ b/test/alexa/test_alexa.rb @@ -22,30 +22,30 @@ def setup def test_rank return TestCredentials.get_credentials - rank = @alexa.alexa_rank("http://www.youtube.com") + rank = @alexa.rank("http://www.youtube.com") assert ! rank.empty? assert ! rank[:rank][:text].empty? assert rank[:rank][:text].to_i > 0 assert ! rank[:data_url][:text].empty? end - def test_alexa_urlinfo + def test_urlinfo TestCredentials.get_credentials - result = @alexa.alexa_url_info("http://www.yahoo.com") + result = @alexa.url_info("http://www.yahoo.com") assert result[:url_info_response][:response][:url_info_result][:alexa][:contact_info][:company_stock_ticker][:text] == "YHOO" end - def test_alexa_category_browse + def test_category_browse TestCredentials.get_credentials - category_browse = @alexa.alexa_category_browse("Top/Computers/Software/Operating_Systems") + category_browse = @alexa.category_browse("Top/Computers/Software/Operating_Systems") assert ! category_browse.empty? assert ! category_browse[:category_browse_response][:response][:category_browse_result][:alexa][:category_browse][:categories][:category].first[:path][:text].empty? end - def test_alexa_category_listings + def test_category_listings TestCredentials.get_credentials - category_browse = @alexa.alexa_category_listings("Top/Computers/Software/Operating_Systems") + category_browse = @alexa.category_listings("Top/Computers/Software/Operating_Systems") assert ! category_browse.empty? assert category_browse[:category_listings_response][:response][:category_listings_result][:alexa][:category_listings][:listings][:listing].length > 0 end