diff --git a/.rvmrc b/.rvmrc index e326942..28982a0 100644 --- a/.rvmrc +++ b/.rvmrc @@ -1 +1 @@ -rvm 1.9.2@embedly-ruby +rvm use 1.9.2@embedly-ruby diff --git a/README.rdoc b/README.rdoc index c90b1cf..5f8f40e 100644 --- a/README.rdoc +++ b/README.rdoc @@ -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 @@ -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 diff --git a/VERSION b/VERSION index 60a2d3e..afaf360 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.4.0 \ No newline at end of file +1.0.0 \ No newline at end of file diff --git a/bin/embedly_oembed b/bin/embedly_oembed index da946ef..7fb4fa7 100755 --- a/bin/embedly_oembed +++ b/bin/embedly_oembed @@ -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] @@ -20,17 +21,26 @@ Usage #{action} [OPTIONS] [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('=') diff --git a/features/objectify.feature b/features/objectify.feature index 2a44338..52d0e50 100644 --- a/features/objectify.feature +++ b/features/objectify.feature @@ -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 Then the meta.description should start with And objectify api_version is 2 diff --git a/features/oembed.feature b/features/oembed.feature index 4b8ff1c..1c1718a 100644 --- a/features/oembed.feature +++ b/features/oembed.feature @@ -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 Then the provider_url should be @@ -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 Then the type should be @@ -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 and force flag Then the provider_url should be @@ -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 Then provider_url should be @@ -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 Then the provider_url should be @@ -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 Then type should be error And error_code should be 404 @@ -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 Then error_code should be And type should be @@ -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 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: diff --git a/features/steps/api_steps.rb b/features/steps/api_steps.rb index 8d73802..6c19103 100644 --- a/features/steps/api_steps.rb +++ b/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| diff --git a/lib/embedly/api.rb b/lib/embedly/api.rb index c2f66d0..ce3bd20 100644 --- a/lib/embedly/api.rb +++ b/lib/embedly/api.rb @@ -15,7 +15,7 @@ # # * +oembed+ # * +objectify+ -# * +preview+ _pro-only_ +# * +preview+ # # All methods return ostructs, so fields can be accessed with the dot operator. ex. # @@ -35,7 +35,7 @@ # 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 @@ -43,21 +43,18 @@ def logger *args # === 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 # Use methods oembed, objectify, preview in favor of this method. @@ -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 @@ -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 @@ -140,11 +137,10 @@ 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) @@ -152,7 +148,7 @@ def services @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 @@ -164,7 +160,7 @@ def services_regex # # - +oembed+ # - +objectify+ - # - +preview+ _pro-only_ + # - +preview+ # def method_missing(name, *args, &block) if @endpoints.include?name