layer-ruby 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0b96d42aefcf7cdc0a293085a3d93d678c62663d
4
- data.tar.gz: cde6707f405032ad6f696c9c7a7d8ffea647cafc
3
+ metadata.gz: e7272a2505f0eeb66b69d0420d6f3a69ffc85edc
4
+ data.tar.gz: 699b31ddf54ee940cf8889e3242b1b8d13833471
5
5
  SHA512:
6
- metadata.gz: 60090694dd66693d41775605a1d17f160b8372e47e9b55350b03ac0bc3a8080b9fcc45d9180eb8a09c24b34978d54e519a4c170d79eed2684a9b863d9d3fbaca
7
- data.tar.gz: 7b0e2b54c9540030562c1ac029aedee2a63a89d6bfcf1024830f25cc34c3740f718a66290337bf89cdf917caec4f45da06cab386aa5a3722345cf052643bb46e
6
+ metadata.gz: 8f2a3055d5272da8e7661868593f713cfc8d261ae789d3897714a9c5efcbed3c8191482183492c134284aa26d7eb1e063db57b22b49530554ad2d702e824879f
7
+ data.tar.gz: c1bbc99b6ebcc2f81ac374284859d434709708ec584f9229f20a6d1855d32d011647e56d4b84c43c99131eec348d74be134fa23e9a0fd02181095bef6a171716
data/README.md CHANGED
@@ -59,6 +59,26 @@ conversation = Layer::Conversation.find('CONVERSATION_ID_HERE')
59
59
  conversation.messages.create({ sender: { name: 'Server' }, parts: [{ body: 'Hello!', mime_type: 'text/plain' }])
60
60
  ```
61
61
 
62
+ ### Sending Messages
63
+
64
+ You can send a announcements like this:
65
+
66
+ ```ruby
67
+ Layer::Announcement.create({ recipients: 'everyone', sender: { name: 'Server' }, parts: [{ body: 'Hello!', mime_type: 'text/plain' }])
68
+ ```
69
+
70
+ ### Managing block lists
71
+
72
+ Managing block lists is also possible:
73
+
74
+ ```ruby
75
+ user = Layer::User.find('user_id')
76
+ user.blocks.all # Returns a list of blocks
77
+ user.blocks.create({ user_id: 'other_user' }) # Adds the other user to the block list
78
+ user.blocks.delete('other_user') # Removes the other user from the block list
79
+ user.blocks.all.first.delete # Removes the first entry from the block list
80
+ ```
81
+
62
82
  ### Using the gem with multiple applications at once
63
83
 
64
84
  It's possible to create a new instance of `Layer::Client` and passing both the app id and the token to the initializer:
data/lib/layer.rb CHANGED
@@ -5,6 +5,9 @@ require 'layer/resource'
5
5
  require 'layer/relation_proxy'
6
6
  require 'layer/conversation'
7
7
  require 'layer/message'
8
+ require 'layer/user'
9
+ require 'layer/block'
10
+ require 'layer/announcement'
8
11
 
9
12
  module Layer
10
13
  end
@@ -0,0 +1,11 @@
1
+ module Layer
2
+ class Announcement < Resource
3
+ include Operations::Create
4
+
5
+ def sent_at
6
+ Time.parse(attributes['sent_at'])
7
+ end
8
+
9
+ end
10
+ end
11
+
@@ -0,0 +1,5 @@
1
+ module Layer
2
+ class Block < Resource
3
+ include Operations::Delete
4
+ end
5
+ end
data/lib/layer/client.rb CHANGED
@@ -67,7 +67,7 @@ module Layer
67
67
  payload = payload.to_json
68
68
 
69
69
  response = RestClient::Request.execute(method: method, url: url, payload: payload, headers: headers)
70
- JSON.parse(response)
70
+ response.empty? ? nil : JSON.parse(response)
71
71
  end
72
72
 
73
73
  end
@@ -1,2 +1,4 @@
1
1
  require 'layer/operations/create'
2
2
  require 'layer/operations/find'
3
+ require 'layer/operations/list'
4
+ require 'layer/operations/delete'
@@ -3,7 +3,7 @@ module Layer
3
3
  module Create
4
4
 
5
5
  module ClassMethods
6
- def create(attributes = {}, client = Client.new)
6
+ def create(attributes = {}, client = self.client)
7
7
  response = client.post(url, attributes)
8
8
  from_response(response, client)
9
9
  end
@@ -0,0 +1,21 @@
1
+ module Layer
2
+ module Operations
3
+ module Delete
4
+
5
+ module ClassMethods
6
+ def delete(id, client = self.client)
7
+ client.delete("#{url}/#{id}")
8
+ end
9
+ end
10
+
11
+ def self.included(base)
12
+ base.extend(ClassMethods)
13
+ end
14
+
15
+ def delete
16
+ client.delete(url)
17
+ end
18
+
19
+ end
20
+ end
21
+ end
@@ -3,7 +3,7 @@ module Layer
3
3
  module Find
4
4
 
5
5
  module ClassMethods
6
- def find(id, client = Client.new)
6
+ def find(id, client = self.client)
7
7
  response = client.get("#{url}/#{id}")
8
8
  from_response(response, client)
9
9
  end
@@ -13,6 +13,11 @@ module Layer
13
13
  base.extend(ClassMethods)
14
14
  end
15
15
 
16
+ def reload
17
+ @attributes = client.get(url)
18
+ self
19
+ end
20
+
16
21
  end
17
22
  end
18
23
  end
@@ -0,0 +1,18 @@
1
+ module Layer
2
+ module Operations
3
+ module List
4
+
5
+ module ClassMethods
6
+ def all(client = self.client)
7
+ response = client.get(url)
8
+ response.map { |attributes| from_response(attributes, client) }
9
+ end
10
+ end
11
+
12
+ def self.included(base)
13
+ base.extend(ClassMethods)
14
+ end
15
+
16
+ end
17
+ end
18
+ end
@@ -3,11 +3,13 @@ module Layer
3
3
 
4
4
  attr_reader :resource_type, :base
5
5
 
6
- def initialize(base, resource_type, operations = [])
6
+ def initialize(base, resource_type, operations = [], &block)
7
7
  @resource_type = resource_type
8
8
  @base = base
9
9
 
10
10
  operations.each { |operation| singleton_class.include(operation::ClassMethods) }
11
+
12
+ instance_eval(&block) if block_given?
11
13
  end
12
14
 
13
15
  def url
@@ -18,6 +20,10 @@ module Layer
18
20
  resource_type.respond_to?(method, include_private) || super
19
21
  end
20
22
 
23
+ def client
24
+ base.client
25
+ end
26
+
21
27
  private
22
28
 
23
29
  def method_missing(method, *args, &block)
@@ -2,6 +2,8 @@ module Layer
2
2
  class Resource
3
3
 
4
4
  class << self
5
+ attr_writer :client
6
+
5
7
  def class_name
6
8
  name.split('::')[-1]
7
9
  end
@@ -13,9 +15,15 @@ module Layer
13
15
  def from_response(attributes, client)
14
16
  new(attributes, client)
15
17
  end
18
+
19
+ def client
20
+ @client ||= Client.new
21
+ end
16
22
  end
17
23
 
18
- def initialize(attributes = {}, client = Client.new)
24
+ attr_reader :client
25
+
26
+ def initialize(attributes = {}, client = self.class.client)
19
27
  @attributes = attributes
20
28
  @client = client
21
29
  end
@@ -34,7 +42,7 @@ module Layer
34
42
 
35
43
  private
36
44
 
37
- attr_reader :attributes, :client
45
+ attr_reader :attributes
38
46
 
39
47
  def method_missing(method, *args, &block)
40
48
  if attributes.has_key?(method.to_s)
data/lib/layer/user.rb ADDED
@@ -0,0 +1,23 @@
1
+ module Layer
2
+ class User < Resource
3
+
4
+ def self.find(id, client = self.client)
5
+ from_response({ 'url' => "/users/#{id}" }, client)
6
+ end
7
+
8
+ def blocks
9
+ RelationProxy.new(self, Block, [Operations::Create, Operations::List, Operations::Delete]) do
10
+ def from_response(response, client)
11
+ response['url'] ||= "#{base.url}#{resource_type.url}/#{response['user_id']}"
12
+ super
13
+ end
14
+
15
+ def create(attributes, client = self.client)
16
+ response = client.post(url, attributes)
17
+ from_response({ 'user_id' => attributes['user_id'] }, client)
18
+ end
19
+ end
20
+ end
21
+
22
+ end
23
+ end
data/lib/layer/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Layer
2
- VERSION = "0.1.0"
2
+ VERSION = '0.2.0'
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.1.0
4
+ version: 0.2.0
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-06-19 00:00:00.000000000 Z
11
+ date: 2015-06-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rest-client
@@ -100,15 +100,20 @@ files:
100
100
  - bin/setup
101
101
  - layer-ruby.gemspec
102
102
  - lib/layer.rb
103
+ - lib/layer/announcement.rb
104
+ - lib/layer/block.rb
103
105
  - lib/layer/client.rb
104
106
  - lib/layer/conversation.rb
105
107
  - lib/layer/message.rb
106
108
  - lib/layer/operations.rb
107
109
  - lib/layer/operations/create.rb
110
+ - lib/layer/operations/delete.rb
108
111
  - lib/layer/operations/find.rb
112
+ - lib/layer/operations/list.rb
109
113
  - lib/layer/relation_proxy.rb
110
114
  - lib/layer/resource.rb
111
115
  - lib/layer/ruby.rb
116
+ - lib/layer/user.rb
112
117
  - lib/layer/version.rb
113
118
  homepage: https://github.com/benedikt/layer-ruby
114
119
  licenses:
@@ -135,4 +140,3 @@ signing_key:
135
140
  specification_version: 4
136
141
  summary: Ruby bindings for the Layer Platform API
137
142
  test_files: []
138
- has_rdoc: