talkgh 0.0.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 9d7123a6858aa39ee476856f48e8d9825e1ae246264676404d9f9f7a42339c5a
4
+ data.tar.gz: ee372d9c20209b41e820fce88c612327126544b446e58822b6c7d41b1c94534b
5
+ SHA512:
6
+ metadata.gz: 40cc974674ecef226c95d03e61485821fd9bfb72ba3b35e2e895c6d056d799173a243725c8c8b0ef7050052e07f4caea739b183e72621a00b829af201d9ef659
7
+ data.tar.gz: 68c5146a93b53038b7a9f71283084904b184c43ac2bce1654386db834396a8d0a2eb859f5553f5a6fcdd4254dddec30ba83301c46fd3cf6727b9d5bdaab0e541
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 TODO: Write your name
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.
data/README.md ADDED
@@ -0,0 +1,43 @@
1
+ # Talkgh
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/talkgh`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'talkgh'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install talkgh
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ 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).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/talkgh. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
40
+
41
+ ## Code of Conduct
42
+
43
+ Everyone interacting in the Talkgh project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/talkgh/blob/master/CODE_OF_CONDUCT.md).
data/lib/talkgh.rb ADDED
@@ -0,0 +1,12 @@
1
+ require "talkgh/version"
2
+ require "talkgh/keys"
3
+ require "talkgh/errors/error"
4
+ require "talkgh/errors/client_error"
5
+ require "talkgh/errors/server_error"
6
+ require "talkgh/errors/authentication_error"
7
+ require "talkgh/namespace"
8
+ require "talkgh/client"
9
+ require "talkgh/sms"
10
+ require "talkgh/params"
11
+ require "talkgh/entity"
12
+ require "talkgh/logger"
@@ -0,0 +1,34 @@
1
+ module Talkgh
2
+ class Client
3
+ attr_writer :api_token
4
+
5
+ def initialize(options = {})
6
+ @api_token = options[:api_token] || ENV['TALKGH_API_TOKEN']
7
+
8
+ self.logger = options[:logger] || (defined?(Rails.logger) && Rails.logger)
9
+ end
10
+
11
+ def api_token
12
+ unless @api_token
13
+ raise AuthenticationError.new('No API token provided. ' \
14
+ 'See https://talkgh.com for details.')
15
+ end
16
+
17
+ @api_token
18
+ end
19
+
20
+ def sms
21
+ @sms ||= SMS.new(self)
22
+ end
23
+
24
+ def logger
25
+ @logger
26
+ end
27
+
28
+ def logger=(logger)
29
+ @logger = Logger.new(logger)
30
+ end
31
+
32
+ end
33
+ end
34
+
@@ -0,0 +1,36 @@
1
+ module Talkgh
2
+ class Entity
3
+ include Keys
4
+
5
+ def initialize(**kwargs)
6
+ @attributes = kwargs
7
+ end
8
+
9
+ def []=(key, value)
10
+ @attributes[attribute_key(key)] = value
11
+ end
12
+
13
+ def respond_to_missing?(name, include_private = false)
14
+ @attributes.key?(name) or super
15
+ end
16
+
17
+ def method_missing(name, *args)
18
+ return super unless @attributes.key?(name)
19
+
20
+ @attributes[name]
21
+ end
22
+
23
+ def ==(entity)
24
+ entity.class == self.class && entity.attributes == @attributes
25
+ end
26
+
27
+ def to_h
28
+ @attributes
29
+ end
30
+
31
+ attr_reader :attributes
32
+
33
+ protected :attributes
34
+ end
35
+ end
36
+
@@ -0,0 +1,4 @@
1
+ module Talkgh
2
+ class AuthenticationError < ClientError
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module Talkgh
2
+ class ClientError < Error
3
+ end
4
+ end
@@ -0,0 +1,31 @@
1
+ require 'json'
2
+
3
+ module Talkgh
4
+ class Error < StandardError
5
+ def self.parse(response)
6
+ exception_class = case response
7
+ when Net::HTTPUnauthorized
8
+ AuthenticationError
9
+ when Net::HTTPClientError
10
+ ClientError
11
+ when Net::HTTPServerError
12
+ ServerError
13
+ else
14
+ Error
15
+ end
16
+
17
+ message = if content_type = response['Content-Type']
18
+ case content_type.split(';').first
19
+ when 'application/problem+json'
20
+ Problem.parse(response.body)
21
+ when 'application/json'
22
+ hash = ::JSON.parse(response.body)
23
+ hash['error_title']
24
+ end
25
+ end
26
+
27
+ exception_class.new(message)
28
+ end
29
+
30
+ end
31
+ end
@@ -0,0 +1,4 @@
1
+ module Talkgh
2
+ class ServerError < Error
3
+ end
4
+ end
@@ -0,0 +1,45 @@
1
+
2
+ module Talkgh
3
+ module Keys
4
+ if {}.respond_to?(:transform_keys)
5
+ def hyphenate(hash)
6
+ hash.transform_keys { |k| hyphenate_key(k) }
7
+ end
8
+
9
+ def camelcase(hash)
10
+ hash.transform_keys { |k| camelcase_key(k) }
11
+ end
12
+ else
13
+ def hyphenate(hash)
14
+ hash.each_with_object({}) { |(k, v), h| h[hyphenate_key(k)] = v }
15
+ end
16
+
17
+ def camelcase(hash)
18
+ hash.each_with_object({}) { |(k, v), h| h[camelcase_key(k)] = v }
19
+ end
20
+ end
21
+
22
+ def hyphenate_key(k)
23
+ k.to_s.tr('_', '-')
24
+ end
25
+
26
+ def camelcase_key(k)
27
+ k.to_s.gsub(/_(\w)/) { $1.upcase }
28
+ end
29
+
30
+ ATTRIBUTE_KEYS = Hash.new { |h, k| h[k] = k.split(PATTERN).join('_').downcase.to_sym }
31
+
32
+ PATTERN = /[\-_]|(?<=\w)(?=[A-Z])/
33
+
34
+ private_constant :ATTRIBUTE_KEYS
35
+
36
+ private_constant :PATTERN
37
+
38
+ def attribute_key(k)
39
+ return k if k.is_a?(Symbol)
40
+
41
+ ATTRIBUTE_KEYS[k]
42
+ end
43
+ end
44
+ end
45
+
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+ require 'logger'
3
+ require 'forwardable'
4
+
5
+ module Talkgh
6
+ class Logger # :nodoc:
7
+ def initialize(logger)
8
+ @logger = logger || ::Logger.new(nil)
9
+ end
10
+
11
+ extend Forwardable
12
+
13
+ ::Logger::Severity.constants.map(&:downcase).each do |name|
14
+ def_delegator :@logger, name, name
15
+ end
16
+
17
+ def log_request_info(request)
18
+ info do
19
+ format('Talkgh API request', {
20
+ method: request.method,
21
+ path: request.uri.path
22
+ })
23
+ end
24
+ end
25
+
26
+ def log_response_info(response, host)
27
+ info do
28
+ format('Talkgh API response', {
29
+ host: host,
30
+ status: response.code,
31
+ type: response.content_type,
32
+ length: response.content_length
33
+ })
34
+ end
35
+ end
36
+
37
+ private
38
+
39
+ def format(message, hash)
40
+ return message if hash.nil?
41
+
42
+ fields = hash.map { |key, value| "#{key}=#{value}" if value }.compact
43
+ fields.unshift(message)
44
+ fields.join(' ')
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,71 @@
1
+ require 'net/http'
2
+ require 'json'
3
+
4
+ module Talkgh
5
+ class Namespace
6
+ def initialize(client)
7
+ @client = client
8
+ @host = self.class.host
9
+ @logger = client.logger
10
+ @api_token = client.api_token
11
+ end
12
+
13
+ def self.host
14
+ @host ||= 'api.talkgh.com/api/'
15
+ end
16
+
17
+ def self.host=(host)
18
+ @host = host
19
+ end
20
+
21
+
22
+ private
23
+
24
+ Get = Net::HTTP::Get
25
+ Put = Net::HTTP::Put
26
+ Post = Net::HTTP::Post
27
+ Delete = Net::HTTP::Delete
28
+
29
+ def request(params: nil, type: Get)
30
+ uri = URI('https://' + @host + @api_token)
31
+
32
+ params ||= {}
33
+
34
+ unless type::REQUEST_HAS_BODY || params.empty?
35
+ uri.query = Params.encode(params)
36
+ end
37
+
38
+ http = Net::HTTP.new(uri.host, uri.port)
39
+ http.use_ssl = true
40
+
41
+ message = type.new(uri)
42
+
43
+ @logger.log_request_info(message)
44
+
45
+ response = http.request(message)
46
+
47
+ @logger.log_response_info(response, @host)
48
+
49
+ @logger.debug(response.body) if response.body
50
+
51
+ parse(response)
52
+ end
53
+
54
+ def parse(response)
55
+ case response
56
+ when Net::HTTPNoContent
57
+ :no_content
58
+ when Net::HTTPSuccess
59
+ if response['Content-Type'].split(';').first == 'application/json'
60
+ ::JSON.parse(response.body, object_class: Talkgh::Entity)
61
+ else
62
+ response
63
+ end
64
+ else
65
+ raise Error.parse(response)
66
+ end
67
+ end
68
+
69
+
70
+ end
71
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+ require 'cgi'
3
+
4
+ module Talkgh
5
+ class Params
6
+ def self.encode(params)
7
+ params.flat_map { |k, vs| Array(vs).map { |v| "#{escape(k)}=#{escape(v)}" } }.join('&')
8
+ end
9
+
10
+ def self.join(string, params)
11
+ encoded = encode(params)
12
+
13
+ return encoded if string.nil?
14
+
15
+ string + '&' + encoded
16
+ end
17
+
18
+ def self.escape(component)
19
+ CGI.escape(component.to_s)
20
+ end
21
+
22
+ private_class_method :escape
23
+ end
24
+ end
data/lib/talkgh/sms.rb ADDED
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Talkgh
4
+ class SMS < Namespace
5
+ include Keys
6
+
7
+ self.host = 'api.talkgh.com/api/'
8
+
9
+ def send(params)
10
+ request(params: hyphenate(params), type: Get)
11
+ end
12
+ end
13
+ end
14
+
@@ -0,0 +1,3 @@
1
+ module Talkgh
2
+ VERSION = "0.0.1"
3
+ end
data/talkgh.gemspec ADDED
@@ -0,0 +1,27 @@
1
+ require File.expand_path('lib/talkgh/version', File.dirname(__FILE__))
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = 'talkgh'
5
+ s.version = Talkgh::VERSION
6
+ s.license = 'MIT'
7
+ s.platform = Gem::Platform::RUBY
8
+ s.authors = ['Talkgh']
9
+ s.email = ['devrel@talkgh.com']
10
+ s.homepage = 'https://github.com/Talkgh/talkgh-ruby'
11
+ s.description = 'Talkgh Client Library for Ruby'
12
+ s.summary = 'This is the Ruby client library for Talkgh\'s API. To use it you\'ll need a Talkgh account. Sign up for free at https://www.talkgh.com'
13
+ s.files = Dir.glob('lib/**/*.rb') + %w(LICENSE.txt README.md talkgh.gemspec)
14
+ s.required_ruby_version = '>= 2.1.0'
15
+ s.add_dependency('jwt', '~> 2')
16
+ s.add_development_dependency('rake', '~> 12.0')
17
+ s.add_development_dependency('minitest', '~> 5.0')
18
+ s.add_development_dependency('webmock', '~> 3.0')
19
+ s.add_development_dependency('simplecov', '~> 0.16')
20
+ s.require_path = 'lib'
21
+ s.metadata = {
22
+ 'homepage' => 'https://github.com/Talkgh/talkgh-ruby',
23
+ 'source_code_uri' => 'https://github.com/Talkgh/talkgh-ruby',
24
+ 'bug_tracker_uri' => 'https://github.com/Talkgh/talkgh-ruby/issues',
25
+ 'changelog_uri' => 'https://github.com/Talkgh/talkgh-ruby/blob/master/CHANGES.md'
26
+ }
27
+ end
metadata ADDED
@@ -0,0 +1,134 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: talkgh
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Talkgh
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-12-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: jwt
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '12.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '12.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: webmock
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: simplecov
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0.16'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0.16'
83
+ description: Talkgh Client Library for Ruby
84
+ email:
85
+ - devrel@talkgh.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - LICENSE.txt
91
+ - README.md
92
+ - lib/talkgh.rb
93
+ - lib/talkgh/client.rb
94
+ - lib/talkgh/entity.rb
95
+ - lib/talkgh/errors/authentication_error.rb
96
+ - lib/talkgh/errors/client_error.rb
97
+ - lib/talkgh/errors/error.rb
98
+ - lib/talkgh/errors/server_error.rb
99
+ - lib/talkgh/keys.rb
100
+ - lib/talkgh/logger.rb
101
+ - lib/talkgh/namespace.rb
102
+ - lib/talkgh/params.rb
103
+ - lib/talkgh/sms.rb
104
+ - lib/talkgh/version.rb
105
+ - talkgh.gemspec
106
+ homepage: https://github.com/Talkgh/talkgh-ruby
107
+ licenses:
108
+ - MIT
109
+ metadata:
110
+ homepage: https://github.com/Talkgh/talkgh-ruby
111
+ source_code_uri: https://github.com/Talkgh/talkgh-ruby
112
+ bug_tracker_uri: https://github.com/Talkgh/talkgh-ruby/issues
113
+ changelog_uri: https://github.com/Talkgh/talkgh-ruby/blob/master/CHANGES.md
114
+ post_install_message:
115
+ rdoc_options: []
116
+ require_paths:
117
+ - lib
118
+ required_ruby_version: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - ">="
121
+ - !ruby/object:Gem::Version
122
+ version: 2.1.0
123
+ required_rubygems_version: !ruby/object:Gem::Requirement
124
+ requirements:
125
+ - - ">="
126
+ - !ruby/object:Gem::Version
127
+ version: '0'
128
+ requirements: []
129
+ rubygems_version: 3.0.4
130
+ signing_key:
131
+ specification_version: 4
132
+ summary: This is the Ruby client library for Talkgh's API. To use it you'll need a
133
+ Talkgh account. Sign up for free at https://www.talkgh.com
134
+ test_files: []