api_bucket 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +19 -0
- data/.rspec +1 -0
- data/Gemfile +11 -0
- data/LICENSE.txt +22 -0
- data/README.md +54 -0
- data/Rakefile +1 -0
- data/VERSION +1 -0
- data/api-bucket.gemspec +23 -0
- data/lib/api_bucket/amazon/client/http.rb +61 -0
- data/lib/api_bucket/amazon/client.rb +73 -0
- data/lib/api_bucket/amazon/configration.rb +13 -0
- data/lib/api_bucket/amazon/item.rb +63 -0
- data/lib/api_bucket/amazon/response.rb +28 -0
- data/lib/api_bucket/amazon.rb +12 -0
- data/lib/api_bucket/base/client/http.rb +40 -0
- data/lib/api_bucket/base/element.rb +54 -0
- data/lib/api_bucket/base/item.rb +39 -0
- data/lib/api_bucket/base/response.rb +20 -0
- data/lib/api_bucket/itunes/client/http.rb +9 -0
- data/lib/api_bucket/itunes/client.rb +61 -0
- data/lib/api_bucket/itunes/configuration.rb +13 -0
- data/lib/api_bucket/itunes/item.rb +71 -0
- data/lib/api_bucket/itunes/response.rb +35 -0
- data/lib/api_bucket/itunes.rb +13 -0
- data/lib/api_bucket/rakuten/client/http.rb +9 -0
- data/lib/api_bucket/rakuten/client.rb +80 -0
- data/lib/api_bucket/rakuten/configuration.rb +13 -0
- data/lib/api_bucket/rakuten/item.rb +52 -0
- data/lib/api_bucket/rakuten/response.rb +32 -0
- data/lib/api_bucket/rakuten.rb +13 -0
- data/lib/api_bucket/version.rb +3 -0
- data/lib/api_bucket/yahooauction/client/http.rb +26 -0
- data/lib/api_bucket/yahooauction/client.rb +65 -0
- data/lib/api_bucket/yahooauction/configuration.rb +13 -0
- data/lib/api_bucket/yahooauction/item.rb +29 -0
- data/lib/api_bucket/yahooauction/response.rb +39 -0
- data/lib/api_bucket/yahooauction.rb +16 -0
- data/lib/api_bucket.rb +91 -0
- data/spec/api_bucket/amazon_spec.rb +66 -0
- data/spec/api_bucket/itunes_spec.rb +65 -0
- data/spec/api_bucket/rakuten_spec.rb +67 -0
- data/spec/api_bucket/yahooauction_spec.rb +69 -0
- data/spec/api_bucket_spec.rb +90 -0
- data/spec/secret.rb.skel +14 -0
- data/spec/spec_helper.rb +12 -0
- metadata +146 -0
@@ -0,0 +1,71 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
require "api_bucket/base/item"
|
3
|
+
|
4
|
+
module ApiBucket::Itunes
|
5
|
+
class Item < ApiBucket::Base::Item
|
6
|
+
def initialize(element)
|
7
|
+
|
8
|
+
# product_code
|
9
|
+
if element['wrapperType'] == 'collection'
|
10
|
+
@product_code = element['collectionId']
|
11
|
+
else
|
12
|
+
if element['trackId']
|
13
|
+
@product_code = element['trackId']
|
14
|
+
elsif element['collectionId']
|
15
|
+
@product_code = element['collectionId']
|
16
|
+
else
|
17
|
+
matches = element['trackViewUrl'].match(/\/id([0-9]+)/)
|
18
|
+
@product_code = matches[1]
|
19
|
+
end
|
20
|
+
end
|
21
|
+
@product_code = @product_code.to_s
|
22
|
+
|
23
|
+
# title
|
24
|
+
if element['collectionName']
|
25
|
+
@title = element['collectionName']
|
26
|
+
elsif element['trackName']
|
27
|
+
@title = element['trackName']
|
28
|
+
else
|
29
|
+
@title = element['artistName']
|
30
|
+
end
|
31
|
+
|
32
|
+
# detail_url
|
33
|
+
if element['collectionViewUrl']
|
34
|
+
url = element['collectionViewUrl']
|
35
|
+
elsif element['trackViewUrl']
|
36
|
+
url = element['trackViewUrl']
|
37
|
+
else
|
38
|
+
url = element['artistViewUrl']
|
39
|
+
end
|
40
|
+
@detail_url = url
|
41
|
+
|
42
|
+
# contents
|
43
|
+
@description = element['description']
|
44
|
+
|
45
|
+
# price
|
46
|
+
if element['collectionPrice']
|
47
|
+
@price = element['tcollectionPrice']
|
48
|
+
elsif element['trackPrice']
|
49
|
+
@price = element['trackPrice']
|
50
|
+
else
|
51
|
+
@price = element['price']
|
52
|
+
end
|
53
|
+
|
54
|
+
# release date
|
55
|
+
@release_date = element['release_date']
|
56
|
+
|
57
|
+
# image
|
58
|
+
@image = {}
|
59
|
+
if element.key?('artworkUrl512')
|
60
|
+
%w(s m l).collect {|key| @image[:"#{key}"] = {url: element['artworkUrl512'], width: 512, height: 512}}
|
61
|
+
elsif element.key?('artworkUrl100')
|
62
|
+
%w(s m l).collect {|key| @image[:"#{key}"] = {url: element['artworkUrl100'], width: 100, height: 100}}
|
63
|
+
else
|
64
|
+
%w(s m l).collect {|key| @image[:"#{key}"] = {url: element['artworkUrl60'], width: 60, height: 60}}
|
65
|
+
end
|
66
|
+
|
67
|
+
# PV
|
68
|
+
@preview_url = element['previewUrl']
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require "api_bucket/base/response"
|
2
|
+
|
3
|
+
module ApiBucket::Itunes
|
4
|
+
class Response < ApiBucket::Base::Response
|
5
|
+
|
6
|
+
def initialize(xml)
|
7
|
+
@doc = xml
|
8
|
+
end
|
9
|
+
|
10
|
+
def items
|
11
|
+
return [] if @doc.nil?
|
12
|
+
|
13
|
+
@items ||= @doc["results"].collect { |item| ApiBucket::Itunes::Item.new(item) }
|
14
|
+
end
|
15
|
+
|
16
|
+
def first_item
|
17
|
+
@items[0]
|
18
|
+
end
|
19
|
+
|
20
|
+
# Return current page no if :item_page option is when initiating the request.
|
21
|
+
def item_page
|
22
|
+
@item_page = 1
|
23
|
+
end
|
24
|
+
|
25
|
+
# Return total results.
|
26
|
+
def total_results
|
27
|
+
@total_results ||= @doc[:resultCount].to_i
|
28
|
+
end
|
29
|
+
|
30
|
+
# Return total pages.
|
31
|
+
def total_pages
|
32
|
+
@total_pages = 1
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
require 'api_bucket'
|
3
|
+
require 'open-uri'
|
4
|
+
require 'json'
|
5
|
+
|
6
|
+
require 'api_bucket/itunes/configuration'
|
7
|
+
require 'api_bucket/itunes/item'
|
8
|
+
require 'api_bucket/itunes/response'
|
9
|
+
require 'api_bucket/itunes/client'
|
10
|
+
|
11
|
+
module ApiBucket::Itunes
|
12
|
+
extend Configuration
|
13
|
+
end
|
@@ -0,0 +1,80 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
require "api_bucket/rakuten/client/http"
|
3
|
+
|
4
|
+
module ApiBucket::Rakuten
|
5
|
+
|
6
|
+
class Client
|
7
|
+
include ApiBucket::Rakuten::Client::Http
|
8
|
+
|
9
|
+
attr_accessor :page, :limit, :format, :application_id, :affiliate_id
|
10
|
+
|
11
|
+
REQUEST_URL = 'https://app.rakuten.co.jp/services/api/IchibaItem/Search/20120723'
|
12
|
+
|
13
|
+
def categories
|
14
|
+
{
|
15
|
+
:ALL => '----',
|
16
|
+
:'101240' => 'CD・DVD/BD・楽器',
|
17
|
+
|
18
|
+
:'100371' => 'レディースファッション',
|
19
|
+
:'551177' => 'メンズファッション',
|
20
|
+
:'558885' => '靴',
|
21
|
+
:'216131' => 'バッグ・小物・ブランド雑貨',
|
22
|
+
|
23
|
+
:'100804' => 'インテリア・寝具・収納',
|
24
|
+
:'211742' => '家電・AV・カメラ',
|
25
|
+
:'402853' => 'デジタルコンテンツ',
|
26
|
+
|
27
|
+
:'100227' => '食品',
|
28
|
+
:'100000' => '百貨店・総合通販・ギフト',
|
29
|
+
|
30
|
+
:'101381' => '旅行・出張・チケット',
|
31
|
+
:'100939' => '美容・コスメ・香水',
|
32
|
+
}
|
33
|
+
end
|
34
|
+
|
35
|
+
def initialize(options={})
|
36
|
+
options = ApiBucket::Rakuten.options.merge(options)
|
37
|
+
self.page = options[:page] || 1
|
38
|
+
self.limit = options[:limit] || 20
|
39
|
+
|
40
|
+
self.format = options[:format] || 'json'
|
41
|
+
self.application_id = options[:application_id]
|
42
|
+
self.affiliate_id = options[:affiliate_id]
|
43
|
+
end
|
44
|
+
|
45
|
+
def search(keywords, params={})
|
46
|
+
options = {
|
47
|
+
hits: self.limit,
|
48
|
+
page: self.page,
|
49
|
+
|
50
|
+
genreId: params[:search_index],
|
51
|
+
keyword: keywords,
|
52
|
+
|
53
|
+
applicationId: self.application_id,
|
54
|
+
affiliateId: self.affiliate_id,
|
55
|
+
}
|
56
|
+
options.merge!(params)
|
57
|
+
|
58
|
+
# delete no needed keys
|
59
|
+
options.delete(:keywords)
|
60
|
+
options.delete(:search_index)
|
61
|
+
options[:genreId] = '0' if options[:genreId] == 'All'
|
62
|
+
|
63
|
+
ApiBucket::Rakuten::Response.new(send_request(options, REQUEST_URL))
|
64
|
+
end
|
65
|
+
|
66
|
+
def lookup(id, params={})
|
67
|
+
options = {
|
68
|
+
itemCode: id,
|
69
|
+
operation: 'ItemCodeSearch',
|
70
|
+
version: '2010-08-05',
|
71
|
+
|
72
|
+
applicationId: self.application_id,
|
73
|
+
affiliateId: self.affiliate_id,
|
74
|
+
}
|
75
|
+
options.merge!(params)
|
76
|
+
|
77
|
+
ApiBucket::Rakuten::Response.new(send_request(options, REQUEST_URL))
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module ApiBucket::Rakuten
|
2
|
+
module Configuration
|
3
|
+
attr_accessor :page, :limit, :format, :application_id, :affiliate_id
|
4
|
+
|
5
|
+
def configure
|
6
|
+
yield self
|
7
|
+
end
|
8
|
+
|
9
|
+
def options
|
10
|
+
[:page, :limit, :format, :application_id, :affiliate_id].inject({}){|o,k| o.merge!(k => send(k))}
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
require "api_bucket/base/item"
|
3
|
+
|
4
|
+
module ApiBucket::Rakuten
|
5
|
+
class Item < ApiBucket::Base::Item
|
6
|
+
|
7
|
+
def initialize(element)
|
8
|
+
@product_code = element['itemCode']
|
9
|
+
@detail_url = element['itemUrl']
|
10
|
+
@preview_url = ''
|
11
|
+
@price = element['itemPrice']
|
12
|
+
@title = element['itemName']
|
13
|
+
|
14
|
+
# image
|
15
|
+
# ex(http://thumbnail.image.rakuten.co.jp/@0_mall/ajewelry/cabinet/cddvd15/vibl-501.jpg?_ex=64x64)
|
16
|
+
@image = {}
|
17
|
+
keys = {
|
18
|
+
l: 'mediumImageUrls',
|
19
|
+
m: 'mediumImageUrls',
|
20
|
+
s: 'smallImageUrls'
|
21
|
+
}
|
22
|
+
|
23
|
+
keys.each do |key, image_key|
|
24
|
+
if element[image_key]
|
25
|
+
matches = element[image_key].first['imageUrl'].match(/(.+)\?_ex=(\d+)x(\d+)/)
|
26
|
+
image_url_no_params = matches[1]
|
27
|
+
size = matches[2]
|
28
|
+
|
29
|
+
if key == :l
|
30
|
+
@image[key] = { url: image_url_no_params, width: 500, height: 500}
|
31
|
+
elsif key == :m
|
32
|
+
@image[key] = {
|
33
|
+
url: element[image_key].first['imageUrl'], width: size, height: size}
|
34
|
+
elsif key == :s
|
35
|
+
@image[key] = {url: element[image_key].first['imageUrl'], width: size, height: size}
|
36
|
+
end
|
37
|
+
else
|
38
|
+
@image[key] = {url: nil, width: 0, height: 0}
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
@description = element['itemCaption']
|
43
|
+
@release_date = ''
|
44
|
+
|
45
|
+
if element['availability'] == 1
|
46
|
+
@availablity = "販売可能"
|
47
|
+
else
|
48
|
+
@availablity = "販売不可能"
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require "api_bucket/base/response"
|
2
|
+
|
3
|
+
module ApiBucket::Rakuten
|
4
|
+
class Response < ApiBucket::Base::Response
|
5
|
+
|
6
|
+
def initialize(xml)
|
7
|
+
@doc = xml
|
8
|
+
end
|
9
|
+
|
10
|
+
def items
|
11
|
+
return [] if @doc.nil?
|
12
|
+
|
13
|
+
@items ||= @doc["Items"].map {|item| ApiBucket::Rakuten::Item.new(item['Item']) }
|
14
|
+
end
|
15
|
+
|
16
|
+
def first_item
|
17
|
+
@items[0]
|
18
|
+
end
|
19
|
+
|
20
|
+
def item_page
|
21
|
+
@item_page ||= @doc[:page].to_i
|
22
|
+
end
|
23
|
+
|
24
|
+
def total_results
|
25
|
+
@total_results ||= @doc[:count].to_i
|
26
|
+
end
|
27
|
+
|
28
|
+
def total_pages
|
29
|
+
@total_pages ||= @doc[:pageCount].to_i
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
require 'api_bucket'
|
3
|
+
require 'open-uri'
|
4
|
+
require 'json'
|
5
|
+
|
6
|
+
require 'api_bucket/rakuten/configuration'
|
7
|
+
require 'api_bucket/rakuten/item'
|
8
|
+
require 'api_bucket/rakuten/response'
|
9
|
+
require 'api_bucket/rakuten/client'
|
10
|
+
|
11
|
+
module ApiBucket::Rakuten
|
12
|
+
extend Configuration
|
13
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require "api_bucket/base/client/http"
|
2
|
+
|
3
|
+
module ApiBucket::Yahooauction
|
4
|
+
class Client
|
5
|
+
module Http
|
6
|
+
include ApiBucket::Base::Client::Http
|
7
|
+
|
8
|
+
def send_request(options, url)
|
9
|
+
request_url = "#{url}?#{prepare_query(options)}"
|
10
|
+
|
11
|
+
res = nil
|
12
|
+
begin
|
13
|
+
res = open(request_url, {}) do |f|
|
14
|
+
@raw_response = f.read
|
15
|
+
@raw_response.gsub!(/^loaded\((.+)\)/, '\1')
|
16
|
+
JSON.parse(@raw_response)
|
17
|
+
end
|
18
|
+
rescue => e
|
19
|
+
#p "#{e.message} : #{request_url}"
|
20
|
+
end
|
21
|
+
|
22
|
+
res
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
require "api_bucket/yahooauction/client/http"
|
3
|
+
|
4
|
+
module ApiBucket::Yahooauction
|
5
|
+
class Client
|
6
|
+
include ApiBucket::Yahooauction::Client::Http
|
7
|
+
|
8
|
+
attr_accessor :page, :limit, :appid
|
9
|
+
|
10
|
+
REQUEST_URL = 'http://auctions.yahooapis.jp/AuctionWebService/V2/json/search'
|
11
|
+
REQUEST_URL_ITEM = 'http://auctions.yahooapis.jp/AuctionWebService/V2/json/auctionItem'
|
12
|
+
|
13
|
+
def categories
|
14
|
+
{
|
15
|
+
:'----' => 'ALL',
|
16
|
+
:'25464' => 'おもちゃ、ゲーム',
|
17
|
+
:'24242' => 'ホビー、カルチャー',
|
18
|
+
:'20000' => 'アンティーク、コレクション',
|
19
|
+
:'23000' => 'ファッション',
|
20
|
+
:'42177' => 'ビューティー、ヘルスケア',
|
21
|
+
:'24198' => '住まい、インテリア',
|
22
|
+
:'23140' => 'アクセサリー、時計',
|
23
|
+
:'2084032594' => 'タレントグッズ',
|
24
|
+
:'2084043920' => 'チケット、金券、宿泊予約',
|
25
|
+
}
|
26
|
+
end
|
27
|
+
|
28
|
+
def initialize(options={})
|
29
|
+
options = ApiBucket::Yahooauction.options.merge(options)
|
30
|
+
self.page = options[:page] || 1
|
31
|
+
self.limit = options[:limit] || 20
|
32
|
+
|
33
|
+
self.appid = options[:appid]
|
34
|
+
end
|
35
|
+
|
36
|
+
def search(keywords, params={})
|
37
|
+
options = {
|
38
|
+
hits: self.limit,
|
39
|
+
page: self.page,
|
40
|
+
|
41
|
+
category: params[:search_index],
|
42
|
+
query: keywords,
|
43
|
+
appid: self.appid,
|
44
|
+
}
|
45
|
+
options.merge!(params)
|
46
|
+
|
47
|
+
# delete no needed keys
|
48
|
+
options.delete(:keywords)
|
49
|
+
options.delete(:search_index)
|
50
|
+
options[:category] = 'ALL' if options[:category].nil?
|
51
|
+
|
52
|
+
ApiBucket::Yahooauction::Response.new(send_request(options, REQUEST_URL))
|
53
|
+
end
|
54
|
+
|
55
|
+
def lookup(id, params={})
|
56
|
+
options = {
|
57
|
+
auctionID: id,
|
58
|
+
appid: self.appid
|
59
|
+
}
|
60
|
+
options.merge!(params)
|
61
|
+
|
62
|
+
ApiBucket::Yahooauction::Response.new(send_request(options, REQUEST_URL_ITEM))
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
require "api_bucket/base/item"
|
3
|
+
|
4
|
+
module ApiBucket::Yahooauction
|
5
|
+
class Item < ApiBucket::Base::Item
|
6
|
+
|
7
|
+
def initialize(element)
|
8
|
+
@product_code = element['AuctionID']
|
9
|
+
@title = element['Title']
|
10
|
+
@description = element['Description']
|
11
|
+
@detail_url = element['AuctionItemUrl']
|
12
|
+
|
13
|
+
if element['CurrentPrice']
|
14
|
+
@price = element['CurrentPrice']
|
15
|
+
elsif element['Price']
|
16
|
+
@price = element['Price']
|
17
|
+
end
|
18
|
+
|
19
|
+
@release_date = element['StartTime']
|
20
|
+
|
21
|
+
image_url = element['Image']
|
22
|
+
image_url = element['Img']['Image1'] if element['Img']
|
23
|
+
@image = {}
|
24
|
+
[:l, :m, :s].each do |key|
|
25
|
+
@image[key] = {url: image_url, width: 0, height: 0}
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require "api_bucket/base/response"
|
2
|
+
|
3
|
+
module ApiBucket::Yahooauction
|
4
|
+
class Response < ApiBucket::Base::Response
|
5
|
+
|
6
|
+
def initialize(xml)
|
7
|
+
@doc = xml
|
8
|
+
end
|
9
|
+
|
10
|
+
def items
|
11
|
+
return [] if @doc.nil?
|
12
|
+
|
13
|
+
if @doc['ResultSet']["Result"]['Item']
|
14
|
+
@items ||= @doc['ResultSet']["Result"]['Item'].map {|item| ApiBucket::Yahooauction::Item.new(item) }
|
15
|
+
else
|
16
|
+
@items = [ApiBucket::Yahooauction::Item.new(@doc['ResultSet']['Result'])]
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def first_item
|
21
|
+
@items[0]
|
22
|
+
end
|
23
|
+
|
24
|
+
# Return current page no if :item_page option is when initiating the request.
|
25
|
+
def item_page
|
26
|
+
@item_page ||= @doc[:page].to_i
|
27
|
+
end
|
28
|
+
|
29
|
+
# Return total results.
|
30
|
+
def total_results
|
31
|
+
@total_results ||= @doc[:totalResultsReturned].to_i
|
32
|
+
end
|
33
|
+
|
34
|
+
# Return total pages.
|
35
|
+
def total_pages
|
36
|
+
@total_pages ||= @doc[:totalResultsAvailable].to_i / @doc[:totalResultsAvailable]
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
require 'api_bucket'
|
3
|
+
require 'open-uri'
|
4
|
+
require 'json'
|
5
|
+
|
6
|
+
require 'api_bucket/yahooauction/configuration'
|
7
|
+
require 'api_bucket/yahooauction/item'
|
8
|
+
require 'api_bucket/yahooauction/response'
|
9
|
+
require 'api_bucket/yahooauction/client'
|
10
|
+
|
11
|
+
module ApiBucket::Yahooauction
|
12
|
+
extend Configuration
|
13
|
+
end
|
14
|
+
|
15
|
+
|
16
|
+
|
data/lib/api_bucket.rb
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
|
3
|
+
#--
|
4
|
+
# Copyright (c) 2012 Daichi Nakajima
|
5
|
+
#
|
6
|
+
# Permission is hereby granted, free of charge, to any person obtaining
|
7
|
+
# a copy of this software and associated documentation files (the
|
8
|
+
# "Software"), to deal in the Software without restriction, including
|
9
|
+
# without limitation the rights to use, copy, modify, merge, publish,
|
10
|
+
# distribute, sublicense, and/or sell copies of the Software, and to
|
11
|
+
# permit persons to whom the Software is furnished to do so, subject to
|
12
|
+
# the following conditions:
|
13
|
+
#
|
14
|
+
# The above copyright notice and this permission notice shall be
|
15
|
+
# included in all copies or substantial portions of the Software.
|
16
|
+
#
|
17
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
18
|
+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
19
|
+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
20
|
+
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
21
|
+
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
22
|
+
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
23
|
+
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
24
|
+
#++
|
25
|
+
require 'rubygems'
|
26
|
+
|
27
|
+
require 'nokogiri'
|
28
|
+
require 'net/http'
|
29
|
+
require 'hmac-sha2'
|
30
|
+
require 'base64'
|
31
|
+
require "api_bucket/version"
|
32
|
+
|
33
|
+
# ApiBucket
|
34
|
+
module ApiBucket
|
35
|
+
|
36
|
+
# Service
|
37
|
+
class Service
|
38
|
+
SERVICE_AMAZON = 0
|
39
|
+
SERVICE_YAHOOAUCTION = 2
|
40
|
+
SERVICE_RAKUTEN = 3
|
41
|
+
SERVICE_ITUNES = 4
|
42
|
+
SERVICE_FRUSTRATION = 5
|
43
|
+
|
44
|
+
@@services = {
|
45
|
+
amazon: SERVICE_AMAZON,
|
46
|
+
yahooauction: SERVICE_YAHOOAUCTION,
|
47
|
+
rakuten: SERVICE_RAKUTEN,
|
48
|
+
itunes: SERVICE_ITUNES,
|
49
|
+
frustration: SERVICE_FRUSTRATION,
|
50
|
+
}
|
51
|
+
|
52
|
+
def self.instance(type)
|
53
|
+
|
54
|
+
case type
|
55
|
+
when :amazon
|
56
|
+
service = ApiBucket::Amazon::Client.new
|
57
|
+
when :yahooauction
|
58
|
+
service = ApiBucket::Yahooauction::Client.new
|
59
|
+
when :rakuten
|
60
|
+
service = ApiBucket::Rakuten::Client.new
|
61
|
+
when :itunes
|
62
|
+
service = ApiBucket::Itunes::Client.new
|
63
|
+
when :frustration
|
64
|
+
else
|
65
|
+
raise ArgumentError, 'no api module'
|
66
|
+
end
|
67
|
+
|
68
|
+
service
|
69
|
+
end
|
70
|
+
|
71
|
+
def self.code(name)
|
72
|
+
raise ArgumentError, 'no api code' unless @@services.key?(:"#{name}")
|
73
|
+
|
74
|
+
@@services[:"#{name}"]
|
75
|
+
end
|
76
|
+
|
77
|
+
def self.name(code)
|
78
|
+
@@services.each do |k, v|
|
79
|
+
return k if v == code
|
80
|
+
end
|
81
|
+
|
82
|
+
raise ArgumentError, 'no api code'
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
module ApiBucket::Base end
|
88
|
+
module ApiBucket::Amazon end
|
89
|
+
module ApiBucket::Itunes end
|
90
|
+
module ApiBucket::Rakuten end
|
91
|
+
module ApiBucket::Yahooauction end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
3
|
+
|
4
|
+
require File.expand_path(File.dirname(__FILE__) + '/../../lib/api_bucket')
|
5
|
+
require File.expand_path(File.dirname(__FILE__) + '/../../lib/api_bucket/amazon')
|
6
|
+
require File.expand_path(File.dirname(__FILE__) + '/../../lib/api_bucket/rakuten')
|
7
|
+
require File.expand_path(File.dirname(__FILE__) + '/../../lib/api_bucket/yahooauction')
|
8
|
+
require File.expand_path(File.dirname(__FILE__) + '/../../spec/secret')
|
9
|
+
|
10
|
+
describe "ApiBucket::Amazon" do
|
11
|
+
|
12
|
+
describe '#search_item' do
|
13
|
+
it 'response is ApiBucket::Amazon::Response' do
|
14
|
+
service = ApiBucket::Amazon::Client.new
|
15
|
+
expect(service.search('ruby').class).to eq ApiBucket::Amazon::Response
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'item in response is ApiBucket::Amazon::Item' do
|
19
|
+
service = ApiBucket::Amazon::Client.new
|
20
|
+
expect(service.search('ruby').items[0].class).to eq ApiBucket::Amazon::Item
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'can search items' do
|
24
|
+
service = ApiBucket::Amazon::Client.new
|
25
|
+
response = service.search('ruby')
|
26
|
+
expect(response.items.count).to be > 2
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'is invalid setting' do
|
30
|
+
service = ApiBucket::Amazon::Client.new
|
31
|
+
expect(service.search(nil).items).to be_empty
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
describe '#lookup' do
|
36
|
+
before(:all) do
|
37
|
+
service = ApiBucket::Amazon::Client.new
|
38
|
+
@response = service.lookup('B0084WYCCI')
|
39
|
+
end
|
40
|
+
|
41
|
+
it 'is invalid setting' do
|
42
|
+
service = ApiBucket::Amazon::Client.new
|
43
|
+
expect(service.lookup(nil).items.first).to be_nil
|
44
|
+
end
|
45
|
+
|
46
|
+
it 'can lookup a item' do
|
47
|
+
expect(@response.items.count).to eq 1
|
48
|
+
end
|
49
|
+
|
50
|
+
it 'can get product_code' do
|
51
|
+
expect(@response.items.first.product_code).to match(/[a-zA-Z1-9]+/)
|
52
|
+
end
|
53
|
+
|
54
|
+
it 'can get detail_url' do
|
55
|
+
expect(@response.items.first.detail_url).to match(/[a-zA-Z1-9:\/]+/)
|
56
|
+
end
|
57
|
+
|
58
|
+
it 'can get images' do
|
59
|
+
expect(@response.items.first.image.count).to be > 2
|
60
|
+
end
|
61
|
+
|
62
|
+
it 'can get image' do
|
63
|
+
expect(@response.items.first.image[:l][:url]).to match(/[a-zA-Z1-9:\/]+/)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|