loginator 0.0.4 → 0.0.6

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: 786d218c02c6f8d91065e156d7b6d1f417870fb6
4
- data.tar.gz: 3f9fb43095603359294a611e5ceda12214a39a55
3
+ metadata.gz: e096d117d2fc6ad0d615b29e0c99a28f74e58835
4
+ data.tar.gz: faab9d2862bdf8a389628ed91978d63be0d25f2c
5
5
  SHA512:
6
- metadata.gz: e5dbe147241c25a0e2f55a6abbb6ec320d0f0e0f7146a6adec0c40ad474d4c4fd0ec528e0bfbfc4dab9fbc5cb6abe3ef081152f219cd7cee05bbeee95c9b54bf
7
- data.tar.gz: 28d1dae5fc3242317d06a335a961718439aa28b54ba4b2f7f0980f03ff250d006c5ea6b9f85a3309cd2fb347a5cb27db4965fb6873b5f1a7f21e66ab638bbba9
6
+ metadata.gz: daec5f46f2cbc7096182e1316058c1356578bb28080f05a6b526e72dbecd071ed99ff7d4078259237be029b787f952cc40f1be94307f95f2192b3ed75cdd597c
7
+ data.tar.gz: 271a9200bdf43ba6cfb1c06514f159df266d9474049fd4ac71d3ba6f0ba788de962ca344c274e64ed2102810c9fdf0f0606614da87432120433022c544c191a9
@@ -2,7 +2,7 @@ require 'multi_json'
2
2
 
3
3
  module Loginator
4
4
  # Makes a Struct easily serializable and deserializable. Adds the
5
- # from_hash class method and to_json instance method to Struct
5
+ # from_json class method and to_json instance method to Struct
6
6
  # classes.
7
7
  module JsonableStruct
8
8
  def self.included(base)
@@ -11,9 +11,15 @@ module Loginator
11
11
 
12
12
  # class level mixins
13
13
  module ClassMethods #:nodoc
14
- def from_hash(hsh)
15
- fail(ArgumentError, format('Hash must contain keys: %s', members.join(', '))) unless valid_hash?(hsh)
16
- new(*hsh.values)
14
+ def from_json(json)
15
+ json_type = json.delete('type')
16
+ fail(ArgumentError, format('Incorrect message type: %s', json_type)) unless json_type == type
17
+ fail(ArgumentError, format('Hash must contain keys: %s', members.join(', '))) unless valid_hash?(json)
18
+ new(*json.values)
19
+ end
20
+
21
+ def type
22
+ @type ||= name.split('::').last.downcase.freeze
17
23
  end
18
24
 
19
25
  private
@@ -27,7 +33,8 @@ module Loginator
27
33
  end #:rubocop:enable documentation
28
34
 
29
35
  def to_json
30
- MultiJson.dump(to_h)
36
+ MultiJson.dump(to_h.merge(type: self.class.type))
31
37
  end
38
+ alias_method :to_s, :to_json
32
39
  end
33
40
  end
@@ -1,4 +1,4 @@
1
1
  # Increment when releasing.
2
2
  module Loginator
3
- VERSION = '0.0.4'
3
+ VERSION = '0.0.6'
4
4
  end
data/lib/loginator.rb CHANGED
@@ -1,4 +1,6 @@
1
1
  require 'loginator/version'
2
+ require 'loginator/request'
3
+ require 'loginator/response'
2
4
 
3
5
  # Loginator
4
6
  module Loginator
@@ -1,13 +1,13 @@
1
1
  shared_examples_for 'jsonable_struct' do
2
2
  describe '#to_json' do
3
3
  it 'faithfully serializes' do
4
- expect(subject.to_json).to eq(MultiJson.dump(subject.to_h))
4
+ expect(subject.to_json).to eq(MultiJson.dump(subject.to_h.merge(type: described_class.type)))
5
5
  end
6
6
  end
7
7
 
8
- describe '.from_hash' do
8
+ describe '.from_json' do
9
9
  it 'faithfully deserializes' do
10
- expect(described_class.from_hash(MultiJson.load(subject.to_json))).to eq(subject)
10
+ expect(described_class.from_json(MultiJson.load(subject.to_json))).to eq(subject)
11
11
  end
12
12
  end
13
13
  end
@@ -29,9 +29,9 @@ shared_examples_for 'struct_with_defaults' do
29
29
  end
30
30
  end
31
31
 
32
- describe '.from_hash' do
32
+ describe '.from_json' do
33
33
  it 'faithfully deserializes' do
34
- expect(described_class.from_hash(MultiJson.load(subject.to_json))).to eq(subject)
34
+ expect(described_class.from_json(MultiJson.load(subject.to_json))).to eq(subject)
35
35
  end
36
36
  end
37
37
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: loginator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Greg Poirier
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-02-13 00:00:00.000000000 Z
11
+ date: 2015-02-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor