octoparts 0.0.3 → 0.0.4

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: 158d78c413e947f7fd3f43a8a889e36e7569c2d8
4
- data.tar.gz: dad9e133b9fe95ecacc1cb9255a7feb14c6527b1
3
+ metadata.gz: 17d05e5117d19c25f74463c3aeae163725ddbd98
4
+ data.tar.gz: 428bf3e2d09ef65d32b6358ac71432e30e5be179
5
5
  SHA512:
6
- metadata.gz: ef61d18776ed079e6c6793ee627f30cf76575b90aecfaa6e38365d7bab1329ef05bbba4987ebdddc838ea92e3c160bc45117bc0018ae202edb6f875b7ff8b999
7
- data.tar.gz: 6db5751507046998c8599835684eb0ca8521937079b2b4f5a91398e6841c46e273c49a1342209038d0d4ef6b6717fd0cef6ed2825101162e2117bb0c6b56d5cf
6
+ metadata.gz: b02d084a1a122380bb16439b7a3a16e47fe7b3e3c68341a5ba73561bbeb834014b18f7755d4fbdd89b932d7ab597b355fc7045573cea312ccda740164e8a3065
7
+ data.tar.gz: 5787486dca931f6d3c84ae53dca6c456857e87953f2be122c83b65487a60fa197ffe4a5ac48f0f6fd04d26554eba21533ba2b15e566743ebabfd568e15a83496
data/README.md CHANGED
@@ -31,7 +31,7 @@ end
31
31
  # create client
32
32
  client = Octoparts::Client.new
33
33
 
34
- # invoke aggregate request
34
+ # invoke aggregate request with Hash
35
35
  response = client.invoke({
36
36
  request_meta: {
37
37
  id: "test",
@@ -52,6 +52,16 @@ response = client.invoke({
52
52
 
53
53
  response.status
54
54
  response.body.responses.first.contents
55
+
56
+ # invoke with builder
57
+ aggregate_request = Octoparts.build_aggregate_request do
58
+ request_meta(id: 'test', timeout: 500)
59
+ requests do
60
+ part_request(part_id: 'echo').add_param('fooValue', 'test')
61
+ end
62
+ end
63
+ response = client.invoke(aggregate_request)
64
+
55
65
  ```
56
66
 
57
67
  ## Contributing
@@ -6,6 +6,7 @@ require "octoparts/client"
6
6
  require "octoparts/model"
7
7
  require "octoparts/representer"
8
8
  require "octoparts/response"
9
+ require "octoparts/builder"
9
10
 
10
11
  module Octoparts
11
12
  class << self
@@ -16,5 +17,10 @@ module Octoparts
16
17
  def configure(&block)
17
18
  configuration.instance_eval(&block)
18
19
  end
20
+
21
+ def build_aggregate_request(&block)
22
+ builder = Octoparts::AggregateRequestBuilder.new(&block)
23
+ builder.build
24
+ end
19
25
  end
20
26
  end
@@ -0,0 +1,32 @@
1
+ module Octoparts
2
+ class AggregateRequestBuilder
3
+ def initialize(&block)
4
+ self.instance_eval(&block)
5
+ end
6
+
7
+ def request_meta(params)
8
+ @request_meta = Octoparts::Model::RequestMeta.new
9
+ params.each do |key, value|
10
+ @request_meta.send("#{key}=", value)
11
+ end
12
+ end
13
+
14
+ def requests(&block)
15
+ self.instance_eval(&block)
16
+ end
17
+
18
+ def part_request(params)
19
+ @requests ||= []
20
+ part_request = Octoparts::Model::PartRequest.new
21
+ params.each do |key, value|
22
+ part_request.send("#{key}=", value)
23
+ end
24
+ @requests << part_request
25
+ part_request
26
+ end
27
+
28
+ def build
29
+ Octoparts::Model::AggregateRequest.create(@request_meta, @requests)
30
+ end
31
+ end
32
+ end
@@ -21,8 +21,15 @@ module Octoparts
21
21
 
22
22
  # TODO: doc
23
23
  def invoke(params)
24
- stringify_params = params.deep_stringify_keys
25
- aggregate_request = Model::AggregateRequest.new.extend(Representer::AggregateRequestRepresenter).from_hash(stringify_params)
24
+ aggregate_request = case params
25
+ when Hash
26
+ stringify_params = params.deep_stringify_keys
27
+ Model::AggregateRequest.new.extend(Representer::AggregateRequestRepresenter).from_hash(stringify_params)
28
+ when Octoparts::Model::AggregateRequest
29
+ params.extend(Representer::AggregateRequestRepresenter)
30
+ else
31
+ raise Octopart::ArgumentError
32
+ end
26
33
  body = aggregate_request.to_json(camelize: true)
27
34
  headers = { content_type: 'application/json' }
28
35
  resp = post(OCTOPARTS_API_ENDPOINT_PATH, body, headers)
@@ -70,7 +77,7 @@ module Octoparts
70
77
  connection.adapter Faraday.default_adapter
71
78
  end
72
79
  response = @connection.send(method, path, params, @headers.merge(headers || {}))
73
- if error = Octoparts::Error.from_response(response)
80
+ if error = Octoparts::ResponseError.from_response(response)
74
81
  raise error
75
82
  end
76
83
  response
@@ -1,5 +1,7 @@
1
1
  module Octoparts
2
- class Error < StandardError
2
+ class Error < StandardError; end
3
+
4
+ class ResponseError < Error
3
5
  attr_reader :response
4
6
 
5
7
  def self.from_response(response)
@@ -22,6 +24,7 @@ module Octoparts
22
24
  end
23
25
  end
24
26
 
25
- class ClientError < Error; end
26
- class ServerError < Error; end
27
+ class ClientError < ResponseError; end
28
+ class ServerError < ResponseError; end
29
+ class ArgumentError < Error; end
27
30
  end
@@ -2,6 +2,13 @@ module Octoparts
2
2
  module Model
3
3
  class AggregateRequest
4
4
  attr_accessor :request_meta, :requests
5
+
6
+ def self.create(request_meta, requests)
7
+ new.tap do |s|
8
+ s.request_meta = request_meta
9
+ s.requests = requests
10
+ end
11
+ end
5
12
  end
6
13
  end
7
14
  end
@@ -2,6 +2,12 @@ module Octoparts
2
2
  module Model
3
3
  class PartRequest
4
4
  attr_accessor :part_id, :id, :params
5
+
6
+ def add_param(key, value)
7
+ @params ||= []
8
+ @params << Octoparts::Model::PartRequestParam.create(key, value)
9
+ self
10
+ end
5
11
  end
6
12
  end
7
13
  end
@@ -2,6 +2,13 @@ module Octoparts
2
2
  module Model
3
3
  class PartRequestParam
4
4
  attr_accessor :key, :value
5
+
6
+ def self.create(key, value)
7
+ new.tap do |s|
8
+ s.key = key
9
+ s.value = value
10
+ end
11
+ end
5
12
  end
6
13
  end
7
14
  end
@@ -1,3 +1,3 @@
1
1
  module Octoparts
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  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.3
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
+ - '310'
27
+ body:
28
+ encoding: UTF-8
29
+ string: '{"responseMeta":{"id":"test","processTime":55},"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: Thu, 08 Jan 2015 14:28:44 GMT
33
+ recorded_with: VCR 2.9.3
File without changes
@@ -33,6 +33,25 @@ class TestClient < Test::Unit::TestCase
33
33
  end
34
34
  end
35
35
 
36
+ test "normal invoke with AggregateRequest model" do
37
+ VCR.use_cassette 'invoke_with_aggregate_request' do
38
+ aggregate_request = Octoparts.build_aggregate_request do
39
+ request_meta(id: 'test', timeout: 500)
40
+ requests do
41
+ part_request(part_id: 'echo').add_param('fooValue', 'test')
42
+ end
43
+ end
44
+ response = @client.invoke(aggregate_request)
45
+ body = response.body
46
+ assert { body.class == Octoparts::Model::AggregateResponse }
47
+ assert { body.response_meta.class == Octoparts::Model::ResponseMeta }
48
+ assert { body.responses.first.class == Octoparts::Model::PartResponse }
49
+ assert { body.responses.first.cache_control.class == Octoparts::Model::CacheControl }
50
+ assert { body.responses.size == 1 }
51
+ assert { body.responses.first.contents =~ /"test"/ }
52
+ end
53
+ end
54
+
36
55
  test "normal invoke when 2 requests" do
37
56
  VCR.use_cassette 'invoke_with_2_requests' do
38
57
  response = @client.invoke({
@@ -8,4 +8,11 @@ class TestOctoparts < Test::Unit::TestCase
8
8
  def test_it_does_something_useful
9
9
  assert { ['exist'].empty? == false }
10
10
  end
11
+
12
+ test "Octoparts.create_aggregate_request" do
13
+ aggregate_request = Octoparts.build_aggregate_request do
14
+ request_meta(id: 'id')
15
+ end
16
+ assert { aggregate_request.request_meta.class == Octoparts::Model::RequestMeta }
17
+ end
11
18
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: octoparts
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Takayuki Matsubara
@@ -149,6 +149,7 @@ files:
149
149
  - README.md
150
150
  - Rakefile
151
151
  - lib/octoparts.rb
152
+ - lib/octoparts/builder.rb
152
153
  - lib/octoparts/client.rb
153
154
  - lib/octoparts/configuration.rb
154
155
  - lib/octoparts/error.rb
@@ -170,9 +171,11 @@ files:
170
171
  - octoparts.gemspec
171
172
  - test/fixtures/vcr/invoke_example.yml
172
173
  - test/fixtures/vcr/invoke_with_2_requests.yml
174
+ - test/fixtures/vcr/invoke_with_aggregate_request.yml
173
175
  - test/fixtures/vcr/invoke_with_invalid_parameters.yml
174
176
  - test/helper.rb
175
177
  - test/representer/test_aggregate_request_representer.rb
178
+ - test/test_builder.rb
176
179
  - test/test_client.rb
177
180
  - test/test_octoparts.rb
178
181
  homepage: https://github.com/ma2gedev/octoparts-rb
@@ -202,8 +205,10 @@ summary: Ruby client for the Octoparts API
202
205
  test_files:
203
206
  - test/fixtures/vcr/invoke_example.yml
204
207
  - test/fixtures/vcr/invoke_with_2_requests.yml
208
+ - test/fixtures/vcr/invoke_with_aggregate_request.yml
205
209
  - test/fixtures/vcr/invoke_with_invalid_parameters.yml
206
210
  - test/helper.rb
207
211
  - test/representer/test_aggregate_request_representer.rb
212
+ - test/test_builder.rb
208
213
  - test/test_client.rb
209
214
  - test/test_octoparts.rb