Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
V
venshop
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
huanvl
venshop
Commits
a7c40115
Commit
a7c40115
authored
Nov 06, 2013
by
vulehuan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
update models and seeds file for dummy products from yahoo shopping api
parent
1cd32a56
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
129 additions
and
172 deletions
+129
-172
venshop_app/app/models/product.rb
+8
-8
venshop_app/app/models/user.rb
+2
-1
venshop_app/db/seeds.rb
+119
-0
venshop_app/lib/tasks/sample_data.rake
+0
-117
venshop_app/lib/tasks/solr.rake
+0
-46
No files found.
venshop_app/app/models/product.rb
View file @
a7c40115
...
...
@@ -13,7 +13,7 @@ class Product
field
:price_currency
,
type:
String
field
:price
,
type:
Float
field
:product_category_id
,
type:
Integer
field
:user_id
,
type:
Integer
field
:user_id
,
type:
String
field
:status
,
type:
Mongoid
::
Boolean
belongs_to
:product_category
...
...
@@ -22,15 +22,15 @@ class Product
validates
:user
,
presence:
true
validates
:name
,
:code
,
:condition
,
:image_medium
,
:price
,
presence:
true
validates
:name
,
length:
{
maximum:
255
}
validates
:price
,
:product_category_id
,
:user_id
,
numericality:
true
validates
:price
,
:product_category_id
,
numericality:
true
validates
:price_currency
,
presence:
true
,
if:
"price > 0"
# searchable :auto_index => false do
# text :name, :description
# string :headline, :code, :condition, :price_currency
# double :price
# integer :product_category_id, :user_id
#
end
include
Sunspot
::
Mongoid
searchable
:auto_index
=>
false
do
text
:name
,
:description
string
:headline
,
:code
,
:condition
,
:price_currency
double
:price
end
# Returns recommended products (order by rate, and available)
def
self
.
get_recommended_products
(
options
=
{
limit:
8
})
...
...
venshop_app/app/models/user.rb
View file @
a7c40115
class
User
include
Mongoid
::
Document
include
ActiveModel
::
SecurePassword
field
:name
,
type:
String
field
:email
,
type:
String
field
:password_digest
,
type:
String
has_secure_password
field
:remember_token
,
type:
String
field
:admin
,
type:
Mongoid
::
Boolean
...
...
@@ -12,7 +14,6 @@ class User
validates
:name
,
presence:
true
,
length:
{
maximum:
50
}
VALID_EMAIL_REGEX
=
/\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates
:email
,
presence:
true
,
format:
{
with:
VALID_EMAIL_REGEX
},
uniqueness:
{
case_sensitive:
false
}
# has_secure_password
validates
:password
,
length:
{
minimum:
6
}
def
User
.
new_remember_token
...
...
venshop_app/db/seeds.rb
View file @
a7c40115
...
...
@@ -5,3 +5,122 @@
#
# cities = City.create([{ name: 'Chicago' }, { name: 'Copenhagen' }])
# Mayor.create(name: 'Emanuel', city: cities.first)
require
'net/http'
require
'rexml/document'
# Product Category
# http://developer.yahoo.co.jp/webapi/dir/category/v1/category.html
category_url
=
'http://shopping.yahooapis.jp/ShoppingWebService/V1/categorySearch'
request_url
=
category_url
+
'?appid='
+
YAHOO_SHOPPING_DATA_APP_ID
+
'&category_id=1'
# get the XML data as a string
xml_data
=
Net
::
HTTP
.
get_response
(
URI
.
parse
(
request_url
)).
body
# extract category information
doc
=
REXML
::
Document
.
new
(
xml_data
)
i
=
0
category_ids
=
Array
.
new
doc
.
elements
.
each
(
'ResultSet/Result/Categories/Children/Child'
)
do
|
ele
|
i
+=
1
break
if
i
>
20
id
=
ele
.
elements
[
'Id'
].
text
title
=
ele
.
elements
[
'Title'
]
title
.
elements
.
each
(
'Long'
)
do
|
child_ele
|
title
=
child_ele
.
text
end
category_ids
.
push
(
id
)
ProductCategory
.
create!
(
id:
id
,
name:
title
,
weight:
id
,
status:
true
)
end
# Create admin account (product belongs to user)
@user
=
User
.
new
(
name:
"admin"
,
email:
"admin@local.net"
,
admin:
true
,
password:
"123456"
,
password_confirmation:
"123456"
)
@user
.
save
# Product list
# http://developer.yahoo.co.jp/webapi/shopping/shopping/v1/itemsearch.html
list_url
=
'http://shopping.yahooapis.jp/ShoppingWebService/V1/itemSearch'
i
=
0
max_item_count
=
10000
category_ids
.
each
do
|
category_id
|
break
if
i
>
max_item_count
(
1
..
20
).
each
do
|
offset
|
break
if
i
>
max_item_count
request_url
=
list_url
+
'?appid='
+
YAHOO_SHOPPING_DATA_APP_ID
+
'&category_id='
+
category_id
.
to_s
+
'&offset='
+
offset
.
to_s
+
'&hits=50'
# get the XML data as a string
xml_data
=
Net
::
HTTP
.
get_response
(
URI
.
parse
(
request_url
)).
body
# extract product information
doc
=
REXML
::
Document
.
new
(
xml_data
)
doc
.
elements
.
each
(
'ResultSet/Result/Hit'
)
do
|
ele
|
next
if
ele
.
elements
[
'Name'
]
==
nil
i
+=
1
break
if
i
>
max_item_count
name
=
ele
.
elements
[
'Name'
].
text
description
=
ele
.
elements
[
'Description'
].
text
headline
=
ele
.
elements
[
'Headline'
].
text
availability
=
ele
.
elements
[
'Availability'
].
text
code
=
ele
.
elements
[
'Code'
].
text
condition
=
ele
.
elements
[
'Condition'
].
text
image
=
ele
.
elements
[
'Image'
]
image_small
=
''
image_medium
=
''
image
.
elements
.
each
(
'Small'
)
do
|
child_ele
|
image_small
=
child_ele
.
text
end
image
.
elements
.
each
(
'Medium'
)
do
|
child_ele
|
image_medium
=
child_ele
.
text
end
review
=
ele
.
elements
[
'Review'
]
review_rate
=
0
review_count
=
0
review
.
elements
.
each
(
'Rate'
)
do
|
child_ele
|
review_rate
=
child_ele
.
text
end
review
.
elements
.
each
(
'Count'
)
do
|
child_ele
|
review_count
=
child_ele
.
text
end
price
=
ele
.
elements
[
'Price'
]
price_currency
=
price
.
attributes
[
"currency"
]
price
=
price
.
text
begin
Product
.
create!
(
name:
name
,
description:
description
,
headline:
headline
,
availability:
availability
,
code:
code
,
condition:
condition
,
image_small:
image_small
,
image_medium:
image_medium
,
review_rate:
review_rate
,
review_count:
review_count
,
price_currency:
price_currency
,
price:
price
,
product_category_id:
category_id
,
user_id:
@user
.
id
,
status:
true
)
rescue
next
end
end
end
end
puts
'==========='
puts
'DONE'
puts
'==========='
venshop_app/lib/tasks/sample_data.rake
deleted
100644 → 0
View file @
1cd32a56
namespace
:db
do
desc
"Fill database with Yahoo shopping data"
task
populate: :environment
do
require
'net/http'
require
'rexml/document'
# Product Category
# http://developer.yahoo.co.jp/webapi/dir/category/v1/category.html
category_url
=
'http://shopping.yahooapis.jp/ShoppingWebService/V1/categorySearch'
request_url
=
category_url
+
'?appid='
+
YAHOO_SHOPPING_DATA_APP_ID
+
'&category_id=1'
# get the XML data as a string
xml_data
=
Net
::
HTTP
.
get_response
(
URI
.
parse
(
request_url
)).
body
# extract category information
doc
=
REXML
::
Document
.
new
(
xml_data
)
i
=
0
category_ids
=
Array
.
new
doc
.
elements
.
each
(
'ResultSet/Result/Categories/Children/Child'
)
do
|
ele
|
i
+=
1
break
if
i
>
20
id
=
ele
.
elements
[
'Id'
].
text
title
=
ele
.
elements
[
'Title'
]
title
.
elements
.
each
(
'Long'
)
do
|
child_ele
|
title
=
child_ele
.
text
end
category_ids
.
push
(
id
)
ProductCategory
.
create!
(
id:
id
,
name:
title
,
weight:
id
,
status:
true
)
end
# Create admin account (product belongs to user)
@user
=
User
.
new
(
name:
"admin"
,
email:
"admin@local.net"
,
admin:
true
,
password:
"123456"
,
password_confirmation:
"123456"
)
@user
.
save
# Product list
# http://developer.yahoo.co.jp/webapi/shopping/shopping/v1/itemsearch.html
list_url
=
'http://shopping.yahooapis.jp/ShoppingWebService/V1/itemSearch'
i
=
0
category_ids
.
each
do
|
category_id
|
(
1
..
20
).
each
do
|
offset
|
request_url
=
list_url
+
'?appid='
+
YAHOO_SHOPPING_DATA_APP_ID
+
'&category_id='
+
category_id
.
to_s
+
'&offset='
+
offset
.
to_s
+
'&hits=50'
# get the XML data as a string
xml_data
=
Net
::
HTTP
.
get_response
(
URI
.
parse
(
request_url
)).
body
# extract product information
doc
=
REXML
::
Document
.
new
(
xml_data
)
doc
.
elements
.
each
(
'ResultSet/Result/Hit'
)
do
|
ele
|
next
if
ele
.
elements
[
'Name'
]
==
nil
i
+=
1
break
if
i
>
10000
name
=
ele
.
elements
[
'Name'
].
text
description
=
ele
.
elements
[
'Description'
].
text
headline
=
ele
.
elements
[
'Headline'
].
text
availability
=
ele
.
elements
[
'Availability'
].
text
code
=
ele
.
elements
[
'Code'
].
text
condition
=
ele
.
elements
[
'Condition'
].
text
image
=
ele
.
elements
[
'Image'
]
image_small
=
''
image_medium
=
''
image
.
elements
.
each
(
'Small'
)
do
|
child_ele
|
image_small
=
child_ele
.
text
end
image
.
elements
.
each
(
'Medium'
)
do
|
child_ele
|
image_medium
=
child_ele
.
text
end
review
=
ele
.
elements
[
'Review'
]
review_rate
=
0
review_count
=
0
review
.
elements
.
each
(
'Rate'
)
do
|
child_ele
|
review_rate
=
child_ele
.
text
end
review
.
elements
.
each
(
'Count'
)
do
|
child_ele
|
review_count
=
child_ele
.
text
end
price
=
ele
.
elements
[
'Price'
]
price_currency
=
price
.
attributes
[
"currency"
]
price
=
price
.
text
begin
Product
.
create!
(
name:
name
,
description:
description
,
headline:
headline
,
availability:
availability
,
code:
code
,
condition:
condition
,
image_small:
image_small
,
image_medium:
image_medium
,
review_rate:
review_rate
,
review_count:
review_count
,
price_currency:
price_currency
,
price:
price
,
product_category_id:
category_id
,
user_id:
@user
.
id
,
status:
true
)
rescue
next
end
end
end
end
end
end
venshop_app/lib/tasks/solr.rake
deleted
100644 → 0
View file @
1cd32a56
namespace
:sunspot
do
namespace
:solr
do
desc
'Start the Solr instance'
task
:start
=>
:environment
do
case
RUBY_PLATFORM
when
/w(in)?32$/
,
/java$/
abort
(
"This command is not supported on
#{
RUBY_PLATFORM
}
. "
+
"Use rake sunspot:solr:run to run Solr in the foreground."
)
end
if
defined?
(
Sunspot
::
Rails
::
Server
)
Sunspot
::
Rails
::
Server
.
new
.
start
else
Sunspot
::
Solr
::
Server
.
new
.
start
end
puts
"Successfully started Solr ..."
end
desc
'Run the Solr instance in the foreground'
task
:run
=>
:environment
do
if
defined?
(
Sunspot
::
Rails
::
Server
)
Sunspot
::
Rails
::
Server
.
new
.
run
else
Sunspot
::
Solr
::
Server
.
new
.
run
end
end
desc
'Stop the Solr instance'
task
:stop
=>
:environment
do
case
RUBY_PLATFORM
when
/w(in)?32$/
,
/java$/
abort
(
"This command is not supported on
#{
RUBY_PLATFORM
}
. "
+
"Use rake sunspot:solr:run to run Solr in the foreground."
)
end
if
defined?
(
Sunspot
::
Rails
::
Server
)
Sunspot
::
Rails
::
Server
.
new
.
stop
else
Sunspot
::
Solr
::
Server
.
new
.
stop
end
puts
"Successfully stopped Solr ..."
end
task
:reindex
=>
:"sunspot:reindex"
end
end
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment