octoparts 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 13fef2aab30cd287c94672a9bdc9ce0ab4b1edd4
4
- data.tar.gz: 6c41cb1d2aa6883b9d66dbb48afc3b6440fcd7ab
3
+ metadata.gz: 158d78c413e947f7fd3f43a8a889e36e7569c2d8
4
+ data.tar.gz: dad9e133b9fe95ecacc1cb9255a7feb14c6527b1
5
5
  SHA512:
6
- metadata.gz: 4a55dcf838978766b8a5b048979369fa68671ce94968aaa35403c9bafe2caf586cd811f4a3edd085c68ea67e78713d1e243b1d34c272a9fba420b9baad72c353
7
- data.tar.gz: 589fcf6a4364f88b6e7d53093d6aef59d59ef95cd32d5e063de00cb8fb5edde1c3edb7b34d517d5c4203a6b2aac1c8e956c151bf6fb15a3d5a0857ffebfac0ca
6
+ metadata.gz: ef61d18776ed079e6c6793ee627f30cf76575b90aecfaa6e38365d7bab1329ef05bbba4987ebdddc838ea92e3c160bc45117bc0018ae202edb6f875b7ff8b999
7
+ data.tar.gz: 6db5751507046998c8599835684eb0ca8521937079b2b4f5a91398e6841c46e273c49a1342209038d0d4ef6b6717fd0cef6ed2825101162e2117bb0c6b56d5cf
data/README.md CHANGED
@@ -33,18 +33,20 @@ client = Octoparts::Client.new
33
33
 
34
34
  # invoke aggregate request
35
35
  response = client.invoke({
36
- "request_meta" => {
37
- "id" => "test",
38
- "timeout" => 500
36
+ request_meta: {
37
+ id: "test",
38
+ timeout: 500
39
39
  },
40
- "requests" => [
41
- "part_id" => "echo",
42
- "params" => [
43
- {
44
- "key" => "fooValue",
45
- "value" => "test"
46
- }
47
- ]
40
+ requests: [
41
+ {
42
+ part_id: "echo",
43
+ params: [
44
+ {
45
+ key: "fooValue",
46
+ value: "test"
47
+ }
48
+ ]
49
+ }
48
50
  ]
49
51
  })
50
52
 
@@ -69,7 +69,11 @@ module Octoparts
69
69
  @connection ||= Faraday.new(url: @endpoint) do |connection|
70
70
  connection.adapter Faraday.default_adapter
71
71
  end
72
- @connection.send(method, path, params, @headers.merge(headers || {}))
72
+ response = @connection.send(method, path, params, @headers.merge(headers || {}))
73
+ if error = Octoparts::Error.from_response(response)
74
+ raise error
75
+ end
76
+ response
73
77
  end
74
78
  end
75
79
  end
@@ -0,0 +1,27 @@
1
+ module Octoparts
2
+ class Error < StandardError
3
+ attr_reader :response
4
+
5
+ def self.from_response(response)
6
+ status = response.status.to_i
7
+
8
+ klass = case status
9
+ when 400..499 then Octoparts::ClientError
10
+ when 500..599 then Octoparts::ServerError
11
+ end
12
+ klass.new(response) unless klass.nil?
13
+ end
14
+
15
+ def initialize(response)
16
+ @response = response
17
+ super(error_message)
18
+ end
19
+
20
+ def error_message
21
+ "status: #{@response.status}, body: #{@response.body}"
22
+ end
23
+ end
24
+
25
+ class ClientError < Error; end
26
+ class ServerError < Error; end
27
+ end
@@ -1,3 +1,3 @@
1
1
  module Octoparts
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
data/lib/octoparts.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  require "faraday"
2
2
  require "octoparts/version"
3
3
  require "octoparts/configuration"
4
+ require "octoparts/error"
4
5
  require "octoparts/client"
5
6
  require "octoparts/model"
6
7
  require "octoparts/representer"
data/octoparts.gemspec CHANGED
@@ -26,4 +26,6 @@ Gem::Specification.new do |spec|
26
26
  spec.add_development_dependency "rake", "~> 10.0"
27
27
  spec.add_development_dependency "pry"
28
28
  spec.add_development_dependency "test-unit", "~> 3.0"
29
+ spec.add_development_dependency "webmock"
30
+ spec.add_development_dependency "vcr"
29
31
  end
@@ -0,0 +1,33 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: post
5
+ uri: http://localhost:9000/octoparts/2
6
+ body:
7
+ encoding: UTF-8
8
+ string: '{"requestMeta":{"id":"test","timeout":500},"requests":[{"partId":"echo","params":[{"key":"fooValue","value":"test"}]}]}'
9
+ headers:
10
+ User-Agent:
11
+ - Octoparts client ruby/0.0.2
12
+ Content-Type:
13
+ - application/json
14
+ Accept-Encoding:
15
+ - gzip;q=1.0,deflate;q=0.6,identity;q=0.3
16
+ Accept:
17
+ - "*/*"
18
+ response:
19
+ status:
20
+ code: 200
21
+ message: OK
22
+ headers:
23
+ Content-Type:
24
+ - application/json; charset=utf-8
25
+ Content-Length:
26
+ - '309'
27
+ body:
28
+ encoding: UTF-8
29
+ string: '{"responseMeta":{"id":"test","processTime":1},"responses":[{"partId":"echo","id":"echo","cookies":[],"statusCode":200,"mimeType":"application/json","charset":"ISO-8859-1","cacheControl":{"noStore":false,"noCache":false},"contents":"{\"foo\":
30
+ \"test\"}\n","warnings":[],"errors":[],"retrievedFromCache":true}]}'
31
+ http_version:
32
+ recorded_at: Wed, 07 Jan 2015 22:27:38 GMT
33
+ recorded_with: VCR 2.9.3
@@ -0,0 +1,34 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: post
5
+ uri: http://localhost:9000/octoparts/2
6
+ body:
7
+ encoding: UTF-8
8
+ string: '{"requestMeta":{"id":"test","timeout":500},"requests":[{"partId":"echo","params":[{"key":"fooValue","value":"test"}]},{"partId":"echo","params":[{"key":"fooValue","value":"hoge"}]}]}'
9
+ headers:
10
+ User-Agent:
11
+ - Octoparts client ruby/0.0.2
12
+ Content-Type:
13
+ - application/json
14
+ Accept-Encoding:
15
+ - gzip;q=1.0,deflate;q=0.6,identity;q=0.3
16
+ Accept:
17
+ - "*/*"
18
+ response:
19
+ status:
20
+ code: 200
21
+ message: OK
22
+ headers:
23
+ Content-Type:
24
+ - application/json; charset=utf-8
25
+ Content-Length:
26
+ - '559'
27
+ body:
28
+ encoding: UTF-8
29
+ string: '{"responseMeta":{"id":"test","processTime":10},"responses":[{"partId":"echo","id":"echo","cookies":[],"statusCode":200,"mimeType":"application/json","charset":"ISO-8859-1","cacheControl":{"noStore":false,"noCache":false},"contents":"{\"foo\":
30
+ \"test\"}\n","warnings":[],"errors":[],"retrievedFromCache":true},{"partId":"echo","id":"echo","cookies":[],"statusCode":200,"mimeType":"application/json","charset":"ISO-8859-1","cacheControl":{"noStore":false,"noCache":false},"contents":"{\"foo\":
31
+ \"hoge\"}\n","warnings":[],"errors":[],"retrievedFromCache":true}]}'
32
+ http_version:
33
+ recorded_at: Wed, 07 Jan 2015 22:48:47 GMT
34
+ recorded_with: VCR 2.9.3
@@ -0,0 +1,32 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: post
5
+ uri: http://localhost:9000/octoparts/2
6
+ body:
7
+ encoding: UTF-8
8
+ string: '{"requestMeta":{"timeout":500}}'
9
+ headers:
10
+ User-Agent:
11
+ - Octoparts client ruby/0.0.2
12
+ Content-Type:
13
+ - application/json
14
+ Accept-Encoding:
15
+ - gzip;q=1.0,deflate;q=0.6,identity;q=0.3
16
+ Accept:
17
+ - "*/*"
18
+ response:
19
+ status:
20
+ code: 400
21
+ message: Bad Request
22
+ headers:
23
+ Content-Type:
24
+ - text/plain; charset=utf-8
25
+ Content-Length:
26
+ - '27'
27
+ body:
28
+ encoding: UTF-8
29
+ string: Unrecognized request object
30
+ http_version:
31
+ recorded_at: Thu, 08 Jan 2015 05:57:31 GMT
32
+ recorded_with: VCR 2.9.3
data/test/helper.rb CHANGED
@@ -3,3 +3,11 @@ require 'octoparts'
3
3
 
4
4
  require 'pry'
5
5
  require 'test/unit'
6
+ require 'webmock'
7
+ require 'vcr'
8
+
9
+ VCR.configure do |c|
10
+ c.cassette_library_dir = 'test/fixtures/vcr'
11
+ c.hook_into :webmock
12
+ c.allow_http_connections_when_no_cassette = true
13
+ end
@@ -0,0 +1,87 @@
1
+ require 'helper'
2
+
3
+ class TestClient < Test::Unit::TestCase
4
+ def setup
5
+ @client = Octoparts::Client.new
6
+ end
7
+
8
+ sub_test_case "#invoke" do
9
+ test "normal invoke" do
10
+ VCR.use_cassette 'invoke_example' do
11
+ response = @client.invoke({
12
+ "request_meta" => {
13
+ "id" => "test",
14
+ "timeout" => 500
15
+ },
16
+ "requests" => [
17
+ "part_id" => "echo",
18
+ "params" => [
19
+ {
20
+ "key" => "fooValue",
21
+ "value" => "test"
22
+ }
23
+ ]
24
+ ]
25
+ })
26
+ body = response.body
27
+ assert { body.class == Octoparts::Model::AggregateResponse }
28
+ assert { body.response_meta.class == Octoparts::Model::ResponseMeta }
29
+ assert { body.responses.first.class == Octoparts::Model::PartResponse }
30
+ assert { body.responses.first.cache_control.class == Octoparts::Model::CacheControl }
31
+ assert { body.responses.size == 1 }
32
+ assert { body.responses.first.contents =~ /"test"/ }
33
+ end
34
+ end
35
+
36
+ test "normal invoke when 2 requests" do
37
+ VCR.use_cassette 'invoke_with_2_requests' do
38
+ response = @client.invoke({
39
+ request_meta: {
40
+ id: "test",
41
+ timeout: 500
42
+ },
43
+ requests: [
44
+ {
45
+ part_id: "echo",
46
+ params: [
47
+ {
48
+ key: "fooValue",
49
+ value: "test"
50
+ }
51
+ ]
52
+ },
53
+ {
54
+ part_id: "echo",
55
+ params: [
56
+ {
57
+ key: "fooValue",
58
+ value: "hoge"
59
+ }
60
+ ]
61
+ }
62
+ ]
63
+ })
64
+ body = response.body
65
+ assert { body.class == Octoparts::Model::AggregateResponse }
66
+ assert { body.response_meta.class == Octoparts::Model::ResponseMeta }
67
+ assert { body.responses.first.class == Octoparts::Model::PartResponse }
68
+ assert { body.responses.first.cache_control.class == Octoparts::Model::CacheControl }
69
+ assert { body.responses.size == 2 }
70
+ assert { body.responses.first.contents =~ /"test"/ }
71
+ assert { body.responses.last.contents =~ /"hoge"/ }
72
+ end
73
+ end
74
+
75
+ test "invalid parameters" do
76
+ VCR.use_cassette 'invoke_with_invalid_parameters' do
77
+ assert_raise Octoparts::ClientError do
78
+ response = @client.invoke({
79
+ "request_meta" => {
80
+ "timeout" => 500
81
+ }
82
+ })
83
+ end
84
+ end
85
+ end
86
+ end
87
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: octoparts
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Takayuki Matsubara
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-01-07 00:00:00.000000000 Z
11
+ date: 2015-01-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: representable
@@ -108,6 +108,34 @@ dependencies:
108
108
  - - "~>"
109
109
  - !ruby/object:Gem::Version
110
110
  version: '3.0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: webmock
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: vcr
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
111
139
  description: " Ruby client library for the Octoparts API "
112
140
  email:
113
141
  - takayuki.1229@gmail.com
@@ -123,6 +151,7 @@ files:
123
151
  - lib/octoparts.rb
124
152
  - lib/octoparts/client.rb
125
153
  - lib/octoparts/configuration.rb
154
+ - lib/octoparts/error.rb
126
155
  - lib/octoparts/model.rb
127
156
  - lib/octoparts/model/aggregate_request.rb
128
157
  - lib/octoparts/model/aggregate_response.rb
@@ -139,8 +168,12 @@ files:
139
168
  - lib/octoparts/response.rb
140
169
  - lib/octoparts/version.rb
141
170
  - octoparts.gemspec
171
+ - test/fixtures/vcr/invoke_example.yml
172
+ - test/fixtures/vcr/invoke_with_2_requests.yml
173
+ - test/fixtures/vcr/invoke_with_invalid_parameters.yml
142
174
  - test/helper.rb
143
175
  - test/representer/test_aggregate_request_representer.rb
176
+ - test/test_client.rb
144
177
  - test/test_octoparts.rb
145
178
  homepage: https://github.com/ma2gedev/octoparts-rb
146
179
  licenses:
@@ -167,6 +200,10 @@ signing_key:
167
200
  specification_version: 4
168
201
  summary: Ruby client for the Octoparts API
169
202
  test_files:
203
+ - test/fixtures/vcr/invoke_example.yml
204
+ - test/fixtures/vcr/invoke_with_2_requests.yml
205
+ - test/fixtures/vcr/invoke_with_invalid_parameters.yml
170
206
  - test/helper.rb
171
207
  - test/representer/test_aggregate_request_representer.rb
208
+ - test/test_client.rb
172
209
  - test/test_octoparts.rb