layer-ruby 0.3.0 → 0.4.1

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: 1175f3c645d381961648fee03212ff4148a4cce7
4
- data.tar.gz: 57265f20d7c15ff918cd4e96b34c0211b1b8c488
3
+ metadata.gz: 6b2e92f9be129ac6265ff24ab73852387a9e438a
4
+ data.tar.gz: 1f3911782934d15e49f0feeb3964357acd28873c
5
5
  SHA512:
6
- metadata.gz: 419b39243567daeaca8e3b18ede67330b460f8dd478b4851cb1afb6e4529af84f33807b618909e66d5e477caf9c9c01c25d77326a4cef8a2cad3c4b0cbe0d726
7
- data.tar.gz: e9a09eb831bd7759f177397c8e9a9e9fd301c793f833dd68a9a2367d7d27156a282d5add4371b6dddbb5a2d58062a7c45dab8ca567440f15994b0e9417021160
6
+ metadata.gz: 57229aa42c27f78cef0f959c68d24299792cee2803142cfa304bab995a9c1268decd4b055abae6d8ed5c49ce014139de36129c9ba3c21d31a9d375a1266c0d46
7
+ data.tar.gz: 9681349bdb101b4ec80219bcc69990454353b8c9b9f72c33896e9673f555c7f932a5128655fe773a2131f7df060775aedc61700999338d35b2e7985ded664203
data/README.md CHANGED
@@ -3,7 +3,7 @@
3
3
  [![Build Status](https://travis-ci.org/benedikt/layer-ruby.svg)](https://travis-ci.org/benedikt/layer-ruby)
4
4
  [![Gem Version](https://badge.fury.io/rb/layer-ruby.svg)](http://badge.fury.io/rb/layer-ruby)
5
5
 
6
- Ruby bindings for the [Layer Platform API](https://developer.layer.com/docs/platform).
6
+ Ruby bindings for the [Layer Platform API](https://developer.layer.com/docs/platform).
7
7
 
8
8
  ## Installation
9
9
 
@@ -23,7 +23,7 @@ Or install it yourself as:
23
23
 
24
24
  ## Usage
25
25
 
26
- Please refer to the [Layer Platform API documentation](https://developer.layer.com/docs/platform) for details about the required payloads and responses.
26
+ Please refer to the [Layer Platform API documentation](https://developer.layer.com/docs/platform) for details about the required payloads and responses.
27
27
 
28
28
  ### Configuration
29
29
 
@@ -50,13 +50,25 @@ To retrieve a existing conversation, just use `Conversation.find` passing it the
50
50
  conversation = Layer::Conversation.find('CONVERSATION_ID_HERE')
51
51
  ```
52
52
 
53
+ ### Updating Conversations
54
+
55
+ To update a conversation, just update the conversation's attributes and call `save`.
56
+
57
+ ```ruby
58
+ conversation = Layer::Conversation.find('CONVERSATION_ID_HERE')
59
+ conversation.participants << 3
60
+ conversation.metadata[:foo] = 'bar'
61
+ conversation.save
62
+ ```
63
+
64
+
53
65
  ### Sending Messages
54
66
 
55
67
  In order to send messages, you first have to load (or create) a Conversation. Afterwards you can send a message to the conversation like this:
56
68
 
57
69
  ```ruby
58
70
  conversation = Layer::Conversation.find('CONVERSATION_ID_HERE')
59
- conversation.messages.create({ sender: { name: 'Server' }, parts: [{ body: 'Hello!', mime_type: 'text/plain' }])
71
+ conversation.messages.create({ sender: { name: 'Server' }, parts: [{ body: 'Hello!', mime_type: 'text/plain' }]})
60
72
  ```
61
73
 
62
74
  ### Sending Announcements
@@ -64,7 +76,7 @@ conversation.messages.create({ sender: { name: 'Server' }, parts: [{ body: 'Hell
64
76
  You can send a announcements like this:
65
77
 
66
78
  ```ruby
67
- Layer::Announcement.create({ recipients: 'everyone', sender: { name: 'Server' }, parts: [{ body: 'Hello!', mime_type: 'text/plain' }])
79
+ Layer::Announcement.create({ recipients: 'everyone', sender: { name: 'Server' }, parts: [{ body: 'Hello!', mime_type: 'text/plain' }]})
68
80
  ```
69
81
 
70
82
  ### Managing block lists
@@ -87,7 +99,7 @@ It's possible to create a new instance of `Layer::Client` and passing both the a
87
99
  client = Layer::Client.new('YOUR_APP_ID_HERE', 'YOUR_PLATFORM_TOKEN_HERE')
88
100
  ```
89
101
 
90
- The client will not use any global configuration. You can pass the client as a second parameter to any operations (`create`, `find`) described above.
102
+ The client will not use any global configuration. You can pass the client as a second parameter to any operations (`create`, `find`) described above.
91
103
 
92
104
  ## Development
93
105
 
@@ -106,5 +118,4 @@ The gem is available as open source under the terms of the [MIT License](http://
106
118
 
107
119
  ## Related Work
108
120
 
109
- Check out the [layer-identity_token](https://github.com/dreimannzelt/layer-identity_token) gem to generate authentication tokens for the Layer SDK.
110
-
121
+ Check out the [layer-identity_token](https://github.com/dreimannzelt/layer-identity_token) gem to generate authentication tokens for the Layer SDK.
data/lib/layer.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require 'layer/version'
2
2
  require 'layer/client'
3
+ require 'layer/patch'
3
4
  require 'layer/operations'
4
5
  require 'layer/resource'
5
6
  require 'layer/relation_proxy'
data/lib/layer/client.rb CHANGED
@@ -38,8 +38,10 @@ module Layer
38
38
  request(:post, *args)
39
39
  end
40
40
 
41
- def patch(*args)
42
- request(:patch, *args)
41
+ def patch(url, payload = {}, headers = {})
42
+ headers['Content-Type'] = 'application/vnd.layer-patch+json'
43
+
44
+ request(:patch, url, payload, headers)
43
45
  end
44
46
 
45
47
  def put(*args)
@@ -2,6 +2,7 @@ module Layer
2
2
  class Conversation < Resource
3
3
  include Operations::Find
4
4
  include Operations::Create
5
+ include Operations::Patch
5
6
 
6
7
  def messages
7
8
  RelationProxy.new(self, Message, [Operations::Create])
@@ -2,3 +2,4 @@ require 'layer/operations/create'
2
2
  require 'layer/operations/find'
3
3
  require 'layer/operations/list'
4
4
  require 'layer/operations/delete'
5
+ require 'layer/operations/patch'
@@ -15,7 +15,7 @@ module Layer
15
15
  end
16
16
 
17
17
  def reload
18
- @attributes = client.get(url)
18
+ self.attributes = client.get(url)
19
19
  self
20
20
  end
21
21
 
@@ -0,0 +1,23 @@
1
+ module Layer
2
+ module Operations
3
+ module Patch
4
+
5
+ def save
6
+ client.patch(url, patch.operations)
7
+ patch.reset
8
+ true
9
+ end
10
+
11
+ def attributes=(attributes)
12
+ @attributes = Layer::Patch::Hash.new(patch, attributes)
13
+ end
14
+
15
+ private
16
+
17
+ def patch
18
+ @patch ||= Layer::Patch.new
19
+ end
20
+
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,62 @@
1
+ require 'layer/patch/base'
2
+ require 'layer/patch/array'
3
+ require 'layer/patch/hash'
4
+
5
+ module Layer
6
+ class Patch
7
+ def initialize(property = nil, parent = nil)
8
+ @property = property
9
+ @parent = parent
10
+ end
11
+
12
+ def add(property = nil, value)
13
+ operation(:add, property, value: value)
14
+ end
15
+
16
+ def add_index(property = nil, index, value)
17
+ operation(:add, property, index: index, value: value)
18
+ end
19
+
20
+ def remove(property = nil, value)
21
+ operation(:remove, property, value: value)
22
+ end
23
+
24
+ def remove_index(property = nil, index)
25
+ operation(:remove, property, index: index)
26
+ end
27
+
28
+ def set(property = nil, value)
29
+ operation(:set, property, value: value)
30
+ end
31
+
32
+ def replace(property = nil, value)
33
+ operation(:replace, property, value: value)
34
+ end
35
+
36
+ def delete(property)
37
+ operation(:delete, property)
38
+ end
39
+
40
+ def nested(property)
41
+ self.class.new(property, self)
42
+ end
43
+
44
+ def operations
45
+ @operations ||= @parent ? @parent.operations : []
46
+ end
47
+
48
+ def reset
49
+ @parent ? parent.reset : @operations = []
50
+ end
51
+
52
+ private
53
+
54
+ def operation(type, property = nil, options = {})
55
+ operations << options.merge(operation: type.to_s, property: expand_property(property))
56
+ end
57
+
58
+ def expand_property(property)
59
+ [@property, property].compact.join('.')
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,69 @@
1
+ module Layer
2
+ class Patch
3
+ class Array < Base
4
+
5
+ def prepare_base(base)
6
+ base.each_with_index.map do |(value, index)|
7
+ wrap(index, value)
8
+ end
9
+ end
10
+
11
+ def []=(index, value)
12
+ patch.add_index(index, value)
13
+ super
14
+ end
15
+
16
+ def <<(value)
17
+ patch.add(value)
18
+ super
19
+ end
20
+ alias :push :<<
21
+
22
+ def unshift(value)
23
+ patch.add_index(0, value)
24
+ super
25
+ end
26
+
27
+ def concat(values)
28
+ values.each { |value| patch.add(value) }
29
+ super
30
+ end
31
+
32
+ def insert(offset, values)
33
+ values.each_with_index { |value, index| patch.add_index(offset + index, value) }
34
+ super
35
+ end
36
+
37
+ def clear
38
+ patch.replace([])
39
+ super
40
+ end
41
+
42
+ def delete_at(index)
43
+ patch.remove_index(index)
44
+ super
45
+ end
46
+
47
+ def delete(value)
48
+ patch.remove(value)
49
+ super
50
+ end
51
+
52
+ def pop
53
+ patch.remove_index(length)
54
+ super
55
+ end
56
+
57
+ def shift
58
+ patch.remove_index(0)
59
+ super
60
+ end
61
+
62
+ def replace(values)
63
+ patch.replace(values)
64
+ super
65
+ end
66
+
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,25 @@
1
+ module Layer
2
+ class Patch
3
+ class Base < SimpleDelegator
4
+
5
+ attr_reader :patch
6
+
7
+ def initialize(patch, object)
8
+ @patch = patch
9
+ super(prepare_base(object))
10
+ end
11
+
12
+ def wrap(property, object)
13
+ case object
14
+ when ::Array
15
+ Layer::Patch::Array.new(patch.nested(property), object)
16
+ when ::Hash
17
+ Layer::Patch::Hash.new(patch.nested(property), object)
18
+ else
19
+ object
20
+ end
21
+ end
22
+
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,55 @@
1
+ module Layer
2
+ class Patch
3
+ class Hash < Base
4
+
5
+ def prepare_base(base)
6
+ base.each_pair do |key, value|
7
+ base[key] = wrap(key, value)
8
+ end
9
+ end
10
+
11
+ def []=(key, value)
12
+ patch.set(key, value)
13
+ super
14
+ end
15
+ alias :store :[]=
16
+
17
+ def clear
18
+ patch.replace({})
19
+ super
20
+ end
21
+
22
+ def delete(key)
23
+ patch.delete(key)
24
+ super
25
+ end
26
+
27
+ def delete_if(&block)
28
+ super do |key, value|
29
+ result = yield(key, value)
30
+ delete(key) if result
31
+ result
32
+ end
33
+ end
34
+ alias :reject! :delete_if
35
+
36
+ def shift
37
+ patch.delete(keys.first)
38
+ super
39
+ end
40
+
41
+ def replace(other_hash)
42
+ patch.replace(other_hash)
43
+ super
44
+ end
45
+
46
+ def merge!(other_hash)
47
+ other_hash.each_pair do |key, value|
48
+ patch.set(key, value)
49
+ end
50
+ super
51
+ end
52
+
53
+ end
54
+ end
55
+ end
@@ -21,11 +21,11 @@ module Layer
21
21
  end
22
22
  end
23
23
 
24
- attr_reader :client
24
+ attr_accessor :client, :attributes
25
25
 
26
26
  def initialize(attributes = {}, client = self.class.client)
27
- @attributes = attributes
28
- @client = client
27
+ self.attributes = attributes
28
+ self.client = client
29
29
  end
30
30
 
31
31
  def url
@@ -42,8 +42,6 @@ module Layer
42
42
 
43
43
  private
44
44
 
45
- attr_reader :attributes
46
-
47
45
  def method_missing(method, *args, &block)
48
46
  if attributes.has_key?(method.to_s)
49
47
  attributes[method.to_s]
data/lib/layer/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Layer
2
- VERSION = '0.3.0'
2
+ VERSION = '0.4.1'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: layer-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Benedikt Deicke
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-07-15 00:00:00.000000000 Z
11
+ date: 2015-07-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rest-client
@@ -110,6 +110,11 @@ files:
110
110
  - lib/layer/operations/delete.rb
111
111
  - lib/layer/operations/find.rb
112
112
  - lib/layer/operations/list.rb
113
+ - lib/layer/operations/patch.rb
114
+ - lib/layer/patch.rb
115
+ - lib/layer/patch/array.rb
116
+ - lib/layer/patch/base.rb
117
+ - lib/layer/patch/hash.rb
113
118
  - lib/layer/relation_proxy.rb
114
119
  - lib/layer/resource.rb
115
120
  - lib/layer/ruby.rb
@@ -140,3 +145,4 @@ signing_key:
140
145
  specification_version: 4
141
146
  summary: Ruby bindings for the Layer Platform API
142
147
  test_files: []
148
+ has_rdoc: