Permalink
Browse files

Initial Import.

  • Loading branch information...
0 parents commit 5b4c6ef00b9fa7fba0cd660b6f06c9ceeb1b25d4 @hypomodern hypomodern committed Jan 17, 2012
@@ -0,0 +1,4 @@
+*.gem
+.bundle
+Gemfile.lock
+pkg/*
3 .rspec
@@ -0,0 +1,3 @@
+--color
+--format nested
+
1 .rvmrc
@@ -0,0 +1 @@
+rvm use --create 1.9.3@oembedr_gem
@@ -0,0 +1,3 @@
+# 1.0.0 (2012-01-17)
+
+* Hello world: initial release
@@ -0,0 +1,4 @@
+source "http://rubygems.org"
+
+# Specify your gem's dependencies in oembedr.gemspec
+gemspec
@@ -0,0 +1,12 @@
+guard 'bundler' do
+ watch('Gemfile')
+ # Uncomment next line if Gemfile contain `gemspec' command
+ # watch(/^.+\.gemspec/)
+end
+
+
+guard 'rspec', version: 2, cli: '--color --format nested', all_on_start: true, all_after_pass: true do
+ watch('spec/spec_helper.rb') { "spec" }
+ watch(%r{^spec/.+_spec\.rb$})
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
+end
@@ -0,0 +1,20 @@
+Copyright (c) 2012 Matthew Wilson
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,56 @@
+## OEmbedr
+
+Lightweight, Flexible OEmbed Consumer Library.
+
+### Installation
+
+`gem install oembedr`
+
+or in your `Gemfile`
+
+```ruby
+gem 'oembedr'
+```
+
+### Goals
+
+Make consuming oembed from **any source** simple and easy. Uses fast http and fast json parsing by default thanks to Faraday and MultiJSON.
+
+### Usage
+
+`Oembedr` just returns the raw Faraday response object on success. On failure, it raises an appropriate error, e.g. `Faraday::Error::ResourceNotFound` for 404s. It parses the JSON for you.
+
+*N.B.* XML is not supported yet. I've no idea why you might **need** it, but it's on the todo list :).
+
+```ruby
+response = OEmbedr.fetch("http://www.youtube.com/watch?v=BxhqVrbixZc")
+response.body # => { "type" => "video", ... }
+# Now you can do whatever you want!
+```
+
+#### Configuration
+
+```ruby
+Oembedr.configure do |configuration|
+ configuration.user_agent = "MyCoolApp Oembed Client v2.51"
+ configuration.adapter = :excon # default :typhoeus
+end
+```
+
+### Recommendations
+
+Use a fast http library, like excon or typhoeus. Use yajl-ruby.
+
+## Contributing to oembedr
+
+* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
+* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
+* Fork the project
+* Start a feature/bugfix branch
+* Commit and push until you are happy with your contribution
+* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
+* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
+
+## Copyright
+
+Copyright (c) 2012 Matthew Wilson. MIT-license: see LICENSE.txt for further details.
@@ -0,0 +1,7 @@
+require "bundler/gem_tasks"
+require 'rspec/core/rake_task'
+
+RSpec::Core::RakeTask.new('spec')
+
+# If you want to make this the default task
+task :default => :spec
@@ -0,0 +1,27 @@
+require "oembedr/version"
+require "oembedr/configuration"
+require "oembedr/client"
+require "oembedr/providers"
+require "typhoeus"
+
+module Oembedr
+ extend Configuration
+ extend Providers
+
+ # Convience method: fetch me an oembed!
+ #
+ # Example:
+ # Oembedr.fetch("http://www.youtube.com/watch?v=b9XsTtFu64Y")
+ # Oembedr.fetch("http://www.youtube.com/watch?v=b9XsTtFu64Y",
+ # :params => { :maxwidth => "150", :maxheight => "100" })
+ #
+ # @return Faraday::Response or raises error
+ def self.fetch url, options = {}
+ client = Oembedr::Client.new(url)
+ if client.ready?
+ client.get(options)
+ else
+ false
+ end
+ end
+end
@@ -0,0 +1,58 @@
+require "oembedr/parsed_url"
+require "faraday_middleware"
+
+module Oembedr
+ class Client
+ attr_accessor :resource_url, :parsed_url
+
+ def initialize url
+ self.resource_url = url
+ self.parsed_url = ParsedUrl.new(url)
+
+ end
+
+ # Sets up a Faraday::Connection object
+ #
+ # @param options [optional]
+ #
+ # @return Faraday::Connection
+ def connection options = {}
+ the_options = {
+ :headers => {
+ "Accept" => "application/json",
+ "User-Agent" => Oembedr.user_agent
+ },
+ :url => parsed_url.host
+ }.merge(options)
+
+ Faraday.new(the_options) do |builder|
+ builder.use Faraday::Request::UrlEncoded
+ builder.use Faraday::Response::ParseJson
+ builder.use Faraday::Response::RaiseError
+ builder.adapter Oembedr.adapter
+ end
+ end
+
+ # Requests the oembeddable resource
+ #
+ # @return Faraday::Response
+ def get options = {}
+ additional_params = options.delete(:params) || {}
+ connection(options).get do |req|
+ req.url parsed_url.path
+ req.params = additional_params.merge({
+ :url => resource_url,
+ :format => "json"
+ })
+ end
+ end
+
+ # Returns a boolean true/false if we can process this request
+ #
+ # @return Boolean
+ def ready?
+ !!parsed_url.host
+ end
+
+ end
+end
@@ -0,0 +1,30 @@
+module Oembedr
+ module Configuration
+ # Set a custom User-Agent string (default: Oembedr [Rubygem] #{version})
+ attr_accessor :user_agent
+
+ # Select a different http library for Faraday to use (default: Typhoeus)
+ attr_accessor :adapter
+
+ # Yield self to be able to configure Oembedr
+ #
+ # Example:
+ #
+ # Oembedr.configure do |configuration|
+ # configuration.user_agent = "MyCoolApp Oembed Client v2.51"
+ # configuration.adapter = :excon
+ # end
+ def configure
+ yield self
+ end
+
+ def user_agent
+ @user_agent || "Oembedr Gem #{Oembedr::VERSION}"
+ end
+
+ def adapter
+ @adapter || :typhoeus
+ end
+
+ end
+end
@@ -0,0 +1,42 @@
+require "oembedr/providers"
+require "uri"
+
+module Oembedr
+ class ParsedUrl
+ attr_accessor :raw_url, :url
+
+ include Oembedr::Providers
+
+ # TODO: support xml at some point :)
+ def initialize the_url
+ self.raw_url = the_url
+ endpoint = service_endpoint(raw_url)
+ if endpoint
+ self.url = URI.parse(endpoint.gsub(/\{format\}/, "json"))
+ else
+ self.url = false
+ end
+ end
+
+ # Returns the scheme and host portion of the uri, intelligently concatenated
+ #
+ # Examples
+ #
+ # self.new("https://twitter.com/#!/hypomodern").host # => "https://twitter.com"
+ # self.new("http://youtu.be/v/234543").host # => "http://youtube.com"
+ #
+ # @return url [String]
+ def host
+ return false unless url
+ url.scheme + "://" + url.host
+ end
+
+ # Returns the path portion of the uri
+ #
+ # @return path [String]
+ def path
+ return false unless url
+ url.path
+ end
+ end
+end
@@ -0,0 +1,47 @@
+module Oembedr
+ module Providers
+
+ # from ruby-oembed with a couple more I know of from user feedback :).
+ LIST = {
+ /(yotu\.be|youtube)/ => "http://www.youtube.com/oembed",
+ /(flic\.kr|flickr)/ => "http://www.flickr.com/services/oembed/",
+ /viddler/ => "http://lab.viddler.com/services/oembed/",
+ /qik/ => "http://qik.com/api/oembed.{format}",
+ /revision3/ => "http://revision3.com/api/oembed/",
+ /hulu/ => "http://www.hulu.com/api/oembed.{format}",
+ /vimeo/ => "http://www.vimeo.com/api/oembed.{format}",
+ /instagr.am/ => "http://api.instagram.com/oembed",
+ /slideshare/ => "http://www.slideshare.net/api/oembed/2",
+ /(mlg\.tv|tv\.majorleaguegaming)/ => "http://tv.majorleaguegaming.com/oembed",
+ /yfrog/ => "http://www.yfrog.com/api/oembed",
+ /blip\.tv/ => "http://blip.tv/oembed/",
+ /opera\.com/ => 'http://my.opera.com/service/oembed',
+ /skitch\.com/ => 'http://skitch.com/oembed',
+ /twitch\.tv/ => false, # OMG! Support OEmbed!
+ /twitter\.com/ => 'https://api.twitter.com/1/statuses/oembed.{format}',
+ # ...
+ }
+
+ # Locate the correct service endpoint for the given resource URL.
+ #
+ # @param url [String] a fully-qualified URL to an oembeddable resource
+ #
+ # @return the url, or false if no known endpoint.
+ def service_endpoint url
+ endpoint = LIST.find do |(pattern, endpoint)|
+ pattern.match(url)
+ end
+ endpoint ? endpoint.last : false
+ end
+
+ # Return a boolean true or false if we can handle the given domain
+ #
+ # @param url [String] a fully-qualified URL to an oembeddable resource
+ #
+ # @return Boolean
+ def known_service? url
+ !!service_endpoint(url)
+ end
+
+ end
+end
@@ -0,0 +1,3 @@
+module Oembedr
+ VERSION = "0.0.1"
+end
@@ -0,0 +1,34 @@
+# -*- encoding: utf-8 -*-
+$:.push File.expand_path("../lib", __FILE__)
+require "oembedr/version"
+
+Gem::Specification.new do |s|
+ s.name = "oembedr"
+ s.version = Oembedr::VERSION
+ s.authors = ["Matt Wilson"]
+ s.email = ["mhw@hypomodern.com"]
+ s.homepage = ""
+ s.summary = %q{Lightweight, Flexible OEmbed Consumer Library}
+ s.description = %q{oembedr aims to make consuming oembed resources easy}
+
+ # s.rubyforge_project = "oembedr"
+
+ s.files = `git ls-files`.split("\n")
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
+ s.require_paths = ["lib"]
+
+ # specify any dependencies here; for example:
+ s.add_development_dependency "rspec"
+ s.add_development_dependency "yajl-ruby"
+ s.add_development_dependency "typhoeus"
+ s.add_development_dependency "vcr", "~> 2.0.0.rc1"
+ s.add_development_dependency "fakeweb"
+ s.add_development_dependency "guard"
+ s.add_development_dependency "guard-rspec"
+ s.add_development_dependency "guard-bundler"
+
+ s.add_dependency "multi_json"
+ s.add_dependency "faraday"
+ s.add_dependency "faraday_middleware"
+end
Oops, something went wrong.

0 comments on commit 5b4c6ef

Please sign in to comment.