layer-ruby 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0b96d42aefcf7cdc0a293085a3d93d678c62663d
4
+ data.tar.gz: cde6707f405032ad6f696c9c7a7d8ffea647cafc
5
+ SHA512:
6
+ metadata.gz: 60090694dd66693d41775605a1d17f160b8372e47e9b55350b03ac0bc3a8080b9fcc45d9180eb8a09c24b34978d54e519a4c170d79eed2684a9b863d9d3fbaca
7
+ data.tar.gz: 7b0e2b54c9540030562c1ac029aedee2a63a89d6bfcf1024830f25cc34c3740f718a66290337bf89cdf917caec4f45da06cab386aa5a3722345cf052643bb46e
@@ -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
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.0
4
+ before_install: gem install bundler -v 1.10.1
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in layer-ruby.gemspec
4
+ gemspec
@@ -0,0 +1,16 @@
1
+ guard :rspec, cmd: "bundle exec rspec" do
2
+ require "guard/rspec/dsl"
3
+ dsl = Guard::RSpec::Dsl.new(self)
4
+
5
+ # Feel free to open issues for suggestions and improvements
6
+
7
+ # RSpec files
8
+ rspec = dsl.rspec
9
+ watch(rspec.spec_helper) { rspec.spec_dir }
10
+ watch(rspec.spec_support) { rspec.spec_dir }
11
+ watch(rspec.spec_files)
12
+
13
+ # Ruby files
14
+ ruby = dsl.ruby
15
+ dsl.watch_spec_files_for(ruby.lib_files)
16
+ end
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Benedikt Deicke
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,90 @@
1
+ # Layer Platform API
2
+
3
+ [![Build Status](https://travis-ci.org/benedikt/layer-ruby.svg)](https://travis-ci.org/benedikt/layer-ruby)
4
+ [![Gem Version](https://badge.fury.io/rb/layer-ruby.svg)](http://badge.fury.io/rb/layer-ruby)
5
+
6
+ Ruby bindings for the [Layer Platform API](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-ruby', require: 'layer'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install layer-ruby
23
+
24
+ ## Usage
25
+
26
+ Please refer to the [Layer Platform API documentation](https://developer.layer.com/docs/platform) for details about the required payloads and responses.
27
+
28
+ ### Configuration
29
+
30
+ To use interact with the Layer Platform API, you need your Layer APP ID as well as a Layer Platform API Token. The gem reads both values from the `LAYER_APP_ID` and `LAYER_PLATFORM_TOKEN` environment variables. Alternatively you can specify the values by configuring the `Layer::Client` like this:
31
+
32
+ ```ruby
33
+ Layer::Client.configure do |config|
34
+ config.app_id = 'YOUR_APP_ID_HERE'
35
+ config.token = 'YOUR_PLATFORM_TOKEN_HERE'
36
+ end
37
+ ```
38
+
39
+ ### Creating Conversations
40
+
41
+ ```ruby
42
+ conversation = Layer::Conversation.create({ participants: ['1', '2'] })
43
+ ```
44
+
45
+ ### Retrieving Conversations
46
+
47
+ To retrieve a existing conversation, just use `Conversation.find` passing it the conversation id:
48
+
49
+ ```ruby
50
+ conversation = Layer::Conversation.find('CONVERSATION_ID_HERE')
51
+ ```
52
+
53
+ ### Sending Messages
54
+
55
+ 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
+
57
+ ```ruby
58
+ conversation = Layer::Conversation.find('CONVERSATION_ID_HERE')
59
+ conversation.messages.create({ sender: { name: 'Server' }, parts: [{ body: 'Hello!', mime_type: 'text/plain' }])
60
+ ```
61
+
62
+ ### Using the gem with multiple applications at once
63
+
64
+ It's possible to create a new instance of `Layer::Client` and passing both the app id and the token to the initializer:
65
+
66
+ ```ruby
67
+ client = Layer::Client.new('YOUR_APP_ID_HERE', 'YOUR_PLATFORM_TOKEN_HERE')
68
+ ```
69
+
70
+ The client will not use any global configuration. You can pass the client as a second parameter to any operations (`create`, `find`) described above.
71
+
72
+ ## Development
73
+
74
+ 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.
75
+
76
+ 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).
77
+
78
+ ## Contributing
79
+
80
+ Bug reports and pull requests are welcome on GitHub at https://github.com/benedikt/layer-ruby. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
81
+
82
+
83
+ ## License
84
+
85
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
86
+
87
+ ## Related Work
88
+
89
+ Check out the [layer-identity_token](https://github.com/dreimannzelt/layer-identity_token) gem to generate authentication tokens for the Layer SDK.
90
+
@@ -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
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "layer/ruby"
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
@@ -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
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'layer/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "layer-ruby"
8
+ spec.version = Layer::VERSION
9
+ spec.authors = ["Benedikt Deicke"]
10
+ spec.email = ["benedikt@benediktdeicke.com"]
11
+
12
+ spec.summary = %q{Ruby bindings for the Layer Platform API}
13
+ spec.description = %q{Ruby bindings for the Layer Platform API}
14
+ spec.homepage = "https://github.com/benedikt/layer-ruby"
15
+ spec.license = "MIT"
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_dependency "rest-client", "~> 1.8"
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.10"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "rspec"
27
+ spec.add_development_dependency "guard-rspec"
28
+ end
@@ -0,0 +1,10 @@
1
+ require 'layer/version'
2
+ require 'layer/client'
3
+ require 'layer/operations'
4
+ require 'layer/resource'
5
+ require 'layer/relation_proxy'
6
+ require 'layer/conversation'
7
+ require 'layer/message'
8
+
9
+ module Layer
10
+ end
@@ -0,0 +1,74 @@
1
+ require 'rest-client'
2
+
3
+ module Layer
4
+ class Client
5
+ class << self
6
+ attr_writer :app_id, :token
7
+
8
+ def app_id
9
+ @app_id || ENV['LAYER_APP_ID']
10
+ end
11
+
12
+ def token
13
+ @token ||= ENV['LAYER_PLATFORM_TOKEN']
14
+ end
15
+
16
+ def configure
17
+ yield self
18
+ end
19
+ end
20
+
21
+ attr_reader :app_id, :token
22
+
23
+ def initialize(app_id = self.class.app_id, token = self.class.token)
24
+ @app_id = app_id
25
+ @token = token
26
+ end
27
+
28
+ def get(*args)
29
+ request(:get, *args)
30
+ end
31
+
32
+ def post(*args)
33
+ request(:post, *args)
34
+ end
35
+
36
+ def patch(*args)
37
+ request(:patch, *args)
38
+ end
39
+
40
+ def put(*args)
41
+ request(:put, *args)
42
+ end
43
+
44
+ def delete(*args)
45
+ request(:delete, *args)
46
+ end
47
+
48
+ def head(*args)
49
+ request(:head, *args)
50
+ end
51
+
52
+ def options(*args)
53
+ request(:options, *args)
54
+ end
55
+
56
+ private
57
+
58
+ def request(method, url, payload = {}, headers = {})
59
+ url = "https://api.layer.com/apps/#{app_id}#{url}" unless url.start_with?('https://api.layer.com')
60
+
61
+ headers = {
62
+ 'Authorization' => "Bearer #{token}",
63
+ 'Accept' => 'application/vnd.layer+json; version=1.0',
64
+ 'Content-Type' => 'application/json'
65
+ }.merge(headers)
66
+
67
+ payload = payload.to_json
68
+
69
+ response = RestClient::Request.execute(method: method, url: url, payload: payload, headers: headers)
70
+ JSON.parse(response)
71
+ end
72
+
73
+ end
74
+ end
@@ -0,0 +1,19 @@
1
+ module Layer
2
+ class Conversation < Resource
3
+ include Operations::Find
4
+ include Operations::Create
5
+
6
+ def messages
7
+ RelationProxy.new(self, Message, [Operations::Create])
8
+ end
9
+
10
+ def distinct?
11
+ attributes['distinct']
12
+ end
13
+
14
+ def created_at
15
+ Time.parse(attributes['created_at'])
16
+ end
17
+
18
+ end
19
+ end
@@ -0,0 +1,13 @@
1
+ module Layer
2
+ class Message < Resource
3
+
4
+ def conversation
5
+ Conversation.from_response(attributes['conversation'], client)
6
+ end
7
+
8
+ def sent_at
9
+ Time.parse(attributes['sent_at'])
10
+ end
11
+
12
+ end
13
+ end
@@ -0,0 +1,2 @@
1
+ require 'layer/operations/create'
2
+ require 'layer/operations/find'
@@ -0,0 +1,18 @@
1
+ module Layer
2
+ module Operations
3
+ module Create
4
+
5
+ module ClassMethods
6
+ def create(attributes = {}, client = Client.new)
7
+ response = client.post(url, attributes)
8
+ from_response(response, client)
9
+ end
10
+ end
11
+
12
+ def self.included(base)
13
+ base.extend(ClassMethods)
14
+ end
15
+
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,19 @@
1
+ module Layer
2
+ module Operations
3
+ module Find
4
+
5
+ module ClassMethods
6
+ def find(id, client = Client.new)
7
+ response = client.get("#{url}/#{id}")
8
+ from_response(response, client)
9
+ end
10
+ end
11
+
12
+ def self.included(base)
13
+ base.extend(ClassMethods)
14
+ end
15
+
16
+ end
17
+ end
18
+ end
19
+
@@ -0,0 +1,32 @@
1
+ module Layer
2
+ class RelationProxy
3
+
4
+ attr_reader :resource_type, :base
5
+
6
+ def initialize(base, resource_type, operations = [])
7
+ @resource_type = resource_type
8
+ @base = base
9
+
10
+ operations.each { |operation| singleton_class.include(operation::ClassMethods) }
11
+ end
12
+
13
+ def url
14
+ "#{base.url}#{resource_type.url}"
15
+ end
16
+
17
+ def respond_to_missing?(method, include_private = false)
18
+ resource_type.respond_to?(method, include_private) || super
19
+ end
20
+
21
+ private
22
+
23
+ def method_missing(method, *args, &block)
24
+ if resource_type.respond_to?(method)
25
+ resource_type.public_send(method, *args, &block)
26
+ else
27
+ super
28
+ end
29
+ end
30
+
31
+ end
32
+ end
@@ -0,0 +1,48 @@
1
+ module Layer
2
+ class Resource
3
+
4
+ class << self
5
+ def class_name
6
+ name.split('::')[-1]
7
+ end
8
+
9
+ def url
10
+ "/#{class_name.downcase.to_s}s"
11
+ end
12
+
13
+ def from_response(attributes, client)
14
+ new(attributes, client)
15
+ end
16
+ end
17
+
18
+ def initialize(attributes = {}, client = Client.new)
19
+ @attributes = attributes
20
+ @client = client
21
+ end
22
+
23
+ def url
24
+ attributes['url']
25
+ end
26
+
27
+ def id
28
+ attributes['id']
29
+ end
30
+
31
+ def respond_to_missing?(method, include_private = false)
32
+ attributes.has_key?(method.to_s) || super
33
+ end
34
+
35
+ private
36
+
37
+ attr_reader :attributes, :client
38
+
39
+ def method_missing(method, *args, &block)
40
+ if attributes.has_key?(method.to_s)
41
+ attributes[method.to_s]
42
+ else
43
+ super
44
+ end
45
+ end
46
+
47
+ end
48
+ end
@@ -0,0 +1 @@
1
+ require 'layer'
@@ -0,0 +1,3 @@
1
+ module Layer
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,138 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: layer-ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Benedikt Deicke
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-06-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rest-client
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.8'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.8'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.10'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.10'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: guard-rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Ruby bindings for the Layer Platform API
84
+ email:
85
+ - benedikt@benediktdeicke.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - ".travis.yml"
93
+ - CODE_OF_CONDUCT.md
94
+ - Gemfile
95
+ - Guardfile
96
+ - LICENSE.txt
97
+ - README.md
98
+ - Rakefile
99
+ - bin/console
100
+ - bin/setup
101
+ - layer-ruby.gemspec
102
+ - lib/layer.rb
103
+ - lib/layer/client.rb
104
+ - lib/layer/conversation.rb
105
+ - lib/layer/message.rb
106
+ - lib/layer/operations.rb
107
+ - lib/layer/operations/create.rb
108
+ - lib/layer/operations/find.rb
109
+ - lib/layer/relation_proxy.rb
110
+ - lib/layer/resource.rb
111
+ - lib/layer/ruby.rb
112
+ - lib/layer/version.rb
113
+ homepage: https://github.com/benedikt/layer-ruby
114
+ licenses:
115
+ - MIT
116
+ metadata: {}
117
+ post_install_message:
118
+ rdoc_options: []
119
+ require_paths:
120
+ - lib
121
+ required_ruby_version: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - ">="
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ required_rubygems_version: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - ">="
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
131
+ requirements: []
132
+ rubyforge_project:
133
+ rubygems_version: 2.4.5
134
+ signing_key:
135
+ specification_version: 4
136
+ summary: Ruby bindings for the Layer Platform API
137
+ test_files: []
138
+ has_rdoc: