brainstem-adaptor 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
- SHA1:
3
- metadata.gz: 6746d8612cec0a387c002ad99c0a75615ef2c00c
4
- data.tar.gz: f9cc76d40aaa1bc0fc53461f6caa7e6e34bbfc68
2
+ SHA256:
3
+ metadata.gz: 0510a423223a9044e50d7233a1a9ac806e2608c207fc433b58bd06ca4cb1378f
4
+ data.tar.gz: a7577a5628c351ac34cc3e74f0f3338a271e4906d4c5f1ec0216bdf6dcaf743c
5
5
  SHA512:
6
- metadata.gz: fa62456fbccacbaa9236b7174acef004b4fdf101a85048432498d802bcf54b4910df7a11306de996467b5e5458ca284dd6d059929b3dc8a5cfc4396158940753
7
- data.tar.gz: 09887da73e019a9b9ab8bc9b7f2a10b270ca780dcf3aede4cca0be0f2951907106e995eeeb1acb23c1f86ba448505fbe0db72863ab419b1decea0e44b7e65534
6
+ metadata.gz: 593cc4ecc3ff7e99be5229a47c4cd94586b50622e42fc9dc567e45ab41cb0beaa92a0fb1e066c771736196a25ffda23cd2cb3be1f400fba8b830e1866e4b9bdd
7
+ data.tar.gz: e8dc4dc4e62f9a985e7fae69eac224ed6bc888aa479dcaf2ea6dcc4cdd012bb071bbae750486a66f7eddf0654287d40418f4f4445190f737573a4cb484070466
data/README.md CHANGED
@@ -1,6 +1,7 @@
1
1
  ## Ruby Brainstem Adaptor
2
2
  [![Build Status](https://travis-ci.org/einzige/brainstem-adaptor.svg?branch=master)](https://travis-ci.org/einzige/brainstem-adaptor)
3
3
  [![Dependency Status](https://gemnasium.com/einzige/brainstem-ruby.svg)](https://gemnasium.com/einzige/brainstem-ruby)
4
+ [![Gem Version](https://badge.fury.io/rb/brainstem-adaptor.svg)](http://badge.fury.io/rb/brainstem-adaptor)
4
5
 
5
6
  Brainstem Adaptor provides an easy to use interface for [Brainstem](https://github.com/mavenlink/brainstem) API.
6
7
 
@@ -95,4 +96,4 @@ response = BrainstemAdaptor::Response.new(response_data, BrainstemAdaptor::Speci
95
96
 
96
97
  ## License
97
98
 
98
- Brainstem Adaptor was created by Mavenlink, Inc. and are available under the MIT License.
99
+ Brainstem Adaptor was created by Mavenlink, Inc. and available under the MIT License.
@@ -2,12 +2,12 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{brainstem-adaptor}
5
- s.version = "0.0.3"
5
+ s.version = "0.0.4"
6
6
 
7
7
  s.date = %q{2014-03-10}
8
- s.authors = ["Sergei Zinin (einzige)"]
9
- s.email = %q{szinin@gmail.com}
10
- s.homepage = %q{http://github.com/einzige/brainstem-adaptor}
8
+ s.authors = ["Mavenlink", "Sergei Zinin (einzige)"]
9
+ s.email = ["opensource@mavenlink.com", "szinin@gmail.com"]
10
+ s.homepage = %q{https://github.com/mavenlink/brainstem-adaptor}
11
11
 
12
12
  s.licenses = ["MIT"]
13
13
 
@@ -7,10 +7,11 @@ require 'brainstem_adaptor/specification'
7
7
  require 'brainstem_adaptor/association'
8
8
  require 'brainstem_adaptor/record'
9
9
  require 'brainstem_adaptor/invalid_response_error'
10
+ require 'brainstem_adaptor/parsers/array_parser'
10
11
  require 'brainstem_adaptor/response'
11
12
 
12
13
  module BrainstemAdaptor
13
- VERSION = '0.0.3'
14
+ VERSION = '0.0.4'
14
15
 
15
16
  def self.parser
16
17
  @parser ||= JSON
@@ -0,0 +1,24 @@
1
+ module BrainstemAdaptor
2
+ module Parsers
3
+ module ArrayParser
4
+ # Parse irregular endpoints that return an array of objects instead of the standard JSON response
5
+ def self.parse(response_data, collection_name)
6
+ raise InvalidResponseError, "collection name is not specified" unless collection_name
7
+
8
+ {
9
+ "count" => response_data.count,
10
+ "results" => response_data.map do |obj|
11
+ {
12
+ "key" => collection_name,
13
+ "id" => obj["id"].to_s
14
+ }
15
+ end,
16
+ collection_name => response_data.reduce({}) do |hash, obj|
17
+ hash[obj["id"].to_s] = obj
18
+ hash
19
+ end
20
+ }
21
+ end
22
+ end
23
+ end
24
+ end
@@ -4,7 +4,7 @@ module BrainstemAdaptor
4
4
 
5
5
  # @param response_data [String, Hash]
6
6
  # @param specification [BrainstemAdaptor::Specification]
7
- def initialize(response_data, specification = BrainstemAdaptor.default_specification)
7
+ def initialize(response_data, specification = BrainstemAdaptor.default_specification, **options)
8
8
  @specification = specification or raise ArgumentError, 'Specification is not set'
9
9
 
10
10
  case response_data
@@ -12,6 +12,8 @@ module BrainstemAdaptor
12
12
  @response_data = BrainstemAdaptor.parser.parse(response_data)
13
13
  when Hash
14
14
  @response_data = response_data
15
+ when Array
16
+ @response_data = BrainstemAdaptor::Parsers::ArrayParser.parse(response_data, options[:collection_name])
15
17
  else
16
18
  raise ArgumentError, "Expected String, got #{@response_data.class.name}"
17
19
  end
@@ -0,0 +1,54 @@
1
+ require "spec_helper"
2
+
3
+ describe BrainstemAdaptor::Parsers::ArrayParser do
4
+ describe ".parse" do
5
+ subject { described_class.parse(response_data, collection_name) }
6
+
7
+ let(:response_data) do
8
+ [
9
+ {
10
+ "id" => "1",
11
+ "title" => "An object"
12
+ },
13
+ {
14
+ "id" => "2",
15
+ "title" => "A newer object"
16
+ }
17
+ ]
18
+ end
19
+
20
+ let(:collection_name) { "workspaces" }
21
+
22
+ let(:formatted_response) do
23
+ {
24
+ "count" => response_data.count,
25
+ "results" => [
26
+ {
27
+ "key" => collection_name,
28
+ "id" => response_data[0]["id"]
29
+ },
30
+ {
31
+ "key" => collection_name,
32
+ "id" => response_data[1]["id"]
33
+ }
34
+ ],
35
+ collection_name => {
36
+ response_data[0]["id"] => response_data[0],
37
+ response_data[1]["id"] => response_data[1]
38
+ }
39
+ }
40
+ end
41
+
42
+ it "formats the response to match brainstem" do
43
+ expect(subject).to eq formatted_response
44
+ end
45
+
46
+ context "when collection name is not given" do
47
+ let(:collection_name) { nil }
48
+
49
+ it "raises an InvalidResponseError" do
50
+ expect { subject }.to raise_error(BrainstemAdaptor::InvalidResponseError)
51
+ end
52
+ end
53
+ end
54
+ end
@@ -181,6 +181,17 @@ describe BrainstemAdaptor::Response do
181
181
  end
182
182
  end
183
183
 
184
+ context "when array input" do
185
+ subject { described_class.new(response_data, collection_name: collection_name) }
186
+ let(:response_data) { [{ "id" => "1" }] }
187
+ let(:collection_name) { "object" }
188
+
189
+ it "formats the response data using the array parser" do
190
+ expect(BrainstemAdaptor::Parsers::ArrayParser).to receive(:parse).with(response_data, collection_name).and_call_original
191
+ subject
192
+ end
193
+ end
194
+
184
195
  describe '#to_hash' do
185
196
  specify do
186
197
  expect(subject.to_hash).to eq(response_hash)
metadata CHANGED
@@ -1,9 +1,10 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: brainstem-adaptor
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
+ - Mavenlink
7
8
  - Sergei Zinin (einzige)
8
9
  autorequire:
9
10
  bindir: bin
@@ -14,40 +15,42 @@ dependencies:
14
15
  name: rspec
15
16
  requirement: !ruby/object:Gem::Requirement
16
17
  requirements:
17
- - - '>='
18
+ - - ">="
18
19
  - !ruby/object:Gem::Version
19
20
  version: '0'
20
21
  type: :development
21
22
  prerelease: false
22
23
  version_requirements: !ruby/object:Gem::Requirement
23
24
  requirements:
24
- - - '>='
25
+ - - ">="
25
26
  - !ruby/object:Gem::Version
26
27
  version: '0'
27
28
  - !ruby/object:Gem::Dependency
28
29
  name: activesupport
29
30
  requirement: !ruby/object:Gem::Requirement
30
31
  requirements:
31
- - - '>='
32
+ - - ">="
32
33
  - !ruby/object:Gem::Version
33
34
  version: 3.0.0
34
35
  type: :runtime
35
36
  prerelease: false
36
37
  version_requirements: !ruby/object:Gem::Requirement
37
38
  requirements:
38
- - - '>='
39
+ - - ">="
39
40
  - !ruby/object:Gem::Version
40
41
  version: 3.0.0
41
42
  description: Parses Brainstem responses, makes it convenient to organize access to
42
43
  your data.
43
- email: szinin@gmail.com
44
+ email:
45
+ - opensource@mavenlink.com
46
+ - szinin@gmail.com
44
47
  executables: []
45
48
  extensions: []
46
49
  extra_rdoc_files:
47
50
  - README.md
48
51
  files:
49
- - .gitignore
50
- - .travis.yml
52
+ - ".gitignore"
53
+ - ".travis.yml"
51
54
  - Gemfile
52
55
  - README.md
53
56
  - Rakefile
@@ -55,17 +58,19 @@ files:
55
58
  - lib/brainstem-adaptor.rb
56
59
  - lib/brainstem_adaptor/association.rb
57
60
  - lib/brainstem_adaptor/invalid_response_error.rb
61
+ - lib/brainstem_adaptor/parsers/array_parser.rb
58
62
  - lib/brainstem_adaptor/record.rb
59
63
  - lib/brainstem_adaptor/response.rb
60
64
  - lib/brainstem_adaptor/specification.rb
61
65
  - spec/lib/brainstem_adaptor/association_spec.rb
62
66
  - spec/lib/brainstem_adaptor/invalid_response_error_spec.rb
67
+ - spec/lib/brainstem_adaptor/parsers/array_parser_spec.rb
63
68
  - spec/lib/brainstem_adaptor/record_spec.rb
64
69
  - spec/lib/brainstem_adaptor/response_spec.rb
65
70
  - spec/lib/brainstem_adaptor/specification_spec.rb
66
71
  - spec/lib/brainstem_adaptor_spec.rb
67
72
  - spec/spec_helper.rb
68
- homepage: http://github.com/einzige/brainstem-adaptor
73
+ homepage: https://github.com/mavenlink/brainstem-adaptor
69
74
  licenses:
70
75
  - MIT
71
76
  metadata: {}
@@ -75,17 +80,16 @@ require_paths:
75
80
  - lib
76
81
  required_ruby_version: !ruby/object:Gem::Requirement
77
82
  requirements:
78
- - - '>='
83
+ - - ">="
79
84
  - !ruby/object:Gem::Version
80
85
  version: '0'
81
86
  required_rubygems_version: !ruby/object:Gem::Requirement
82
87
  requirements:
83
- - - '>='
88
+ - - ">="
84
89
  - !ruby/object:Gem::Version
85
90
  version: '0'
86
91
  requirements: []
87
- rubyforge_project:
88
- rubygems_version: 2.1.5
92
+ rubygems_version: 3.0.6
89
93
  signing_key:
90
94
  specification_version: 4
91
95
  summary: Brainstem API Adaptor