Skip to content

Commit 861010b

Browse files
committed
Merge pull request #12 from cakejelly/rich-content
Adds rich content support
2 parents a4b8f18 + 06e082f commit 861010b

File tree

12 files changed

+149
-8
lines changed

12 files changed

+149
-8
lines changed

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,30 @@ conv.destroy
123123

124124
```
125125

126+
#### Initiating a Rich Content Upload
127+
128+
```ruby
129+
conv = platform.conversations.find("conversation_id")
130+
content = conv.content.create(mime_type: "image/png", file: File.open("image.png"))
131+
# => #<Layer::Resources::RichContent @attributes={...}>
132+
133+
content.upload_url
134+
# => "https://www.googleapis.com/upload/storage/path/to/content"
135+
```
136+
137+
#### Refreshing the download URL for a Content Object
138+
139+
```ruby
140+
content = conv.content.find("content_id")
141+
# => #<Layer::Resources::RichContent @attributes={...}>
142+
143+
content.download_url
144+
# => "http://google-testbucket.storage.googleapis.com/some/download/path"
145+
146+
content.refresh_url
147+
"https://api.layer.com/apps/<APP_ID>/conversations/<CONVERSATION_ID>/content/<CONTENT_ID>"
148+
```
149+
126150
#### Sending Messages ####
127151

128152
```ruby

lib/layer/api.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,6 @@
2323
require "layer/resources/user"
2424
require "layer/resources/block"
2525
require "layer/resources/webhook"
26+
require "layer/resources/rich_content"
2627

2728
require "layer/middleware/api_errors"

lib/layer/http_client.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ def connection
1313
@connection ||= Faraday.new(url: base_url) do |faraday|
1414
faraday.headers = default_headers
1515
faraday.request :url_encoded
16+
faraday.request :multipart
1617
faraday.adapter Faraday.default_adapter
1718
faraday.use Middleware::ApiErrors
1819
end
@@ -22,6 +23,10 @@ def get(url, options = {})
2223
call(:get, url, options)
2324
end
2425

26+
def put(url, options = {})
27+
call(:put, url, options)
28+
end
29+
2530
def post(url, options = {})
2631
call(:post, url, options)
2732
end

lib/layer/resource_proxy.rb

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
module Layer
22
class ResourceProxy
3+
attr_accessor :url
4+
35
def initialize(client, base, resource)
46
@client = client
57
@base = base
68
@resource = resource
9+
@url = get_url(base, resource)
710
end
811

9-
def url
10-
@base.nil? ? @resource.url : "#{@base.url}/#{@resource.url}"
12+
def get_url(base, resource)
13+
base.nil? ? resource.url : "#{base.url}/#{resource.url}"
1114
end
1215

1316
def method_missing(method, *args, &block)
14-
@resource.public_send(method, *([@client, url] + args), &block)
17+
@resource.public_send(method, *([@client, @url] + args), &block)
1518
end
1619

1720
def respond_to_missing?(method, include_private = false)

lib/layer/resources/conversation.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,16 @@ class Conversation < Layer::Resource
44
def messages
55
Layer::ResourceProxy.new(client, self, Message)
66
end
7+
8+
def content
9+
proxy = Layer::ResourceProxy.new(client, self, RichContent)
10+
proxy.url = content_url
11+
proxy
12+
end
13+
14+
def content_url
15+
"conversations/#{uuid}/content"
16+
end
717
end
818
end
919
end
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
module Layer
2+
module Resources
3+
class RichContent < Layer::Resource
4+
def self.create(client, url, params = {})
5+
mime_type = params[:mime_type]
6+
file = params[:file]
7+
8+
headers = {
9+
"Upload-Content-Type" => mime_type,
10+
"Upload-Content-Length" => file.size.to_s
11+
}
12+
13+
response = client.post(url, body: params.to_json, headers: headers)
14+
response.merge!("mime_type" => mime_type)
15+
16+
new(response, client).upload(file)
17+
end
18+
19+
def upload(file)
20+
file_header = { "Content-Length" => file.size.to_s }
21+
client.put(upload_url, body: file, headers: file_header)
22+
self
23+
end
24+
end
25+
end
26+
end
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"etag": "CODY+uDM2ssCEAE=",
3+
"crc32c": "8OJNpw==",
4+
"owner": {
5+
"entityId": "00b4903a9739f8dce455a6e623e7d62928c6655eeed3047adbdc30f553bcca5c",
6+
"entity": "user-00b4903a9739f8dce455a6e623e7d62928c6655eeed3047adbdc30f553bcca5c"
7+
},
8+
"mediaLink": "https:\/\/www.googleapis.com\/download\/storage\/v1\/b\/content-prod1\/o\/c2fc75ce-2f0e-11e5-a716-4290ac022c3d%2Fb127ccbe-5f95-4d6a-9c01-c1e98e147f4f%2Ffe776bf0-f221-11e5-9ce5-0242ac110083?generation=1458866376060000&alt=media",
9+
"md5Hash": "PE3ns7xp6S1lfEewRXFB3A==",
10+
"size": "6486",
11+
"storageClass": "STANDARD",
12+
"updated": "2016-03-25T00:39:36.058Z",
13+
"timeCreated": "2016-03-25T00:39:36.058Z",
14+
"contentType": "image\/png",
15+
"metageneration": "1",
16+
"generation": "1458866376060000",
17+
"bucket": "content-prod1",
18+
"name": "c2fc75ce-2f0e-11e5-a716-4290ac022c3d\/b127ccbe-5f95-4d6a-9c01-c1e98e147f4f\/fe776bf0-f221-11e5-9ce5-0242ac110083",
19+
"selfLink": "https:\/\/www.googleapis.com\/storage\/v1\/b\/content-prod1\/o\/c2fc75ce-2f0e-11e5-a716-4290ac022c3d%2Fb127ccbe-5f95-4d6a-9c01-c1e98e147f4f%2Ffe776bf0-f221-11e5-9ce5-0242ac110083",
20+
"id": "content-prod1\/c2fc75ce-2f0e-11e5-a716-4290ac022c3d\/b127ccbe-5f95-4d6a-9c01-c1e98e147f4f\/fe776bf0-f221-11e5-9ce5-0242ac110083\/1458866376060000",
21+
"kind": "storage#object"
22+
}

spec/layer/fixtures/image.png

6.33 KB
Loading
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"upload_url": "https:\/\/www.googleapis.com\/upload\/storage\/v1\/b\/content-prod1\/o?uploadType=resumable&name=c2fc75ce-2f0e-11e5-a716-4290ac022c3d\/b127ccbe-5f95-4d6a-9c01-c1e98e147f4f\/fe776bf0-f221-11e5-9ce5-0242ac110083&upload_id=AEnB2UqIp4Lpou_9x_pg4CVFxwl3tA7Tq--6lAjCtqkKe7KL6n98rDn719hUEByn2J8ycOSJp6EPPrq8LBgibGrl5jkJ3f9Qyw",
3+
"size": 6486,
4+
"id": "layer:\/\/\/content\/fe776bf0-f221-11e5-9ce5-0242ac110083"
5+
}

spec/layer/resource_proxy_spec.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,21 @@
66
describe "#new" do
77
it "should assign a HttpClient instance" do
88
http_client = Layer::Platform::Client.new.client
9-
proxy = described_class.new(http_client, nil, nil)
9+
proxy = described_class.new(http_client, Layer::Resource, Layer::Resource)
1010

1111
expect(proxy.instance_variable_get("@client")).to eq(http_client)
1212
end
1313

1414
it "should assign a base resource" do
15-
base = Class.new
16-
proxy = described_class.new(nil, base, nil)
15+
base = Layer::Resource
16+
proxy = described_class.new(nil, base, Layer::Resource)
1717

1818
expect(proxy.instance_variable_get("@base")).to eq(base)
1919
end
2020

2121
it "should assign a target resource" do
22-
target = Class.new
23-
proxy = described_class.new(nil, nil, target)
22+
target = Layer::Resource
23+
proxy = described_class.new(nil, Layer::Resource, target)
2424

2525
expect(proxy.instance_variable_get("@resource")).to eq(target)
2626
end

0 commit comments

Comments
 (0)