ted_api 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.
Files changed (75) hide show
  1. data/.gitignore +17 -0
  2. data/Gemfile +4 -0
  3. data/LICENSE.txt +22 -0
  4. data/README.md +56 -0
  5. data/Rakefile +8 -0
  6. data/lib/faraday/response/raise_ted_api_error.rb +43 -0
  7. data/lib/ted_api.rb +20 -0
  8. data/lib/ted_api/client.rb +39 -0
  9. data/lib/ted_api/client/countries.rb +18 -0
  10. data/lib/ted_api/client/events.rb +18 -0
  11. data/lib/ted_api/client/languages.rb +18 -0
  12. data/lib/ted_api/client/quotes.rb +18 -0
  13. data/lib/ted_api/client/rating_words.rb +18 -0
  14. data/lib/ted_api/client/speakers.rb +18 -0
  15. data/lib/ted_api/client/tags.rb +18 -0
  16. data/lib/ted_api/client/talks.rb +26 -0
  17. data/lib/ted_api/client/themes.rb +18 -0
  18. data/lib/ted_api/configuration.rb +54 -0
  19. data/lib/ted_api/connection.rb +30 -0
  20. data/lib/ted_api/error.rb +34 -0
  21. data/lib/ted_api/request.rb +34 -0
  22. data/lib/ted_api/version.rb +3 -0
  23. data/spec/faraday/response_spec.rb +35 -0
  24. data/spec/fixtures/countries.json +1 -0
  25. data/spec/fixtures/countries.xml +109 -0
  26. data/spec/fixtures/country.json +1 -0
  27. data/spec/fixtures/country.xml +8 -0
  28. data/spec/fixtures/event.json +1 -0
  29. data/spec/fixtures/event.xml +12 -0
  30. data/spec/fixtures/events.json +1 -0
  31. data/spec/fixtures/events.xml +189 -0
  32. data/spec/fixtures/language.json +1 -0
  33. data/spec/fixtures/language.xml +8 -0
  34. data/spec/fixtures/languages.json +1 -0
  35. data/spec/fixtures/languages.xml +106 -0
  36. data/spec/fixtures/quote.json +1 -0
  37. data/spec/fixtures/quote.xml +14 -0
  38. data/spec/fixtures/quotes.json +2 -0
  39. data/spec/fixtures/quotes.xml +230 -0
  40. data/spec/fixtures/rating_word.json +1 -0
  41. data/spec/fixtures/rating_word.xml +7 -0
  42. data/spec/fixtures/rating_words.json +1 -0
  43. data/spec/fixtures/rating_words.xml +65 -0
  44. data/spec/fixtures/speaker.json +1 -0
  45. data/spec/fixtures/speaker.xml +16 -0
  46. data/spec/fixtures/speakers.json +1 -0
  47. data/spec/fixtures/speakers.xml +269 -0
  48. data/spec/fixtures/speakers_by_talk.json +1 -0
  49. data/spec/fixtures/subtitles.json +2 -0
  50. data/spec/fixtures/tag.json +1 -0
  51. data/spec/fixtures/tag.xml +11 -0
  52. data/spec/fixtures/tags.json +1 -0
  53. data/spec/fixtures/tags.xml +169 -0
  54. data/spec/fixtures/talk.json +1 -0
  55. data/spec/fixtures/talk.xml +302 -0
  56. data/spec/fixtures/talks.json +1 -0
  57. data/spec/fixtures/talks.xml +249 -0
  58. data/spec/fixtures/theme.json +1 -0
  59. data/spec/fixtures/theme.xml +19 -0
  60. data/spec/fixtures/themes.json +1 -0
  61. data/spec/fixtures/themes.xml +294 -0
  62. data/spec/spec_helper.rb +38 -0
  63. data/spec/ted_api/client/countries_spec.rb +46 -0
  64. data/spec/ted_api/client/events_spec.rb +46 -0
  65. data/spec/ted_api/client/languages_spec.rb +46 -0
  66. data/spec/ted_api/client/quotes_spec.rb +46 -0
  67. data/spec/ted_api/client/rating_words_spec.rb +46 -0
  68. data/spec/ted_api/client/speakers_spec.rb +46 -0
  69. data/spec/ted_api/client/tags_spec.rb +46 -0
  70. data/spec/ted_api/client/talks_spec.rb +78 -0
  71. data/spec/ted_api/client/themes_spec.rb +46 -0
  72. data/spec/ted_api/client_spec.rb +26 -0
  73. data/spec/ted_api_spec.rb +21 -0
  74. data/ted_api.gemspec +33 -0
  75. metadata +347 -0
