Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

First implementation for the localized store pricing api

  • Loading branch information...
commit 1d2ff0cbf1763dde87d04855344ff1a4763e66ca 1 parent feec1fa
@plindelauf plindelauf authored
View
2  Gemfile
@@ -6,9 +6,11 @@ gem 'rake'
group :development do
gem 'bundler'
gem 'jeweler'
+ gem 'ruby-debug19', :require => 'ruby-debug'
end
group :test do
gem 'rspec'
gem 'webmock'
+ gem 'ruby-debug19', :require => 'ruby-debug'
end
View
23 Gemfile.lock
@@ -2,6 +2,8 @@ GEM
remote: http://rubygems.org/
specs:
addressable (2.2.6)
+ archive-tar-minitar (0.5.2)
+ columnize (0.3.5)
crack (0.3.1)
diff-lcs (1.1.3)
git (1.2.5)
@@ -12,18 +14,30 @@ GEM
bundler (~> 1.0)
git (>= 1.2.5)
rake
- multi_json (1.0.3)
+ linecache19 (0.5.12)
+ ruby_core_source (>= 0.1.4)
+ multi_json (1.0.4)
multi_xml (0.4.1)
- rake (0.9.2)
+ rake (0.9.2.2)
rspec (2.7.0)
rspec-core (~> 2.7.0)
rspec-expectations (~> 2.7.0)
rspec-mocks (~> 2.7.0)
- rspec-core (2.7.0)
+ rspec-core (2.7.1)
rspec-expectations (2.7.0)
diff-lcs (~> 1.1.2)
rspec-mocks (2.7.0)
- webmock (1.7.7)
+ ruby-debug-base19 (0.11.25)
+ columnize (>= 0.3.1)
+ linecache19 (>= 0.5.11)
+ ruby_core_source (>= 0.1.4)
+ ruby-debug19 (0.11.6)
+ columnize (>= 0.3.1)
+ linecache19 (>= 0.5.11)
+ ruby-debug-base19 (>= 0.11.19)
+ ruby_core_source (0.1.5)
+ archive-tar-minitar (>= 0.5.2)
+ webmock (1.7.8)
addressable (~> 2.2, > 2.2.5)
crack (>= 0.1.7)
@@ -36,4 +50,5 @@ DEPENDENCIES
jeweler
rake
rspec
+ ruby-debug19
webmock
View
3  lib/fastspring-saasy.rb
@@ -1,5 +1,7 @@
require 'httparty'
+require_relative 'httparty/txt_parser'
+
require_relative 'fastspring-saasy/base'
require_relative 'fastspring-saasy/account'
require_relative 'fastspring-saasy/subscription'
@@ -8,4 +10,5 @@
require_relative 'fastspring-saasy/item'
require_relative 'fastspring-saasy/payment'
require_relative 'fastspring-saasy/address'
+require_relative 'fastspring-saasy/localized_store_pricing'
require_relative 'fastspring-saasy/error'
View
97 lib/fastspring-saasy/localized_store_pricing.rb
@@ -0,0 +1,97 @@
+module FastSpring
+ class LocalizedStorePricing
+ include HTTParty
+ base_uri 'http://api.fastspring.com'
+
+ def initialize(product_path, remote_ip="", http_accept_language="en", http_x_forwarded_for="")
+ @company = FastSpring::Account.fetch(:company)
+ @product_path = product_path
+ @remote_ip = remote_ip
+ @http_accept_language = http_accept_language
+ @http_x_forwarded_for = http_x_forwarded_for
+ end
+
+ def self.find(product_path, remote_ip="", http_accept_language="en", http_x_forwarded_for="")
+ self.new(product_path, remote_ip, http_accept_language, http_x_forwarded_for).find
+ end
+
+ # Get the localized store pricing from Saasy
+ def find
+ @response = self.class.get(base_localized_store_pricing_path, :query => query)
+ self
+ end
+
+ def base_localized_store_pricing_path
+ "/#{@company}/api/price"
+ end
+
+ def query
+ {
+ :product_1_path => @product_path,
+ :user_remote_addr => @remote_ip,
+ :user_x_forwarded_for => @http_accept_language,
+ :user_accept_language => @http_x_forwarded_for
+ }
+ end
+
+ def user_country
+ parsed_response['user_country']
+ end
+
+ def user_language
+ parsed_response['user_language']
+ end
+
+ def user_currency
+ parsed_response['user_currency']
+ end
+
+ def product_path(index)
+ parsed_response["product_#{index}_path"]
+ end
+
+ def product_quantity(index)
+ parsed_response["product_#{index}_quantity"]
+ end
+
+ def product_unit_value(index)
+ parsed_response["product_#{index}_unit_value"]
+ end
+
+ def product_unit_currency(index)
+ parsed_response["product_#{index}_unit_currency"]
+ end
+
+ def product_unit_display(index)
+ parsed_response["product_#{index}_unit_display"]
+ end
+
+ def product_unit_html(index)
+ parsed_response["product_#{index}_unit_html"]
+ end
+
+ private
+
+ def parsed_response
+ # @formatted_response ||= formatted_response
+ @response.parsed_response
+ end
+
+ def formatted_response
+ formatted_response = Hash.new
+ formatted_response["products"] = Array.new
+ @response.parsed_response.each do |key, value|
+ if key.start_with?("product")
+ index = key.match(/(\d*)/)[0].to_i
+ product_key = key.match(/_(\D*)$/)[0]
+
+ formatted_response["products"][index] ||= Hash.new
+ formatted_response["products"][index][product_key] = value
+ else
+ formatted_response[key] = value
+ end
+ end
+ formatted_response
+ end
+ end
+end
View
20 lib/httparty/txt_parser.rb
@@ -0,0 +1,20 @@
+class ParseTxt
+ include HTTParty
+
+ # Replace the :plain parser with this text parser
+ class Parser::Txt < HTTParty::Parser
+ SupportedFormats["text/plain"] = :txt
+
+ # perform text parsing on body
+ def txt
+ parsed_response = Hash.new
+ body.each_line do |line|
+ name_value = line.split("=")
+ parsed_response[name_value[0].strip] = name_value[1].strip if name_value.count == 2
+ end
+ parsed_response
+ end
+ end
+
+ parser Parser::Txt
+end
View
9 spec/fixtures/basic_localized_store_pricing.txt
@@ -0,0 +1,9 @@
+user_country=US
+user_language=en
+user_currency=USD
+product_1_path=/standard
+product_1_quantity=1
+product_1_unit_value=35.00
+product_1_unit_currency=USD
+product_1_unit_display=$35.00
+product_1_unit_html=$35.00
View
69 spec/localized_store_pricing_spec.rb
@@ -0,0 +1,69 @@
+require File.expand_path(File.join(File.dirname(__FILE__), '../lib/fastspring-saasy.rb'))
+require File.expand_path(File.join(File.dirname(__FILE__), 'spec_helper.rb'))
+
+describe FastSpring::LocalizedStorePricing do
+
+ before do
+ FastSpring::Account.setup do |config|
+ config[:username] = 'admin'
+ config[:password] = 'test'
+ config[:company] = 'acme'
+ end
+ end
+
+ context 'url for localized store pricing' do
+ subject { FastSpring::LocalizedStorePricing.find('/standard', "1.2.3.4") }
+ before do
+ stub_request(:get, "http://api.fastspring.com/bizplay/api/price?product_1_path=/standard&user_accept_language=&user_remote_addr=1.2.3.4&http_accept_language=en").
+ to_return(:status => 200, :body => "", :headers => {})
+ end
+
+ it 'returns the path for the company' do
+ subject.base_localized_store_pricing_path.should == "/acme/api/price"
+ end
+ end
+
+ context "parsed response" do
+ subject { FastSpring::LocalizedStorePricing.find('/standard', "1.2.3.4") }
+ before do
+ stub_request(:get, "http://api.fastspring.com/bizplay/api/price?product_1_path=/standard&user_accept_language=&user_remote_addr=1.2.3.4&http_accept_language=en").
+ to_return(stub_http_response_with('basic_localized_store_pricing.txt'))
+ end
+
+ it 'returns "US" as the user country' do
+ subject.user_country.should == "US"
+ end
+
+ it 'returns "en" as the user language' do
+ subject.user_language.should == "en"
+ end
+
+ it 'returns "USD" as the user currency' do
+ subject.user_currency.should == "USD"
+ end
+
+ it 'returns "/standard" as the path of product 1' do
+ subject.product_path(1).should == "/standard"
+ end
+
+ it 'returns "1" as the quantity of product 1' do
+ subject.product_quantity(1).should == "1"
+ end
+
+ it 'returns "35.00" as unit value of product 1' do
+ subject.product_unit_value(1).should == "35.00"
+ end
+
+ it 'returns "USD" as the unit currency of product 1' do
+ subject.product_unit_currency(1).should == "USD"
+ end
+
+ it 'returns "$35.00" as the unit display of product 1' do
+ subject.product_unit_display(1).should == "$35.00"
+ end
+
+ it 'returns "$35.00" as the unit html of product 1' do
+ subject.product_unit_html(1).should == "$35.00"
+ end
+ end
+end
View
6 spec/spec_helper.rb
@@ -14,7 +14,11 @@ def stub_http_response_with(filename)
response = Net::HTTPOK.new("1.1", 200, "Content for you")
response.stub!(:body).and_return(data)
- http_request = HTTParty::Request.new(Net::HTTP::Get, 'http://localhost', :format => format)
+ if format == :txt
+ http_request = HTTParty::Request.new(Net::HTTP::Get, 'http://localhost', :format => format, :parser => HTTParty::Parser::Txt)
+ else
+ http_request = HTTParty::Request.new(Net::HTTP::Get, 'http://localhost', :format => format)
+ end
http_request.stub_chain(:http, :request).and_return(response)
HTTParty::Request.should_receive(:new).and_return(http_request)
Please sign in to comment.
Something went wrong with that request. Please try again.