embedify 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -13,7 +13,5 @@ group :development do
13
13
  gem 'rspec'
14
14
  end
15
15
 
16
- gem 'nokogiri'
17
- gem 'httparty'
18
- gem 'opengraph', git: 'git://github.com/parasquid/opengraph.git'
19
- #gem 'opengraph', path: '../opengraph'
16
+ gem 'nokogiri', '~> 1.5.0'
17
+ gem 'faraday'
data/Gemfile.lock CHANGED
@@ -1,36 +1,25 @@
1
- GIT
2
- remote: git://github.com/parasquid/opengraph.git
3
- revision: f750abbdb50d36f32e2b390cfec58a76d23ede49
4
- specs:
5
- opengraph (0.0.4)
6
- hashie
7
- nokogiri (~> 1.4.0)
8
- rest-client (~> 1.6.0)
9
-
10
1
  GEM
11
2
  remote: http://rubygems.org/
12
3
  specs:
4
+ addressable (2.2.7)
13
5
  diff-lcs (1.1.3)
6
+ faraday (0.7.6)
7
+ addressable (~> 2.2)
8
+ multipart-post (~> 1.1)
9
+ rack (~> 1.1)
14
10
  git (1.2.5)
15
- hashie (1.2.0)
16
- httparty (0.8.1)
17
- multi_json
18
- multi_xml
19
11
  jeweler (1.8.3)
20
12
  bundler (~> 1.0)
21
13
  git (>= 1.2.5)
22
14
  rake
23
15
  rdoc
24
16
  json (1.6.5)
25
- mime-types (1.17.2)
26
- multi_json (1.1.0)
27
- multi_xml (0.4.1)
28
- nokogiri (1.4.7)
17
+ multipart-post (1.1.5)
18
+ nokogiri (1.5.0)
19
+ rack (1.4.1)
29
20
  rake (0.9.2.2)
30
21
  rdoc (3.12)
31
22
  json (~> 1.4)
32
- rest-client (1.6.7)
33
- mime-types (>= 1.16)
34
23
  rspec (2.8.0)
35
24
  rspec-core (~> 2.8.0)
36
25
  rspec-expectations (~> 2.8.0)
@@ -46,10 +35,9 @@ PLATFORMS
46
35
 
47
36
  DEPENDENCIES
48
37
  bundler (~> 1.0.0)
49
- httparty
38
+ faraday
50
39
  jeweler (~> 1.8.3)
51
- nokogiri
52
- opengraph!
40
+ nokogiri (~> 1.5.0)
53
41
  rdoc (~> 3.12)
54
42
  rspec
55
43
  shoulda
data/LICENSE.txt CHANGED
@@ -163,3 +163,5 @@ whether future versions of the GNU Lesser General Public License shall
163
163
  apply, that proxy's public statement of acceptance of any version is
164
164
  permanent authorization for you to choose that version for the
165
165
  Library.
166
+
167
+ Original opengraph module Copyright (c) 2009 Intridea, Inc. and Michael Bleigh
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.0
1
+ 0.3.0
data/embedify.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "embedify"
8
- s.version = "0.2.0"
8
+ s.version = "0.3.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Tristan Gomez"]
12
- s.date = "2012-02-20"
12
+ s.date = "2012-03-09"
13
13
  s.description = "Embedify is a ruby gem that parses and returns an\n embeddable version of a page, similar to how Facebook fetches and retrieves\n page data when posting a link."
14
14
  s.email = "tristan.gomez+embedify@gmail.com"
15
15
  s.extra_rdoc_files = [
@@ -26,6 +26,12 @@ Gem::Specification.new do |s|
26
26
  "Rakefile",
27
27
  "VERSION",
28
28
  "embedify.gemspec",
29
+ "examples/embedify_server/Gemfile",
30
+ "examples/embedify_server/Gemfile.lock",
31
+ "examples/embedify_server/Rakefile",
32
+ "examples/embedify_server/config.ru",
33
+ "examples/embedify_server/lib/embedify_server.rb",
34
+ "examples/embedify_server/lib/views/index.erb",
29
35
  "lib/embedify.rb",
30
36
  "test/helper.rb",
31
37
  "test/test_embedify.rb"
@@ -40,18 +46,16 @@ Gem::Specification.new do |s|
40
46
  s.specification_version = 3
41
47
 
42
48
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
43
- s.add_runtime_dependency(%q<nokogiri>, [">= 0"])
44
- s.add_runtime_dependency(%q<httparty>, [">= 0"])
45
- s.add_runtime_dependency(%q<opengraph>, [">= 0"])
49
+ s.add_runtime_dependency(%q<nokogiri>, ["~> 1.5.0"])
50
+ s.add_runtime_dependency(%q<faraday>, [">= 0"])
46
51
  s.add_development_dependency(%q<shoulda>, [">= 0"])
47
52
  s.add_development_dependency(%q<rdoc>, ["~> 3.12"])
48
53
  s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
49
54
  s.add_development_dependency(%q<jeweler>, ["~> 1.8.3"])
50
55
  s.add_development_dependency(%q<rspec>, [">= 0"])
51
56
  else
52
- s.add_dependency(%q<nokogiri>, [">= 0"])
53
- s.add_dependency(%q<httparty>, [">= 0"])
54
- s.add_dependency(%q<opengraph>, [">= 0"])
57
+ s.add_dependency(%q<nokogiri>, ["~> 1.5.0"])
58
+ s.add_dependency(%q<faraday>, [">= 0"])
55
59
  s.add_dependency(%q<shoulda>, [">= 0"])
56
60
  s.add_dependency(%q<rdoc>, ["~> 3.12"])
57
61
  s.add_dependency(%q<bundler>, ["~> 1.0.0"])
@@ -59,9 +63,8 @@ Gem::Specification.new do |s|
59
63
  s.add_dependency(%q<rspec>, [">= 0"])
60
64
  end
61
65
  else
62
- s.add_dependency(%q<nokogiri>, [">= 0"])
63
- s.add_dependency(%q<httparty>, [">= 0"])
64
- s.add_dependency(%q<opengraph>, [">= 0"])
66
+ s.add_dependency(%q<nokogiri>, ["~> 1.5.0"])
67
+ s.add_dependency(%q<faraday>, [">= 0"])
65
68
  s.add_dependency(%q<shoulda>, [">= 0"])
66
69
  s.add_dependency(%q<rdoc>, ["~> 3.12"])
67
70
  s.add_dependency(%q<bundler>, ["~> 1.0.0"])
@@ -0,0 +1,7 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem 'sinatra'
4
+ gem 'embedify', git: 'git://github.com/parasquid/embedify.git'
5
+ gem 'shotgun'
6
+ gem 'thin'
7
+ gem 'sinatra-jsonp', :require => 'sinatra/jsonp'
@@ -0,0 +1,61 @@
1
+ GIT
2
+ remote: git://github.com/parasquid/embedify.git
3
+ revision: 419b8f4d84975bfcb69e7b3804ab8135e8a585e7
4
+ specs:
5
+ embedify (0.2.0)
6
+ httparty
7
+ nokogiri
8
+ opengraph
9
+
10
+ GIT
11
+ remote: git://github.com/parasquid/opengraph.git
12
+ revision: dbcfc27a40be6b4681bfd55c31b767ecf685ff96
13
+ specs:
14
+ opengraph (0.0.4)
15
+ hashie
16
+ httparty
17
+ httparty (~> 0.8.1)
18
+ nokogiri
19
+ nokogiri (~> 1.5.0)
20
+
21
+ GEM
22
+ remote: http://rubygems.org/
23
+ specs:
24
+ daemons (1.1.8)
25
+ eventmachine (0.12.10)
26
+ hashie (1.2.0)
27
+ httparty (0.8.1)
28
+ multi_json
29
+ multi_xml
30
+ json_pure (1.6.5)
31
+ multi_json (1.1.0)
32
+ multi_xml (0.4.1)
33
+ nokogiri (1.5.0)
34
+ rack (1.4.1)
35
+ rack-protection (1.2.0)
36
+ rack
37
+ shotgun (0.9)
38
+ rack (>= 1.0)
39
+ sinatra (1.3.2)
40
+ rack (~> 1.3, >= 1.3.6)
41
+ rack-protection (~> 1.2)
42
+ tilt (~> 1.3, >= 1.3.3)
43
+ sinatra-jsonp (0.3.2)
44
+ json_pure (~> 1.4)
45
+ sinatra (~> 1.0)
46
+ thin (1.3.1)
47
+ daemons (>= 1.0.9)
48
+ eventmachine (>= 0.12.6)
49
+ rack (>= 1.0.0)
50
+ tilt (1.3.3)
51
+
52
+ PLATFORMS
53
+ ruby
54
+
55
+ DEPENDENCIES
56
+ embedify!
57
+ opengraph!
58
+ shotgun
59
+ sinatra
60
+ sinatra-jsonp
61
+ thin
@@ -0,0 +1,45 @@
1
+ #
2
+ # To change this template, choose Tools | Templates
3
+ # and open the template in the editor.
4
+
5
+
6
+ require 'rubygems'
7
+ require 'rake'
8
+ require 'rake/clean'
9
+ require 'rake/gempackagetask'
10
+ require 'rake/rdoctask'
11
+ require 'rake/testtask'
12
+
13
+ spec = Gem::Specification.new do |s|
14
+ s.name = 'embedify_server'
15
+ s.version = '0.0.1'
16
+ s.has_rdoc = true
17
+ s.extra_rdoc_files = ['README', 'LICENSE']
18
+ s.summary = 'Your summary here'
19
+ s.description = s.summary
20
+ s.author = ''
21
+ s.email = ''
22
+ # s.executables = ['your_executable_here']
23
+ s.files = %w(LICENSE README Rakefile) + Dir.glob("{bin,lib,spec}/**/*")
24
+ s.require_path = "lib"
25
+ s.bindir = "bin"
26
+ end
27
+
28
+ Rake::GemPackageTask.new(spec) do |p|
29
+ p.gem_spec = spec
30
+ p.need_tar = true
31
+ p.need_zip = true
32
+ end
33
+
34
+ Rake::RDocTask.new do |rdoc|
35
+ files =['README', 'LICENSE', 'lib/**/*.rb']
36
+ rdoc.rdoc_files.add(files)
37
+ rdoc.main = "README" # page to start on
38
+ rdoc.title = "embedify_server Docs"
39
+ rdoc.rdoc_dir = 'doc/rdoc' # rdoc output folder
40
+ rdoc.options << '--line-numbers'
41
+ end
42
+
43
+ Rake::TestTask.new do |t|
44
+ t.test_files = FileList['test/**/*.rb']
45
+ end
@@ -0,0 +1,4 @@
1
+ $LOAD_PATH << File.expand_path(File.dirname(__FILE__) + "/lib")
2
+
3
+ require 'embedify_server'
4
+ run EmbedifyServer
@@ -0,0 +1,43 @@
1
+ require 'sinatra/base'
2
+ require "sinatra/jsonp"
3
+ require 'json'
4
+ require 'cgi'
5
+
6
+ $LOAD_PATH << File.expand_path('../../lib')
7
+
8
+ require 'embedify'
9
+ require 'sinatra/jsonp'
10
+
11
+ class EmbedifyServer < Sinatra::Base
12
+ helpers Sinatra::Jsonp
13
+ enable :sessions
14
+ set :session_secret, "My session secret"
15
+
16
+ def self.get_or_post(url,&block)
17
+ get(url,&block)
18
+ post(url,&block)
19
+ end
20
+
21
+ get_or_post '/' do
22
+ puts "processing #{params[:url]}"
23
+
24
+ properties = Embedify.fetch(CGI.unescapeHTML(params[:url]))
25
+
26
+ # we don't want to return the nokogiri doc
27
+ properties.delete :nokogiri_parsed_document
28
+
29
+ session[:properties] = properties
30
+
31
+ puts "returning #{properties.to_json}"
32
+
33
+ headers 'Access-Control-Allow-Origin' => '*',
34
+ 'Access-Control-Allow-Methods' => '*',
35
+ 'Access-Control-Allow-Headers' => '*'
36
+ content_type 'application/json'
37
+ jsonp properties.to_json, params[:callback] || :callback
38
+ end
39
+
40
+ # start the server if ruby file executed directly
41
+ run! if app_file == $0
42
+
43
+ end
@@ -0,0 +1,17 @@
1
+ <html>
2
+ <head></head>
3
+ <body>
4
+ <% if @properties %>
5
+ <ul>
6
+ <% @properties.each do |key, value| %>
7
+ <li><%= key %> : <%= value %></li>
8
+ <% end %>
9
+ </ul>
10
+ <% end %>
11
+
12
+ <form action="/" method="post">
13
+ <input type="url" name="url"/>
14
+ <input type="submit" />
15
+ </form>
16
+ </body>
17
+ </html>
data/lib/embedify.rb CHANGED
@@ -1,14 +1,67 @@
1
+ $:.unshift(File.expand_path(File.dirname(__FILE__))) unless $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
2
+
1
3
  require 'nokogiri'
2
- require 'httparty'
3
- require 'opengraph'
4
+ require 'faraday'
5
+ require 'hashie'
4
6
 
5
7
  module Embedify
6
8
 
7
- OPENGRAPH_REGEX = /^og:(.+)$/i
9
+ TYPES = {
10
+ 'activity' => %w(activity sport),
11
+ 'business' => %w(bar company cafe hotel restaurant),
12
+ 'group' => %w(cause sports_league sports_team),
13
+ 'organization' => %w(band government non_profit school university),
14
+ 'person' => %w(actor athlete author director musician politician public_figure),
15
+ 'place' => %w(city country landmark state_province),
16
+ 'product' => %w(album book drink food game movie product song tv_show),
17
+ 'website' => %w(blog website)
18
+ }
19
+
20
+ # The OpenGraph::Object is a Hash with method accessors for
21
+ # all detected Open Graph attributes.
22
+ class Object < Hashie::Mash
23
+ MANDATORY_ATTRIBUTES = %w(title type image url)
24
+
25
+ # The object type.
26
+ def type
27
+ self['type']
28
+ end
29
+
30
+ # The schema under which this particular object lies. May be any of
31
+ # the keys of the TYPES constant.
32
+ def schema
33
+ Embedify::TYPES.each_pair do |schema, types|
34
+ return schema if types.include?(self.type)
35
+ end
36
+ nil
37
+ end
38
+
39
+ Embedify::TYPES.values.flatten.each do |type|
40
+ define_method "#{type}?" do
41
+ self.type == type
42
+ end
43
+ end
44
+
45
+ Embedify::TYPES.keys.each do |scheme|
46
+ define_method "#{scheme}?" do
47
+ self.type == scheme || OpenGraph::TYPES[scheme].include?(self.type)
48
+ end
49
+ end
50
+
51
+ # If the Open Graph information for this object doesn't contain
52
+ # the mandatory attributes, this will be <tt>false</tt>.
53
+ def valid?
54
+ MANDATORY_ATTRIBUTES.each{|a| return false unless (self[a] && !self[a].empty?)}
55
+ true
56
+ end
57
+ end
8
58
 
9
59
  def self.fetch(uri, options = {})
10
- begin
11
- opengraph = OpenGraph.fetch(uri, false)
60
+ opengraph = begin
61
+ html = Faraday.get(uri)
62
+ page = parse(html.body, false)
63
+ page['url'] = html.request.last_uri.to_s unless page.include? 'url'
64
+ page
12
65
  rescue SocketError => e
13
66
  raise e
14
67
  end
@@ -16,27 +69,76 @@ module Embedify
16
69
  # check if the opengraph object is complete
17
70
  unless opengraph.valid?
18
71
  # the opengraph object is lacking some mandatory attributes
19
- attributes = OpenGraph::Object::MANDATORY_ATTRIBUTES
72
+ attributes = Embedify::Object::MANDATORY_ATTRIBUTES
20
73
  attributes.delete 'url'
21
74
  attributes.each do |attribute|
22
75
  self.send(attribute.to_sym, opengraph)
23
76
  end
24
77
  end
78
+
79
+ # fill in extra attributes
80
+ unless opengraph.include? 'description'
81
+ self.description(opengraph)
82
+ end
25
83
  opengraph
26
84
  end
27
85
 
28
86
  private
29
87
 
88
+ def self.parse(html, strict = true)
89
+ doc = Nokogiri::HTML.parse(html)
90
+ page = Embedify::Object.new
91
+ doc.css('meta').each do |m|
92
+ if m.attribute('property') && m.attribute('property').to_s.match(/^og:(.+)$/i)
93
+ page[$1.gsub('-','_')] = m.attribute('content').to_s
94
+ end
95
+ end
96
+ page[:nokogiri_parsed_document] = doc
97
+ page
98
+ end
99
+
30
100
  def self.title(document)
31
- document['title'] = document[:nokogiri_parsed_document].css('title').first.inner_text
101
+ document['title'] ||= document[:nokogiri_parsed_document].css('title').first.inner_text
32
102
  end
33
103
 
34
104
  def self.type(document)
35
- document['type'] = 'website'
105
+ document['type'] ||= 'website'
36
106
  end
37
107
 
38
108
  def self.image(document)
109
+ # TODO: loop through all images and bring back up to 30 good candidates
110
+ # Good candidates: at least 50x50, max aspect ratio of 3:1, png/jpeg/gif format
39
111
  puts "image"
40
112
  end
41
113
 
42
- end
114
+ def self.description(document)
115
+ meta_tags = document[:nokogiri_parsed_document].css('meta')
116
+ meta_tags.each do |meta_tag|
117
+ if meta_tag.attribute('name').to_s.match(/^description$/i)
118
+ document['description'] = meta_tag.attribute('content').to_s
119
+ return
120
+ end
121
+ end
122
+
123
+ # TODO: make the description be the first few words of the first <p></p>
124
+ document['description'] = ''
125
+ end
126
+
127
+ end
128
+
129
+
130
+
131
+
132
+ require 'nokogiri'
133
+ require 'faraday'
134
+
135
+ module OpenGraph
136
+ # Fetch Open Graph data from the specified URI. Makes an
137
+ # HTTP GET request and returns an OpenGraph::Object if there
138
+ # is data to be found or <tt>false</tt> if there isn't.
139
+ #
140
+ # Pass <tt>false</tt> for the second argument if you want to
141
+ # see invalid (i.e. missing a required attribute) data.
142
+
143
+
144
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: embedify
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,33 +9,22 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-02-20 00:00:00.000000000 Z
12
+ date: 2012-03-09 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: nokogiri
16
- requirement: &75319620 !ruby/object:Gem::Requirement
16
+ requirement: &71264200 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
- - - ! '>='
20
- - !ruby/object:Gem::Version
21
- version: '0'
22
- type: :runtime
23
- prerelease: false
24
- version_requirements: *75319620
25
- - !ruby/object:Gem::Dependency
26
- name: httparty
27
- requirement: &75318830 !ruby/object:Gem::Requirement
28
- none: false
29
- requirements:
30
- - - ! '>='
19
+ - - ~>
31
20
  - !ruby/object:Gem::Version
32
- version: '0'
21
+ version: 1.5.0
33
22
  type: :runtime
34
23
  prerelease: false
35
- version_requirements: *75318830
24
+ version_requirements: *71264200
36
25
  - !ruby/object:Gem::Dependency
37
- name: opengraph
38
- requirement: &75318120 !ruby/object:Gem::Requirement
26
+ name: faraday
27
+ requirement: &71262990 !ruby/object:Gem::Requirement
39
28
  none: false
40
29
  requirements:
41
30
  - - ! '>='
@@ -43,10 +32,10 @@ dependencies:
43
32
  version: '0'
44
33
  type: :runtime
45
34
  prerelease: false
46
- version_requirements: *75318120
35
+ version_requirements: *71262990
47
36
  - !ruby/object:Gem::Dependency
48
37
  name: shoulda
49
- requirement: &75317400 !ruby/object:Gem::Requirement
38
+ requirement: &71259700 !ruby/object:Gem::Requirement
50
39
  none: false
51
40
  requirements:
52
41
  - - ! '>='
@@ -54,10 +43,10 @@ dependencies:
54
43
  version: '0'
55
44
  type: :development
56
45
  prerelease: false
57
- version_requirements: *75317400
46
+ version_requirements: *71259700
58
47
  - !ruby/object:Gem::Dependency
59
48
  name: rdoc
60
- requirement: &75316510 !ruby/object:Gem::Requirement
49
+ requirement: &71258770 !ruby/object:Gem::Requirement
61
50
  none: false
62
51
  requirements:
63
52
  - - ~>
@@ -65,10 +54,10 @@ dependencies:
65
54
  version: '3.12'
66
55
  type: :development
67
56
  prerelease: false
68
- version_requirements: *75316510
57
+ version_requirements: *71258770
69
58
  - !ruby/object:Gem::Dependency
70
59
  name: bundler
71
- requirement: &75315750 !ruby/object:Gem::Requirement
60
+ requirement: &71257430 !ruby/object:Gem::Requirement
72
61
  none: false
73
62
  requirements:
74
63
  - - ~>
@@ -76,10 +65,10 @@ dependencies:
76
65
  version: 1.0.0
77
66
  type: :development
78
67
  prerelease: false
79
- version_requirements: *75315750
68
+ version_requirements: *71257430
80
69
  - !ruby/object:Gem::Dependency
81
70
  name: jeweler
82
- requirement: &75315260 !ruby/object:Gem::Requirement
71
+ requirement: &71295870 !ruby/object:Gem::Requirement
83
72
  none: false
84
73
  requirements:
85
74
  - - ~>
@@ -87,10 +76,10 @@ dependencies:
87
76
  version: 1.8.3
88
77
  type: :development
89
78
  prerelease: false
90
- version_requirements: *75315260
79
+ version_requirements: *71295870
91
80
  - !ruby/object:Gem::Dependency
92
81
  name: rspec
93
- requirement: &75314780 !ruby/object:Gem::Requirement
82
+ requirement: &71293630 !ruby/object:Gem::Requirement
94
83
  none: false
95
84
  requirements:
96
85
  - - ! '>='
@@ -98,7 +87,7 @@ dependencies:
98
87
  version: '0'
99
88
  type: :development
100
89
  prerelease: false
101
- version_requirements: *75314780
90
+ version_requirements: *71293630
102
91
  description: ! "Embedify is a ruby gem that parses and returns an\n embeddable
103
92
  version of a page, similar to how Facebook fetches and retrieves\n page data
104
93
  when posting a link."
@@ -118,6 +107,12 @@ files:
118
107
  - Rakefile
119
108
  - VERSION
120
109
  - embedify.gemspec
110
+ - examples/embedify_server/Gemfile
111
+ - examples/embedify_server/Gemfile.lock
112
+ - examples/embedify_server/Rakefile
113
+ - examples/embedify_server/config.ru
114
+ - examples/embedify_server/lib/embedify_server.rb
115
+ - examples/embedify_server/lib/views/index.erb
121
116
  - lib/embedify.rb
122
117
  - test/helper.rb
123
118
  - test/test_embedify.rb
@@ -136,7 +131,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
136
131
  version: '0'
137
132
  segments:
138
133
  - 0
139
- hash: 700225327
134
+ hash: -523818223
140
135
  required_rubygems_version: !ruby/object:Gem::Requirement
141
136
  none: false
142
137
  requirements: