rakuten_web_service 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ MjUzYzEwODZhZmIwNGNhODVjNTE0NWEwZjk5ZGEwMGU5OGRjYWYwOA==
5
+ data.tar.gz: !binary |-
6
+ MzlkMjI0MGM3MzU4NTQ4NWQ1Y2VkN2EyMjdjMzVlNzA1NTM4YWVlYQ==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ MjliYzdiZGJiYjBkOTA2M2I0ZjNmN2FkNDYwZmYyY2E5ZmQ2MmY4OTIzN2Nh
10
+ MGQzMDUxMjQ0NGZhMDAxYmVjMzlmM2JhODE1OWU5NWQ4NmI4ZWJlNTkxZDI0
11
+ MjE1MDUwYzRmOGY2YzUyNjg5ZGYzMWZjMjk5NWFmNGZiNGZkMWQ=
12
+ data.tar.gz: !binary |-
13
+ MzQzOTM1MDY4NDM3ZmRiYzI2ZTRkYzcxOWQ1ZWUwZGNjMzRhNzUyYmI4MDQ4
14
+ ODc4MDRmNmJiMDFhMzliMWUyNTgyZDNmNDFkYzYxNTFiZTVmODIxYzAyOWU1
15
+ YWE1MGZlYTZlNWM5MGQzOTM3ZDEwMWMzYmVlNDFlZWRjNzNkMzM=
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ *.swp
4
+ .bundle
5
+ .config
6
+ .yardoc
7
+ Gemfile.lock
8
+ InstalledFiles
9
+ _yardoc
10
+ coverage
11
+ doc/
12
+ lib/bundler/man
13
+ pkg
14
+ rdoc
15
+ spec/reports
16
+ test/tmp
17
+ test/version_tmp
18
+ tmp
data/.travis.yml ADDED
@@ -0,0 +1,8 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.2
4
+ - 1.9.3
5
+ - 2.0.0
6
+ branches:
7
+ only:
8
+ - master
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rakuten_web_service.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Tatsuya Sato
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # RakutenWebService
2
+
3
+ [![Build Status](https://travis-ci.org/satoryu/rakuten_web_service.png?branch=master)](https://travis-ci.org/satoryu/rakuten_web_service)
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'rakuten_web_service'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install rakuten_web_service
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new do |t|
5
+ t.pattern = 'spec/**/*_spec.rb'
6
+ t.rspec_opts = '-c -fd'
7
+ end
8
+
9
+ task :rspec => :spec
10
+ task :default => :spec
@@ -0,0 +1,16 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rakuten_web_service'
4
+
5
+ application_id = ARGV.shift
6
+ keyword = ARGV[0..-1].join(' ')
7
+
8
+ RakutenWebService.configuration do |c|
9
+ c.application_id = application_id
10
+ end
11
+
12
+ items = RakutenWebService::Ichiba::Item.search(:keyword => keyword)
13
+
14
+ items.first(10).each do |item|
15
+ puts "#{item['itemName']}, #{item['itemPrice']} yen"
16
+ end
@@ -0,0 +1,13 @@
1
+ require 'rakuten_web_service/version'
2
+ require 'rakuten_web_service/configuration'
3
+ require 'rakuten_web_service/client'
4
+ require 'rakuten_web_service/search_result'
5
+ require 'rakuten_web_service/resource'
6
+ require 'rakuten_web_service/ichiba/item'
7
+ require 'rakuten_web_service/ichiba/genre'
8
+ require 'rakuten_web_service/ichiba/ranking'
9
+
10
+ module RakutenWebService
11
+ end
12
+
13
+ RWS = RakutenWebService
@@ -0,0 +1,39 @@
1
+ require 'faraday'
2
+ require 'faraday_middleware'
3
+
4
+ module RakutenWebService
5
+ class Client
6
+ attr_reader :url, :path
7
+
8
+ def initialize(endpoint)
9
+ url = URI.parse(endpoint)
10
+ @url = "#{url.scheme}://#{url.host}"
11
+ @path = url.path
12
+ end
13
+
14
+ def get(query)
15
+ query = RakutenWebService.configuration.generate_parameters.merge(query)
16
+ query = convert_snake_key_to_camel_key(query)
17
+ connection.get(path, query)
18
+ end
19
+
20
+ private
21
+ def connection
22
+ return @connection if @connection
23
+ @connection = Faraday.new(:url => url) do |conn|
24
+ conn.request :url_encoded
25
+ conn.response :json
26
+ conn.adapter Faraday.default_adapter
27
+ end
28
+ end
29
+
30
+ def convert_snake_key_to_camel_key(params)
31
+ converted = {}
32
+ params.each do |k, v|
33
+ k = k.to_s.gsub(/([a-z]+)_([a-z]+)/) { "#{$1}#{$2.capitalize}" }
34
+ converted[k] = v
35
+ end
36
+ return converted
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,22 @@
1
+ module RakutenWebService
2
+ class Configuration
3
+ attr_accessor :application_id, :affiliate_id
4
+
5
+ def generate_parameters
6
+ { :application_id => application_id, :affiliate_id => affiliate_id }
7
+ end
8
+ end
9
+
10
+ def configuration(&block)
11
+ @configuration ||= Configuration.new
12
+ if block
13
+ if block.arity != 1
14
+ raise ArgumentError, 'Block is required to have one argument'
15
+ end
16
+ block.call(@configuration)
17
+ end
18
+ return @configuration
19
+ end
20
+
21
+ module_function :configuration
22
+ end
@@ -0,0 +1,46 @@
1
+ module RakutenWebService
2
+ module Ichiba
3
+ class Genre < Resource
4
+ @@repository = {}
5
+
6
+ class << self
7
+ def parse_response(response)
8
+ [Genre.new(response['current'])]
9
+ end
10
+
11
+ def new(params)
12
+ case params
13
+ when Integer, String
14
+ Genre[params.to_s] || search(:genre_id => params.to_s).first
15
+ when Hash
16
+ super
17
+ end
18
+ end
19
+
20
+ def root
21
+ self.new(0)
22
+ end
23
+
24
+ def [](id)
25
+ @@repository[id.to_s]
26
+ end
27
+
28
+ def []=(id, genre)
29
+ @@repository[id.to_s] = genre
30
+ end
31
+ end
32
+
33
+ endpoint 'https://app.rakuten.co.jp/services/api/IchibaGenre/Search/20120723'
34
+ attribute :genreId, :genreName, :genreLevel
35
+
36
+ def initialize(params)
37
+ super
38
+ Genre[self.id.to_s] = self
39
+ end
40
+
41
+ def ranking(options={})
42
+ RakutenWebService::Ichiba::RankingItem.search(:genre_id => self.id)
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,36 @@
1
+ module RakutenWebService
2
+ module Ichiba
3
+ class Item < Resource
4
+ class << self
5
+ def parse_response(response)
6
+ response['Items'].map { |item| Item.new(item['Item']) }
7
+ end
8
+
9
+ def ranking(options)
10
+ RakutenWebService::Ichiba::RankingItem.search(options)
11
+ end
12
+ end
13
+
14
+ endpoint 'https://app.rakuten.co.jp/services/api/IchibaItem/Search/20130805'
15
+
16
+ attribute :itemName, :catchcopy, :itemCode, :itemPrice,
17
+ :itemCaption, :itemUrl, :affiliateUrl, :imageFlag,
18
+ :smallImageUrls, :mediumImageUrls,
19
+ :availability, :taxFlag,
20
+ :postageFlag, :creditCardFlag,
21
+ :shopOfTheYearFlag,
22
+ :shipOverseasFlag, :shipOverseasArea,
23
+ :asurakuFlag, :asurakuClosingTime, :asurakuArea,
24
+ :affiliateRate,
25
+ :startTime, :endTime,
26
+ :reviewCount, :reviewAverage,
27
+ :pointRate, :pointRateStartTime, :pointRateEndTime<
28
+ :shopName, :shopCode, :shopUrl,
29
+ :genreId
30
+
31
+ def genre
32
+ Genre[self.genre_id]
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,14 @@
1
+ require 'rakuten_web_service/ichiba/item'
2
+
3
+ module RakutenWebService
4
+ module Ichiba
5
+ class RankingItem < RakutenWebService::Ichiba::Item
6
+ endpoint 'https://app.rakuten.co.jp/services/api/IchibaItem/Ranking/20120927'
7
+ attribute :rank
8
+
9
+ def self.parse_response(response)
10
+ response['Items'].map { |item| RankingItem.new(item['Item']) }
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,46 @@
1
+ require 'rakuten_web_service/search_result'
2
+
3
+ module RakutenWebService
4
+ class Resource
5
+ class << self
6
+ def attribute(*attributes)
7
+ attributes.each do |attribute|
8
+ method_name = attribute.to_s.gsub(/([a-z]+)([A-Z]{1})/) do
9
+ "#{$1}_#{$2.downcase}"
10
+ end
11
+ method_name = method_name.sub(/^#{resource_name}_(\w+)$/) { $1 }
12
+ self.class_eval <<-CODE
13
+ def #{method_name}
14
+ @params['#{attribute.to_s}']
15
+ end
16
+ CODE
17
+ end
18
+ end
19
+
20
+ def search(options)
21
+ SearchResult.new(options, self, client)
22
+ end
23
+
24
+ def resource_name
25
+ self.name.split('::').last.downcase
26
+ end
27
+
28
+ def endpoint(url=nil)
29
+ @endpoint = url || @endpoint
30
+ end
31
+
32
+ def client
33
+ @client ||= RakutenWebService::Client.new(endpoint)
34
+ end
35
+ end
36
+
37
+ def initialize(params)
38
+ @params = params
39
+ end
40
+
41
+ def [](key)
42
+ camel_key = key.gsub(/([a-z]+)_(\w{1})/) { "#{$1}#{$2.capitalize}" }
43
+ @params[key] || @params[camel_key]
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,41 @@
1
+ module RakutenWebService
2
+ class SearchResult
3
+ include Enumerable
4
+
5
+ def initialize(params, resource_class, client)
6
+ @params = params
7
+ @resource_class = resource_class
8
+ @client = client
9
+ end
10
+
11
+ def each
12
+ if @results
13
+ @results.each do |item|
14
+ yield item
15
+ end
16
+ else
17
+ @results = []
18
+ params = @params
19
+ response = query
20
+ begin
21
+ resources = @resource_class.parse_response(response.body)
22
+ resources.each do |resource|
23
+ yield resource
24
+ @results << resource
25
+ end
26
+
27
+ if response.body['page'] && response.body['page'] < response.body['pageCount']
28
+ response = query(params.merge('page' => response.body['page'] + 1))
29
+ else
30
+ response = nil
31
+ end
32
+ end while(response)
33
+ end
34
+ end
35
+
36
+ private
37
+ def query(params=nil)
38
+ @client.get(params || @params)
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,3 @@
1
+ module RakutenWebService
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rakuten_web_service/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rakuten_web_service"
8
+ spec.version = RakutenWebService::VERSION
9
+ spec.authors = ["Tatsuya Sato"]
10
+ spec.email = ["tatsuya.b.sato@mail.rakuten.com"]
11
+ spec.description = %q{Ruby Client for Rakuten Web Service}
12
+ spec.summary = %q{Ruby Client for Rakuten Web Service}
13
+ spec.homepage = "http://webservice.rakuten.co.jp/"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency 'faraday', '~> 0.8.8'
22
+ spec.add_dependency 'faraday_middleware'
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.3"
25
+ spec.add_development_dependency "rake"
26
+ spec.add_development_dependency "webmock"
27
+ spec.add_development_dependency "rspec"
28
+ spec.add_development_dependency "tapp"
29
+ end