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
b7d37151
Commit
b7d37151
authored
Oct 23, 2013
by
vulehuan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
get product from yahoo shopping api
parent
079a8c34
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
123 additions
and
7 deletions
+123
-7
venshop_app/app/models/product.rb
+2
-0
venshop_app/db/migrate/20131023094033_create_products.rb
+21
-0
venshop_app/db/schema.rb
+19
-1
venshop_app/lib/tasks/sample_data.rake
+76
-6
venshop_app/spec/models/product_spec.rb
+5
-0
No files found.
venshop_app/app/models/product.rb
0 → 100644
View file @
b7d37151
class
Product
<
ActiveRecord
::
Base
end
venshop_app/db/migrate/20131023094033_create_products.rb
0 → 100644
View file @
b7d37151
class
CreateProducts
<
ActiveRecord
::
Migration
def
change
create_table
:products
do
|
t
|
t
.
string
:name
t
.
text
:description
t
.
string
:headline
t
.
string
:availability
t
.
string
:code
t
.
string
:condition
t
.
string
:image_small
t
.
string
:image_medium
t
.
decimal
:review_rate
t
.
integer
:review_count
t
.
string
:price_currency
t
.
decimal
:price
t
.
integer
:product_category_id
t
.
timestamps
end
end
end
venshop_app/db/schema.rb
View file @
b7d37151
...
@@ -11,7 +11,7 @@
...
@@ -11,7 +11,7 @@
#
#
# It's strongly recommended that you check this file into your version control system.
# It's strongly recommended that you check this file into your version control system.
ActiveRecord
::
Schema
.
define
(
version:
201310230
83438
)
do
ActiveRecord
::
Schema
.
define
(
version:
201310230
94033
)
do
create_table
"product_categories"
,
force:
true
do
|
t
|
create_table
"product_categories"
,
force:
true
do
|
t
|
t
.
string
"name"
t
.
string
"name"
...
@@ -21,6 +21,24 @@ ActiveRecord::Schema.define(version: 20131023083438) do
...
@@ -21,6 +21,24 @@ ActiveRecord::Schema.define(version: 20131023083438) do
t
.
datetime
"updated_at"
t
.
datetime
"updated_at"
end
end
create_table
"products"
,
force:
true
do
|
t
|
t
.
string
"name"
t
.
text
"description"
t
.
string
"headline"
t
.
string
"availability"
t
.
string
"code"
t
.
string
"condition"
t
.
string
"image_small"
t
.
string
"image_medium"
t
.
decimal
"review_rate"
,
precision:
10
,
scale:
0
t
.
integer
"review_count"
t
.
string
"price_currency"
t
.
decimal
"price"
,
precision:
10
,
scale:
0
t
.
integer
"product_category_id"
t
.
datetime
"created_at"
t
.
datetime
"updated_at"
end
create_table
"users"
,
force:
true
do
|
t
|
create_table
"users"
,
force:
true
do
|
t
|
t
.
string
"name"
t
.
string
"name"
t
.
string
"email"
t
.
string
"email"
...
...
venshop_app/lib/tasks/sample_data.rake
View file @
b7d37151
namespace
:db
do
namespace
:db
do
desc
"Fill database with Yahoo shopping data"
desc
"Fill database with Yahoo shopping data"
task
populate: :environment
do
task
populate: :environment
do
# Product Category
category_url
=
'http://dir.yahooapis.jp/Category/V1/Category'
category_path
=
'/Computers_and_Internet/Hardware/SmartPhone/'
request_url
=
category_url
+
'?appid='
+
YAHOO_SHOPPING_DATA_APP_ID
+
'&path='
+
category_path
require
'net/http'
require
'net/http'
require
'rexml/document'
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
# get the XML data as a string
xml_data
=
Net
::
HTTP
.
get_response
(
URI
.
parse
(
request_url
)).
body
xml_data
=
Net
::
HTTP
.
get_response
(
URI
.
parse
(
request_url
)).
body
# extract category information
# extract category information
doc
=
REXML
::
Document
.
new
(
xml_data
)
doc
=
REXML
::
Document
.
new
(
xml_data
)
i
=
0
i
=
0
doc
.
elements
.
each
(
'CategoryResult/Category/Item'
)
do
|
ele
|
category_ids
=
Array
.
new
doc
.
elements
.
each
(
'ResultSet/Result/Categories/Children/Child'
)
do
|
ele
|
i
+=
1
i
+=
1
break
if
i
>
5
break
if
i
>
5
id
=
ele
.
elements
[
'Id'
].
text
id
=
ele
.
elements
[
'Id'
].
text
title
=
ele
.
elements
[
'Title'
].
text
title
=
ele
.
elements
[
'Title'
]
title
.
elements
.
each
(
'Long'
)
do
|
child_ele
|
title
=
child_ele
.
text
end
category_ids
.
push
(
id
)
ProductCategory
.
create!
(
ProductCategory
.
create!
(
id:
id
,
id:
id
,
...
@@ -26,5 +33,67 @@ namespace :db do
...
@@ -26,5 +33,67 @@ namespace :db do
status:
true
status:
true
)
)
end
end
# Product list
# http://shopping.yahooapis.jp/ShoppingWebService/V1/itemSearch
list_url
=
'http://shopping.yahooapis.jp/ShoppingWebService/V1/itemSearch'
category_ids
.
each
do
|
category_id
|
request_url
=
list_url
+
'?appid='
+
YAHOO_SHOPPING_DATA_APP_ID
+
'&category_id='
+
category_id
# 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
)
i
=
0
doc
.
elements
.
each
(
'ResultSet/Result/Hit'
)
do
|
ele
|
next
if
ele
.
elements
[
'Name'
]
==
nil
i
+=
1
break
if
i
>
20
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_currency
=
ele
.
attributes
[
"currency"
]
price
=
ele
.
elements
[
'Price'
].
text
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
)
end
end
end
end
end
end
\ No newline at end of file
venshop_app/spec/models/product_spec.rb
0 → 100644
View file @
b7d37151
require
'spec_helper'
describe
Product
do
pending
"add some examples to (or delete)
#{
__FILE__
}
"
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