embedly 1.7.1 → 1.8.0

Sign up to get free protection for your applications and to get access to all the features.
data/ChangeLog CHANGED
@@ -1,3 +1,21 @@
1
+ *embedly-1.8.0 (17 Jun 2013)
2
+
3
+ 17 Jun 2013: Felipe Elias Philipp <felipeelias@gmail.com>
4
+ Abstracts http client to allow for custom requesters
5
+
6
+ 17 Jun 2013: Nitesh <me@coffeebite.com>
7
+ Gets key from Embedly::Configuration
8
+
9
+ *embedly-1.7.0 (17 Jun 2013)
10
+
11
+ 17 Jun 2013: Anton Dieterle <antondie@gmail.com>
12
+ Adds Net::HTTP::Proxy support
13
+
14
+ *embedly-1.6.0 (2 Apr 2013)
15
+
16
+ 2 Apr 2013: Bob Corsaro <bob@embed.ly>
17
+ Adds extract endpoint
18
+
1
19
  *embedly-1.5.6 (23 May 2012)
2
20
 
3
21
  23 May 2012: Bob Corsaro <bob@embed.ly>
@@ -67,7 +67,7 @@ You can configure some parameters in the api:
67
67
  config.logger = MyAwesomeLogger.new(STDERR)
68
68
 
69
69
  # disable typhoeus and use Net::HTTP instead
70
- config.typhoeus = false
70
+ config.request_with :net_http
71
71
  end
72
72
 
73
73
  == Testing
data/Rakefile CHANGED
@@ -22,7 +22,9 @@ Jeweler::Tasks.new do |gem|
22
22
  "Bob Corsaro",
23
23
  "Felipe Elias Philipp",
24
24
  "Russ Bradberry",
25
- "Arun Thampi"
25
+ "Arun Thampi",
26
+ "Anton Dieterle",
27
+ "Nitesh"
26
28
  ]
27
29
  gem.license = "MIT"
28
30
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.7.1
1
+ 1.8.0
@@ -5,10 +5,10 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "embedly"
8
- s.version = "1.7.1"
8
+ s.version = "1.8.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
- s.authors = ["Bob Corsaro", "Felipe Elias Philipp", "Russ Bradberry", "Arun Thampi"]
11
+ s.authors = ["Bob Corsaro", "Felipe Elias Philipp", "Russ Bradberry", "Arun Thampi", "Anton Dieterle", "Nitesh"]
12
12
  s.date = "2013-06-17"
13
13
  s.description = "Ruby Embedly client library"
14
14
  s.email = "bob@embed.ly"
@@ -41,6 +41,10 @@ Gem::Specification.new do |s|
41
41
  "lib/embedly/configuration.rb",
42
42
  "lib/embedly/exceptions.rb",
43
43
  "lib/embedly/model.rb",
44
+ "lib/embedly/request.rb",
45
+ "lib/embedly/request/base.rb",
46
+ "lib/embedly/request/net_http.rb",
47
+ "lib/embedly/request/typhoeus.rb",
44
48
  "spec/embedly/api_spec.rb",
45
49
  "spec/embedly/command_line_spec.rb",
46
50
  "spec/embedly/configuration_spec.rb",
@@ -1,16 +1,11 @@
1
- require 'net/http'
2
- require 'net/https'
3
1
  require 'json'
4
2
  require 'ostruct'
5
3
  require 'embedly/configuration'
6
4
  require 'embedly/model'
7
5
  require 'embedly/exceptions'
6
+ require 'embedly/request'
8
7
  require 'querystring'
9
8
  require 'oauth'
10
- begin
11
- require 'typhoeus'
12
- rescue LoadError => e
13
- end
14
9
 
15
10
  # Performs api calls to embedly.
16
11
  #
@@ -55,11 +50,11 @@ class Embedly::API
55
50
  # [:+proxy+] Proxy settings in format {:host => '', :port => '', :user => '', :password => ''}
56
51
  def initialize opts={}
57
52
  @endpoints = [:oembed, :objectify, :preview, :extract]
58
- @key = opts[:key]
53
+ @key = opts[:key] || configuration.key
59
54
  @secret = opts[:secret] == "" ? nil : opts[:secret]
60
55
  @api_version = Hash.new('1')
61
56
  @api_version.merge!({:objectify => '2'})
62
- @hostname = opts[:hostname] || 'api.embed.ly'
57
+ @hostname = opts[:hostname] || 'http://api.embed.ly'
63
58
  @timeout = opts[:timeout] || 180
64
59
  @headers = {
65
60
  'User-Agent' => opts[:user_agent] || "Mozilla/5.0 (compatible; embedly-ruby/#{Embedly::VERSION};)"
@@ -67,31 +62,6 @@ class Embedly::API
67
62
  @proxy = opts[:proxy]
68
63
  end
69
64
 
70
- def _do_typhoeus_call path
71
- logger.debug { 'using Typhoeus HTTP Client' }
72
- scheme, host, port = uri_parse hostname
73
- url = "#{scheme}://#{hostname}:#{port}#{path}"
74
- logger.debug { "calling #{site}#{path} with headers #{headers} using Typhoeus" }
75
- Typhoeus::Request.get(url, {:headers => headers, :timeout => (@timeout*1000) })
76
- end
77
-
78
- def _do_basic_call path
79
- logger.debug { 'using Net:HTTP client' }
80
- scheme, host, port = uri_parse hostname
81
- logger.debug { "calling #{site}#{path} with headers #{headers} using Net::HTTP" }
82
- http_class = if proxy
83
- logger.debug { 'using Net::HTTP::Proxy' }
84
- http_class = Net::HTTP::Proxy(proxy[:host], proxy[:port], proxy[:user], proxy[:password])
85
- else
86
- Net::HTTP
87
- end
88
- http_class.start(host, port) do |http|
89
- http.use_ssl = (scheme == 'https')
90
- http.read_timeout = @timeout
91
- http.get(path, headers)
92
- end
93
- end
94
-
95
65
  def _do_oauth_call path
96
66
  consumer = OAuth::Consumer.new(key, secret,
97
67
  :site => site,
@@ -109,7 +79,9 @@ class Embedly::API
109
79
  if key and secret
110
80
  _do_oauth_call path
111
81
  else
112
- configuration.typhoeus ? _do_typhoeus_call(path) : _do_basic_call(path)
82
+ logger.debug { "calling #{site}#{path} with headers #{headers} using #{request}" }
83
+ uri = URI.join(hostname, path)
84
+ request.get(uri, :headers => headers, :timeout => @timeout, :proxy => @proxy)
113
85
  end
114
86
  end
115
87
 
@@ -230,7 +202,12 @@ class Embedly::API
230
202
  end
231
203
  end
232
204
 
205
+ def request
206
+ configuration.current_requester.call(self)
207
+ end
208
+
233
209
  private
210
+
234
211
  def uri_parse uri
235
212
  uri =~ %r{^((http(s?))://)?([^:/]+)(:([\d]+))?(/.*)?$}
236
213
  scheme = $2 || 'http'
@@ -1,12 +1,5 @@
1
1
  require "optparse"
2
2
 
3
- begin
4
- require "typhoeus"
5
- Embedly.configuration.typhoeus = true
6
- rescue LoadError
7
- Embedly.configuration.typhoeus = false
8
- end
9
-
10
3
  module Embedly
11
4
  class CommandLine
12
5
 
@@ -40,8 +33,7 @@ module Embedly
40
33
  :secret => ENV['EMBEDLY_SECRET'],
41
34
  :timeout => nil,
42
35
  :headers => {},
43
- :query => {},
44
- :typhoeus => Embedly.configuration.typhoeus
36
+ :query => {}
45
37
  }
46
38
  end
47
39
 
@@ -99,7 +91,7 @@ Usage [OPTIONS] <url> [url] ..
99
91
  end
100
92
 
101
93
  parser.on("--no-typhoeus", "Don't use typhoeus.") do
102
- Embedly.configuration.typhoeus = false
94
+ Embedly.configuration.request_with :net_http
103
95
  end
104
96
 
105
97
  parser.separator ""
@@ -6,6 +6,7 @@ require 'logger'
6
6
  #
7
7
  # * [+debug+] Prints debugging information to logger. Default +false+. Errors still will be logged
8
8
  # * [+logger+] Configure the logger; set this if you want to use a custom logger.
9
+ # * [+request_with+] Sets the desired library to perform requests. Default is +Typhoeus+
9
10
  #
10
11
  # === Usage
11
12
  #
@@ -15,10 +16,13 @@ require 'logger'
15
16
  #
16
17
  # # customize the logger
17
18
  # config.logger = MyAwesomeLogger.new(STDERR)
19
+ #
20
+ # # performs requests with net/http
21
+ # config.request_with :net_http
18
22
  # end
19
23
  #
20
24
  class Embedly::Configuration
21
- attr_accessor :key, :typhoeus # :nodoc:
25
+ attr_accessor :key, :requester # :nodoc:
22
26
 
23
27
  def initialize # :nodoc:
24
28
  self.reset
@@ -41,10 +45,47 @@ class Embedly::Configuration
41
45
  set_logger_level(self.debug?)
42
46
  end
43
47
 
48
+ # Configures a new requester
49
+ #
50
+ # To add a new requester class, you can do the following:
51
+ #
52
+ # Embedly.configuration.add_requester :custom do |api|
53
+ # MyRequester.new(api)
54
+ # end
55
+ #
56
+ # The requester class should respond to +get+ method, which performs the request
57
+ # for more details, see +embedly/request/base.rb+
58
+ def add_requester(name, &block)
59
+ requesters[name] = block
60
+ end
61
+
62
+ def requesters # :nodoc:
63
+ @requesters ||= {}
64
+ end
65
+
66
+ # Sets api to use the desired requester class
67
+ #
68
+ # When configuring the API, you can do the following:
69
+ #
70
+ # Embedly.configure do |config|
71
+ # config.request_with :net_http
72
+ # end
73
+ #
74
+ # This way, the API will use the +net_http+ class to perform requests
75
+ def request_with(adapter_name)
76
+ self.requester = adapter_name
77
+ end
78
+
79
+ # Returns the current configured request block
80
+ def current_requester
81
+ requesters[requester]
82
+ end
83
+
44
84
  # reset configuration
45
85
  def reset
46
- self.logger = default_logger
47
- self.debug = false
86
+ self.logger = default_logger
87
+ self.debug = false
88
+ self.request_with :typhoeus
48
89
  end
49
90
 
50
91
  private
@@ -57,3 +98,11 @@ class Embedly::Configuration
57
98
  logger.level = true_or_false ? Logger::DEBUG : Logger::ERROR
58
99
  end
59
100
  end
101
+
102
+ # Use typhoeus by default if it is installed
103
+ begin
104
+ require "typhoeus"
105
+ Embedly.configuration.request_with :typhoeus
106
+ rescue LoadError
107
+ Embedly.configuration.request_with :net_http
108
+ end
@@ -0,0 +1,3 @@
1
+ require "embedly/request/base"
2
+ require "embedly/request/net_http"
3
+ require "embedly/request/typhoeus"
@@ -0,0 +1,50 @@
1
+ module Embedly
2
+ module Request # :nodoc:
3
+ # Interface to create custom requesters
4
+ #
5
+ # == The class
6
+ #
7
+ # If you want to define a custom requester, you should create a class that
8
+ # inherits from this base class.
9
+ #
10
+ # The class should respond to +get+ that receives the uri object and the api
11
+ # options. The method should perform a get request to Embedly api and return
12
+ # a response object:
13
+ #
14
+ # class MyRequester < Embedly::Request::Base
15
+ # def get(uri, options = {})
16
+ # # performs the request
17
+ # end
18
+ # end
19
+ #
20
+ # For more examples, see embedly/requests/*.rb files
21
+ #
22
+ # == Adding to configuration
23
+ #
24
+ # Make sure to add your class to +embedly+ requesters, like the following:
25
+ #
26
+ # Embedly.configuration.add_requester :my_requester do |api|
27
+ # MyRequester.new(api)
28
+ # end
29
+ #
30
+ # This way you can configure the API to use your custom request object:
31
+ #
32
+ # Embedly.configure do |config|
33
+ # config.request_with :my_requester
34
+ # end
35
+ #
36
+ class Base
37
+ attr_accessor :api # :nodoc:
38
+
39
+ # Receives the current api object
40
+ def initialize(api)
41
+ @api = api
42
+ end
43
+
44
+ # Implement this method
45
+ def get(uri, options = {})
46
+ raise NotImplementedError
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,27 @@
1
+ require "net/http"
2
+
3
+ module Embedly
4
+ module NetHTTP # :nodoc:
5
+ class Request < Embedly::Request::Base
6
+ # Perform request using net/http library
7
+ def get(uri, options = {})
8
+ proxy = options['proxy']
9
+ http_class = if proxy
10
+ logger.debug { 'using Net::HTTP::Proxy' }
11
+ http_class = Net::HTTP::Proxy(proxy[:host], proxy[:port], proxy[:user], proxy[:password])
12
+ else
13
+ Net::HTTP
14
+ end
15
+
16
+ http_class.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') do |http|
17
+ http.read_timeout = options[:timeout]
18
+ http.get([uri.path, uri.query].join('?'), options[:headers])
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
24
+
25
+ Embedly.configuration.add_requester :net_http do |api|
26
+ Embedly::NetHTTP::Request.new(api)
27
+ end
@@ -0,0 +1,25 @@
1
+ # TODO: a better solution is to only load the requester if it is in use,
2
+ # rather than loading them all on startup.
3
+ begin
4
+ require "typhoeus"
5
+
6
+ module Embedly
7
+ module Typhoeus # :nodoc:
8
+ class Request < Embedly::Request::Base
9
+ # Perform request using typhoeus
10
+ def get(uri, options = {})
11
+ options[:timeout] *= 1000
12
+ ::Typhoeus::Request.get uri.to_s, :headers => options[:headers], :timeout => options[:timeout]
13
+ end
14
+ end
15
+ end
16
+ end
17
+
18
+ Embedly.configuration.add_requester :typhoeus do |api|
19
+ Embedly::Typhoeus::Request.new(api)
20
+ end
21
+ rescue LoadError
22
+ require "logger"
23
+ logger = Logger.new(STDERR)
24
+ logger.warn { "typhoeus failed to load" }
25
+ end
@@ -1,5 +1,6 @@
1
1
  require "spec_helper"
2
2
 
3
+
3
4
  module Embedly
4
5
  describe API do
5
6
  let(:api) { API.new :key => ENV['EMBEDLY_KEY'], :secret => ENV['EMBEDLY_SECRET'] }
@@ -19,5 +20,27 @@ module Embedly
19
20
  io.string.should =~ %r{.*DEBUG -- : .*}
20
21
  end
21
22
  end
23
+
24
+ describe "requesters" do
25
+ describe "net/http" do
26
+ before do
27
+ Embedly.configure { |c| c.request_with :net_http }
28
+ end
29
+
30
+ it "sets the correct request adapter" do
31
+ api.request.should be_a(Embedly::NetHTTP::Request)
32
+ end
33
+ end
34
+
35
+ describe "typhoeus" do
36
+ before do
37
+ Embedly.configure { |c| c.request_with :typhoeus }
38
+ end
39
+
40
+ it "sets the correct request adapter" do
41
+ api.request.should be_a(Embedly::Typhoeus::Request)
42
+ end
43
+ end
44
+ end
22
45
  end
23
46
  end
@@ -13,7 +13,7 @@ module Embedly
13
13
  let(:api) { mock(API) }
14
14
 
15
15
  it "calls api with options" do
16
- API.should_receive(:new).with(:key => 'MY_KEY', :headers => {}, :typhoeus => true) { api }
16
+ API.should_receive(:new).with(:key => 'MY_KEY', :headers => {}) { api }
17
17
  api.should_receive(:oembed).with(:urls => ['http://yfrog.com/h7qqespj'], :maxwidth => '10')
18
18
  CommandLine.run!(:oembed, arguments)
19
19
  end
@@ -87,6 +87,13 @@ module Embedly
87
87
  end
88
88
  end
89
89
 
90
+ describe "with --no-typhoeus" do
91
+ it "sets the request with net/http" do
92
+ command(['--no-typhoeus'])
93
+ Embedly.configuration.requester.should == :net_http
94
+ end
95
+ end
96
+
90
97
  describe "with --option" do
91
98
  %w[-o --option].each do |option|
92
99
  it "sets custom option with #{option}" do
@@ -48,6 +48,20 @@ module Embedly
48
48
  config.key = 'my_api_key'
49
49
  config.key.should == 'my_api_key'
50
50
  end
51
+
52
+ it "requests typhoeus by default" do
53
+ config.requester.should == :typhoeus
54
+ end
55
+ end
56
+
57
+ describe "registering a requester" do
58
+ it "adds a new requester" do
59
+ config.add_requester :sample do |api|
60
+ Embedly::NetHTTP::Request.new(api)
61
+ end
62
+ config.request_with :sample
63
+ config.current_requester.call({}).should be_a(Embedly::NetHTTP::Request)
64
+ end
51
65
  end
52
66
  end
53
67
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: embedly
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.7.1
4
+ version: 1.8.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,6 +9,8 @@ authors:
9
9
  - Felipe Elias Philipp
10
10
  - Russ Bradberry
11
11
  - Arun Thampi
12
+ - Anton Dieterle
13
+ - Nitesh
12
14
  autorequire:
13
15
  bindir: bin
14
16
  cert_chain: []
@@ -209,6 +211,10 @@ files:
209
211
  - lib/embedly/configuration.rb
210
212
  - lib/embedly/exceptions.rb
211
213
  - lib/embedly/model.rb
214
+ - lib/embedly/request.rb
215
+ - lib/embedly/request/base.rb
216
+ - lib/embedly/request/net_http.rb
217
+ - lib/embedly/request/typhoeus.rb
212
218
  - spec/embedly/api_spec.rb
213
219
  - spec/embedly/command_line_spec.rb
214
220
  - spec/embedly/configuration_spec.rb
@@ -228,7 +234,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
228
234
  version: '0'
229
235
  segments:
230
236
  - 0
231
- hash: -1648280040429517220
237
+ hash: 1864796039289790567
232
238
  required_rubygems_version: !ruby/object:Gem::Requirement
233
239
  none: false
234
240
  requirements: