oxford_dictionary 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +8 -0
- data/CODE_OF_CONDUCT.md +73 -0
- data/Gemfile +9 -0
- data/LICENSE.txt +21 -0
- data/README.md +105 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/oxford_dictionary/api_objects/entry.rb +11 -0
- data/lib/oxford_dictionary/api_objects/entry_response.rb +12 -0
- data/lib/oxford_dictionary/api_objects/lexical_entry.rb +16 -0
- data/lib/oxford_dictionary/api_objects/list_response.rb +7 -0
- data/lib/oxford_dictionary/api_objects/pronunciation.rb +10 -0
- data/lib/oxford_dictionary/api_objects/sense.rb +19 -0
- data/lib/oxford_dictionary/client.rb +25 -0
- data/lib/oxford_dictionary/endpoints/entry_endpoint.rb +60 -0
- data/lib/oxford_dictionary/endpoints/inflection_endpoint.rb +16 -0
- data/lib/oxford_dictionary/endpoints/search_endpoint.rb +17 -0
- data/lib/oxford_dictionary/endpoints/wordlist_endpoint.rb +32 -0
- data/lib/oxford_dictionary/error.rb +10 -0
- data/lib/oxford_dictionary/request.rb +114 -0
- data/lib/oxford_dictionary/version.rb +3 -0
- data/lib/oxford_dictionary.rb +18 -0
- data/oxford_dictionary.gemspec +33 -0
- metadata +182 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: c2bc4731fbb7b2901e23070998aaeb6fb8d61131
|
4
|
+
data.tar.gz: c6332d8e683d8baac52a0e415508c08e08d7af08
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 445ece08c1459cb3fdc1d47696f1b37b1472a32f259070ab37ceadb4f553d7e99980b215e0cf2de9034d725a0b957f2726de38540870bdce07d55bea551e8362
|
7
|
+
data.tar.gz: 1016dac1ce6bafc64f200118e39224d2c8d0f07af89ded910999faed3b07876c1120205203f7f0c475d79bdf17c2e9b869b486b30010bff55fef193a33020d07
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
complaints will be reviewed and investigated and will result in a response that
|
59
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
60
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
61
|
+
Further details of specific enforcement policies may be posted separately.
|
62
|
+
|
63
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
64
|
+
faith may face temporary or permanent repercussions as determined by other
|
65
|
+
members of the project's leadership.
|
66
|
+
|
67
|
+
## Attribution
|
68
|
+
|
69
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
70
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
71
|
+
|
72
|
+
[homepage]: http://contributor-covenant.org
|
73
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 swcraig
|
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,105 @@
|
|
1
|
+
[![Build Status](https://travis-ci.org/swcraig/oxford-dictionary.svg?branch=master)](https://travis-ci.org/swcraig/oxford-dictionary)
|
2
|
+
[![Test Coverage](https://codeclimate.com/github/swcraig/oxford-dictionary/badges/coverage.svg)](https://codeclimate.com/github/swcraig/oxford-dictionary/coverage)
|
3
|
+
[![Code Climate](https://codeclimate.com/github/swcraig/oxford-dictionary/badges/gpa.svg)](https://codeclimate.com/github/swcraig/oxford-dictionary)
|
4
|
+
# OxfordDictionary
|
5
|
+
|
6
|
+
Ruby wrapper to consume the [Oxford Dictionary API](https://developer.oxforddictionaries.com/documentation)
|
7
|
+
|
8
|
+
## Getting Started
|
9
|
+
|
10
|
+
$ gem install oxford_dictionary
|
11
|
+
|
12
|
+
# To use in your script/application
|
13
|
+
require 'oxford_dictionary'
|
14
|
+
|
15
|
+
After registering for an API key, setup the client:
|
16
|
+
|
17
|
+
client = OxfordDictionary::Client.new(app_id: 'ID', app_key: 'SECRET')
|
18
|
+
client = OxfordDictionary.new(app_id: 'ID', app_key: 'SECRET')
|
19
|
+
|
20
|
+
### Usage Examples
|
21
|
+
###### Get the results for an entry
|
22
|
+
|
23
|
+
entry = client.entry('vapid')
|
24
|
+
|
25
|
+
# Access the first entry
|
26
|
+
# Refer to the API documentation for the schema of the returned data structure
|
27
|
+
first_lexical_entry = entry.lexical_entries[0]
|
28
|
+
|
29
|
+
# With some filters
|
30
|
+
filters = { lexicalCategory: 'Verb', domains: 'Art'}
|
31
|
+
client.entry('truth', filters)
|
32
|
+
|
33
|
+
# Or do them "inline"
|
34
|
+
client.entry('truth', lexicalCategory: 'Verb', domains: 'Art')
|
35
|
+
|
36
|
+
# From a dictionary of a specific language (default is 'en')
|
37
|
+
client.entry('ace', lang: 'es')
|
38
|
+
|
39
|
+
###### Or return some subset of information
|
40
|
+
|
41
|
+
# Like just the examples
|
42
|
+
examples = client.entry_examples('explain')
|
43
|
+
|
44
|
+
# Or only the pronunciations...
|
45
|
+
the_noises = client.entry_pronunciations('knight')
|
46
|
+
|
47
|
+
# Or the translations (for Swahili in this example)
|
48
|
+
en_to_es = client.entry_translations('change', translations: 'sw')
|
49
|
+
# If no :translations filter is supplied, default is 'es'
|
50
|
+
|
51
|
+
# Or some of the other documented API calls
|
52
|
+
client.entry_sentences('scholar')
|
53
|
+
client.entry_definitions('correct')
|
54
|
+
client.entry_antonyms_synonyms('monotonous')
|
55
|
+
# Etc...
|
56
|
+
|
57
|
+
# Generally the method names follow the documented API closely
|
58
|
+
|
59
|
+
###### Other endpoint calls
|
60
|
+
|
61
|
+
# Inflections of a word
|
62
|
+
inflections = client.inflection('changed')
|
63
|
+
|
64
|
+
# Wordlist results (based on categorys, filters, etc...)
|
65
|
+
related = client.wordlist(lexicalCategory: 'Noun', word_length: '>5,<10')
|
66
|
+
|
67
|
+
# Or the search endpoint
|
68
|
+
search_results = client.search('condition', prefix: true)
|
69
|
+
|
70
|
+
###### A quick note on how to add filters to queries
|
71
|
+
There isn't much argument checking at the moment. Some endpoints do not accept filter arguments, refer to the API documentation to check for endpoints that accept filters.
|
72
|
+
|
73
|
+
# All endpoints accept the :lang filter. This specifies which dictionary to use
|
74
|
+
# If no argument is supplied, default is 'en'
|
75
|
+
filters = { lang: 'es' }
|
76
|
+
|
77
|
+
# To use multiple values on a single filter, make it an array
|
78
|
+
filters = { lexicalCategory: ['Noun', 'Verb'] }
|
79
|
+
|
80
|
+
# The wordlist endpoint specifically may include "nested" filters
|
81
|
+
# These filters (exclude, exclude_senses, etc...) require arrays
|
82
|
+
filters = { exclude: [domains: %w(sport art)] }
|
83
|
+
|
84
|
+
Argument names need to be in camelCase, not snake_case. However, the objects returned from API calls use snake_case attributes.
|
85
|
+
|
86
|
+
## Development
|
87
|
+
|
88
|
+
After checking out the repo, run `bin/setup` to install dependencies.
|
89
|
+
Run `bin/console` for an interactive prompt that will allow you to experiment.
|
90
|
+
|
91
|
+
## Contributing
|
92
|
+
|
93
|
+
Bug reports and pull requests are more than welcome!
|
94
|
+
Please make tests for anything that is added.
|
95
|
+
`bundle exec rake` will run rspec/rubocop.
|
96
|
+
|
97
|
+
#### Pull Requests
|
98
|
+
- Read [this often cited resource on contributing to open source projects on GitHub](https://gun.io/blog/how-to-github-fork-branch-and-pull-request)
|
99
|
+
- Fork the project
|
100
|
+
- Code and commit in your own feature branch
|
101
|
+
- Open a PR and nag me to close it!
|
102
|
+
|
103
|
+
## License
|
104
|
+
|
105
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'oxford_dictionary'
|
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,11 @@
|
|
1
|
+
require 'virtus'
|
2
|
+
require 'oxford_dictionary/api_objects/sense'
|
3
|
+
|
4
|
+
# Entry
|
5
|
+
class Entry
|
6
|
+
include Virtus.model
|
7
|
+
attribute :etymologies, Array[String]
|
8
|
+
attribute :grammatical_features, Array[OpenStruct]
|
9
|
+
attribute :homograph_number, String
|
10
|
+
attribute :senses, Array[Sense]
|
11
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'virtus'
|
2
|
+
require 'oxford_dictionary/api_objects/lexical_entry'
|
3
|
+
|
4
|
+
# Top level response (excluding metadata) from entries endpoints
|
5
|
+
class EntryResponse
|
6
|
+
include Virtus.model
|
7
|
+
attribute :id, String
|
8
|
+
attribute :language, String
|
9
|
+
attribute :lexical_entries, Array[LexicalEntry]
|
10
|
+
attribute :type, String
|
11
|
+
attribute :word, String
|
12
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'virtus'
|
2
|
+
require 'oxford_dictionary/api_objects/entry'
|
3
|
+
require 'oxford_dictionary/api_objects/pronunciation'
|
4
|
+
|
5
|
+
# LexicalEntry
|
6
|
+
class LexicalEntry
|
7
|
+
include Virtus.model
|
8
|
+
attribute :entries, Array[Entry]
|
9
|
+
attribute :grammatical_features, Array[OpenStruct]
|
10
|
+
attribute :inflection_of, Array[OpenStruct]
|
11
|
+
attribute :language, String
|
12
|
+
attribute :lexical_category, String
|
13
|
+
attribute :pronunciations, Array[Pronunciation]
|
14
|
+
attribute :sentences, Array[OpenStruct]
|
15
|
+
attribute :text, String
|
16
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'virtus'
|
2
|
+
|
3
|
+
# Sense
|
4
|
+
class Sense
|
5
|
+
include Virtus.model
|
6
|
+
attribute :id, String
|
7
|
+
attribute :antonyms, Array[OpenStruct]
|
8
|
+
attribute :cross_references, Array[OpenStruct]
|
9
|
+
attribute :cross_reference_markers, Array[String]
|
10
|
+
attribute :definitions, Array[String]
|
11
|
+
attribute :domains, Array[String]
|
12
|
+
attribute :examples, Array[OpenStruct]
|
13
|
+
attribute :regions, Array[String]
|
14
|
+
attribute :registers, Array[String]
|
15
|
+
attribute :subsenses, Array[Sense]
|
16
|
+
attribute :synonyms, Array[OpenStruct]
|
17
|
+
attribute :translations, Array[OpenStruct]
|
18
|
+
attribute :variant_forms, Array[OpenStruct]
|
19
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'oxford_dictionary/endpoints/entry_endpoint'
|
2
|
+
require 'oxford_dictionary/endpoints/inflection_endpoint'
|
3
|
+
require 'oxford_dictionary/endpoints/search_endpoint'
|
4
|
+
require 'oxford_dictionary/endpoints/wordlist_endpoint'
|
5
|
+
|
6
|
+
module OxfordDictionary
|
7
|
+
# The client object to interact with
|
8
|
+
class Client
|
9
|
+
include OxfordDictionary::Endpoints::EntryEndpoint
|
10
|
+
include OxfordDictionary::Endpoints::InflectionEndpoint
|
11
|
+
include OxfordDictionary::Endpoints::SearchEndpoint
|
12
|
+
include OxfordDictionary::Endpoints::WordlistEndpoint
|
13
|
+
|
14
|
+
attr_reader :app_id, :app_key
|
15
|
+
|
16
|
+
def initialize(params)
|
17
|
+
unless params.is_a?(Hash) && params.key?(:app_id) && params.key?(:app_key)
|
18
|
+
raise(ArgumentError, 'API id and key required.')
|
19
|
+
end
|
20
|
+
params.each do |key, value|
|
21
|
+
instance_variable_set("@#{key}", value)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
require 'oxford_dictionary/request'
|
2
|
+
require 'oxford_dictionary/api_objects/entry_response'
|
3
|
+
|
4
|
+
module OxfordDictionary
|
5
|
+
module Endpoints
|
6
|
+
# Interface to '/entries' endpoint
|
7
|
+
module EntryEndpoint
|
8
|
+
include OxfordDictionary::Request
|
9
|
+
ENDPOINT = 'entries'.freeze
|
10
|
+
|
11
|
+
def entry(query, params = {})
|
12
|
+
entry_request(query, params)
|
13
|
+
end
|
14
|
+
|
15
|
+
def entry_definitions(query, params = {})
|
16
|
+
params[:end] = 'definitions'
|
17
|
+
entry_request(query, params)
|
18
|
+
end
|
19
|
+
|
20
|
+
def entry_examples(query, params = {})
|
21
|
+
params[:end] = 'examples'
|
22
|
+
entry_request(query, params)
|
23
|
+
end
|
24
|
+
|
25
|
+
def entry_pronunciations(query, params = {})
|
26
|
+
params[:end] = 'pronunciations'
|
27
|
+
entry_request(query, params)
|
28
|
+
end
|
29
|
+
|
30
|
+
def entry_sentences(query, params = {})
|
31
|
+
params[:end] = 'sentences'
|
32
|
+
entry_request(query, params)
|
33
|
+
end
|
34
|
+
|
35
|
+
def entry_antonyms(query, params = {})
|
36
|
+
params[:end] = 'antonyms'
|
37
|
+
entry_request(query, params)
|
38
|
+
end
|
39
|
+
|
40
|
+
def entry_synonyms(query, params = {})
|
41
|
+
params[:end] = 'synonyms'
|
42
|
+
entry_request(query, params)
|
43
|
+
end
|
44
|
+
|
45
|
+
def entry_antonyms_synonyms(query, params = {})
|
46
|
+
params[:end] = 'synonyms;antonyms'
|
47
|
+
entry_request(query, params)
|
48
|
+
end
|
49
|
+
|
50
|
+
def entry_translations(query, params = {})
|
51
|
+
params.key?(:translations) || params[:translations] = 'es'
|
52
|
+
entry_request(query, params)
|
53
|
+
end
|
54
|
+
|
55
|
+
def entry_request(query, params)
|
56
|
+
EntryResponse.new(request(ENDPOINT, query, params)['results'][0])
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'oxford_dictionary/request'
|
2
|
+
require 'oxford_dictionary/api_objects/entry_response'
|
3
|
+
|
4
|
+
module OxfordDictionary
|
5
|
+
module Endpoints
|
6
|
+
# Interface to '/inflections' endpoint
|
7
|
+
module InflectionEndpoint
|
8
|
+
include OxfordDictionary::Request
|
9
|
+
ENDPOINT = 'inflections'.freeze
|
10
|
+
|
11
|
+
def inflection(query, params = {})
|
12
|
+
EntryResponse.new(request(ENDPOINT, query, params)['results'][0])
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'oxford_dictionary/request'
|
2
|
+
require 'oxford_dictionary/api_objects/list_response'
|
3
|
+
|
4
|
+
module OxfordDictionary
|
5
|
+
module Endpoints
|
6
|
+
# Interface to '/search' endpoint
|
7
|
+
module SearchEndpoint
|
8
|
+
include OxfordDictionary::Request
|
9
|
+
ENDPOINT = 'search'.freeze
|
10
|
+
|
11
|
+
def search(query, params = {})
|
12
|
+
params[:q] = query
|
13
|
+
ListResponse.new(request(ENDPOINT, query, params))
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'oxford_dictionary/request'
|
2
|
+
require 'oxford_dictionary/api_objects/list_response'
|
3
|
+
|
4
|
+
module OxfordDictionary
|
5
|
+
module Endpoints
|
6
|
+
# Interface to '/wordlist' endpoint
|
7
|
+
module WordlistEndpoint
|
8
|
+
include OxfordDictionary::Request
|
9
|
+
ENDPOINT = 'wordlist'.freeze
|
10
|
+
ADVANCED_FILTERS = [:exact, :exclude, :exclude_senses,
|
11
|
+
:exclude_prime_senses, :limit, :offset,
|
12
|
+
:prefix, :word_length].freeze
|
13
|
+
|
14
|
+
def wordlist(params = {})
|
15
|
+
# Check first so that we don't waste an API call
|
16
|
+
if too_many_filter_values(params)
|
17
|
+
raise(Error.new(400), 'Do not use more than 5 values for a filter')
|
18
|
+
end
|
19
|
+
ListResponse.new(request(ENDPOINT, nil, params))
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
def too_many_filter_values(params)
|
25
|
+
params.each do |k, v|
|
26
|
+
return true if v.size > 5 && !ADVANCED_FILTERS.include?(k)
|
27
|
+
end
|
28
|
+
false
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,114 @@
|
|
1
|
+
require 'httparty'
|
2
|
+
require 'json'
|
3
|
+
require 'plissken'
|
4
|
+
require 'oxford_dictionary/error'
|
5
|
+
|
6
|
+
module OxfordDictionary
|
7
|
+
# Handles all of the actual API calls
|
8
|
+
module Request
|
9
|
+
include HTTParty
|
10
|
+
|
11
|
+
BASE = 'https://od-api.oxforddictionaries.com/api/v1'.freeze
|
12
|
+
ACCEPT_TYPE = 'application/json'.freeze
|
13
|
+
# May be used by the wordlist endpoint
|
14
|
+
ADVANCED_FILTERS = [:exact, :exclude, :exclude_senses,
|
15
|
+
:exclude_prime_senses, :limit, :offset,
|
16
|
+
:prefix, :word_length].freeze
|
17
|
+
|
18
|
+
def request(endpoint, q, params)
|
19
|
+
url = build_url(endpoint, q, params)
|
20
|
+
resp = HTTParty.get(url, headers: request_headers)
|
21
|
+
unless resp.code == 200
|
22
|
+
raise(Error.new(resp.code), error_message(resp.body))
|
23
|
+
end
|
24
|
+
JSON.parse(resp.body).to_snake_keys
|
25
|
+
end
|
26
|
+
|
27
|
+
def build_url(endpoint, q, params)
|
28
|
+
params[:lang] || params[:lang] = 'en'
|
29
|
+
url_start = "#{BASE}/#{endpoint}/#{params[:lang]}"
|
30
|
+
if params[:q]
|
31
|
+
"#{url_start}#{search_endpoint_url(params)}".chomp('/')
|
32
|
+
else
|
33
|
+
unless q
|
34
|
+
# The wordlist endpoint uses a slightly different url structure
|
35
|
+
return "#{url_start}/#{build_advanced_url(params)}".chomp('/')
|
36
|
+
end
|
37
|
+
"#{url_start}/#{q}/#{finish_url(params)}".chomp('/')
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def build_advanced_url(params)
|
42
|
+
advanced_params = {}
|
43
|
+
params.each do |k, v|
|
44
|
+
if ADVANCED_FILTERS.include?(k)
|
45
|
+
params.delete(k)
|
46
|
+
advanced_params[k] = v
|
47
|
+
end
|
48
|
+
end
|
49
|
+
"#{create_query_string(params)}#{advanced_query(advanced_params)}"
|
50
|
+
end
|
51
|
+
|
52
|
+
def advanced_query(params)
|
53
|
+
unless params.empty?
|
54
|
+
params[:exact] || params[:exact] = false
|
55
|
+
return "?#{create_query_string(params, '&')}"
|
56
|
+
end
|
57
|
+
''
|
58
|
+
end
|
59
|
+
|
60
|
+
# The search endpoint has a slightly different url structure
|
61
|
+
def search_endpoint_url(params)
|
62
|
+
params[:prefix] || params[:prefix] = false
|
63
|
+
append = ''
|
64
|
+
if params[:translations]
|
65
|
+
append = "/translations=#{params[:translations]}"
|
66
|
+
params.delete(:translations)
|
67
|
+
end
|
68
|
+
"#{append}?#{create_query_string(params, '&')}"
|
69
|
+
end
|
70
|
+
|
71
|
+
def finish_url(params)
|
72
|
+
params[:end] || create_query_string(params)
|
73
|
+
end
|
74
|
+
|
75
|
+
def create_query_string(params, seperator = ';')
|
76
|
+
params.delete(:lang)
|
77
|
+
count = 0
|
78
|
+
query = ''
|
79
|
+
params.each do |k, v|
|
80
|
+
query += "#{k}=#{options(v)}"
|
81
|
+
query += seperator if count < params.size - 1
|
82
|
+
count += 1
|
83
|
+
end
|
84
|
+
query
|
85
|
+
end
|
86
|
+
|
87
|
+
def options(v)
|
88
|
+
if v.is_a?(Array)
|
89
|
+
hash_element?(v[0]) ? query_from_hash(v) : v.join(',')
|
90
|
+
else
|
91
|
+
v
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
def query_from_hash(hash)
|
96
|
+
query = ''
|
97
|
+
hash.each { |h| query += create_query_string(h) }
|
98
|
+
query
|
99
|
+
end
|
100
|
+
|
101
|
+
def error_message(response)
|
102
|
+
response.lines.last.chomp[3..-5]
|
103
|
+
end
|
104
|
+
|
105
|
+
# The wordlist endpoint may nest filters
|
106
|
+
def hash_element?(element)
|
107
|
+
element.is_a?(Hash)
|
108
|
+
end
|
109
|
+
|
110
|
+
def request_headers
|
111
|
+
{ 'Accept' => ACCEPT_TYPE, 'app_id' => app_id, 'app_key' => app_key }
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'oxford_dictionary/client'
|
2
|
+
require 'oxford_dictionary/error'
|
3
|
+
require 'oxford_dictionary/request'
|
4
|
+
require 'oxford_dictionary/version'
|
5
|
+
require 'oxford_dictionary/endpoints/entry_endpoint'
|
6
|
+
require 'oxford_dictionary/endpoints/inflection_endpoint'
|
7
|
+
require 'oxford_dictionary/endpoints/search_endpoint'
|
8
|
+
require 'oxford_dictionary/endpoints/wordlist_endpoint'
|
9
|
+
|
10
|
+
# Adds some aliasing
|
11
|
+
module OxfordDictionary
|
12
|
+
class << self
|
13
|
+
# Alias for OxfordDictionary::Client.new
|
14
|
+
def new(params)
|
15
|
+
Client.new(params)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'oxford_dictionary/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'oxford_dictionary'
|
8
|
+
spec.version = OxfordDictionary::VERSION
|
9
|
+
spec.authors = ['swcraig']
|
10
|
+
spec.email = ['coverless.info@gmail.com']
|
11
|
+
|
12
|
+
spec.summary = 'A wrapper for the Oxford Dictionary API'
|
13
|
+
spec.description = 'https://developer.oxforddictionaries.com/documentation'
|
14
|
+
spec.homepage = 'https://github.com'
|
15
|
+
spec.license = 'MIT'
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
18
|
+
f.match(%r{^(test|spec|features)/})
|
19
|
+
end
|
20
|
+
spec.bindir = 'exe'
|
21
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
22
|
+
spec.require_paths = ['lib']
|
23
|
+
|
24
|
+
spec.add_development_dependency 'bundler', '~> 1.13'
|
25
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
26
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
27
|
+
spec.add_development_dependency 'webmock', '~> 2.1.0'
|
28
|
+
spec.add_development_dependency 'rubocop', '~> 0.45.0'
|
29
|
+
|
30
|
+
spec.add_runtime_dependency 'httparty', '~> 0.14.0'
|
31
|
+
spec.add_runtime_dependency 'virtus', '~> 1.0.5'
|
32
|
+
spec.add_runtime_dependency 'plissken', '~> 0.1.0'
|
33
|
+
end
|
metadata
ADDED
@@ -0,0 +1,182 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: oxford_dictionary
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- swcraig
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-12-14 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.13'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.13'
|
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.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.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: 2.1.0
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 2.1.0
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rubocop
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 0.45.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.45.0
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: httparty
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: 0.14.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.14.0
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: virtus
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: 1.0.5
|
104
|
+
type: :runtime
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: 1.0.5
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: plissken
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - "~>"
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: 0.1.0
|
118
|
+
type: :runtime
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - "~>"
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: 0.1.0
|
125
|
+
description: https://developer.oxforddictionaries.com/documentation
|
126
|
+
email:
|
127
|
+
- coverless.info@gmail.com
|
128
|
+
executables: []
|
129
|
+
extensions: []
|
130
|
+
extra_rdoc_files: []
|
131
|
+
files:
|
132
|
+
- ".gitignore"
|
133
|
+
- ".rspec"
|
134
|
+
- ".travis.yml"
|
135
|
+
- CODE_OF_CONDUCT.md
|
136
|
+
- Gemfile
|
137
|
+
- LICENSE.txt
|
138
|
+
- README.md
|
139
|
+
- Rakefile
|
140
|
+
- bin/console
|
141
|
+
- bin/setup
|
142
|
+
- lib/oxford_dictionary.rb
|
143
|
+
- lib/oxford_dictionary/api_objects/entry.rb
|
144
|
+
- lib/oxford_dictionary/api_objects/entry_response.rb
|
145
|
+
- lib/oxford_dictionary/api_objects/lexical_entry.rb
|
146
|
+
- lib/oxford_dictionary/api_objects/list_response.rb
|
147
|
+
- lib/oxford_dictionary/api_objects/pronunciation.rb
|
148
|
+
- lib/oxford_dictionary/api_objects/sense.rb
|
149
|
+
- lib/oxford_dictionary/client.rb
|
150
|
+
- lib/oxford_dictionary/endpoints/entry_endpoint.rb
|
151
|
+
- lib/oxford_dictionary/endpoints/inflection_endpoint.rb
|
152
|
+
- lib/oxford_dictionary/endpoints/search_endpoint.rb
|
153
|
+
- lib/oxford_dictionary/endpoints/wordlist_endpoint.rb
|
154
|
+
- lib/oxford_dictionary/error.rb
|
155
|
+
- lib/oxford_dictionary/request.rb
|
156
|
+
- lib/oxford_dictionary/version.rb
|
157
|
+
- oxford_dictionary.gemspec
|
158
|
+
homepage: https://github.com
|
159
|
+
licenses:
|
160
|
+
- MIT
|
161
|
+
metadata: {}
|
162
|
+
post_install_message:
|
163
|
+
rdoc_options: []
|
164
|
+
require_paths:
|
165
|
+
- lib
|
166
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
167
|
+
requirements:
|
168
|
+
- - ">="
|
169
|
+
- !ruby/object:Gem::Version
|
170
|
+
version: '0'
|
171
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
172
|
+
requirements:
|
173
|
+
- - ">="
|
174
|
+
- !ruby/object:Gem::Version
|
175
|
+
version: '0'
|
176
|
+
requirements: []
|
177
|
+
rubyforge_project:
|
178
|
+
rubygems_version: 2.5.1
|
179
|
+
signing_key:
|
180
|
+
specification_version: 4
|
181
|
+
summary: A wrapper for the Oxford Dictionary API
|
182
|
+
test_files: []
|