gurunavi 0.0.2 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Gemfile +0 -6
- data/Rakefile +2 -2
- data/VERSION +1 -1
- data/gurunavi.gemspec +8 -7
- data/lib/gurunavi/api_error.rb +56 -5
- data/lib/gurunavi/client.rb +7 -2
- data/lib/gurunavi/photos.rb +50 -1
- data/test/fixtures/errors/{error.json → 601_error.json} +0 -0
- data/test/fixtures/errors/{error_gnavi.json → 601_error_gnavi.json} +0 -0
- data/test/test_client.rb +0 -20
- data/test/test_errors.rb +67 -0
- data/test/test_photos.rb +3 -1
- metadata +7 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 52cc104865af1a3821ac74a30115293154046f87
|
4
|
+
data.tar.gz: 5b3a0a1fe1798b1de197666da2b9cd3113c52c2e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4c9c5357c178c9462244d4de6ba4b4064719d277a0a5b952528bd77bf55d02822d6728b0cdf440452b8942a4ec493cb99c581af7f2101da0bfd2734609af0d46
|
7
|
+
data.tar.gz: 5948be2c21d9354644ac4eba8025a48a88a113fbb47f33d432b2336fe406a54770f5dffa51c2451d14cb83d4e5f3cc2cd9999025cf73646f6403159ca61f113c
|
data/Gemfile
CHANGED
@@ -1,10 +1,4 @@
|
|
1
1
|
source "https://rubygems.org"
|
2
|
-
# Add dependencies required to use your gem here.
|
3
|
-
# Example:
|
4
|
-
# gem "activesupport", ">= 2.3.5"
|
5
|
-
|
6
|
-
# Add dependencies to develop your gem here.
|
7
|
-
# Include everything needed to run rake, tests, features, etc.
|
8
2
|
|
9
3
|
gem 'faraday', '~> 0.8'
|
10
4
|
gem 'faraday_middleware', '>= 0.8'
|
data/Rakefile
CHANGED
@@ -17,8 +17,8 @@ Jeweler::Tasks.new do |gem|
|
|
17
17
|
gem.name = "gurunavi"
|
18
18
|
gem.homepage = "http://github.com/kobayang/gurunavi"
|
19
19
|
gem.license = "MIT"
|
20
|
-
gem.summary = %Q{A
|
21
|
-
gem.description = %Q{A
|
20
|
+
gem.summary = %Q{A Ruby wrapper for the Gurunavi API}
|
21
|
+
gem.description = %Q{A Ruby wrapper for the Gurunavi API}
|
22
22
|
gem.email = "naokiod@gmail.com"
|
23
23
|
gem.authors = ["kobayang"]
|
24
24
|
# dependencies defined in Gemfile
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0
|
1
|
+
0.1.0
|
data/gurunavi.gemspec
CHANGED
@@ -2,17 +2,17 @@
|
|
2
2
|
# DO NOT EDIT THIS FILE DIRECTLY
|
3
3
|
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
4
|
# -*- encoding: utf-8 -*-
|
5
|
-
# stub: gurunavi 0.0
|
5
|
+
# stub: gurunavi 0.1.0 ruby lib
|
6
6
|
|
7
7
|
Gem::Specification.new do |s|
|
8
8
|
s.name = "gurunavi".freeze
|
9
|
-
s.version = "0.0
|
9
|
+
s.version = "0.1.0"
|
10
10
|
|
11
11
|
s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version=
|
12
12
|
s.require_paths = ["lib".freeze]
|
13
13
|
s.authors = ["kobayang".freeze]
|
14
|
-
s.date = "2017-02-
|
15
|
-
s.description = "A
|
14
|
+
s.date = "2017-02-12"
|
15
|
+
s.description = "A Ruby wrapper for the Gurunavi API".freeze
|
16
16
|
s.email = "naokiod@gmail.com".freeze
|
17
17
|
s.extra_rdoc_files = [
|
18
18
|
"LICENSE.txt",
|
@@ -42,8 +42,8 @@ Gem::Specification.new do |s|
|
|
42
42
|
"test/fixtures/areas/area_search.json",
|
43
43
|
"test/fixtures/categories/category_large_search.json",
|
44
44
|
"test/fixtures/categories/category_small_search.json",
|
45
|
-
"test/fixtures/errors/
|
46
|
-
"test/fixtures/errors/
|
45
|
+
"test/fixtures/errors/601_error.json",
|
46
|
+
"test/fixtures/errors/601_error_gnavi.json",
|
47
47
|
"test/fixtures/photos/photo_search.json",
|
48
48
|
"test/fixtures/prefs/pref_search.json",
|
49
49
|
"test/fixtures/rests/foreign_rest_search.json",
|
@@ -53,6 +53,7 @@ Gem::Specification.new do |s|
|
|
53
53
|
"test/test_areas.rb",
|
54
54
|
"test/test_categories.rb",
|
55
55
|
"test/test_client.rb",
|
56
|
+
"test/test_errors.rb",
|
56
57
|
"test/test_photos.rb",
|
57
58
|
"test/test_prefs.rb",
|
58
59
|
"test/test_rests.rb"
|
@@ -60,7 +61,7 @@ Gem::Specification.new do |s|
|
|
60
61
|
s.homepage = "http://github.com/kobayang/gurunavi".freeze
|
61
62
|
s.licenses = ["MIT".freeze]
|
62
63
|
s.rubygems_version = "2.5.2".freeze
|
63
|
-
s.summary = "A
|
64
|
+
s.summary = "A Ruby wrapper for the Gurunavi API".freeze
|
64
65
|
|
65
66
|
if s.respond_to? :specification_version then
|
66
67
|
s.specification_version = 4
|
data/lib/gurunavi/api_error.rb
CHANGED
@@ -1,16 +1,67 @@
|
|
1
1
|
module Gurunavi
|
2
|
-
class APIError < StandardError
|
3
2
|
|
3
|
+
class APIErrorFactory
|
4
|
+
class << self
|
5
|
+
# error responces document is bellow:
|
6
|
+
# http://api.gnavi.co.jp/api/manual/restsearch/#errors
|
7
|
+
ERRORS = {
|
8
|
+
"429" => "TooManyAccess",
|
9
|
+
"600" => "NoShop",
|
10
|
+
"601" => "InvalidAccess",
|
11
|
+
"602" => "InvalidShopNumber",
|
12
|
+
"603" => "InvalidType",
|
13
|
+
"604" => "InternalServerError",
|
14
|
+
}.freeze
|
15
|
+
|
16
|
+
def call_api_errors(code, message)
|
17
|
+
if ERRORS.include?(code.to_s)
|
18
|
+
return Gurunavi.const_get(ERRORS[code.to_s]).new(code, message)
|
19
|
+
end
|
20
|
+
# no definition status in ERRORS, return APIError
|
21
|
+
return Gurunavi::APIError.new(code, message)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
# Custom error class for rescuing from all Gurunavi API errors
|
27
|
+
class APIError < StandardError
|
4
28
|
attr_reader :code, :message
|
5
29
|
|
6
|
-
def initialize(
|
7
|
-
@code
|
8
|
-
@message =
|
30
|
+
def initialize(code, message)
|
31
|
+
@code = code
|
32
|
+
@message = message
|
9
33
|
end
|
10
34
|
|
11
35
|
def message
|
12
|
-
|
36
|
+
"#{@code} - #{@message}"
|
13
37
|
end
|
14
38
|
alias :to_s :message
|
15
39
|
end
|
40
|
+
|
41
|
+
# ClientError is base for api errors of client error
|
42
|
+
# Raised when Gurunavi returns a 4xx HTTP status code
|
43
|
+
class ClientError < APIError; end
|
44
|
+
|
45
|
+
# Raised when Gurunavi returns the HTTP status code 429
|
46
|
+
class TooManyAccess < ClientError; end
|
47
|
+
|
48
|
+
# ServerError is base for api errors of server error
|
49
|
+
# Raised when Gurunavi returns a 6xx HTTP status code
|
50
|
+
class ServerError < APIError; end
|
51
|
+
|
52
|
+
# Raised when Gurunavi returns the HTTP status code 600
|
53
|
+
class NoShop < ServerError; end
|
54
|
+
|
55
|
+
# Raised when Gurunavi returns the HTTP status code 601
|
56
|
+
class InvalidAccess < ServerError; end
|
57
|
+
|
58
|
+
# Raised when Gurunavi returns the HTTP status code 602
|
59
|
+
class InvalidShopNumber < ServerError; end
|
60
|
+
|
61
|
+
# Raised when Gurunavi returns the HTTP status code 603
|
62
|
+
class InvalidType < ServerError; end
|
63
|
+
|
64
|
+
# Raised when Gurunavi returns the HTTP status code 604
|
65
|
+
class InternalServerError < ServerError; end
|
66
|
+
|
16
67
|
end
|
data/lib/gurunavi/client.rb
CHANGED
@@ -68,8 +68,13 @@ module Gurunavi
|
|
68
68
|
#
|
69
69
|
# Added just for convenience to avoid having to traverse farther down the response just to get to returned data.
|
70
70
|
def return_error_or_body(response, response_body)
|
71
|
-
|
72
|
-
|
71
|
+
error_status = nil
|
72
|
+
error_status = response_body["error"] if response.body["error"]
|
73
|
+
error_status = response_body["gnavi"]["error"] if response.body["gnavi"] && response.body["gnavi"]["error"]
|
74
|
+
|
75
|
+
if error_status
|
76
|
+
raise Gurunavi::APIErrorFactory.call_api_errors(error_status.code, error_status.message)
|
77
|
+
end
|
73
78
|
response_body
|
74
79
|
end
|
75
80
|
|
data/lib/gurunavi/photos.rb
CHANGED
@@ -6,7 +6,56 @@ module Gurunavi
|
|
6
6
|
response = connection.get do |req|
|
7
7
|
req.url "PhotoSearchAPI/#{@api_version}", options
|
8
8
|
end
|
9
|
-
|
9
|
+
# if error happens, raise APIError from return_error_or_body function.
|
10
|
+
body = return_error_or_body(response, response.body.response)
|
11
|
+
map_photos_from_response(body)
|
12
|
+
end
|
13
|
+
|
14
|
+
# photo api json format is different from other api schemes.
|
15
|
+
# rest api response scheme:
|
16
|
+
# {
|
17
|
+
# "@attributes": {
|
18
|
+
# "api_version": "..."
|
19
|
+
# },
|
20
|
+
# "total_hit_count": "...",
|
21
|
+
# "hit_per_page": "...",
|
22
|
+
# "page_offset": "...",
|
23
|
+
# "rest": [
|
24
|
+
# ...
|
25
|
+
# ]
|
26
|
+
# }
|
27
|
+
# }
|
28
|
+
|
29
|
+
# photo api response scheme:
|
30
|
+
# {
|
31
|
+
# "response": {
|
32
|
+
# "@attributes": {
|
33
|
+
# "api_version": "20150630"
|
34
|
+
# },
|
35
|
+
# "total_hit_count": 2,
|
36
|
+
# "hit_per_page": 2,
|
37
|
+
# "0": {
|
38
|
+
# "photo": {
|
39
|
+
# ...
|
40
|
+
# }
|
41
|
+
# },
|
42
|
+
# "1": ...
|
43
|
+
# }
|
44
|
+
# }
|
45
|
+
|
46
|
+
# therefore convert response for photos, it means convert response to that:
|
47
|
+
# [
|
48
|
+
# {photo hashie},
|
49
|
+
# ...
|
50
|
+
# ]
|
51
|
+
def map_photos_from_response(res)
|
52
|
+
index = 0
|
53
|
+
photos = Hashie::Array.new
|
54
|
+
while res.include?(index.to_s)
|
55
|
+
photos.push(res[index.to_s].photo)
|
56
|
+
index += 1
|
57
|
+
end
|
58
|
+
photos
|
10
59
|
end
|
11
60
|
|
12
61
|
end
|
File without changes
|
File without changes
|
data/test/test_client.rb
CHANGED
@@ -10,24 +10,4 @@ class TestGurunavi < Test::Unit::TestCase
|
|
10
10
|
@client.keyid.should == keyid_test
|
11
11
|
end
|
12
12
|
end
|
13
|
-
|
14
|
-
context "When returning a unsucessful response(error)" do
|
15
|
-
should "raise Gurunavi::Error with error.json" do
|
16
|
-
response = Faraday::Response.new(body: fixture_file('errors/error.json', parse: true))
|
17
|
-
client = Gurunavi::Client.new
|
18
|
-
|
19
|
-
assert_raises(Gurunavi::APIError) do
|
20
|
-
client.return_error_or_body(response, response.body)
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
should "raise Gurunavi::Error with gnavi error json" do
|
25
|
-
response = Faraday::Response.new(body: fixture_file('errors/error_gnavi.json', parse: true))
|
26
|
-
client = Gurunavi::Client.new
|
27
|
-
|
28
|
-
assert_raises(Gurunavi::APIError) do
|
29
|
-
client.return_error_or_body(response, response.body)
|
30
|
-
end
|
31
|
-
end
|
32
|
-
end
|
33
13
|
end
|
data/test/test_errors.rb
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class TestGurunavi < Test::Unit::TestCase
|
4
|
+
context "test raise API error with status code" do
|
5
|
+
should "raise TooManyAccess error with 429 status code" do
|
6
|
+
assert_raises(Gurunavi::TooManyAccess) do
|
7
|
+
raise Gurunavi::APIErrorFactory.call_api_errors(429, "")
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
should "raise NoShop error with 600 status code" do
|
12
|
+
assert_raises(Gurunavi::NoShop) do
|
13
|
+
raise Gurunavi::APIErrorFactory.call_api_errors(600, "")
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
should "raise InvalidAccess error with 601 status code" do
|
18
|
+
assert_raises(Gurunavi::InvalidAccess) do
|
19
|
+
raise Gurunavi::APIErrorFactory.call_api_errors(601, "")
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
should "raise InvalidShopNumber error with 602 status code" do
|
24
|
+
assert_raises(Gurunavi::InvalidShopNumber) do
|
25
|
+
raise Gurunavi::APIErrorFactory.call_api_errors(602, "")
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
should "raise InvalidType error with 603 status code" do
|
30
|
+
assert_raises(Gurunavi::InvalidType) do
|
31
|
+
raise Gurunavi::APIErrorFactory.call_api_errors(603, "")
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
should "raise InternalServerError error with 603 status code" do
|
36
|
+
assert_raises(Gurunavi::InternalServerError) do
|
37
|
+
raise Gurunavi::APIErrorFactory.call_api_errors(604, "")
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
should "raise APIError error with other status code" do
|
42
|
+
assert_raises(Gurunavi::APIError) do
|
43
|
+
raise Gurunavi::APIErrorFactory.call_api_errors(1000, "")
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
context "When returning a unsucessful response(error)" do
|
49
|
+
should "raise Gurunavi::InvalidAccess with error.json status is 601" do
|
50
|
+
response = Faraday::Response.new(body: fixture_file('errors/601_error.json', parse: true))
|
51
|
+
client = Gurunavi::Client.new
|
52
|
+
|
53
|
+
assert_raises(Gurunavi::InvalidAccess) do
|
54
|
+
client.return_error_or_body(response, response.body)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
should "raise Gurunavi::InvalidAccess with gnavi error json status is 601" do
|
59
|
+
response = Faraday::Response.new(body: fixture_file('errors/601_error_gnavi.json', parse: true))
|
60
|
+
client = Gurunavi::Client.new
|
61
|
+
|
62
|
+
assert_raises(Gurunavi::InvalidAccess) do
|
63
|
+
client.return_error_or_body(response, response.body)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
data/test/test_photos.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: gurunavi
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- kobayang
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-02-
|
11
|
+
date: 2017-02-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: faraday
|
@@ -198,7 +198,7 @@ dependencies:
|
|
198
198
|
- - ">="
|
199
199
|
- !ruby/object:Gem::Version
|
200
200
|
version: '0'
|
201
|
-
description: A
|
201
|
+
description: A Ruby wrapper for the Gurunavi API
|
202
202
|
email: naokiod@gmail.com
|
203
203
|
executables: []
|
204
204
|
extensions: []
|
@@ -229,8 +229,8 @@ files:
|
|
229
229
|
- test/fixtures/areas/area_search.json
|
230
230
|
- test/fixtures/categories/category_large_search.json
|
231
231
|
- test/fixtures/categories/category_small_search.json
|
232
|
-
- test/fixtures/errors/
|
233
|
-
- test/fixtures/errors/
|
232
|
+
- test/fixtures/errors/601_error.json
|
233
|
+
- test/fixtures/errors/601_error_gnavi.json
|
234
234
|
- test/fixtures/photos/photo_search.json
|
235
235
|
- test/fixtures/prefs/pref_search.json
|
236
236
|
- test/fixtures/rests/foreign_rest_search.json
|
@@ -240,6 +240,7 @@ files:
|
|
240
240
|
- test/test_areas.rb
|
241
241
|
- test/test_categories.rb
|
242
242
|
- test/test_client.rb
|
243
|
+
- test/test_errors.rb
|
243
244
|
- test/test_photos.rb
|
244
245
|
- test/test_prefs.rb
|
245
246
|
- test/test_rests.rb
|
@@ -266,5 +267,5 @@ rubyforge_project:
|
|
266
267
|
rubygems_version: 2.5.2
|
267
268
|
signing_key:
|
268
269
|
specification_version: 4
|
269
|
-
summary: A
|
270
|
+
summary: A Ruby wrapper for the Gurunavi API
|
270
271
|
test_files: []
|