Skip to content

Commit

Permalink
Adds embedly exception
Browse files Browse the repository at this point in the history
  • Loading branch information
Bob Corsaro committed Jun 23, 2011
1 parent 63100a4 commit a9d05c9
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
10 changes: 8 additions & 2 deletions bin/embedly_oembed
Expand Up @@ -65,5 +65,11 @@ Embedly::Config.logging = true if options.verbose

options.args[:urls] = ARGV
api = Embedly::API.new options.marshal_dump
objs = [api.send(action.to_sym, options.args)].flatten.collect{|o| o.marshal_dump}
puts JSON.pretty_generate(objs)
begin
objs = [api.send(action.to_sym, options.args)].flatten.collect{|o| o.marshal_dump}
puts JSON.pretty_generate(objs)
rescue Embedly::BadResponseException => e
puts "#{e.response.code} :: #{e.response.message}"
end


7 changes: 4 additions & 3 deletions lib/embedly/api.rb
Expand Up @@ -2,6 +2,7 @@
require 'json'
require 'ostruct'
require 'embedly/model'
require 'embedly/exceptions'
require 'querystring'


Expand Down Expand Up @@ -107,7 +108,7 @@ def apicall opts

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

if response.code.to_i == 200
Expand All @@ -117,8 +118,8 @@ def apicall opts
Embedly::EmbedlyObject.new(o)
end
else
logger.error { response.inspect }
raise 'An unexpected error occurred'
logger.debug { response }
raise Embedly::BadResponseException.new response
end

# re-insert rejects into response
Expand Down
7 changes: 7 additions & 0 deletions lib/embedly/exceptions.rb
@@ -0,0 +1,7 @@
class Embedly::BadResponseException < RuntimeError
attr_accessor :response

def initialize response
@response ||= response
end
end

0 comments on commit a9d05c9

Please sign in to comment.