Commit cabef3e3 by Dao Minh Nhut

commit

parent 3c9b6462
namespace :db do
desc "Fill database with sample data"
task create_sample_items: :environment do
cat= [2498,2500,2501,2502,2504,2505,2506,2507,2508,2503]
users = User.all(limit: 6)
10.times do
name = Faker::Name.name
description = Faker::Lorem.sentence(5)
image = "http://www.armsandbadges.com/Sample/Sample.png"
price= rand(100)
rate= rand(5)
category_id= cat[rand(9)]
users.each { |user| user.items.create!( name: name,
description: description,
image: image,
price: price,
rate: rate,
category_id: category_id) }
end
end
task task: :environment do
end
end
\ No newline at end of file
require 'open-uri'
require 'json'
namespace :db do
desc "Fill category and item table with data from yahoo api"
task get_data: :environment do
base_url= 'http://shopping.yahooapis.jp/ShoppingWebService/V1/json/'
appid= 'appid=dj0zaiZpPXpIMzBsMUQyTk55dSZkPVlXazlZWGxzYjNoWU0yVW1jR285TUEtLSZzPWNvbnN1bWVyc2VjcmV0Jng9MzI-&'
contents = URI.parse(base_url+'categorySearch?'+appid+'category_id=1&sort=-sold').read
data= JSON.parse(contents)
category= data["ResultSet"]["0"]["Result"]["Categories"]["Children"]
# store id of category to request item
category_id= Array.new
for i in 1..10 do
cat= category["#{i}"]
category_id << cat["Id"].to_i
Category.create!(name: cat["Title"]["Medium"])
end
puts "category done"
cat_id=1
category_id.each do |id|
offset= 0
puts "category #{id} started"
while offset < 1000 do
user= User.find(rand(99)+1)
contents = URI.parse(base_url+'itemSearch?'+appid+"category_id=#{id}&sort=-sold&offset=#{offset}").read
data= data= JSON.parse(contents)
item= data["ResultSet"]["0"]["Result"]
number_of_item_return= data["ResultSet"]["totalResultsReturned"].to_i
number_of_item_return.times do |n|
i= item["#{n}"]
name= i["Name"]
des= i["Description"]
img_url= i["Image"]["Medium"]
rate= i["Review"]["Rate"].to_i
price= i["PriceLabel"]["DefaultPrice"].to_i
Category.find(cat_id).items << user.items.create!( name: name,
description: des,
image: img_url,
price: price,
rate: rate)
end
offset += number_of_item_return
end
cat_id += 1
puts "category #{id} completed"
end
end
end
\ No newline at end of file
namespace :import_product do
desc 'Fill category and item table with data'
task get_data: :environment do
request = Vacuum.new('GB')
request.configure(
aws_access_key_id: 'key',
aws_secret_access_key: 'secret',
associate_tag: 'tag'
)
params = {
'SearchIndex' => 'Books',
'Keywords'=> 'Ruby on Rails',
'ResponseGroup' => "ItemAttributes,Images"
}
raw_products = request.item_search(query: params)
hashed_products = raw_products.to_h
end
end
\ No newline at end of file
namespace :db do
desc "Fill database with sample data"
task populate: :environment do
admin = User.create!(name: "Example User",
email: "example@railstutorial.org",
password: "foobar",
password_confirmation: "foobar")
admin.toggle!(:admin)
99.times do |n|
name = Faker::Name.name
email = "example-#{n+1}@railstutorial.org"
password = "password"
User.create!(name: name,
email: email,
password: password,
password_confirmation: password)
end
users = User.all(limit: 6)
50.times do
content = Faker::Lorem.sentence(5)
users.each { |user| user.microposts.create!(content: content) }
end
end
end
\ No newline at end of file
require 'delegate'
require 'multi_xml'
module Vacuum
# A wrapper around the Amazon Product Advertising API response.
class Response < SimpleDelegator
class << self
attr_accessor :parser
end
def parser
@parser || self.class.parser
end
attr_writer :parser
def parse
parser ? parser.parse(body) : to_h
end
def to_h
MultiXml.parse(body)
end
def body
__getobj__.body.force_encoding('UTF-8')
end
end
end
\ No newline at end of file
This source diff could not be displayed because it is too large. You can view the blob instead.
require 'minitest/autorun'
require 'minitest/pride'
require 'vcr'
require_relative '../lib/vacuum'
VCR.configure do |c|
c.hook_into :excon
c.cassette_library_dir = 'test/cassettes'
c.default_cassette_options = {
match_requests_on: [VCR.request_matchers.uri_without_param(
'AWSAccessKeyId', 'AssociateTag', 'Signature', 'Timestamp', 'Version'
)],
record: :new_episodes
}
end
class TestIntegration < Minitest::Test
include Vacuum
def setup
VCR.insert_cassette('vacuum')
end
def teardown
VCR.eject_cassette
end
# rubocop:disable AbcSize
def test_encoding_issues
params = { 'SearchIndex' => 'All', 'Keywords' => 'google' }
titles = %w(BR CA CN DE ES FR GB IN IT JP US).flat_map do |locale|
req = Vacuum.new(locale)
req.associate_tag = 'foo'
res = req.item_search(query: params)
items = res.to_h['ItemSearchResponse']['Items']['Item']
items.map { |item| item['ItemAttributes']['Title'] }
end
encodings = titles.map { |t| t.encoding.name }.uniq
# Newer JRuby now appears to return both US-ASCII and UTF-8, depending on
# whether the string has non-ASCII characters. MRI will only return latter.
assert encodings.any? { |encoding| encoding == 'UTF-8' }
end
end
\ No newline at end of file
require 'minitest/autorun'
require 'minitest/pride'
require_relative '../lib/vacuum'
class TestVacuum < Minitest::Test
include Vacuum
def setup
@req = Request.new
end
def teardown
Excon.stubs.clear
end
def test_requires_valid_locale
assert_raises(Request::BadLocale) { Request.new('foo') }
end
def test_defaults_to_us_endpoint
assert_equal 'http://webservices.amazon.com/onca/xml', @req.aws_endpoint
end
def test_defaults_to_latest_api_version
assert_equal Request::LATEST_VERSION, @req.version
end
def test_accepts_uk_as_locale
Request.new("UK")
end
def test_fetches_parsable_response
Excon.stub({}, body: '<foo/>')
res = @req.item_lookup({}, mock: true)
refute_empty res.parse
end
def test_alternative_query_syntax
Excon.stub({}, body: '<foo/>')
res = @req.item_lookup(query: {}, mock: true)
refute_empty res.parse
end
def test_force_encodes_body
res = Object.new
def res.body
''.force_encoding('ASCII-8BIT')
end
assert_equal 'UTF-8', Response.new(res).body.encoding.name
end
def test_sets_custom_parser_on_class_level
original_parser = Response.parser
Excon.stub({}, body: '<foo/>')
parser = MiniTest::Mock.new
parser.expect(:parse, '123', ['<foo/>'])
Response.parser = parser
res = @req.item_lookup(query: {}, mock: true)
assert_equal '123', res.parse
Response.parser = original_parser # clean up
end
def test_sets_custom_parser_on_instance_level
Excon.stub({}, body: '<foo/>')
res = @req.item_lookup(query: {}, mock: true)
parser = MiniTest::Mock.new
parser.expect(:parse, '123', ['<foo/>'])
res.parser = parser
assert_equal '123', res.parse
end
def test_casts_to_hash
Excon.stub({}, body: '<foo/>')
parser = MiniTest::Mock.new
res = @req.item_lookup(query: {}, mock: true)
assert_kind_of Hash, res.to_h
res.parser = parser
assert_kind_of Hash, res.to_h
end
end
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment