layer-api 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8b4456ca45c29a36081fc82245729c848fc6f301
4
+ data.tar.gz: e28f390d1b9be7eaa4f9d68fafae58ca0a06c646
5
+ SHA512:
6
+ metadata.gz: 85a250b727905e5d1bcddd805e1ca0a94c0b14782831e8c00c1ec6f7103600439871c2c4739e823148dbe40faeec51732ad9cfd411651f294a23cbaf5e260b64
7
+ data.tar.gz: 74047150a0f0f44ebbd314a5a672be5fde226bb6ab2d8088277f4645eb2c152941e581a7e003b36942bde77440f84ebecd44a931768452286546e25ea8b743f8
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.5
4
+ before_install: gem install bundler -v 1.10.4
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in layer-api.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,153 @@
1
+ # Layer::Api
2
+ [![Build Status](https://travis-ci.org/cakejelly/layer-api.svg?branch=master)](https://travis-ci.org/cakejelly/layer-api) [![Gem Version](https://badge.fury.io/rb/layer-api.svg)](http://badge.fury.io/rb/layer-api)
3
+
4
+ A very simple wrapper for the Layer Platform API.
5
+
6
+ If you want to learn more, check out the [official documentation](https://developer.layer.com/docs/platform).
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'layer-api'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install layer-api
23
+
24
+ ## Usage
25
+
26
+ ### Authentication/setup
27
+
28
+ ```ruby
29
+ layer = Layer::Api::Client.new(
30
+ api_token: "your_api_token",
31
+ app_id: "your_app_id"
32
+ )
33
+ ```
34
+ If you have `ENV['LAYER_API_TOKEN']` and `ENV['LAYER_APP_ID']` environment variables setup, they will be used by default don't need to be included:
35
+ ```ruby
36
+ layer = Layer::Api::Client.new
37
+ ```
38
+
39
+ ### Retrieving conversations
40
+
41
+ ```ruby
42
+ layer.get_conversation("conversation_id")
43
+ ```
44
+
45
+ ### Creating conversations
46
+
47
+ ```ruby
48
+ # A sample conversation
49
+ conversation = {
50
+ participants: [
51
+ "1234",
52
+ "5678"
53
+ ],
54
+ distinct: false,
55
+ metadata: {
56
+ background_color: "#3c3c3c"
57
+ }
58
+ }
59
+
60
+ layer.create_conversation(conversation)
61
+ ```
62
+
63
+ ### Editing conversations
64
+
65
+ ```ruby
66
+ # Sample edit operations
67
+ operations = [
68
+ {operation: "add", property: "participants", value: "user1"},
69
+ {operation: "add", property: "participants", value: "user2"}
70
+ ]
71
+
72
+ layer.edit_conversation(operations)
73
+ ```
74
+
75
+ ### Sending messages
76
+ ```ruby
77
+ # A sample message to send
78
+ message = {
79
+ sender: {
80
+ name: "t-bone"
81
+ },
82
+ parts: [
83
+ {
84
+ body: "Hello, World!",
85
+ mime_type: "text/plain"
86
+ },
87
+ {
88
+ body: "YW55IGNhcm5hbCBwbGVhc3VyZQ==",
89
+ mime_type: "image/jpeg",
90
+ encoding: "base64"
91
+ }
92
+ ],
93
+ notification: {
94
+ text: "This is the alert text to include with the Push Notification.",
95
+ sound: "chime.aiff"
96
+ }
97
+ }
98
+
99
+ layer.send_message("conversation_id", message)
100
+
101
+ ```
102
+
103
+ ### Sending Announcements
104
+
105
+ ```ruby
106
+ # A sample announcement
107
+ announcement = {
108
+ recipients: [ "1234", "5678" ],
109
+ sender: {
110
+ name: "The System"
111
+ },
112
+ parts: [
113
+ {
114
+ body: "Hello, World!",
115
+ mime_type: "text/plain"
116
+ },
117
+ {
118
+ body: "YW55IGNhcm5hbCBwbGVhc3VyZQ==",
119
+ mime_type: "image/jpeg",
120
+ encoding: "base64"
121
+ }
122
+ ],
123
+ notification: {
124
+ text: "This is the alert text to include with the Push Notification.",
125
+ sound: "chime.aiff"
126
+ }
127
+ }
128
+
129
+ layer.send_announcement(announcement)
130
+ ```
131
+
132
+ ### Managing User Block Lists
133
+
134
+ ```ruby
135
+ # Retrieves a users blocklist
136
+ layer.get_blocklist("user_id")
137
+
138
+ # Adds a user to another users blocklist
139
+ layer.block_user("owner_id", "user_id")
140
+
141
+ # Removes a user from another users blocklist
142
+ layer.unblock_user("owner_id", "user_id")
143
+ ```
144
+
145
+ ## Development
146
+
147
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake rspec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
148
+
149
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
150
+
151
+ ## Contributing
152
+
153
+ Bug reports and pull requests are welcome on GitHub at https://github.com/cakejelly/layer-api.
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "layer/api"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
data/layer-api.gemspec ADDED
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'layer/api/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "layer-api"
8
+ spec.version = Layer::Api::VERSION
9
+ spec.authors = ["Jake Kelly"]
10
+ spec.email = ["jake.kelly10@gmail.com"]
11
+ spec.license = "MIT"
12
+
13
+ spec.summary = "A ruby toolkit for Layer's Platform API (https://developer.layer.com/docs/platform)"
14
+ spec.description = "Simple wrapper for the Layer Platform API"
15
+ spec.homepage = "https://github.com/cakejelly/layer-api"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.10"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "rspec", "~> 3.3", ">= 3.3.0"
25
+ spec.add_development_dependency "vcr", "~> 2.9", ">= 2.9.3"
26
+ spec.add_development_dependency "webmock", "~> 1.21", ">= 1.21.0"
27
+ spec.add_development_dependency "pry", "~> 0.10.1"
28
+
29
+ spec.add_dependency "faraday", "~> 0.9.1"
30
+ end
data/lib/layer/api.rb ADDED
@@ -0,0 +1,15 @@
1
+ require 'faraday'
2
+ require 'json'
3
+
4
+ require "layer/api/version"
5
+ require "layer/api/configuration"
6
+ require "layer/api/connection"
7
+ require "layer/api/client"
8
+ require "layer/api/error"
9
+
10
+ require "layer/api/middleware/api_errors"
11
+
12
+ module Layer
13
+ module Api
14
+ end
15
+ end
@@ -0,0 +1,23 @@
1
+ require "layer/api/client/conversations"
2
+ require "layer/api/client/announcements"
3
+ require "layer/api/client/users"
4
+
5
+ module Layer
6
+ module Api
7
+ class Client
8
+ include Layer::Api::Connection
9
+ include Layer::Api::Configuration
10
+
11
+ include Layer::Api::Conversations
12
+ include Layer::Api::Announcements
13
+ include Layer::Api::Users
14
+
15
+ attr_accessor :api_token, :app_id
16
+
17
+ def initialize(options = {})
18
+ @api_token = options[:api_token] || ENV['LAYER_API_TOKEN']
19
+ @app_id = options[:app_id] || ENV['LAYER_APP_ID']
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,9 @@
1
+ module Layer
2
+ module Api
3
+ module Announcements
4
+ def send_announcement(announcement = {})
5
+ post("announcements", body: announcement.to_json)
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,30 @@
1
+ module Layer
2
+ module Api
3
+ module Conversations
4
+ def get_conversation(conversation_id)
5
+ get("conversations/#{conversation_id}")
6
+ end
7
+
8
+ def create_conversation(params = {})
9
+ post("conversations", body: params.to_json)
10
+ end
11
+
12
+ def edit_conversation(conversation_id, params = {})
13
+ patch("conversations/#{conversation_id}",
14
+ body: params.to_json,
15
+ headers: layer_patch_header
16
+ )
17
+ end
18
+
19
+ def send_message(conversation_id, message = {})
20
+ post("conversations/#{conversation_id}/messages",
21
+ body: message.to_json
22
+ )
23
+ end
24
+
25
+ def get_stripped_id(raw_id)
26
+ raw_id.sub("layer:///conversations/", "")
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,18 @@
1
+ module Layer
2
+ module Api
3
+ module Users
4
+ def get_blocklist(user_id)
5
+ get("users/#{user_id}/blocks")
6
+ end
7
+
8
+ def block_user(owner_id, user_id)
9
+ params = { user_id: user_id }
10
+ post("users/#{owner_id}/blocks", body: params.to_json)
11
+ end
12
+
13
+ def unblock_user(owner_id, user_id)
14
+ delete("users/#{owner_id}/blocks/#{user_id}")
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,23 @@
1
+ module Layer
2
+ module Api
3
+ module Configuration
4
+ DEFAULT_HOST = "https://api.layer.com"
5
+
6
+ def default_layer_headers
7
+ {
8
+ 'Accept' => 'application/vnd.layer+json; version=1.0',
9
+ 'Authorization' => "Bearer #{api_token}",
10
+ 'Content-Type' => 'application/json'
11
+ }
12
+ end
13
+
14
+ def layer_patch_header
15
+ { 'Content-Type' => 'application/vnd.layer-patch+json' }
16
+ end
17
+
18
+ def base_url
19
+ "#{DEFAULT_HOST}/apps/#{app_id}"
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,40 @@
1
+ module Layer
2
+ module Api
3
+ module Connection
4
+ def connection
5
+ connection ||= Faraday.new(url: base_url) do |faraday|
6
+ faraday.headers = default_layer_headers
7
+ faraday.request :url_encoded
8
+ faraday.adapter Faraday.default_adapter
9
+ faraday.use Middleware::ApiErrors
10
+ end
11
+ end
12
+
13
+ def get(url, options = {})
14
+ run_request(:get, url, options)
15
+ end
16
+
17
+ def post(url, options = {})
18
+ run_request(:post, url, options)
19
+ end
20
+
21
+ def patch(url, options = {})
22
+ run_request(:patch, url, options)
23
+ end
24
+
25
+ def delete(url)
26
+ run_request(:delete, url, options = {})
27
+ end
28
+
29
+ def run_request(method, url, options = {})
30
+ response = connection.run_request(
31
+ method,
32
+ url,
33
+ options[:body],
34
+ options[:headers]
35
+ )
36
+ JSON.parse(response.body) if !response.body.empty?
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,36 @@
1
+ module Layer
2
+ module Api
3
+ class Error < StandardError
4
+ def self.from_response(response)
5
+ status = response[:status]
6
+
7
+ if klass = case status
8
+ when 400 then Layer::Api::BadRequest
9
+ when 404 then Layer::Api::NotFound
10
+ when 500..599 then Layer::Api::ServerError
11
+ else self
12
+ end
13
+ end
14
+
15
+ klass.new(response)
16
+ end
17
+
18
+ def initialize(response)
19
+ @response = response
20
+ super(build_error_message)
21
+ end
22
+
23
+ private
24
+
25
+ def build_error_message
26
+ message = "Layer responded with status "
27
+ message << "#{@response[:status]}: #{@response[:body]}"
28
+ message
29
+ end
30
+ end
31
+
32
+ class BadRequest < Error; end
33
+ class NotFound < Error; end
34
+ class ServerError < Error; end
35
+ end
36
+ end
@@ -0,0 +1,15 @@
1
+ module Layer
2
+ module Api
3
+ module Middleware
4
+ class ApiErrors < Faraday::Response::Middleware
5
+ ERROR_CODES = 400...600
6
+
7
+ def on_complete(response)
8
+ if ERROR_CODES.include?(response.status)
9
+ raise Layer::Api::Error.from_response(response)
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,5 @@
1
+ module Layer
2
+ module Api
3
+ VERSION = "0.1.1"
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,180 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: layer-api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Jake Kelly
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-07-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.3'
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: 3.3.0
51
+ type: :development
52
+ prerelease: false
53
+ version_requirements: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - "~>"
56
+ - !ruby/object:Gem::Version
57
+ version: '3.3'
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: 3.3.0
61
+ - !ruby/object:Gem::Dependency
62
+ name: vcr
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '2.9'
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: 2.9.3
71
+ type: :development
72
+ prerelease: false
73
+ version_requirements: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - "~>"
76
+ - !ruby/object:Gem::Version
77
+ version: '2.9'
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: 2.9.3
81
+ - !ruby/object:Gem::Dependency
82
+ name: webmock
83
+ requirement: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - "~>"
86
+ - !ruby/object:Gem::Version
87
+ version: '1.21'
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: 1.21.0
91
+ type: :development
92
+ prerelease: false
93
+ version_requirements: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - "~>"
96
+ - !ruby/object:Gem::Version
97
+ version: '1.21'
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: 1.21.0
101
+ - !ruby/object:Gem::Dependency
102
+ name: pry
103
+ requirement: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - "~>"
106
+ - !ruby/object:Gem::Version
107
+ version: 0.10.1
108
+ type: :development
109
+ prerelease: false
110
+ version_requirements: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - "~>"
113
+ - !ruby/object:Gem::Version
114
+ version: 0.10.1
115
+ - !ruby/object:Gem::Dependency
116
+ name: faraday
117
+ requirement: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - "~>"
120
+ - !ruby/object:Gem::Version
121
+ version: 0.9.1
122
+ type: :runtime
123
+ prerelease: false
124
+ version_requirements: !ruby/object:Gem::Requirement
125
+ requirements:
126
+ - - "~>"
127
+ - !ruby/object:Gem::Version
128
+ version: 0.9.1
129
+ description: Simple wrapper for the Layer Platform API
130
+ email:
131
+ - jake.kelly10@gmail.com
132
+ executables: []
133
+ extensions: []
134
+ extra_rdoc_files: []
135
+ files:
136
+ - ".gitignore"
137
+ - ".rspec"
138
+ - ".travis.yml"
139
+ - Gemfile
140
+ - README.md
141
+ - Rakefile
142
+ - bin/console
143
+ - bin/setup
144
+ - layer-api.gemspec
145
+ - lib/layer/api.rb
146
+ - lib/layer/api/client.rb
147
+ - lib/layer/api/client/announcements.rb
148
+ - lib/layer/api/client/conversations.rb
149
+ - lib/layer/api/client/users.rb
150
+ - lib/layer/api/configuration.rb
151
+ - lib/layer/api/connection.rb
152
+ - lib/layer/api/error.rb
153
+ - lib/layer/api/middleware/api_errors.rb
154
+ - lib/layer/api/version.rb
155
+ homepage: https://github.com/cakejelly/layer-api
156
+ licenses:
157
+ - MIT
158
+ metadata: {}
159
+ post_install_message:
160
+ rdoc_options: []
161
+ require_paths:
162
+ - lib
163
+ required_ruby_version: !ruby/object:Gem::Requirement
164
+ requirements:
165
+ - - ">="
166
+ - !ruby/object:Gem::Version
167
+ version: '0'
168
+ required_rubygems_version: !ruby/object:Gem::Requirement
169
+ requirements:
170
+ - - ">="
171
+ - !ruby/object:Gem::Version
172
+ version: '0'
173
+ requirements: []
174
+ rubyforge_project:
175
+ rubygems_version: 2.4.3
176
+ signing_key:
177
+ specification_version: 4
178
+ summary: A ruby toolkit for Layer's Platform API (https://developer.layer.com/docs/platform)
179
+ test_files: []
180
+ has_rdoc: