Skip to content

Commit

Permalink
Merge branch 'master' of github.com:embedly/embedly-ruby
Browse files Browse the repository at this point in the history
Conflicts:
	lib/embedly/api.rb
  • Loading branch information
Bob Corsaro committed Jun 23, 2011
2 parents a9d05c9 + 5d04ed8 commit 047e3b2
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 54 deletions.
2 changes: 1 addition & 1 deletion .rvmrc
@@ -1 +1 @@
rvm 1.9.2@embedly-ruby
rvm use 1.9.2@embedly-ruby
8 changes: 4 additions & 4 deletions README.rdoc
Expand Up @@ -45,11 +45,11 @@ You can find rdocs at http://rubydoc.info/github/embedly/embedly-ruby/master/fra
json_obj = JSON.pretty_generate(objs.collect{|o| o.marshal_dump})
puts json_obj

# call pro with key (you'll need a real key)
embedly_pro = Embedly::API.new :key => 'xxxxxxxxxxxxxxxxxxxxxxxxxx',
# call api with key (you'll need a real key)
embedly_api = Embedly::API.new :key => 'xxxxxxxxxxxxxxxxxxxxxxxxxx',
:user_agent => 'Mozilla/5.0 (compatible; mytestapp/1.0; my@email.com)'
url = 'http://www.guardian.co.uk/media/2011/jan/21/andy-coulson-phone-hacking-statement'
obj = embedly_pro.preview :url => url
obj = embedly_api.preview :url => url
puts JSON.pretty_generate(obj[0].marshal_dump)

== Testing
Expand All @@ -58,7 +58,7 @@ You can find rdocs at http://rubydoc.info/github/embedly/embedly-ruby/master/fra
rake rspec
rake features # if it complains of missing deps install them

Some tests will fail due to missing pro key. Set the EMBEDLY_KEY environmental
Some tests will fail due to missing api key. Set the EMBEDLY_KEY environmental
variable with your key to get them to pass.

EMBEDLY_KEY=xxxxxxxxxxxxx rake features
Expand Down
2 changes: 1 addition & 1 deletion VERSION
@@ -1 +1 @@
0.4.0
1.0.0
24 changes: 17 additions & 7 deletions bin/embedly_oembed
Expand Up @@ -3,10 +3,11 @@ $:.unshift(File.expand_path('../../lib', __FILE__))
%w{embedly json optparse ostruct}.each {|l| require l}

options = OpenStruct.new({
:endpoint => nil,
:hostname => nil,
:key => ENV['EMBEDLY_KEY'] == '' ? nil : ENV['EMBEDLY_KEY'],
:verbose => false,
:args => {}
:args => {},
:headers => {}
})

action = File.basename(__FILE__)[/embedly_(\w+)/, 1]
Expand All @@ -20,17 +21,26 @@ Usage #{action} [OPTIONS] <url> [url] ..
opts.separator ""
opts.separator "Options:"

opts.on("-e", "--endpoint ENDPOINT",
"Embedly host. If key is present, default is pro.embed.ly, else " +
"it is api.embed.ly.") do |e|
options.endpoint = e
opts.on("-H", "--hostname ENDPOINT",
"Embedly host. Default is api.embed.ly.") do |e|
options.hostname = e
end

opts.on("-k", "--key KEY", "Embedly PRO key [default: " +
opts.on("--header NAME=VALUE",
"HTTP header to send with requests.") do |header|
n,v = header.split '='
options.headers[n] = v
end

opts.on("-k", "--key KEY", "Embedly key [default: " +
"EMBEDLY_KEY environmental variable]") do |k|
options.key = k
end

opts.on("-N", "--no-key", "Ignore EMBEDLY_KEY environmental variable") do
options.key = nil
end

opts.on("-o", "--option NAME=VALUE", "Set option to be passed as " +
"query param.") do |o|
k,v = o.split('=')
Expand Down
2 changes: 1 addition & 1 deletion features/objectify.feature
Expand Up @@ -5,7 +5,7 @@ Feature: Objectify
Because I want to objectify a url

Scenario Outline: Get the meta description with pro
Given an embedly endpoint with key
Given an embedly api with key
When objectify is called with the <url> URL
Then the meta.description should start with <metadesc>
And objectify api_version is 2
Expand Down
18 changes: 9 additions & 9 deletions features/oembed.feature
Expand Up @@ -5,7 +5,7 @@ Feature: OEmbed
Because I want and oembed for a specific url

Scenario Outline: Get the provider_url
Given an embedly endpoint
Given an embedly api
When oembed is called with the <url> URL
Then the provider_url should be <provider_url>

Expand All @@ -18,7 +18,7 @@ Feature: OEmbed


Scenario Outline: Get the types
Given an embedly endpoint
Given an embedly api
When oembed is called with the <url> URL
Then the type should be <type>

Expand All @@ -31,7 +31,7 @@ Feature: OEmbed


Scenario Outline: Get the provider_url with force flag
Given an embedly endpoint
Given an embedly api
When oembed is called with the <url> URL and force flag
Then the provider_url should be <provider_url>

Expand All @@ -41,7 +41,7 @@ Feature: OEmbed


Scenario Outline: Get multiple provider_urls
Given an embedly endpoint
Given an embedly api
When oembed is called with the <urls> URLs
Then provider_url should be <provider_urls>

Expand All @@ -52,7 +52,7 @@ Feature: OEmbed


Scenario Outline: Get the provider_url with pro
Given an embedly endpoint with key
Given an embedly api with key
When oembed is called with the <url> URL
Then the provider_url should be <provider_url>

Expand All @@ -64,7 +64,7 @@ Feature: OEmbed


Scenario Outline: Attempt to get 404 URL
Given an embedly endpoint
Given an embedly api
When oembed is called with the <url> URL
Then type should be error
And error_code should be 404
Expand All @@ -78,7 +78,7 @@ Feature: OEmbed


Scenario Outline: Attempt multi get 404 URLs
Given an embedly endpoint
Given an embedly api
When oembed is called with the <urls> URLs
Then error_code should be <errcode>
And type should be <types>
Expand All @@ -91,10 +91,10 @@ Feature: OEmbed
| http://yfrog.com/h7qqespj,http://www.scribd.com/doc/asdfasdfasdf | ,404 | photo,error |

Scenario Outline: Attempt at non-api service without key
Given an embedly endpoint
Given an embedly api
When oembed is called with the <url> URL
Then error_code should be 401
And error_message should be This service requires an Embedly Pro account
And error_message should be Embedly api key is required.
And type should be error

Examples:
Expand Down
13 changes: 6 additions & 7 deletions features/steps/api_steps.rb
@@ -1,20 +1,19 @@
$:.unshift(File.expand_path('../../../lib',__FILE__))
require 'embedly'

# cache for endpoints
ENDPOINTS = {}
# cache for hostnames
HOSTNAMES = {}

Given /an embedly endpoint( [^\s]+)?( with key)?$/ do |endpoint, key_enabled|
Given /an embedly api( with key)?$/ do |key_enabled|
opts = {}
opts[:endpoint] = endpoint
if key_enabled
raise 'Please set env variable $EMBEDLY_KEY' unless ENV['EMBEDLY_KEY']
opts[:key] = ENV["EMBEDLY_KEY"]
end
if not ENDPOINTS[opts]
ENDPOINTS[opts] = Embedly::API.new opts
if not HOSTNAMES[opts]
HOSTNAMES[opts] = Embedly::API.new opts
end
@api = ENDPOINTS[opts]
@api = HOSTNAMES[opts]
end

When /(\w+) is called with the (.*) URLs?( and ([^\s]+) flag)?$/ do |method, urls, _, flag|
Expand Down
44 changes: 20 additions & 24 deletions lib/embedly/api.rb
Expand Up @@ -15,7 +15,7 @@
#
# * +oembed+
# * +objectify+
# * +preview+ _pro-only_
# * +preview+
#
# All methods return ostructs, so fields can be accessed with the dot operator. ex.
#
Expand All @@ -35,29 +35,26 @@
# api.new_method :arg1 => '1', :arg2 => '2'
#
class Embedly::API
attr_reader :key, :endpoint, :api_version, :user_agent
attr_reader :key, :hostname, :api_version, :headers

def logger *args
Embedly.logger *args
end

# === Options
#
# [:+endpoint+] Hostname of embedly server. Defaults to api.embed.ly if no key is provided, pro.embed.ly if key is provided.
# [:+key+] Your pro.embed.ly api key.
# [:+hostname+] Hostname of embedly server. Defaults to api.embed.ly.
# [:+key+] Your api.embed.ly key.
# [:+user_agent+] Your User-Agent header. Defaults to Mozilla/5.0 (compatible; embedly-ruby/VERSION;)
# [:+headers+] Additional headers to send with requests.
def initialize opts={}
@endpoints = [:oembed, :objectify, :preview]
@key = opts[:key]
@api_version = Hash.new('1')
@api_version.merge!({:objectify => '2'})
if @key
logger.debug('using pro')
@endpoint = opts[:endpoint] || 'pro.embed.ly'
else
@endpoint = opts[:endpoint] || 'api.embed.ly'
end
@user_agent = opts[:user_agent] || "Mozilla/5.0 (compatible; embedly-ruby/#{Embedly::VERSION};)"
@hostname = opts[:hostname] || 'api.embed.ly'
@headers = opts[:headers] || {}
@headers['User-Agent'] = opts[:user_agent] || "Mozilla/5.0 (compatible; embedly-ruby/#{Embedly::VERSION};)"
end

# <b>Use methods oembed, objectify, preview in favor of this method.</b>
Expand All @@ -83,13 +80,13 @@ def apicall opts
# store unsupported services as errors and don't send them to embedly
rejects = []
if not key
params[:urls].reject!.with_index do |url, i|
params[:urls].reject!.with_index do |url, i|
if url !~ services_regex
rejects << [i,
rejects << [i,
Embedly::EmbedlyObject.new(
:type => 'error',
:error_code => 401,
:error_message => 'This service requires an Embedly Pro account'
:type => 'error',
:error_code => 401,
:error_message => 'Embedly api key is required.'
)
]
end
Expand All @@ -104,11 +101,11 @@ def apicall opts

path = "/#{opts[:version]}/#{opts[:action]}?#{QueryString.stringify(params)}"

logger.debug { "calling #{endpoint}#{path}" }
logger.debug { "calling #{hostname}#{path} with headers #{headers}" }

host, port = uri_parse(endpoint)
host, port = uri_parse(hostname)
response = Net::HTTP.start(host, port) do |http|
http.get(path, {'User-Agent' => user_agent, 'Referer' => 'Your mom', 'X-Real-IP' => '204.9.220.42'})
http.get(path, headers)
end

if response.code.to_i == 200
Expand Down Expand Up @@ -140,19 +137,18 @@ def apicall opts
#
# see http://api.embed.ly/docs/service for a description of the response.
def services
logger.warn { "services isn't availble on the pro endpoint" } if key
if not @services
host, port = uri_parse(endpoint)
host, port = uri_parse(hostname)
response = Net::HTTP.start(host, port) do |http|
http.get('/1/services/ruby', {'User-Agent' => user_agent})
http.get('/1/services/ruby', headers)
end
raise 'services call failed', response if response.code.to_i != 200
@services = JSON.parse(response.body)
end
@services
end

# Returns a regex suitable for checking urls against for non-Pro usage
# Returns a regex suitable for checking urls against for non-key usage
def services_regex
r = services.collect {|p| p["regex"].join("|")}.join("|")
Regexp.new r
Expand All @@ -164,7 +160,7 @@ def services_regex
#
# - +oembed+
# - +objectify+
# - +preview+ _pro-only_
# - +preview+
#
def method_missing(name, *args, &block)
if @endpoints.include?name
Expand Down

0 comments on commit 047e3b2

Please sign in to comment.