diff --git a/Gemfile.lock b/Gemfile.lock index 9888bda..f6b3c9e 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - appfigures (0.0.1) + appfigures (0.0.2) faraday faraday_middleware hashie @@ -11,13 +11,13 @@ GEM remote: https://rubygems.org/ specs: diff-lcs (1.1.3) - faraday (0.8.1) - multipart-post (~> 1.1) - faraday_middleware (0.8.8) - faraday (>= 0.7.4, < 0.9) - hashie (1.2.0) - multi_json (1.3.6) - multipart-post (1.1.5) + faraday (0.9.2) + multipart-post (>= 1.2, < 3) + faraday_middleware (0.9.1) + faraday (>= 0.7.4, < 0.10) + hashie (3.3.2) + multi_json (1.10.1) + multipart-post (2.0.0) rake (0.9.2.2) rspec (2.11.0) rspec-core (~> 2.11.0) @@ -35,3 +35,6 @@ DEPENDENCIES appfigures! rake rspec (>= 2.11) + +BUNDLED WITH + 1.16.1 diff --git a/lib/appfigures.rb b/lib/appfigures.rb index 3a69b5b..81f6be0 100644 --- a/lib/appfigures.rb +++ b/lib/appfigures.rb @@ -1,55 +1,176 @@ require 'appfigures/version' require 'appfigures/connection' +require 'utils/hash_extensions' require 'date' +require 'json' class Appfigures attr_reader :connection def initialize(options = {}) - @connection = Appfigures::Connection.new options[:username], options[:password] + @connection = Appfigures::Connection.new options[:username], options[:password], options[:client_key] end + #https://api.appfigures.com/v2/reports/sales/?group_by=product&client_key=c0725e4c875b412fbca6b12b5db44a4e def product_sales - self.connection.get('sales/products').body.map do |id, hash| - Hashie::Mash.new({ - 'product_id' => hash['product']['id'], - 'store_id' => hash['product']['store_id'], - 'store_name' => hash['product']['store_name'], - 'name' => hash['product']['name'], - 'sku' => hash['product']['sku'], - 'downloads' => hash['downloads'].to_i, - 'returns' => hash['returns'].to_i, - 'updates' => hash['updates'].to_i, - 'net_downloads' => hash['net_downloads'].to_i, - 'promos' => hash['promos'].to_i, - 'gift_redemptions'=> hash['gift_redemptions'].to_i, - 'revenue' => hash['revenue'].to_f - }) + url = 'reports/sales' + options = {group_by: 'product'} + + response = self.connection.get(url, options) + response.body.map do |id, hash| + if response.status == 200 + Hashie::Mash.new({ + 'product_id' => hash['product']['id'], + 'store_id' => hash['product']['store_id'], + 'store_name' => hash['product']['store'], + 'name' => hash['product']['name'], + 'sku' => hash['product']['sku'], + 'ref_no' => hash['product']['ref_no'], + 'added_timestamp' => Date.parse(hash['product']['source']['added_timestamp']), + 'icon' => hash['product']['icon'], + 'downloads' => hash['downloads'].to_i, + 'returns' => hash['returns'].to_i, + 'updates' => hash['updates'].to_i, + 'net_downloads' => hash['net_downloads'].to_i, + 'promos' => hash['promos'].to_i, + 'gift_redemptions'=> hash['gift_redemptions'].to_i, + 'revenue' => hash['revenue'].to_f, + 'active' => hash['product']['source']['active'] + }) + else + puts 'Appfigures service error:' + puts hash.to_json + Hashie::Mash.new + end end end - def date_sales(start_date, end_date) - url = "sales/dates+products/#{start_date.strftime('%Y-%m-%d')}/#{end_date.strftime('%Y-%m-%d')}" - self.connection.get(url).body.map do |date, product| - product.map do |product_id, hash| + + # GET /reports/sales/dates+products?start=2017-03-01&end=2017-03-31&products=6403600 + # See http://docs.appfigures.com/api/reference/v2/sales + def date_sales(start_date, end_date, options = {}) + url = "reports/sales/dates+products" + + options = {start: start_date.strftime('%Y-%m-%d'), + end: end_date.strftime('%Y-%m-%d')}.merge(options) + + response = self.connection.get(url, options) + if response.status == 200 + response.body.map do |date, product| + product.map do |product_id, hash| + Hashie::Mash.new({ + 'date' => Date.parse(date), + 'product_id' => hash['product_id'].to_i, + 'downloads' => hash['downloads'].to_i, + 'returns' => hash['returns'].to_i, + 'updates' => hash['updates'].to_i, + 'net_downloads' => hash['net_downloads'].to_i, + 'promos' => hash['promos'].to_i, + 'gift_redemptions'=> hash['gift_redemptions'].to_i, + 'revenue' => hash['revenue'].to_f + }) + end.first + end + else + response.body.map do |id, hash| + puts 'Appfigures service error:' + puts hash.to_json + Hashie::Mash.new + end + end + end + + # GET /reports/sales?group_by=country&start=start_date&end=end_date + # See http://docs.appfigures.com/api/reference/v2/sales + def country_sales(start_date, end_date, options = {}) + url = "reports/sales" + options = {group_by: 'country', + start: start_date.strftime('%Y-%m-%d'), + end: end_date.strftime('%Y-%m-%d')}.merge(options) + #"/?group_by=country&start=#{start_date.strftime('%Y-%m-%d')}/#{end_date.strftime('%Y-%m-%d')}#{options.to_query_string(true)}" + + response = self.connection.get(url, options) + response.body.map do |country, hash| + if response.status == 200 Hashie::Mash.new({ - 'date' => Date.parse(date), - 'product_id' => hash['product']['id'], - 'store_id' => hash['product']['store_id'], - 'store_name' => hash['product']['store_name'], - 'name' => hash['product']['name'], - 'sku' => hash['product']['sku'], - 'downloads' => hash['downloads'].to_i, - 'returns' => hash['returns'].to_i, - 'updates' => hash['updates'].to_i, - 'net_downloads' => hash['net_downloads'].to_i, - 'promos' => hash['promos'].to_i, - 'gift_redemptions'=> hash['gift_redemptions'].to_i, - 'revenue' => hash['revenue'].to_f + 'iso' => hash['iso'], + 'country' => hash['country'], + 'downloads' => hash['downloads'], + 'updates' => hash['updates'], + 'returns' => hash['returns'], + 'net_downloads' => hash['net_downloads'], + 'promos' => hash['promos'], + 'revenue' => hash['revenue'], + 'gift_redemptions' => hash['gift_redemptions'], }) - end.first + else + puts 'Appfigures service error:' + puts hash.to_json + Hashie::Mash.new + end end end + # GET /reviews?products={productId}&lang=en + # See http://docs.appfigures.com/api/reference/v2/reviews + def product_reviews(product_id, options = {}) + url = "reviews" + options = {products: product_id, + lang: 'en' + }.merge(options) + + response = self.connection.get(url, options) + if response.status == 200 + body = response.body + reviews = Hashie::Mash.new({ + 'total' => body['total'].to_i, + 'pages' => body['pages'].to_i, + 'this_page' => body['this_page'].to_i, + 'reviews' => body['reviews'].map do |review| + Hashie::Mash.new({ + 'author' => review['author'], + 'title' => review['title'], + 'review' => review['review'], + 'stars' => review['stars'], + 'iso' => review['iso'], + 'version' => review['version'], + 'date' => review['date'], + 'product' => review['product'], + 'id' => review['id'] + }) + end + }) + else + puts 'Appfigures service error:' + puts response.body.to_json + return Hashie::Mash.new + end + end + + # GET /ratings?group_by=product&start_date={today}&end_date={today}products={productId} + def product_ratings(product_id, options = {}) + url = "ratings" + now = Time.now + today = Date.new(now.year, now.month, now.day).strftime('%Y-%m-%d') + + options = {group_by: 'product', + products: product_id, + start_date: today, + end_date: today}.merge(options) + + response = self.connection.get(url, options) + response.body.map do |product, hash| + if response.status == 200 + Hashie::Mash.new({ + 'stars' => product['stars'], + 'product' => product['product'] + }) + else + puts 'Appfigures service error:' + puts hash.to_json + return Hashie::Mash.new + end + end.first + end end diff --git a/lib/appfigures/connection.rb b/lib/appfigures/connection.rb index 47274e7..f515b21 100644 --- a/lib/appfigures/connection.rb +++ b/lib/appfigures/connection.rb @@ -5,15 +5,16 @@ class Appfigures class Connection < Faraday::Connection - def initialize(username, password) - super('https://api.appfigures.com/v1.1/') do |builder| + def initialize(username, password, client_key) + super('https://api.appfigures.com/v2/') do |builder| builder.use FaradayMiddleware::EncodeJson - builder.adapter Faraday.default_adapter builder.response :json, :content_type => /\bjson$/ + builder.adapter Faraday.default_adapter end self.basic_auth username, password self.headers["Accept"] = 'application/json' + self.headers["X-Client-Key"] = client_key end end end diff --git a/lib/appfigures/version.rb b/lib/appfigures/version.rb index 37411f8..7b71aea 100644 --- a/lib/appfigures/version.rb +++ b/lib/appfigures/version.rb @@ -1,3 +1,3 @@ class Appfigures - VERSION = "0.0.1" + VERSION = "0.0.2" end diff --git a/lib/utils/hash_extensions.rb b/lib/utils/hash_extensions.rb new file mode 100644 index 0000000..6a4722a --- /dev/null +++ b/lib/utils/hash_extensions.rb @@ -0,0 +1,13 @@ + +class Hash + def to_query_string(include_question_mark = true) + query_string = '' + unless empty? + query_string << '?' if include_question_mark + query_string << inject([]) do |params, (key, value)| + params << "#{key}=#{value}" + end.join('&') + end + query_string + end +end diff --git a/spec/country_sales_spec.rb b/spec/country_sales_spec.rb new file mode 100644 index 0000000..7ecf287 --- /dev/null +++ b/spec/country_sales_spec.rb @@ -0,0 +1,64 @@ +require 'spec_helper' + +describe 'Appfigures country sales' do + before do + status_code = 200 + headers = { + 'Cache-Control' => 'private', + 'Content-Type' => 'application/json', + 'Server' => 'Microsoft-IIS/7.5', + 'X-AspNetMvc-Version' => '2.0', + 'X-Request-Limit' => '1000', + 'X-Request-Usage' => '4', + 'X-AspNet-Version' => '4.0.30319', + 'X-Server-ID' => '10', + 'Date' => 'Tue, 24 Jul 2012 19:56:51 GMT', + 'Connection' => 'close', + 'Transfer-Encoding' => 'Identity' + } + body = <<-EOF + { + "US": { + "downloads": 213, + "updates": 715, + "returns": 0, + "net_downloads": 213, + "promos": 0, + "revenue": "0.00", + "gift_redemptions": 0, + "country": "United States", + "iso": "US" + }, + "VE": { + "downloads": 1, + "updates": 2, + "returns": 0, + "net_downloads": 1, + "promos": 0, + "revenue": "0.00", + "gift_redemptions": 0, + "country": "Venezuela", + "iso": "VE" + } + } + EOF + @api = Appfigures.new username: 'test', password: 'test', client_key: 'test' + @stubs = Faraday::Adapter::Test::Stubs.new do |stub| + stub.get('/v2/reports/sales?end=2013-03-31&group_by=country&start=2013-03-01') { [status_code, headers, body] } + end + @api.connection.adapter :test, @stubs + end + + let(:start_date) { Date.parse('2013-03-01') } + let(:end_date) { Date.parse('2013-03-31') } + + it 'returns an iso' do + expect(@api.country_sales(start_date, end_date).first.iso).to eq("US") + end + + it 'returns downloads' do + expect(@api.country_sales(start_date, end_date).first.downloads).to eq(213) + end + + +end diff --git a/spec/date_sales_spec.rb b/spec/date_sales_spec.rb index 2791ad0..45980b4 100644 --- a/spec/date_sales_spec.rb +++ b/spec/date_sales_spec.rb @@ -27,52 +27,37 @@ "promos": 1, "revenue": "100.99", "gift_redemptions": 10, - "product": { - "id": 123123, - "ref_no": "536354432", - "external_account_id": 397, - "store_id": 0, - "store_name": "apple", - "added_timestamp": "2012-07-23T00:00:00", - "name": "Test App", - "icon": "http://a5.mzstatic.com/us/r1000/091/Purple/v4/20/69/65/20696562-4e19-17fe-5ffe-cb77a78e1651/mzl.jtpselsb.png", - "active": true, - "hidden": false, - "sku": "TEST_APP", - "in_apps": [], - "product_type": "app", - "addons": [] - } + "date": "2012-09-01", + "product_id": 123123 } } } EOF - @api = Appfigures.new username: 'test', password: 'test' + body223123 = <<-EOF + { + "2012-09-01": { + "223123": { + "downloads": 30, + "product_id": 223123 + } + } + } + EOF + @api = Appfigures.new username: 'test', password: 'test', client_key: 'test' @stubs = Faraday::Adapter::Test::Stubs.new do |stub| - stub.get('/v1.1/sales/dates+products/2012-09-01/2012-09-01') { [status_code, headers, body] } - end + + stub.get('/v2/reports/sales/dates+products?end=2012-09-30&products=223123&start=2012-09-01') { [status_code, headers, body223123] } + stub.get('/v2/reports/sales/dates+products?end=2012-09-30&start=2012-09-01') { [status_code, headers, body] } + end @api.connection.adapter :test, @stubs end let(:start_date) { Date.parse('2012-09-01') } - let(:end_date) { Date.parse('2012-09-01') } + let(:end_date) { Date.parse('2012-09-30') } it 'returns a product ID' do expect(@api.date_sales(start_date, end_date).first.product_id).to eq(123123) end - - it 'returns a store ID' do - expect(@api.date_sales(start_date, end_date).first.store_id).to eq(0) - end - it 'returns a store name' do - expect(@api.date_sales(start_date, end_date).first.store_name).to eq('apple') - end - it 'returns a name' do - expect(@api.date_sales(start_date, end_date).first.name).to eq('Test App') - end - it 'returns a sku' do - expect(@api.date_sales(start_date, end_date).first.sku).to eq('TEST_APP') - end it 'returns a download count' do expect(@api.date_sales(start_date, end_date).first.downloads).to eq(29) end @@ -94,5 +79,8 @@ it 'returns a revenue number' do expect(@api.date_sales(start_date, end_date).first.revenue).to eq(100.99) end + it 'returns a specific product ID' do + expect(@api.date_sales(start_date, end_date, { :products => 223123 }).first.product_id).to eq(223123) + end end diff --git a/spec/product_reviews_spec.rb b/spec/product_reviews_spec.rb new file mode 100644 index 0000000..a425c13 --- /dev/null +++ b/spec/product_reviews_spec.rb @@ -0,0 +1,236 @@ +require 'spec_helper' + +describe 'Appfigures product reviews' do + before do + status_code = 200 + headers = { + 'Cache-Control' => 'private', + 'Content-Type' => 'application/json', + 'Server' => 'Microsoft-IIS/7.5', + 'X-AspNetMvc-Version' => '2.0', + 'X-Request-Limit' => '1000', + 'X-Request-Usage' => '4', + 'X-AspNet-Version' => '4.0.30319', + 'X-Server-ID' => '10', + 'Date' => 'Tue, 24 Jul 2012 19:56:51 GMT', + 'Connection' => 'close', + 'Transfer-Encoding' => 'Identity' + } + body = <<-EOF +{ + "total": 10, + "pages": 1, + "this_page": 1, + "reviews": [ + { + "author": "Extremely happy customer :)", + "title": "So happy!", + "review": "Great app & great surgeon had my nose done with him 6 months ago and very happy with the results! :)", + "original_title": "So happy!", + "original_review": "Great app & great surgeon had my nose done with him 6 months ago and very happy with the results! :)", + "stars": "5.00", + "iso": "AU", + "version": null, + "date": "2013-09-08T12:31:00", + "product": 6764936, + "weight": 0, + "id": "6764936LMZFNAEP14DA17opqFW0qkw==", + "predicted_langs": [ + "en" + ] + }, + { + "author": "Extremely happy customer :)", + "title": "So happy!", + "review": "Great app & great surgeon had my nose done with him 6 months ago and very happy with the results! :)", + "original_title": "So happy!", + "original_review": "Great app & great surgeon had my nose done with him 6 months ago and very happy with the results! :)", + "stars": "5.00", + "iso": "AU", + "version": null, + "date": "2013-09-08T12:31:00", + "product": 6764936, + "weight": 0, + "id": "6764936LIw1AZww6P2h2ot9ZkjylXQ==", + "predicted_langs": [ + "en" + ] + }, + { + "author": "GelatinousFire", + "title": "Easy to use, helpful", + "review": "I think I've been getting carried away with using this app too much. The only thing I would suggest is having a feature to save the before and after together, especially if the photo was taken in-app and not saved on the device already.", + "original_title": "Easy to use, helpful", + "original_review": "I think I've been getting carried away with using this app too much. The only thing I would suggest is having a feature to save the before and after together, especially if the photo was taken in-app and not saved on the device already.", + "stars": "5.00", + "iso": "AU", + "version": null, + "date": "2013-09-07T03:17:00", + "product": 6764936, + "weight": 0, + "id": "6764936LTWoPGlw4Jv1mL6Is4IfvJA==", + "predicted_langs": [ + "en" + ] + }, + { + "author": "Needanewnose", + "title": "Awesome from me too", + "review": "Excellent idea - very helpful if considering surgery!!", + "original_title": "Awesome from me too", + "original_review": "Excellent idea - very helpful if considering surgery!!", + "stars": "5.00", + "iso": "AU", + "version": null, + "date": "2012-12-31T08:29:00", + "product": 6764936, + "weight": 0, + "id": "6764936LABQtADfYjihiTLnGfoitww==", + "predicted_langs": [ + "en" + ] + }, + { + "author": "Aplen", + "title": "Awesome", + "review": "Love this app. Works fine.", + "original_title": "Awesome", + "original_review": "Love this app. Works fine.", + "stars": "5.00", + "iso": "AU", + "version": null, + "date": "2012-11-01T11:45:00", + "product": 6764936, + "weight": 0, + "id": "6764936LWwOvS-+7wc5CYg1fNybp+w==", + "predicted_langs": [ + "en" + ] + }, + { + "author": "Buybuybuybuy", + "title": "Awesome", + "review": "Aww now I really want a nose job!!!", + "original_title": "Awesome", + "original_review": "Aww now I really want a nose job!!!", + "stars": "5.00", + "iso": "AU", + "version": null, + "date": "2012-08-18T04:28:00", + "product": 6764936, + "weight": 0, + "id": "6764936LbHD103TUYNFH-ef0ZBJl-w==", + "predicted_langs": [ + "en" + ] + }, + { + "author": "Shnoz66", + "title": "Fix my nose", + "review": "Great app Dr Shahidi Love your work", + "original_title": "Fix my nose", + "original_review": "Great app Dr Shahidi Love your work", + "stars": "5.00", + "iso": "AU", + "version": null, + "date": "2012-06-22T22:37:00", + "product": 6764936, + "weight": 0, + "id": "6764936Lk31Vr1HbM0zRYqCw6fwxUw==", + "predicted_langs": [ + "en", + "af", + "la", + "nl" + ] + }, + { + "author": "Shnoz66", + "title": "Fix my nose", + "review": "Great app Dr Shahidi
Love your work", + "original_title": "Fix my nose", + "original_review": "Great app Dr Shahidi
Love your work", + "stars": "5.00", + "iso": "AU", + "version": null, + "date": "2012-06-22T10:37:00", + "product": 6764936, + "weight": 0, + "id": "6764936Lgo8PsGRm1+ZtKI0wLiagdQ==", + "predicted_langs": [ + "en", + "af", + "la", + "nl" + ] + }, + { + "author": "Happypatient", + "title": "Dr Shahidi", + "review": "The app is fun and Dr Shahidi is a genius!", + "original_title": "Dr Shahidi", + "original_review": "The app is fun and Dr Shahidi is a genius!", + "stars": "5.00", + "iso": "AU", + "version": null, + "date": "2012-02-16T07:30:00", + "product": 6764936, + "weight": 0, + "id": "6764936LBbqGDq1qPu6LuLMS4tCj7w==", + "predicted_langs": [ + "la", + "en", + "cy", + "af", + "lb" + ] + }, + { + "author": "Eddy EF", + "title": "Facial plastic surgery", + "review": "It's a great facial surgeries performed by this doctor, I recommend it.", + "original_title": "Facial plastic surgery", + "original_review": "It's a great facial surgeries performed by this doctor, I recommend it.", + "stars": "5.00", + "iso": "AU", + "version": null, + "date": "2011-10-13T07:30:00", + "product": 6764936, + "weight": 0, + "id": "6764936LmBQyYvLr3+vY-hWhn1lt4g==", + "predicted_langs": [ + "en" + ] + } + ] +} + + EOF + @api = Appfigures.new username: 'test', password: 'test', client_key: 'test' + @stubs = Faraday::Adapter::Test::Stubs.new do |stub| + stub.get('/v2/reviews?lang=en&products=6764936') { [status_code, headers, body] } + end + @api.connection.adapter :test, @stubs + end + + let(:product_id) { "6764936" } + + it 'returns 10 reviews' do + expect(@api.product_reviews(product_id).reviews.count).to eq(10) + end + + + it 'returns review text' do + expect(@api.product_reviews(product_id).reviews[0].review).to eq("Great app & great surgeon had my nose done with him 6 months ago and very happy with the results! :)") + end + + it 'returns review title' do + expect(@api.product_reviews(product_id).reviews[1].title).to eq("So happy!") + end + + it 'returns review stars' do + expect(@api.product_reviews(product_id).reviews[0].stars).to eq("5.00") + end + + +end \ No newline at end of file diff --git a/spec/product_sales_spec.rb b/spec/product_sales_spec.rb index 1527bec..ba13e80 100644 --- a/spec/product_sales_spec.rb +++ b/spec/product_sales_spec.rb @@ -25,29 +25,59 @@ "net_downloads": 26, "promos": 1, "revenue": "100.99", + "edu_downloads": 0, + "gifts": 0, "gift_redemptions": 10, "product": { "id": 123123, - "ref_no": "536354432", - "external_account_id": 397, - "store_id": 0, - "store_name": "apple", - "added_timestamp": "2012-07-23T00:00:00", "name": "Test App", + "developer": "Test Inc", "icon": "http://a5.mzstatic.com/us/r1000/091/Purple/v4/20/69/65/20696562-4e19-17fe-5ffe-cb77a78e1651/mzl.jtpselsb.png", - "active": true, - "hidden": false, + "vendor_identifier": "23292092", + "ref_no": "536354432", "sku": "TEST_APP", - "in_apps": [], - "product_type": "app", - "addons": [] - } + "package_name": null, + "store_id": 0, + "store": "apple", + "storefront": "apple:ios", + "release_date": "2010-12-14T05:00:00", + "added_date": "2011-11-30T13:02:38", + "updated_date": "2015-05-06T09:00:53", + "version": "3.8.0", + "source": { + "external_account_id": 397, + "added_timestamp": "2012-07-23T00:00:00", + "active": true, + "hidden": false, + "type": "own" + }, + "type": "app", + "devices": [ + "Handheld", + "Tablet" + ], + "bundle_identifier": "com.appfigures.test", + "accessible_features": [ + "sales", + "reviews", + "reviews_count", + "ranks", + "featured" + ], + "children": [], + "features": [], + "parent_id": null, + "storefronts": [ + "apple:ios" + ] + }, + "product_id": 6403600 } } EOF - @api = Appfigures.new username: 'test', password: 'test' + @api = Appfigures.new username: 'test', password: 'test', client_key: 'test' @stubs = Faraday::Adapter::Test::Stubs.new do |stub| - stub.get('/v1.1/sales/products') { [status_code, headers, body] } + stub.get('/v2/reports/sales?group_by=product') { [status_code, headers, body] } end @api.connection.adapter :test, @stubs end @@ -89,5 +119,17 @@ it 'returns a revenue number' do expect(@api.product_sales.first.revenue).to eq(100.99) end + it 'returns a ref_no' do + expect(@api.product_sales.first.ref_no).to eq('536354432') + end + it 'returns an added timestamp' do + expect(@api.product_sales.first.added_timestamp).to eq(Date.parse('2012-07-23T00:00:00')) + end + it 'returns an icon' do + expect(@api.product_sales.first.icon).to eq('http://a5.mzstatic.com/us/r1000/091/Purple/v4/20/69/65/20696562-4e19-17fe-5ffe-cb77a78e1651/mzl.jtpselsb.png') + end + it 'returns active' do + expect(@api.product_sales.first.active).to eq(true) + end end diff --git a/spec/ratings_spec.rb b/spec/ratings_spec.rb new file mode 100644 index 0000000..621ce56 --- /dev/null +++ b/spec/ratings_spec.rb @@ -0,0 +1,54 @@ +require 'spec_helper' + +describe 'Appfigures country sales' do + before do + status_code = 200 + headers = { + 'Cache-Control' => 'private', + 'Content-Type' => 'application/json', + 'Server' => 'Microsoft-IIS/7.5', + 'X-AspNetMvc-Version' => '2.0', + 'X-Request-Limit' => '1000', + 'X-Request-Usage' => '4', + 'X-AspNet-Version' => '4.0.30319', + 'X-Server-ID' => '10', + 'Date' => 'Tue, 24 Jul 2012 19:56:51 GMT', + 'Connection' => 'close', + 'Transfer-Encoding' => 'Identity' + } + body = <<-EOF +[ + { + "product": 6239092, + "date": "2015-11-01T00:00:00", + "stars": [ + 16, + 4, + 5, + 8, + 29 + ] + } +] + EOF + @api = Appfigures.new username: 'test', password: 'test', client_key: 'test' + @stubs = Faraday::Adapter::Test::Stubs.new do |stub| + stub.get('/v2/ratings?group_by=product&products=6239092') { [status_code, headers, body] } + end + @api.connection.adapter :test, @stubs + end + + let(:product_id) { "6239092" } + + it 'returns star breakdown' do + expect(@api.product_ratings(product_id).stars).to eq([16, 4, 5, 8, 29]) + end + + it 'returns product' do + expect(@api.product_ratings(product_id).product).to eq(product_id.to_i) + end + + + + +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 6d7c93a..b7e15b5 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -7,6 +7,6 @@ require 'appfigures' RSpec.configure do |config| - config.color_enabled = true + config.color = true config.formatter = 'documentation' end