embedly 0.4.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.rvmrc CHANGED
@@ -1 +1 @@
1
- rvm 1.9.2@embedly-ruby
1
+ rvm use 1.9.2@embedly-ruby
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.4.0
1
+ 1.0.0
@@ -3,10 +3,11 @@ $:.unshift(File.expand_path('../../lib', __FILE__))
3
3
  %w{embedly json optparse ostruct}.each {|l| require l}
4
4
 
5
5
  options = OpenStruct.new({
6
- :endpoint => nil,
6
+ :hostname => nil,
7
7
  :key => ENV['EMBEDLY_KEY'] == '' ? nil : ENV['EMBEDLY_KEY'],
8
8
  :verbose => false,
9
- :args => {}
9
+ :args => {},
10
+ :headers => {}
10
11
  })
11
12
 
12
13
  action = File.basename(__FILE__)[/embedly_(\w+)/, 1]
@@ -20,17 +21,26 @@ Usage #{action} [OPTIONS] <url> [url] ..
20
21
  opts.separator ""
21
22
  opts.separator "Options:"
22
23
 
23
- opts.on("-e", "--endpoint ENDPOINT",
24
- "Embedly host. If key is present, default is pro.embed.ly, else " +
25
- "it is api.embed.ly.") do |e|
26
- options.endpoint = e
24
+ opts.on("-H", "--hostname ENDPOINT",
25
+ "Embedly host. Default is api.embed.ly.") do |e|
26
+ options.hostname = e
27
27
  end
28
28
 
