wordsapi 0.1.0

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: ff450a4b563e5fca11d53b9cf94cbb5aac089b493edf3248537b2e8d07e2cae7
4
+ data.tar.gz: 4f7122b5bc61a26dd0b094dd9859d9573009acb90a8562e6c74a0fe8c30e8df9
5
+ SHA512:
6
+ metadata.gz: 7e1055ac20be31841797d96db6ac262e71eb7d5567c16be040c1ce04aee95ba7c4538b2a9b45671ecf5a0e438c290035fcd5771c83f6a8bd520f57c8aa09c7a6
7
+ data.tar.gz: 21000cc30f9e520bd9597bf597ffbdcf0f515aac20f670cf2f306fa3372d2e89bad2e1db3e32b56f20f6d4ad971c9b835e151029717ea7507168df0eb8f4505b
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2018 Nardo Nykolyszyn
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
File without changes
@@ -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,98 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'wordsapi/base'
4
+
5
+ class WordsAPI < Base
6
+
7
+ def service_url
8
+ 'https://wordsapiv1.p.rapidapi.com/words'
9
+ end
10
+
11
+ def has_types?(word)
12
+ response = connection.get has_types_endpoint(word)
13
+ response = process_response(response)
14
+ OpenStruct.new(success?: true, body: response)
15
+ rescue Faraday::ClientError => exception
16
+ OpenStruct.new(success?: false, message: 'Wrong data provided', details: exception.response[:body].to_s)
17
+ rescue Faraday::Error::TimeoutError, Faraday::ConnectionFailed, Timeout::Error => e
18
+ OpenStruct.new(success?: false, message: 'Words API is not available')
19
+ end
20
+
21
+ def type_of?(word)
22
+ response = connection.get type_of_endpoint(word)
23
+ response = process_response(response)
24
+ OpenStruct.new(success?: true, body: response)
25
+ rescue Faraday::ClientError => exception
26
+ OpenStruct.new(success?: false, message: 'Wrong data provided', details: exception.response[:body].to_s)
27
+ rescue Faraday::Error::TimeoutError, Faraday::ConnectionFailed, Timeout::Error => e
28
+ OpenStruct.new(success?: false, message: 'Words API is not available')
29
+ end
30
+
31
+ def part_of?(word)
32
+ response = connection.get part_of_endpoint(word)
33
+ response = process_response(response)
34
+ OpenStruct.new(success?: true, body: response)
35
+ rescue Faraday::ClientError => exception
36
+ OpenStruct.new(success?: false, message: 'Wrong data provided', details: exception.response[:body].to_s)
37
+ rescue Faraday::Error::TimeoutError, Faraday::ConnectionFailed, Timeout::Error => e
38
+ OpenStruct.new(success?: false, message: 'Words API is not available')
39
+ end
40
+
41
+ def has_parts?(word)
42
+ response = connection.get has_parts_endpoint(word)
43
+ response = process_response(response)
44
+ OpenStruct.new(success?: true, body: response)
45
+ rescue Faraday::ClientError => exception
46
+ OpenStruct.new(success?: false, message: 'Wrong data provided', details: exception.response[:body].to_s)
47
+ rescue Faraday::Error::TimeoutError, Faraday::ConnectionFailed, Timeout::Error => e
48
+ OpenStruct.new(success?: false, message: 'Words API is not available')
49
+ end
50
+
51
+ def similar_to(word)
52
+ response = connection.get similar_to_endpoint(word)
53
+ response = process_response(response)
54
+ OpenStruct.new(success?: true, body: response)
55
+ rescue Faraday::ClientError => exception
56
+ OpenStruct.new(success?: false, message: 'Wrong data provided', details: exception.response[:body].to_s)
57
+ rescue Faraday::Error::TimeoutError, Faraday::ConnectionFailed, Timeout::Error => e
58
+ OpenStruct.new(success?: false, message: 'Words API is not available')
59
+ end
60
+
61
+ def usage_of(word)
62
+ response = connection.get usage_of_endpoint(word)
63
+ response = process_response(response)
64
+ OpenStruct.new(success?: true, body: response)
65
+ rescue Faraday::ClientError => exception
66
+ OpenStruct.new(success?: false, message: 'Wrong data provided', details: exception.response[:body].to_s)
67
+ rescue Faraday::Error::TimeoutError, Faraday::ConnectionFailed, Timeout::Error => e
68
+ OpenStruct.new(success?: false, message: 'Words API is not available')
69
+ end
70
+
71
+
72
+
73
+ private
74
+
75
+ def has_types_endpoint(word)
76
+ "/#{word}/hasTypes"
77
+ end
78
+
79
+ def type_of_endpoint(word)
80
+ "/#{word}/typeOf"
81
+ end
82
+
83
+ def part_of_endpoint(word)
84
+ "/#{word}/partOf"
85
+ end
86
+
87
+ def has_parts_endpoint(word)
88
+ "/#{word}/hasParts"
89
+ end
90
+
91
+ def similar_to_endpoint(word)
92
+ "/#{word}/similarTo"
93
+ end
94
+
95
+ def usage_of_endpoint(word)
96
+ "/#{word}/usageOf"
97
+ end
98
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SymbolizeHelper
4
+ extend self
5
+
6
+ def symbolize_recursive(hash)
7
+ {}.tap do |h|
8
+ hash.each { |key, value| h[key.to_sym] = transform(value) }
9
+ end
10
+ end
11
+
12
+ private
13
+
14
+ def transform(thing)
15
+ case thing
16
+ when Hash; symbolize_recursive(thing)
17
+ when Array; thing.map { |v| transform(v) }
18
+ else; thing
19
+ end
20
+ end
21
+
22
+ refine Hash do
23
+ def deep_symbolize_keys
24
+ SymbolizeHelper.symbolize_recursive(self)
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'faraday/request_id'
4
+ require 'faraday_middleware'
5
+ require 'wordsapi/active_support/deep_symbolize_keys'
6
+
7
+ class Base
8
+ using SymbolizeHelper
9
+ attr_reader :access_token
10
+
11
+ def initialize(access_token: '')
12
+ @access_token = access_token
13
+ end
14
+
15
+ def service_url
16
+ raise NotImplementedError
17
+ end
18
+
19
+ private
20
+
21
+ def connection
22
+ @connection ||= Faraday.new(service_url) do |config|
23
+ config.request :json
24
+ config.response :raise_error
25
+ config.response :json, content_type: /\bjson$/
26
+ config.use :instrumentation
27
+ config.options[:timeout] = 10
28
+ config.use Faraday::RequestId
29
+ config.adapter Faraday.default_adapter
30
+ config.headers['X-RapidAPI-Key'] = access_token
31
+ end
32
+ end
33
+
34
+ def process_response(response)
35
+ if response.success?
36
+ body = response.body
37
+ if body.empty?
38
+ {}
39
+ elsif body.is_a?(Array)
40
+ body.map(&:deep_symbolize_keys)
41
+ else
42
+ body.deep_symbolize_keys
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module WordsAPI
4
+ class Version
5
+ MAJOR = 0
6
+ MINOR = 1
7
+ PATCH = 0
8
+
9
+ class << self
10
+ # @return [String]
11
+ def to_s
12
+ [MAJOR, MINOR, PATCH].compact.join('.')
13
+ end
14
+ end
15
+ end
16
+ end
17
+
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('../lib', __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'wordsapi/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'wordsapi'
9
+ spec.version = WordsAPI::Version
10
+ spec.authors = ['Nardo Nykolyszyn']
11
+ spec.email = ['devpolish@protonmail.com']
12
+ spec.summary = 'Words API gem'
13
+ spec.description = 'A Ruby interface to the Words API'
14
+ spec.homepage = 'https://github.com/devpolish/wordsapi'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = %w[LICENSE.txt README.md Rakefile wordsapi.gemspec]
18
+ spec.files += Dir.glob('lib/**/*.rb')
19
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
20
+ spec.test_files = Dir.glob('test/**/*')
21
+ spec.require_paths = ['lib']
22
+ spec.required_rubygems_version = '>= 1.3.5'
23
+
24
+ # Development dependencies
25
+ spec.add_development_dependency 'bundler', '~> 1.5'
26
+ spec.add_development_dependency 'rake', '~> 0'
27
+ spec.add_development_dependency 'rspec', '~> 3.3.0'
28
+
29
+ # Productions dependencies
30
+ spec.add_dependency 'faraday', '~> 0.9.0'
31
+ spec.add_dependency 'faraday_middleware'
32
+ spec.add_dependency 'json'
33
+ spec.add_dependency 'request_id'
34
+ end
metadata ADDED
@@ -0,0 +1,150 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: wordsapi
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Nardo Nykolyszyn
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-01-02 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.5'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '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.0
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 3.3.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: faraday
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.9.0
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.9.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: faraday_middleware
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: json
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: request_id
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: A Ruby interface to the Words API
112
+ email:
113
+ - devpolish@protonmail.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - LICENSE.txt
119
+ - README.md
120
+ - Rakefile
121
+ - lib/wordsapi.rb
122
+ - lib/wordsapi/active_support/deep_symbolize_keys.rb
123
+ - lib/wordsapi/base.rb
124
+ - lib/wordsapi/version.rb
125
+ - wordsapi.gemspec
126
+ homepage: https://github.com/devpolish/wordsapi
127
+ licenses:
128
+ - MIT
129
+ metadata: {}
130
+ post_install_message:
131
+ rdoc_options: []
132
+ require_paths:
133
+ - lib
134
+ required_ruby_version: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ required_rubygems_version: !ruby/object:Gem::Requirement
140
+ requirements:
141
+ - - ">="
142
+ - !ruby/object:Gem::Version
143
+ version: 1.3.5
144
+ requirements: []
145
+ rubyforge_project:
146
+ rubygems_version: 2.7.6
147
+ signing_key:
148
+ specification_version: 4
149
+ summary: Words API gem
150
+ test_files: []