octoparts 0.0.4 → 0.0.5

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: 17d05e5117d19c25f74463c3aeae163725ddbd98
4
- data.tar.gz: 428bf3e2d09ef65d32b6358ac71432e30e5be179
3
+ metadata.gz: 2c9c0489fcb40ffc84c23c381544c54eb5f9b7fa
4
+ data.tar.gz: f5859dfaffceaa5a020beeae377b63b094d99403
5
5
  SHA512:
6
- metadata.gz: b02d084a1a122380bb16439b7a3a16e47fe7b3e3c68341a5ba73561bbeb834014b18f7755d4fbdd89b932d7ab597b355fc7045573cea312ccda740164e8a3065
7
- data.tar.gz: 5787486dca931f6d3c84ae53dca6c456857e87953f2be122c83b65487a60fa197ffe4a5ac48f0f6fd04d26554eba21533ba2b15e566743ebabfd568e15a83496
6
+ metadata.gz: ca03ed6a4484042a147b8faf8fa25cd7b2876819203aeaf5e05bd94395857e1b7850cb0285fc59c133782aa2d7cfdd6ab732b6cbc0d4e199ede84e3ed29a3733
7
+ data.tar.gz: 5c92e83194b68abde9ddc8e85964039a1e5d0f25642c594dd01572b0519e4fa8324fc58ec91ad671d7a27691799cc7a602a3a36099fa114f44e5e329959c252f
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.5
data/README.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # Octoparts
2
2
 