29
- opts.on("-k", "--key KEY", "Embedly PRO key [default: " +
29
+ opts.on("--header NAME=VALUE",
30
+ "HTTP header to send with requests.") do |header|
31
+ n,v = header.split '='
32
+ options.headers[n] = v
33
+ end
34
+
35
+ opts.on("-k", "--key KEY", "Embedly key [default: " +
30
36
  "EMBEDLY_KEY environmental variable]") do |k|
31
37
  options.key = k
32
38
  end
33
39
 
40
+ opts.on("-N", "--no-key", "Ignore EMBEDLY_KEY environmental variable") do
41
+ options.key = nil
42
+ end
43
+
34
44
  opts.on("-o", "--option NAME=VALUE", "Set option to be passed as " +
35
45
  "query param.") do |o|
36
46
  k,v = o.split('=')
data/bin/embedly_oembed CHANGED
@@ -3,10 +3,11 @@ $:.unshift(File.expand_path('../../lib', __FILE__))
3
3
  %w{embedly json optparse ostruct}.each {|l| require l}
4
4
 
5
5
  options = OpenStruct.new({
6
- :endpoint => nil,
6
+ :hostname => nil,
7
7
  :key => ENV['EMBEDLY_KEY'] == '' ? nil : ENV['EMBEDLY_KEY'],
8
8
  :verbose => false,
9
- :args => {}
9
+ :args => {},
10
+ :headers => {}
10
11
  })
11
12
 
12
13
  action = File.basename(__FILE__)[/embedly_(\w+)/, 1]
@@ -20,17 +21,26 @@ Usage #{action} [OPTIONS] <url> [url] ..
20
21
  opts.separator ""
21
22
  opts.separator "Options:"
22
23
 
23
- opts.on("-e", "--endpoint ENDPOINT",
24
- "Embedly host. If key is present, default is pro.embed.ly, else " +
25
- "it is api.embed.ly.") do |e|
26
- options.endpoint = e
24
+ opts.on("-H", "--hostname ENDPOINT",
25
+ "Embedly host. Default is api.embed.ly.") do |e|
26
+ options.hostname = e
27
27
  end
28
28
 
29
- opts.on("-k", "--key KEY", "Embedly PRO key [default: " +
29
+ opts.on("--header NAME=VALUE",
30
+ "HTTP header to send with requests.") do |header|
31
+ n,v = header.split '='
32
+ options.headers[n] = v
33
+ end
34
+
35
+ opts.on("-k", "--key KEY", "Embedly key [default: " +
30
36
  "EMBEDLY_KEY environmental variable]") do |k|
31
37
  options.key = k
32
38
  end
33
39
 
40
+ opts.on("-N", "--no-key", "Ignore EMBEDLY_KEY environmental variable") do
41
+ options.key = nil
42
+ end
43
+
34
44
  opts.on("-o", "--option NAME=VALUE", "Set option to be passed as " +
35
45
  "query param.") do |o|
36
46
  k,v = o.split('=')
data/bin/embedly_preview CHANGED
@@ -3,10 +3,11 @@ $:.unshift(File.expand_path('../../lib', __FILE__))
3
3
  %w{embedly json optparse ostruct}.each {|l| require l}
4
4
 
5
5
  options = OpenStruct.new({
6
- :endpoint => nil,
6
+ :hostname => nil,
7
7
  :key => ENV['EMBEDLY_KEY'] == '' ? nil : ENV['EMBEDLY_KEY'],
8
8
  :verbose => false,
9
- :args => {}
9
+ :args => {},
10
+ :headers => {}
10
11
  })
11
12
 
12
13
  action = File.basename(__FILE__)[/embedly_(\w+)/, 1]
@@ -20,17 +21,26 @@ Usage #{action} [OPTIONS] <url> [url] ..
20
21
  opts.separator ""
21
22
  opts.separator "Options:"
22
23
 
23
- opts.on("-e", "--endpoint ENDPOINT",
24
- "Embedly host. If key is present, default is pro.embed.ly, else " +
25
- "it is api.embed.ly.") do |e|
26
- options.endpoint = e
24
+ opts.on("-H", "--hostname ENDPOINT",
25
+ "Embedly host. Default is api.embed.ly.") do |e|
26
+ options.hostname = e
27
27
  end
28
28
 
29
- opts.on("-k", "--key KEY", "Embedly PRO key [default: " +
29
+ opts.on("--header NAME=VALUE",
30
+ "HTTP header to send with requests.") do |header|
31
+ n,v = header.split '='
32
+ options.headers[n] = v
33
+ end
34
+
35
+ opts.on("-k", "--key KEY", "Embedly key [default: " +
30
36
  "EMBEDLY_KEY environmental variable]") do |k|
31
37
  options.key = k
32
38
  end
33
39
 
40
+ opts.on("-N", "--no-key", "Ignore EMBEDLY_KEY environmental variable") do
41
+ options.key = nil
42
+ end
43
+
34
44
  opts.on("-o", "--option NAME=VALUE", "Set option to be passed as " +
35
45
  "query param.") do |o|
36
46
  k,v = o.split('=')
@@ -5,7 +5,7 @@ Feature: Objectify
5
5
  Because I want to objectify a url
6
6
 
7
7
  Scenario Outline: Get the meta description with pro
8
- Given an embedly endpoint with key
8
+ Given an embedly api with key
9
9
  When objectify is called with the <url> URL
10
10
  Then the meta.description should start with <metadesc>
11
11
  And objectify api_version is 2
@@ -5,7 +5,7 @@ Feature: OEmbed
5
5
  Because I want and oembed for a specific url
6
6
 
7
7
  Scenario Outline: Get the provider_url
8
- Given an embedly endpoint
8
+ Given an embedly api
9
9
  When oembed is called with the <url> URL
10
10
  Then the provider_url should be <provider_url>
11
11
 
@@ -18,7 +18,7 @@ Feature: OEmbed
18
18
 
19
19
 
20
20
  Scenario Outline: Get the types
21
- Given an embedly endpoint
21
+ Given an embedly api
22
22
  When oembed is called with the <url> URL
23
23
  Then the type should be <type>
24
24
 
@@ -31,7 +31,7 @@ Feature: OEmbed
31
31
 
32
32
 
33
33
  Scenario Outline: Get the provider_url with force flag
34
- Given an embedly endpoint
34
+ Given an embedly api
35
35
  When oembed is called with the <url> URL and force flag
36
36
  Then the provider_url should be <provider_url>
37
37
 
@@ -41,7 +41,7 @@ Feature: OEmbed
41
41
 
42
42
 
43
43
  Scenario Outline: Get multiple provider_urls
44
- Given an embedly endpoint
44
+ Given an embedly api
45
45
  When oembed is called with the <urls> URLs
46
46
  Then provider_url should be <provider_urls>
47
47
 
@@ -52,7 +52,7 @@ Feature: OEmbed
52
52
 
53
53
 
54
54
  Scenario Outline: Get the provider_url with pro
55
- Given an embedly endpoint with key
55
+ Given an embedly api with key
56
56
  When oembed is called with the <url> URL
57
57
  Then the provider_url should be <provider_url>
58
58
 
@@ -64,7 +64,7 @@ Feature: OEmbed
64
64
 
65
65
 
66
66
  Scenario Outline: Attempt to get 404 URL
67
- Given an embedly endpoint
67
+ Given an embedly api
68
68
  When oembed is called with the <url> URL
69
69
  Then type should be error
70
70
  And error_code should be 404
@@ -78,7 +78,7 @@ Feature: OEmbed
78
78
 
79
79
 
80
80
  Scenario Outline: Attempt multi get 404 URLs
81
- Given an embedly endpoint
81
+ Given an embedly api
82
82
  When oembed is called with the <urls> URLs
83
83
  Then error_code should be <errcode>
84
84
  And type should be <types>
@@ -91,10 +91,10 @@ Feature: OEmbed
91
91
  | http://yfrog.com/h7qqespj,http://www.scribd.com/doc/asdfasdfasdf | ,404 | photo,error |
92
92
 
93
93
  Scenario Outline: Attempt at non-api service without key
94
- Given an embedly endpoint
94
+ Given an embedly api
95
95
  When oembed is called with the <url> URL
96
96
  Then error_code should be 401
97
- And error_message should be This service requires an Embedly Pro account
97
+ And error_message should be Embedly api key is required.
98
98
  And type should be error
99
99
 
100
100
  Examples:
@@ -1,20 +1,19 @@
1
1
  $:.unshift(File.expand_path('../../../lib',__FILE__))
2
2
  require 'embedly'
3
3
 
4
- # cache for endpoints
5
- ENDPOINTS = {}
4
+ # cache for hostnames
5
+ HOSTNAMES = {}
6
6
 
7
- Given /an embedly endpoint( [^\s]+)?( with key)?$/ do |endpoint, key_enabled|
7
+ Given /an embedly api( with key)?$/ do |key_enabled|
8
8
  opts = {}
9
- opts[:endpoint] = endpoint
10
9
  if key_enabled
11
10
  raise 'Please set env variable $EMBEDLY_KEY' unless ENV['EMBEDLY_KEY']
12
11
  opts[:key] = ENV["EMBEDLY_KEY"]
13
12
  end
14
- if not ENDPOINTS[opts]
15
- ENDPOINTS[opts] = Embedly::API.new opts
13
+ if not HOSTNAMES[opts]
14
+ HOSTNAMES[opts] = Embedly::API.new opts
16
15
  end
17
- @api = ENDPOINTS[opts]
16
+ @api = HOSTNAMES[opts]
18
17
  end
19
18
 
20
19
  When /(\w+) is called with the (.*) URLs?( and ([^\s]+) flag)?$/ do |method, urls, _, flag|
data/lib/embedly/api.rb CHANGED
@@ -14,7 +14,7 @@ require 'querystring'
14
14
  #
15
15
  # * +oembed+
16
16
  # * +objectify+
17
- # * +preview+ _pro-only_
17
+ # * +preview+
18
18
  #
19
19
  # All methods return ostructs, so fields can be accessed with the dot operator. ex.
20
20
  #
@@ -34,7 +34,7 @@ require 'querystring'
34
34
  # api.new_method :arg1 => '1', :arg2 => '2'
35
35
  #
36
36
  class Embedly::API
37
- attr_reader :key, :endpoint, :api_version, :user_agent
37
+ attr_reader :key, :hostname, :api_version, :headers
38
38
 
39
39
  def logger *args
40
40
  Embedly.logger *args
@@ -42,21 +42,18 @@ class Embedly::API
42
42
 
43
43
  # === Options
44
44
  #
45
- # [:+endpoint+] Hostname of embedly server. Defaults to api.embed.ly if no key is provided, pro.embed.ly if key is provided.
46
- # [:+key+] Your pro.embed.ly api key.
45
+ # [:+hostname+] Hostname of embedly server. Defaults to api.embed.ly.
46
+ # [:+key+] Your api.embed.ly key.
47
47
  # [:+user_agent+] Your User-Agent header. Defaults to Mozilla/5.0 (compatible; embedly-ruby/VERSION;)
48
+ # [:+headers+] Additional headers to send with requests.
48
49
  def initialize opts={}
49
50
  @endpoints = [:oembed, :objectify, :preview]
50
51
  @key = opts[:key]
51
52
  @api_version = Hash.new('1')
52
53
  @api_version.merge!({:objectify => '2'})
53
- if @key
54
- logger.debug('using pro')
55
- @endpoint = opts[:endpoint] || 'pro.embed.ly'
56
- else
57
- @endpoint = opts[:endpoint] || 'api.embed.ly'
58
- end
59
- @user_agent = opts[:user_agent] || "Mozilla/5.0 (compatible; embedly-ruby/#{Embedly::VERSION};)"
54
+ @hostname = opts[:hostname] || 'api.embed.ly'
55
+ @headers = opts[:headers] || {}
56
+ @headers['User-Agent'] = opts[:user_agent] || "Mozilla/5.0 (compatible; embedly-ruby/#{Embedly::VERSION};)"
60
57
  end
61
58
 
62
59
  # <b>Use methods oembed, objectify, preview in favor of this method.</b>
@@ -82,13 +79,13 @@ class Embedly::API
82
79
  # store unsupported services as errors and don't send them to embedly
83
80
  rejects = []
84
81
  if not key
85
- params[:urls].reject!.with_index do |url, i|
82
+ params[:urls].reject!.with_index do |url, i|
86
83
  if url !~ services_regex
87
- rejects << [i,
84
+ rejects << [i,
88
85
  Embedly::EmbedlyObject.new(
89
- :type => 'error',
90
- :error_code => 401,
91
- :error_message => 'This service requires an Embedly Pro account'
86
+ :type => 'error',
87
+ :error_code => 401,
88
+ :error_message => 'Embedly api key is required.'
92
89
  )
93
90
  ]
94
91
  end
@@ -103,11 +100,11 @@ class Embedly::API
103
100
 
104
101
  path = "/#{opts[:version]}/#{opts[:action]}?#{QueryString.stringify(params)}"
105
102
 
106
- logger.debug { "calling #{endpoint}#{path}" }
103
+ logger.debug { "calling #{hostname}#{path} with headers #{headers}" }
107
104
 
108
- host, port = uri_parse(endpoint)
105
+ host, port = uri_parse(hostname)
109
106
  response = Net::HTTP.start(host, port) do |http|
110
- http.get(path, {'User-Agent' => user_agent})
107
+ http.get(path, headers)
111
108
  end
112
109
 
113
110
  if response.code.to_i == 200
@@ -139,11 +136,10 @@ class Embedly::API
139
136
  #
140
137
  # see http://api.embed.ly/docs/service for a description of the response.
141
138
  def services
142
- logger.warn { "services isn't availble on the pro endpoint" } if key
143
139
  if not @services
144
- host, port = uri_parse(endpoint)
140
+ host, port = uri_parse(hostname)
145
141
  response = Net::HTTP.start(host, port) do |http|
146
- http.get('/1/services/ruby', {'User-Agent' => user_agent})
142
+ http.get('/1/services/ruby', headers)
147
143
  end
148
144
  raise 'services call failed', response if response.code.to_i != 200
149
145
  @services = JSON.parse(response.body)
@@ -151,7 +147,7 @@ class Embedly::API
151
147
  @services
152
148
  end
153
149
 
154
- # Returns a regex suitable for checking urls against for non-Pro usage
150
+ # Returns a regex suitable for checking urls against for non-key usage
155
151
  def services_regex
156
152
  r = services.collect {|p| p["regex"].join("|")}.join("|")
157
153
  Regexp.new r
@@ -163,7 +159,7 @@ class Embedly::API
163
159
  #
164
160
  # - +oembed+
165
161
  # - +objectify+
166
- # - +preview+ _pro-only_
162
+ # - +preview+
167
163
  #
168
164
  def method_missing(name, *args, &block)
169
165
  if @endpoints.include?name
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: 0.4.0
4
+ version: 1.0.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,12 +9,12 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-05-20 00:00:00.000000000 -04:00
12
+ date: 2011-06-22 00:00:00.000000000 -04:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: querystring
17
- requirement: &68358050 !ruby/object:Gem::Requirement
17
+ requirement: &72141180 !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ! '>='
@@ -22,10 +22,10 @@ dependencies:
22
22
  version: '0'
23
23
  type: :runtime
24
24
  prerelease: false
25
- version_requirements: *68358050
25
+ version_requirements: *72141180
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: jeweler
28
- requirement: &68355990 !ruby/object:Gem::Requirement
28
+ requirement: &72195490 !ruby/object:Gem::Requirement
29
29
  none: false
30
30
  requirements:
31
31
  - - ! '>='
@@ -33,10 +33,10 @@ dependencies:
33
33
  version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
- version_requirements: *68355990
36
+ version_requirements: *72195490
37
37
  - !ruby/object:Gem::Dependency
38
38
  name: cucumber
39
- requirement: &68353970 !ruby/object:Gem::Requirement
39
+ requirement: &72195250 !ruby/object:Gem::Requirement
40
40
  none: false
41
41
  requirements:
42
42
  - - ! '>='
@@ -44,10 +44,10 @@ dependencies:
44
44
  version: '0'
45
45
  type: :development
46
46
  prerelease: false
47
- version_requirements: *68353970
47
+ version_requirements: *72195250
48
48
  - !ruby/object:Gem::Dependency
49
49
  name: rake
50
- requirement: &68353080 !ruby/object:Gem::Requirement
50
+ requirement: &72195010 !ruby/object:Gem::Requirement
51
51
  none: false
52
52
  requirements:
53
53
  - - ! '>='
@@ -55,10 +55,10 @@ dependencies:
55
55
  version: '0'
56
56
  type: :development
57
57
  prerelease: false
58
- version_requirements: *68353080
58
+ version_requirements: *72195010
59
59
  - !ruby/object:Gem::Dependency
60
60
  name: rspec
61
- requirement: &68352090 !ruby/object:Gem::Requirement
61
+ requirement: &72194770 !ruby/object:Gem::Requirement
62
62
  none: false
63
63
  requirements:
64
64
  - - ! '>='
@@ -66,10 +66,10 @@ dependencies:
66
66
  version: '0'
67
67
  type: :development
68
68
  prerelease: false
69
- version_requirements: *68352090
69
+ version_requirements: *72194770
70
70
  - !ruby/object:Gem::Dependency
71
71
  name: yard
72
- requirement: &68319420 !ruby/object:Gem::Requirement
72
+ requirement: &72194530 !ruby/object:Gem::Requirement
73
73
  none: false
74
74
  requirements:
75
75
  - - ! '>='
@@ -77,7 +77,7 @@ dependencies:
77
77
  version: '0'
78
78
  type: :development
79
79
  prerelease: false
80
- version_requirements: *68319420
80
+ version_requirements: *72194530
81
81
  description: Ruby Embedly client library
82
82
  email: bob@embed.ly
83
83
  executables:
@@ -123,7 +123,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
123
123
  version: '0'
124
124
  segments:
125
125
  - 0
126
- hash: -471678371
126
+ hash: 44977427
127
127
  required_rubygems_version: !ruby/object:Gem::Requirement
128
128
  none: false
129
129
  requirements: