ldoce 0.0.1

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of ldoce might be problematic. Click here for more details.

@@ -0,0 +1,19 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.swp
19
+ api_key.yml
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ldoce.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Mark Burns
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,38 @@
1
+ # Ldoce
2
+ Easily interface with the Longman Dictionary of Contemporary English API from Ruby:
3
+
4
+ ## Installation
5
+
6
+ Add this line to your application's Gemfile:
7
+
8
+ gem 'ldoce'
9
+
10
+ And then execute:
11
+
12
+ $ bundle
13
+
14
+ Or install it yourself as:
15
+
16
+ $ gem install ldoce
17
+
18
+ ## Usage
19
+
20
+ ```ruby
21
+ Ldoce::Word.api_key = "<your_key>"
22
+
23
+ cat = Ldoce::Word.search 'cat'
24
+ cat.play #plays mp3 sample - only working for Mac at the moment
25
+ cat.definition
26
+ #=> "A small four legged animal commonly kept as a pet"
27
+
28
+ Ldoce::Word.play 'cat'
29
+ #=> <Ldoce::Word: cat "A small four legged animal commonly kept as a pet">
30
+ ```
31
+
32
+ ## Contributing
33
+
34
+ 1. Fork it
35
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
36
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
37
+ 4. Push to the branch (`git push origin my-new-feature`)
38
+ 5. Create new Pull Request
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/ldoce/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Mark Burns"]
6
+ gem.email = ["markthedeveloper@gmail.com"]
7
+ gem.description = %q{API for the Longman Dictionary of Contemporary English}
8
+ gem.summary = %q{So far covers playing mp3 media files and fetching definitions}
9
+ gem.homepage = "https://github.com/markburns/ldoce"
10
+
11
+ gem.add_dependency 'httparty'
12
+
13
+
14
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
15
+ gem.files = `git ls-files`.split("\n")
16
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
+ gem.name = "ldoce"
18
+ gem.require_paths = ["lib"]
19
+ gem.version = Ldoce::VERSION
20
+ end
@@ -0,0 +1,6 @@
1
+ require "httparty"
2
+ require "ldoce/version"
3
+ require "ldoce/word"
4
+
5
+ module Ldoce
6
+ end
@@ -0,0 +1,3 @@
1
+ module Ldoce
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,122 @@
1
+ module Ldoce
2
+ class Word
3
+ include HTTParty
4
+
5
+ attr_reader :query, :response
6
+
7
+ def initialize query, response
8
+ @query, @response = query, response
9
+ end
10
+
11
+ def word
12
+ query
13
+ end
14
+
15
+ def play
16
+ if mp3?
17
+ unless File.exists? filename
18
+ command = "curl #{mp3_url} -silent > #{filename}"
19
+ `#{command}`
20
+ end
21
+ `afplay #{filename}`
22
+ end
23
+ self
24
+ end
25
+
26
+ def mp3?
27
+ !!mp3_url
28
+ end
29
+
30
+ def entries
31
+ entries = response["Entries"]
32
+ entries = entries["Entry"] rescue []
33
+ arrayify entries
34
+ end
35
+
36
+ def definition
37
+ definitions = each_in entries do |entry|
38
+ each_in entry["Sense"] do |f|
39
+ if f["DEF"]
40
+ f["DEF"]["#text"]
41
+ elsif f["Subsense"]
42
+ each_in f["Subsense"] do |g|
43
+ g["DEF"]["#text"]
44
+ end
45
+ end
46
+ end
47
+ end.flatten.compact
48
+
49
+ definitions.map { |e| "\"#{e}\"" }.join(",")
50
+ end
51
+
52
+ def inspect
53
+ "<Word #{@query}: #{definition} mp3:#{mp3?}>"
54
+ end
55
+
56
+ def american_pronunciations
57
+ entries.map{|e| ["multimedia"].detect{|w| w["@type"]=="US_PRON"}["@href"]}
58
+ end
59
+
60
+ def british_pronunciations
61
+ entries.map{|e| e["multimedia"].detect{|w| w["@type"]=="GB_PRON"}["@href"]}
62
+ end
63
+
64
+ def mp3_url
65
+ pronunciation = british_pronunciations.first || american_pronunciations.first
66
+ url = "https://api.pearson.com/longman/dictionary#{pronunciation}?apikey=#{Word.api_key}"
67
+ rescue
68
+ nil
69
+ end
70
+
71
+ private
72
+
73
+ def arrayify hash_or_array
74
+ hash_or_array.is_a?(Hash) ? [hash_or_array] : hash_or_array
75
+ end
76
+
77
+ def each_in hash_or_array
78
+ arrayify(hash_or_array).map do |el|
79
+ yield el
80
+ end
81
+ end
82
+
83
+ def filename lang="british"
84
+ "tmp/#{lang}_#{query}.mp3"
85
+ end
86
+
87
+ class MissingApiKey < Exception; end
88
+ class << self
89
+ def play query
90
+ search(query).play
91
+ end
92
+
93
+ def find query
94
+ search query
95
+ end
96
+
97
+ def search query
98
+ response = get(url(query)).parsed_response
99
+ Word.new query, response
100
+ end
101
+
102
+ attr_writer :api_key
103
+
104
+ def api_key
105
+ @api_key ||= YAML.load(File.read "api_key.yml")["api_key"]
106
+ rescue
107
+ raise MissingApiKey.new "Either set the API key programmatically:
108
+ Ldoce::Word.api_key = '<your_key>'
109
+
110
+ or
111
+
112
+ Create a file called api_key.yml and add your Longman API Key:
113
+ api_key: <your_key_here>"
114
+ end
115
+
116
+ def url query
117
+ "https://api.pearson.com/longman/dictionary/entry.json?q=#{query}&apikey=#{api_key}"
118
+ end
119
+ end
120
+ end
121
+ end
122
+
metadata ADDED
@@ -0,0 +1,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ldoce
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Mark Burns
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-02-20 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: httparty
16
+ requirement: &70120811014960 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70120811014960
25
+ description: API for the Longman Dictionary of Contemporary English
26
+ email:
27
+ - markthedeveloper@gmail.com
28
+ executables: []
29
+ extensions: []
30
+ extra_rdoc_files: []
31
+ files:
32
+ - .gitignore
33
+ - Gemfile
34
+ - LICENSE
35
+ - README.md
36
+ - Rakefile
37
+ - ldoce.gemspec
38
+ - lib/ldoce.rb
39
+ - lib/ldoce/version.rb
40
+ - lib/ldoce/word.rb
41
+ homepage: https://github.com/markburns/ldoce
42
+ licenses: []
43
+ post_install_message:
44
+ rdoc_options: []
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ none: false
49
+ requirements:
50
+ - - ! '>='
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ! '>='
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ requirements: []
60
+ rubyforge_project:
61
+ rubygems_version: 1.8.15
62
+ signing_key:
63
+ specification_version: 3
64
+ summary: So far covers playing mp3 media files and fetching definitions
65
+ test_files: []