talk_api 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
+ SHA256:
3
+ metadata.gz: 8f8163cd465e95413ae824cd1abc7a3d6f2437f0a0b6af3159b95d0c4bb20195
4
+ data.tar.gz: a950bab495badd9162d99b9a40193ee88f7defd55c0151d4a9ea400263bc75bb
5
+ SHA512:
6
+ metadata.gz: 835918554d5f846929bfbbc629e50469d380ca3eda566bc12a0472c5cfc2ad51fe22138c6b640d476db347a17d37a82a8d14eed18ddadba86933bb9b7adcb4f8
7
+ data.tar.gz: f47db4465c3d1b0312e0b820e9204c2371ff6b0eeee142296921f3d39ca67b482d0e1f0e8d99a599cfc6e723e28b6564bb637e51e18aa6c0be4835a534d8940b
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ /Gemfile.lock
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.7.1
6
+ before_install: gem install bundler -v 2.1.4
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in talk_api.gemspec
4
+ gemspec
5
+
6
+ gem 'rake', '~> 12.0'
7
+ gem 'rspec', '~> 3.0'
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Ryo Nakano
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,40 @@
1
+ # TalkApi
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/talk_api`. 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 'talk_api'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle install
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install talk_api
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. Then, run `rake spec` to run the tests. 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/ryonkn/talk_api.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -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 'talk_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(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'talk_api/version'
4
+ require 'talk_api/client'
5
+ require 'talk_api/request'
6
+ require 'talk_api/response'
7
+ require 'talk_api/result'
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TalkApi
4
+ # Entry point class
5
+ class Client
6
+ def initialize(api_key: nil)
7
+ @api_key = api_key
8
+ end
9
+
10
+ def smalltalk(message)
11
+ Request.new.call(@api_key, message)
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'httpclient'
4
+
5
+ module TalkApi
6
+ # API request class
7
+ class Request
8
+ ENDPOINT_URL = 'https://api.a3rt.recruit-tech.co.jp/talk/v1/smalltalk'.freeze
9
+ USER_AGENT = "#{self} #{VERSION}".freeze
10
+ HEADERS = [
11
+ ['User-Agent', USER_AGENT]
12
+ ].freeze
13
+
14
+ def call(api_key, message)
15
+ query = message.byteslice(0, 2048).scrub('')
16
+ client = HTTPClient.new
17
+ response = client.post(ENDPOINT_URL, { apikey: api_key, query: query }, header: HEADERS)
18
+ Response.new(response.body)
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'json'
4
+
5
+ module TalkApi
6
+ # Response class
7
+ class Response
8
+ attr_reader :status, :message, :results
9
+
10
+ def initialize(data = '{}')
11
+ json = JSON.parse(data, symbolize_names: true)
12
+
13
+ @status = json.key?(:status) ? json[:status] : nil
14
+ @message = json.key?(:message) ? json[:message] : nil
15
+ @results = json[:results].map { |result| Result.new(result[:perplexity], result[:reply]) } if success?
16
+ end
17
+
18
+ def success?
19
+ @status.zero?
20
+ end
21
+
22
+ def first_message
23
+ return nil unless success?
24
+
25
+ @results.first.reply
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TalkApi
4
+ # Store result class
5
+ class Result
6
+ attr_reader :perplexity, :reply
7
+
8
+ def initialize(perplexity, reply)
9
+ @perplexity = perplexity
10
+ @reply = reply
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TalkApi
4
+ VERSION = '0.1.0'
5
+ end
@@ -0,0 +1,29 @@
1
+ require_relative 'lib/talk_api/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = 'talk_api'
5
+ spec.version = TalkApi::VERSION
6
+ spec.authors = ['Ryo Nakano']
7
+ spec.email = ['ryo.z.nakano@gmail.com']
8
+
9
+ spec.summary = 'Client library for talk API'
10
+ spec.description = 'Client library for talk API'
11
+ spec.homepage = 'https://github.com/ryonkn/talk_api'
12
+ spec.license = 'MIT'
13
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.5.0')
14
+
15
+ spec.metadata['homepage_uri'] = spec.homepage
16
+ spec.metadata['source_code_uri'] = spec.homepage
17
+ spec.metadata['changelog_uri'] = spec.homepage
18
+
19
+ # Specify which files should be added to the gem when it is released.
20
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
21
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
22
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
23
+ end
24
+ spec.bindir = 'exe'
25
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
26
+ spec.require_paths = ['lib']
27
+
28
+ spec.add_dependency 'httpclient', '~> 2.8'
29
+ end
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: talk_api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ryo Nakano
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-08-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: httpclient
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.8'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.8'
27
+ description: Client library for talk API
28
+ email:
29
+ - ryo.z.nakano@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".gitignore"
35
+ - ".rspec"
36
+ - ".travis.yml"
37
+ - Gemfile
38
+ - LICENSE.txt
39
+ - README.md
40
+ - Rakefile
41
+ - bin/console
42
+ - bin/setup
43
+ - lib/talk_api.rb
44
+ - lib/talk_api/client.rb
45
+ - lib/talk_api/request.rb
46
+ - lib/talk_api/response.rb
47
+ - lib/talk_api/result.rb
48
+ - lib/talk_api/version.rb
49
+ - talk_api.gemspec
50
+ homepage: https://github.com/ryonkn/talk_api
51
+ licenses:
52
+ - MIT
53
+ metadata:
54
+ homepage_uri: https://github.com/ryonkn/talk_api
55
+ source_code_uri: https://github.com/ryonkn/talk_api
56
+ changelog_uri: https://github.com/ryonkn/talk_api
57
+ post_install_message:
58
+ rdoc_options: []
59
+ require_paths:
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: 2.5.0
66
+ required_rubygems_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ requirements: []
72
+ rubygems_version: 3.1.2
73
+ signing_key:
74
+ specification_version: 4
75
+ summary: Client library for talk API
76
+ test_files: []