twitter_cards 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 909669852c47f4c7104881b7f7f525ca52850e00
4
+ data.tar.gz: a8b9b40b949bc7425b048f2c6dd6f7f8f153e126
5
+ SHA512:
6
+ metadata.gz: c3f6abb0dc6843b75cbad7467e502e946b1ee556709c0ed419e7c798f27dc1f1e3ff38de704291a2fccb2ab6ee25bfddff201fed20b29c40f7fa8ed60b1be995
7
+ data.tar.gz: aa57da079dbc380207438edcfb8e3cb8fa071b8f2b65e4d6c4e207eb16f76ab3d25dc0dd14ed5636568dd90709b038033ef18ec3f8fff9c54d8cefb9950b11f3
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,13 @@
1
+ language: ruby
2
+ install:
3
+ - gem uninstall bundler
4
+ - gem install bundler
5
+ before_script: bin/setup
6
+ script: rspec
7
+ rvm:
8
+ - 2.2.1
9
+ - 2.1.0
10
+ - 2.0.0
11
+ - 1.9.3
12
+ - rbx-2
13
+ - rbx-2.5.2
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in twitter_cards.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Dimitris Klisiaris
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,96 @@
1
+ # TwitterCards
2
+
3
+ A very simple Ruby library for parsing Twitter Cards information from websites.
4
+ See https://dev.twitter.com/cards/types for more information.
5
+
6
+ [![Dependency Status](https://gemnasium.com/dklisiaris/twitter_cards.svg)](https://gemnasium.com/dklisiaris/twitter_cards)
7
+ [![Build Status](https://travis-ci.org/dklisiaris/twitter_cards.svg?branch=master)](https://travis-ci.org/dklisiaris/twitter_cards)
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'twitter_cards', '~> 0.1'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle install
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install twitter_cards
24
+
25
+
26
+ ## Usage
27
+
28
+ ```ruby
29
+ require 'twitter_cards'
30
+
31
+ article = TwitterCards.fetch('http://www.bbc.com/news/technology-32422193')
32
+
33
+ article.title # => "Google launches Project Fi mobile phone network - BBC News"
34
+
35
+ article.description # => "Google announces Project Fi, a mobile network that will piggyback existing services in the US but offer different terms."
36
+
37
+ article.image # => "http://ichef.bbci.co.uk/news/560/media/images/82498000/jpg/_82498457_82498453.jpg"
38
+
39
+ # Check if cards belongs to a specific type
40
+ article.summary_large_image? # => true
41
+ article.gallery? # => false
42
+
43
+ # Get card's type
44
+ article.type # => "summary_large_image"
45
+
46
+ # Check if attribute is present by using attribute?
47
+ article.domain? # => true
48
+ article.domain # => "www.bbc.com"
49
+
50
+ # Check if all required (as specified by twitter) attributes are present
51
+ article.valid?
52
+ ```
53
+
54
+ If you try to fetch Twitter Cards information for a URL that doesn't
55
+ have any, the _fetch_ method will return _false_.
56
+ The TwitterCards::Object that is returned is just a Hash with accessors
57
+ built into it, so you can examine what properties you've retrieved like so:
58
+
59
+ ```ruby
60
+ article.keys # => ["card", "site", "title", "description", "creator", "image_src", "domain"]
61
+ ```
62
+
63
+ If you have some html already downloaded you can use `parse` method.
64
+ ```ruby
65
+ article = TwitterCards.parse(html)
66
+ ```
67
+
68
+ If you already have parsed html with nokogiri you can use `extract` method.
69
+ ```ruby
70
+ article = TwitterCards.extract(doc)
71
+ ```
72
+
73
+ By default, every attribute is extracted from page. If you want to extract cards only when they have all required attributes you can pass _:strict_ option set to true.
74
+ ```ruby
75
+ article = TwitterCards.fetch('http://www.bbc.com/news/technology-32422193', strict: true)
76
+ ```
77
+
78
+ __Note:__ Both `image` and `image_src` attributes can be accessed with `.image` method.
79
+
80
+ __Note 2:__ Attributes like `app:id:googleplay` are converted to `app_id_googleplay`
81
+
82
+ __Note 3:__ This library supports only one card per page.
83
+
84
+ ## Development
85
+
86
+ After forking/checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
87
+
88
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
89
+
90
+ ## Contributing
91
+
92
+ 1. Fork it ( https://github.com/dklisiaris/twitter_cards/fork )
93
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
94
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
95
+ 4. Push to the branch (`git push origin my-new-feature`)
96
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "twitter_cards"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,106 @@
1
+ require "twitter_cards/version"
2
+ require 'nokogiri'
3
+ require 'restclient'
4
+ require 'hashie'
5
+
6
+ module TwitterCards
7
+
8
+ # Fetch Twitter Cards data from the specified URI with a HTTP GET request.
9
+ #
10
+ # @param uri [String] the uri to fetch and extract cards from.
11
+ # @param opts [Hash] options to customize functionality
12
+ # @option :strict [Boolean] If set true, the object is retured only if the data are valid (i.e. not missing required attributes).
13
+ # @return [Hashie Object, false] a TwitterCards::Object if there is data to be found or false otherwise.
14
+ #
15
+ def self.fetch(uri, opts = {})
16
+ parse(RestClient.get(uri).body, opts)
17
+ rescue RestClient::Exception, SocketError
18
+ false
19
+ end
20
+
21
+ # Parses HTML and extracts Twitter Cards data.
22
+ #
23
+ # @param uri [String] the html text to parse and extract cards from.
24
+ # @param opts [Hash] options to customize functionality
25
+ # @option :strict [Boolean] If set true, the object is retured only if the data are valid (i.e. not missing required attributes)
26
+ # @return [Hashie Object, false] a TwitterCards::Object if there is data to be found or false otherwise.
27
+ #
28
+ def self.parse(html, opts = {})
29
+ doc = Nokogiri::HTML.parse(html)
30
+ extract(doc, opts)
31
+ end
32
+
33
+ # Extracts Twitter Cards data from a parsed document.
34
+ #
35
+ # @param uri [Nokogiri::HTML::Document] the nokogiri parsed document extract cards from.
36
+ # @param opts [Hash] options to customize functionality
37
+ # @option :strict [Boolean] If set true, the object is retured only if the data are valid (i.e. not missing required attributes)
38
+ # @return [Hashie Object, false] a TwitterCards::Object if there is data to be found or false otherwise.
39
+ #
40
+ def self.extract(doc, opts = {})
41
+ opts[:strict] ||= false
42
+
43
+ page = TwitterCards::Object.new
44
+ doc.css('meta').each do |m|
45
+ if m.attribute('name') and m.attribute('name').to_s.match(/^twitter:(.+)$/i)
46
+ property = $1
47
+ elsif m.attribute('property') and m.attribute('property').to_s.match(/^twitter:(.+)$/i)
48
+ property = $1
49
+ end
50
+
51
+ if property
52
+ property.gsub!(/[-:]/,'_')
53
+ page[property] = m.attribute('content').to_s unless page[property]
54
+ end
55
+ end
56
+
57
+ return false if page.keys.empty?
58
+ return false unless page.valid? if opts[:strict]
59
+ page
60
+ end
61
+
62
+
63
+ # The TwitterCards::Object is a Hash with method accessors for all detected Twitter Cards attributes.
64
+ #
65
+ class Object < Hashie::Mash
66
+ MANDATORY_ATTRIBUTES = {
67
+ 'summary' => %w(card site title description),
68
+ 'summary_large_image' => %w(site title description),
69
+ 'photo' => %w(card site image description),
70
+ 'gallery' => %w(card site title image0 image1 image2 image3),
71
+ 'app' => %w(card site app:id:iphone app:id:ipad app:id:googleplay),
72
+ 'player' => %w(card site title player player:width player:height image data1 label1 data2 label2),
73
+ 'product' => %w(card site title description image data1 label1 data2 label2),
74
+ }
75
+
76
+ # The object type, summary if no card property found.
77
+ def type
78
+ self['card'] || 'summary'
79
+ end
80
+
81
+ # Quick way to fetch image
82
+ def image
83
+ self['image'] || self['image_src']
84
+ end
85
+
86
+ # Check if image property exists
87
+ def image?
88
+ image ? true : false
89
+ end
90
+
91
+ MANDATORY_ATTRIBUTES.keys.each do |card_type|
92
+ define_method "#{card_type}?" do
93
+ self.type == card_type
94
+ end
95
+ end
96
+
97
+
98
+ # If the Twitter Cards information for this object doesn't contain
99
+ # the mandatory attributes, this will be false.
100
+ def valid?
101
+ MANDATORY_ATTRIBUTES[type].each{|a| return false unless self[a]}
102
+ true
103
+ end
104
+ end
105
+
106
+ end
@@ -0,0 +1,6 @@
1
+ #!/bin/env ruby
2
+ # encoding: utf-8
3
+
4
+ module TwitterCards
5
+ VERSION = "0.1.0"
6
+ end
@@ -0,0 +1,33 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'twitter_cards/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "twitter_cards"
8
+ spec.version = TwitterCards::VERSION
9
+ spec.authors = ["Dimitris Klisiaris"]
10
+ spec.email = ["dklisiaris@gmail.com"]
11
+
12
+ spec.summary = %q{A Ruby wrapper for Twitter Cards.}
13
+ spec.description = %q{A very simple Ruby library for parsing Twitter Cards information from websites. See https://dev.twitter.com/cards/types for more information.}
14
+ spec.homepage = "https://github.com/dklisiaris/twitter_cards"
15
+ spec.license = "MIT"
16
+
17
+ spec.required_ruby_version = '>= 1.9.3'
18
+
19
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_dependency "rest-client", "~> 1.8"
25
+ spec.add_dependency "nokogiri", "~> 1.6", ">= 1.6.6"
26
+ spec.add_dependency "hashie", "~> 3.4"
27
+
28
+ spec.add_development_dependency "bundler", "~> 1.9"
29
+ spec.add_development_dependency "rake", "~> 10.0"
30
+ spec.add_development_dependency "rspec", "~> 3.2"
31
+ spec.add_development_dependency "webmock", "~> 1.21"
32
+
33
+ end
metadata ADDED
@@ -0,0 +1,162 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: twitter_cards
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Dimitris Klisiaris
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-04-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rest-client
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.8'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.8'
27
+ - !ruby/object:Gem::Dependency
28
+ name: nokogiri
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.6'
34
+ - - ">="
35
+ - !ruby/object:Gem::Version
36
+ version: 1.6.6
37
+ type: :runtime
38
+ prerelease: false
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - "~>"
42
+ - !ruby/object:Gem::Version
43
+ version: '1.6'
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: 1.6.6
47
+ - !ruby/object:Gem::Dependency
48
+ name: hashie
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '3.4'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '3.4'
61
+ - !ruby/object:Gem::Dependency
62
+ name: bundler
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '1.9'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: '1.9'
75
+ - !ruby/object:Gem::Dependency
76
+ name: rake
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: '10.0'
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - "~>"
87
+ - !ruby/object:Gem::Version
88
+ version: '10.0'
89
+ - !ruby/object:Gem::Dependency
90
+ name: rspec
91
+ requirement: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - "~>"
94
+ - !ruby/object:Gem::Version
95
+ version: '3.2'
96
+ type: :development
97
+ prerelease: false
98
+ version_requirements: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - "~>"
101
+ - !ruby/object:Gem::Version
102
+ version: '3.2'
103
+ - !ruby/object:Gem::Dependency
104
+ name: webmock
105
+ requirement: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - "~>"
108
+ - !ruby/object:Gem::Version
109
+ version: '1.21'
110
+ type: :development
111
+ prerelease: false
112
+ version_requirements: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - "~>"
115
+ - !ruby/object:Gem::Version
116
+ version: '1.21'
117
+ description: A very simple Ruby library for parsing Twitter Cards information from
118
+ websites. See https://dev.twitter.com/cards/types for more information.
119
+ email:
120
+ - dklisiaris@gmail.com
121
+ executables: []
122
+ extensions: []
123
+ extra_rdoc_files: []
124
+ files:
125
+ - ".gitignore"
126
+ - ".rspec"
127
+ - ".travis.yml"
128
+ - CODE_OF_CONDUCT.md
129
+ - Gemfile
130
+ - LICENSE.txt
131
+ - README.md
132
+ - Rakefile
133
+ - bin/console
134
+ - bin/setup
135
+ - lib/twitter_cards.rb
136
+ - lib/twitter_cards/version.rb
137
+ - twitter_cards.gemspec
138
+ homepage: https://github.com/dklisiaris/twitter_cards
139
+ licenses:
140
+ - MIT
141
+ metadata: {}
142
+ post_install_message:
143
+ rdoc_options: []
144
+ require_paths:
145
+ - lib
146
+ required_ruby_version: !ruby/object:Gem::Requirement
147
+ requirements:
148
+ - - ">="
149
+ - !ruby/object:Gem::Version
150
+ version: 1.9.3
151
+ required_rubygems_version: !ruby/object:Gem::Requirement
152
+ requirements:
153
+ - - ">="
154
+ - !ruby/object:Gem::Version
155
+ version: '0'
156
+ requirements: []
157
+ rubyforge_project:
158
+ rubygems_version: 2.4.6
159
+ signing_key:
160
+ specification_version: 4
161
+ summary: A Ruby wrapper for Twitter Cards.
162
+ test_files: []