@@ -0,0 +1,17 @@
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
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ted_api.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 John Barton
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,56 @@
1
+ # TedApi Client
2
+
3
+ This gem wraps the TED Talks API making calls using Faraday. It includes support for both Hashie parsed JSON and XML in addition to providing raw response formats. It contains a 1:1 match for all API calls including the duplicate and somewhat useless ones such as languages and events. All calls are tested for both JSON and XML responses.
4
+
5
+ You will need a TED API key to consume the API and there are currently only 50 that have been given out. I was lucky enough that my proposal was accepted to receive one for my TED-API-50 project. Hopefully they will open up the API to the public soon so that I won't have written this gem for the 50 of us. See http://developer.ted.com/ for additional information.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'ted_api'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install ted_api
20
+
21
+ ## Usage
22
+
23
+ Basic usage
24
+
25
+ The wrapper defaults to json responses for the API:
26
+
27
+ @client = TedApi::Client.new(api_key: 'xxx')
28
+
29
+ # Returns a list of talks:
30
+ @client.talks
31
+
32
+ # Returns a specific talk id
33
+ @client.talks(1)
34
+
35
+ Consuming the XML API is very straightforward:
36
+
37
+ @client = TedApi::Client.new(api_key: 'xxx', response_format='xml')
38
+
39
+ To bypass Hashie and ParseXML or ParseJSON for the raw response, just set raw to true:
40
+
41
+ @client.talks(1, {}, true)
42
+
43
+ To pass additional options:
44
+
45
+ @client.speakers(nil, {filter: 'lastname:Kurzweil'})
46
+
47
+ See http://developer.ted.com/API_Docs for additional details
48
+
49
+ ## Contributing
50
+
51
+ 1. Fork it
52
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
53
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
54
+ 4. Push to the branch (`git push origin my-new-feature`)
55
+ 5. Create new Pull Request
56
+
@@ -0,0 +1,8 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ require 'rspec/core/rake_task'
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+ task :test => :spec
8
+ task :default => :spec
@@ -0,0 +1,43 @@
1
+ require 'faraday'
2
+ require 'multi_json'
3
+
4
+ module Faraday
5
+ class Response::RaiseTedApiError < Response::Middleware
6
+ def on_complete(response)
7
+ case response[:status].to_i
8
+ when 400
9
+ raise TedApi::BadRequest, error_message(response)
10
+ when 401
11
+ raise TedApi::Unauthorized, error_message(response)
12
+ when 403
13
+ raise TedApi::Forbidden, error_message(response)
14
+ when 404
15
+ raise TedApi::NotFound, error_message(response)
16
+ when 406
17
+ raise TedApi::NotAcceptable, error_message(response)
18
+ when 422
19
+ raise TedApi::UnprocessableEntity, error_message(response)
20
+ when 500
21
+ raise TedApi::InternalServerError, error_message(response)
22
+ when 501
23
+ raise TedApi::NotImplemented, error_message(response)
24
+ when 502
25
+ raise TedApi::BadGateway, error_message(response)
26
+ when 503
27
+ raise TedApi::ServiceUnavailable, error_message(response)
28
+ end
29
+ end
30
+
31
+ def error_message(response)
32
+ message = if (body = response[:body]) && !body.empty?
33
+ if body.is_a?(String)
34
+ body = MultiJson.load(body, :symbolize_keys => true)
35
+ end
36
+ ": #{body[:error] || body[:message] || ''}"
37
+ else
38
+ ''
39
+ end
40
+ "#{response[:method].to_s.upcase} #{response[:url].to_s}: #{response[:status]}#{message}"
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,20 @@
1
+ require 'ted_api/version'
2
+ require 'ted_api/configuration'
3
+ require 'ted_api/client'
4
+ require 'ted_api/error'
5
+
6
+ module TedApi
7
+ extend Configuration
8
+ class << self
9
+ # Alias for TedApi::Client.new
10
+ #
11
+ # @return [TedApi::Client]
12
+ def new(options={})
13
+ TedApi::Client.new(options)
14
+ end
15
+
16
+ def respond_to?(method, include_private=false)
17
+ new.respond_to?(method, include_private) || super(method, include_private)
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,39 @@
1
+ require 'ted_api/connection'
2
+ require 'ted_api/request'
3
+ require 'ted_api/error'
4
+
5
+ require 'ted_api/client/talks'
6
+ require 'ted_api/client/events'
7
+ require 'ted_api/client/languages'
8
+ require 'ted_api/client/quotes'
9
+ require 'ted_api/client/rating_words'
10
+ require 'ted_api/client/speakers'
11
+ require 'ted_api/client/themes'
12
+ require 'ted_api/client/countries'
13
+ require 'ted_api/client/tags'
14
+
15
+ module TedApi
16
+ class Client
17
+ attr_accessor(*Configuration::VALID_OPTIONS_KEYS)
18
+
19
+ def initialize(options={})
20
+ options = TedApi.options.merge(options)
21
+ Configuration::VALID_OPTIONS_KEYS.each do |key|
22
+ send("#{key}=", options[key])
23
+ end
24
+ end
25
+
26
+ include TedApi::Connection
27
+ include TedApi::Request
28
+ include TedApi::Client::Talks
29
+ include TedApi::Client::Events
30
+ include TedApi::Client::Languages
31
+ include TedApi::Client::Quotes
32
+ include TedApi::Client::RatingWords
33
+ include TedApi::Client::Speakers
34
+ include TedApi::Client::Themes
35
+ include TedApi::Client::Countries
36
+ include TedApi::Client::Tags
37
+
38
+ end
39
+ end
@@ -0,0 +1,18 @@
1
+ module TedApi
2
+ class Client
3
+ module Countries
4
+
5
+ ##
6
+ # http://developer.ted.com/API_Docs#countries
7
+
8
+ def countries(country=nil, options={}, raw=false)
9
+ if country.nil?
10
+ get('countries', options, raw)
11
+ else
12
+ get("countries/#{country}", options, raw)
13
+ end
14
+ end
15
+
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,18 @@
1
+ module TedApi
2
+ class Client
3
+ module Events
4
+
5
+ ##
6
+ # http://developer.ted.com/API_Docs#events
7
+
8
+ def events(event=nil, options={}, raw=false)
9
+ if event.nil?
10
+ get('events', options, raw)
11
+ else
12
+ get("events/#{event}", options, raw)
13
+ end
14
+ end
15
+
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,18 @@
1
+ module TedApi
2
+ class Client
3
+ module Languages
4
+
5
+ ##
6
+ # http://developer.ted.com/API_Docs#languages
7
+
8
+ def languages(language=nil, options={}, raw=false)
9
+ if language.nil?
10
+ get('languages', options, raw)
11
+ else
12
+ get("languages/#{language}", options, raw)
13
+ end
14
+ end
15
+
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,18 @@
1
+ module TedApi
2
+ class Client
3
+ module Quotes
4
+
5
+ ##
6
+ # http://developer.ted.com/API_Docs#quotes
7
+
8
+ def quotes(quote=nil, options={}, raw=false)
9
+ if quote.nil?
10
+ get('quotes', options, raw)
11
+ else
12
+ get("quotes/#{quote}", options, raw)
13
+ end
14
+ end
15
+
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,18 @@
1
+ module TedApi
2
+ class Client
3
+ module RatingWords
4
+
5
+ ##
6
+ # http://developer.ted.com/API_Docs#rating_words
7
+
8
+ def rating_words(rating_word=nil, options={}, raw=false)
9
+ if rating_word.nil?
10
+ get('rating_words', options, raw)
11
+ else
12
+ get("rating_words/#{rating_word}", options, raw)
13
+ end
14
+ end
15
+
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,18 @@
1
+ module TedApi
2
+ class Client
3
+ module Speakers
4
+
5
+ ##
6
+ # http://developer.ted.com/API_Docs#speakers
7
+
8
+ def speakers(speaker=nil, options={}, raw=false)
9
+ if speaker.nil?
10
+ get('speakers', options, raw)
11
+ else
12
+ get("speakers/#{speaker}", options, raw)
13
+ end
14
+ end
15
+
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,18 @@
1
+ module TedApi
2
+ class Client
3
+ module Tags
4
+
5
+ ##
6
+ # http://developer.ted.com/API_Docs#tags
7
+
8
+ def tags(tag=nil, options={}, raw=false)
9
+ if tag.nil?
10
+ get('tags', options, raw)
11
+ else
12
+ get("tags/#{tag}", options, raw)
13
+ end
14
+ end
15
+
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,26 @@
1
+ module TedApi
2
+ class Client
3
+ module Talks
4
+
5
+ ##
6
+ # http://developer.ted.com/API_Docs#talks
7
+
8
+ def talks(talk=nil, options={}, raw=false)
9
+ if talk.nil?
10
+ get('talks', options, raw)
11
+ else
12
+ get("talks/#{talk}", options, raw)
13
+ end
14
+ end
15
+
16
+ def subtitles(talk, options={}, raw=false)
17
+ get("talks/#{talk}/subtitles", options, raw)
18
+ end
19
+
20
+ def speakers_by_talk(talk, options={}, raw=false)
21
+ get("talks/#{talk}/speakers", options, raw)
22
+ end
23
+
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,18 @@
1
+ module TedApi
2
+ class Client
3
+ module Themes
4
+
5
+ ##
6
+ # http://developer.ted.com/API_Docs#themes
7
+
8
+ def themes(theme=nil, options={}, raw=false)
9
+ if theme.nil?
10
+ get('themes', options, raw)
11
+ else
12
+ get("themes/#{theme}", options, raw)
13
+ end
14
+ end
15
+
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,54 @@
1
+ require 'faraday'
2
+ require 'ted_api/version'
3
+
4
+ module TedApi
5
+ module Configuration
6
+ VALID_OPTIONS_KEYS = [
7
+ :adapter,
8
+ :api_version,
9
+ :api_endpoint,
10
+ :web_endpoint,
11
+ :api_key,
12
+ :response_format,
13
+ :user_agent].freeze
14
+
15
+ DEFAULT_ADAPTER = Faraday.default_adapter
16
+ DEFAULT_API_VERSION = 'v1'
17
+ DEFAULT_API_ENDPOINT = 'https://api.ted.com/'
18
+ DEFAULT_WEB_ENDPOINT = 'https://ted.com/'
19
+ DEFAULT_USER_AGENT = "ted-api ruby gem #{TedApi::VERSION}".freeze
20
+ DEFAULT_RESPONSE_FORMAT = 'json'
21
+
22
+ attr_accessor(*VALID_OPTIONS_KEYS)
23
+
24
+ def self.extended(base)
25
+ base.reset
26
+ end
27
+
28
+ def configure
29
+ yield self
30
+ end
31
+
32
+ def options
33
+ VALID_OPTIONS_KEYS.inject({}){|o,k| o.merge!(k => send(k)) }
34
+ end
35
+
36
+ def api_endpoint=(value)
37
+ @api_endpoint = File.join(value, "")
38
+ end
39
+
40
+ def web_endpoint=(value)
41
+ @web_endpoint = File.join(value, "")
42
+ end
43
+
44
+ def reset
45
+ self.adapter = DEFAULT_ADAPTER
46
+ self.api_version = DEFAULT_API_VERSION
47
+ self.api_endpoint = DEFAULT_API_ENDPOINT
48
+ self.web_endpoint = DEFAULT_WEB_ENDPOINT
49
+ self.api_key = nil
50
+ self.response_format = DEFAULT_RESPONSE_FORMAT
51
+ self.user_agent = DEFAULT_USER_AGENT
52
+ end
53
+ end
54
+ end