3
+ [![Gem Version](https://badge.fury.io/rb/octoparts.svg)](http://badge.fury.io/rb/octoparts)
4
+ [![Build Status](https://travis-ci.org/ma2gedev/octoparts-rb.svg)](https://travis-ci.org/ma2gedev/octoparts-rb)
5
+ [![Coverage Status](https://img.shields.io/coveralls/ma2gedev/octoparts-rb.svg)](https://coveralls.io/r/ma2gedev/octoparts-rb)
6
+ [![Code Climate](https://codeclimate.com/github/ma2gedev/octoparts-rb/badges/gpa.svg)](https://codeclimate.com/github/ma2gedev/octoparts-rb)
7
+ [![endorse](https://api.coderwall.com/ma2gedev/endorsecount.png)](https://coderwall.com/ma2gedev)
8
+
3
9
  Octoparts Client for Ruby
4
10
 
5
11
  Octoparts is the backend services aggregator. See more details [here](http://m3dev.github.io/octoparts/).
@@ -26,6 +32,8 @@ Or install it yourself as:
26
32
  # configuration
27
33
  Octoparts.configure do |config|
28
34
  config.endpoint = 'http://localhost:9000'
35
+ config.timeout_sec = 3 # open/read timeout in seconds
36
+ config.open_timeout_sec = 1 # connection open timeout in seconds
29
37
  end
30
38
 
31
39
  # create client
@@ -66,7 +74,7 @@ response = client.invoke(aggregate_request)
66
74
 
67
75
  ## Contributing
68
76
 
69
- 1. Fork it ( https://github.com/[my-github-username]/octoparts/fork )
77
+ 1. Fork it ( https://github.com/ma2gedev/octoparts/fork )
70
78
  2. Create your feature branch (`git checkout -b my-new-feature`)
71
79
  3. Commit your changes (`git commit -am 'Add some feature'`)
72
80
  4. Push to the branch (`git push origin my-new-feature`)
@@ -6,31 +6,24 @@ module Octoparts
6
6
  OCTOPARTS_API_ENDPOINT_PATH = '/octoparts/2'
7
7
  CACHE_API_ENDPOINT_PATH = "#{OCTOPARTS_API_ENDPOINT_PATH}/cache"
8
8
 
9
- def initialize(endpoint: nil, headers: {})
9
+ def initialize(endpoint: nil, headers: {}, timeout_sec: nil, open_timeout_sec: nil)
10
10
  @endpoint = endpoint || Octoparts.configuration.endpoint
11
+ @timeout_sec = timeout_sec || Octoparts.configuration.timeout_sec
12
+ @open_timeout_sec = open_timeout_sec || Octoparts.configuration.open_timeout_sec
11
13
  @headers = Octoparts.configuration.headers.merge(headers)
12
14
  end
13
15
 
14
- def get(path, params = nil, headers = {})
15
- process(:get, path, params, headers)
16
+ def get(path, params = {}, headers = {})
17
+ process(:get, path, params, nil, headers)
16
18
  end
17
19
 
18
- def post(path, params = nil, headers = {})
19
- process(:post, path, params, headers)
20
+ def post(path, body = nil, headers = {})
21
+ process(:post, path, {}, body, headers)
20
22
  end
21
23
 
22
24
  # TODO: doc
23
25
  def invoke(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
33
- body = aggregate_request.to_json(camelize: true)
26
+ body = create_request_body(params)
34
27
  headers = { content_type: 'application/json' }
35
28
  resp = post(OCTOPARTS_API_ENDPOINT_PATH, body, headers)
36
29
  Response.new(
@@ -72,15 +65,34 @@ module Octoparts
72
65
  )
73
66
  end
74
67
 
75
- def process(method, path, params, headers)
68
+ def process(method, path, params, body, headers)
76
69
  @connection ||= Faraday.new(url: @endpoint) do |connection|
77
70
  connection.adapter Faraday.default_adapter
78
71
  end
79
- response = @connection.send(method, path, params, @headers.merge(headers || {}))
72
+ response = @connection.send(method) do |request|
73
+ request.url(path, params)
74
+ request.body = body if body
75
+ request.headers.merge!(headers)
76
+ request.options.timeout = @timeout_sec if @timeout_sec
77
+ request.options.open_timeout = @open_timeout_sec if @open_timeout_sec
78
+ end
80
79
  if error = Octoparts::ResponseError.from_response(response)
81
80
  raise error
82
81
  end
83
82
  response
84
83
  end
84
+
85
+ def create_request_body(model)
86
+ aggregate_request = case model
87
+ when Hash
88
+ stringify_params = model.deep_stringify_keys
89
+ Model::AggregateRequest.new.extend(Representer::AggregateRequestRepresenter).from_hash(stringify_params)
90
+ when Octoparts::Model::AggregateRequest
91
+ model.extend(Representer::AggregateRequestRepresenter)
92
+ else
93
+ raise Octopart::ArgumentError
94
+ end
95
+ aggregate_request.to_json(camelize: true)
96
+ end
85
97
  end
86
98
  end
@@ -2,7 +2,7 @@ module Octoparts
2
2
  class Configuration
3
3
  USER_AGENT = "Octoparts client ruby/#{Octoparts::VERSION}"
4
4
 
5
- attr_accessor :endpoint, :headers
5
+ attr_accessor :endpoint, :headers, :timeout_sec, :open_timeout_sec
6
6
 
7
7
  def initialize
8
8
  # set default values
@@ -10,6 +10,8 @@ module Octoparts
10
10
  @headers = {
11
11
  'User-Agent' => USER_AGENT
12
12
  }
13
+ @timeout_sec = nil
14
+ @open_timeout_sec = nil
13
15
  end
14
16
  end
15
17
  end
@@ -1,3 +1,3 @@
1
1
  module Octoparts
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
@@ -28,4 +28,5 @@ Gem::Specification.new do |spec|
28
28
  spec.add_development_dependency "test-unit", "~> 3.0"
29
29
  spec.add_development_dependency "webmock"
30
30
  spec.add_development_dependency "vcr"
31
+ spec.add_development_dependency "coveralls"
31
32
  end
@@ -1,9 +1,12 @@
1
+ require 'coveralls'
2
+ Coveralls.wear!
3
+
1
4
  $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
5
  require 'octoparts'
3
6
 
4
7
  require 'pry'
5
8
  require 'test/unit'
6
- require 'webmock'
9
+ require 'webmock/test_unit'
7
10
  require 'vcr'
8
11
 
9
12
  VCR.configure do |c|
@@ -41,6 +41,7 @@ class TestClient < Test::Unit::TestCase
41
41
  part_request(part_id: 'echo').add_param('fooValue', 'test')
42
42
  end
43
43
  end
44
+ stub_request(:post, 'localhost:9000')
44
45
  response = @client.invoke(aggregate_request)
45
46
  body = response.body
46
47
  assert { body.class == Octoparts::Model::AggregateResponse }
@@ -49,6 +50,9 @@ class TestClient < Test::Unit::TestCase
49
50
  assert { body.responses.first.cache_control.class == Octoparts::Model::CacheControl }
50
51
  assert { body.responses.size == 1 }
51
52
  assert { body.responses.first.contents =~ /"test"/ }
53
+ assert_requested(:post, "http://localhost:9000/octoparts/2") do |req|
54
+ req.body == '{"requestMeta":{"id":"test","timeout":500},"requests":[{"partId":"echo","params":[{"key":"fooValue","value":"test"}]}]}'
55
+ end
52
56
  end
53
57
  end
54
58
 
@@ -103,4 +107,27 @@ class TestClient < Test::Unit::TestCase
103
107
  end
104
108
  end
105
109
  end
110
+
111
+ sub_test_case "timeout" do
112
+ teardown do
113
+ Octoparts.configure do |c|
114
+ c.timeout_sec = nil
115
+ end
116
+ end
117
+
118
+ test "timeout_sec option" do
119
+ assert_raise Faraday::TimeoutError do
120
+ Octoparts::Client.new(timeout_sec: 0).get('/')
121
+ end
122
+ end
123
+
124
+ test "open_timeout_sec option" do
125
+ Octoparts.configure do |c|
126
+ c.timeout_sec = 0
127
+ end
128
+ assert_raise Faraday::TimeoutError do
129
+ Octoparts::Client.new.get('/')
130
+ end
131
+ end
132
+ end
106
133
  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.4
4
+ version: 0.0.5
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-08 00:00:00.000000000 Z
11
+ date: 2015-01-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: representable
@@ -136,6 +136,20 @@ dependencies:
136
136
  - - ">="
137
137
  - !ruby/object:Gem::Version
138
138
  version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: coveralls
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
139
153
  description: " Ruby client library for the Octoparts API "
140
154
  email:
141
155
  - takayuki.1229@gmail.com
@@ -144,6 +158,7 @@ extensions: []
144
158
  extra_rdoc_files: []
145
159
  files:
146
160
  - ".gitignore"
161
+ - ".travis.yml"
147
162
  - Gemfile
148
163
  - LICENSE.txt
149
164
  - README.md