social_rails 2.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/README.md +72 -0
- data/Rakefile +37 -0
- data/app/assets/config/social_rails_manifest.js +2 -0
- data/app/assets/javascripts/app.js +35 -0
- data/app/assets/javascripts/social_rails/social-rails.js +36 -0
- data/app/assets/stylesheets/social_rails/application.css +15 -0
- data/app/controllers/social_rails/social_controller.rb +5 -0
- data/app/views/social_rails/facebook/_description.html.erb +11 -0
- data/app/views/social_rails/facebook/_full_picture.html.erb +15 -0
- data/app/views/social_rails/facebook/latest.html.erb +8 -0
- data/app/views/social_rails/instagram/_caption.html.erb +10 -0
- data/app/views/social_rails/instagram/_picture.html.erb +11 -0
- data/app/views/social_rails/instagram/latest.html.erb +8 -0
- data/app/views/social_rails/shared/_placeholder.html.erb +6 -0
- data/app/views/social_rails/shared/config.html.erb +1 -0
- data/app/views/social_rails/twitter/_tweet.html.erb +7 -0
- data/app/views/social_rails/twitter/latest.html.erb +7 -0
- data/config/routes.rb +6 -0
- data/lib/generators/social_rails/config_generator.rb +15 -0
- data/lib/generators/social_rails/templates/social_rails_config.rb +27 -0
- data/lib/generators/social_rails/views_generator.rb +40 -0
- data/lib/social_rails/apis/base.rb +44 -0
- data/lib/social_rails/apis/facebook.rb +42 -0
- data/lib/social_rails/apis/instagram.rb +27 -0
- data/lib/social_rails/apis/twitter.rb +36 -0
- data/lib/social_rails/apis.rb +3 -0
- data/lib/social_rails/cache.rb +31 -0
- data/lib/social_rails/engine.rb +10 -0
- data/lib/social_rails/helpers/base.rb +33 -0
- data/lib/social_rails/helpers/facebook.rb +32 -0
- data/lib/social_rails/helpers/helper_methods.rb +58 -0
- data/lib/social_rails/helpers/instagram.rb +36 -0
- data/lib/social_rails/helpers/tag.rb +32 -0
- data/lib/social_rails/helpers/twitter.rb +19 -0
- data/lib/social_rails/version.rb +3 -0
- data/lib/social_rails.rb +24 -0
- metadata +144 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 8376c8b58ecc4daf929f6bf8074702a152b52ee7
|
4
|
+
data.tar.gz: 0ff1d9f7965b8668102362a36b180ab008f3f16a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: fd99ba347ca3474e243e626f578030924ee746eae641d0fc669faf9e05e4c053c92f2992bbc694e010b6e81d95122b87357588d24e36811a074ee8b1843da8ca
|
7
|
+
data.tar.gz: c1c9571ac340811077b59a47e8c79dd2d83fb7772f68f8045b308a544cdd97c96eb3db9eed38692d8d65a119d3a49db60c9ada4599ec9a3958c5033f42e37606
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2016 Francis
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
# SocialRails
|
2
|
+
|
3
|
+
Ease the pain of integrating **Facebook**, **Instagram** and **Twitter** posts in a Rails app. Includes a configurable cache to keep your app from tipping over Facebook and Instagram
|
4
|
+
api limits.
|
5
|
+
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
**Gemfile**
|
10
|
+
|
11
|
+
``` ruby
|
12
|
+
gem 'social_rails'
|
13
|
+
```
|
14
|
+
|
15
|
+
**app/assets/javascripts/application.js**
|
16
|
+
|
17
|
+
``` ruby
|
18
|
+
//= require social_rails/social-rails.js
|
19
|
+
```
|
20
|
+
|
21
|
+
**config/routes.rb**
|
22
|
+
|
23
|
+
``` ruby
|
24
|
+
mount SocialRails::Engine => '/social'
|
25
|
+
```
|
26
|
+
|
27
|
+
---------
|
28
|
+
|
29
|
+
## Configuration
|
30
|
+
|
31
|
+
Run `rails g social_rails:config`
|
32
|
+
|
33
|
+
Edit `[Your project]/config/initializers/social_rails.rb`
|
34
|
+
|
35
|
+
Each media options can be configured individually using `SocialRails::[Media].configure` method.
|
36
|
+
|
37
|
+
```
|
38
|
+
countdown #15.minutes by default
|
39
|
+
public: {
|
40
|
+
post_count: # 1 by default
|
41
|
+
max_characters: # 0 by default
|
42
|
+
}
|
43
|
+
```
|
44
|
+
|
45
|
+
|
46
|
+
-------
|
47
|
+
|
48
|
+
## Utilisation
|
49
|
+
|
50
|
+
> Pour utiliser la cache de Rails en developement:
|
51
|
+
>
|
52
|
+
> - cd to your project installation
|
53
|
+
> - run `touch tmp/caching-dev.txt`
|
54
|
+
|
55
|
+
Use the view helper:
|
56
|
+
|
57
|
+
```ruby
|
58
|
+
<%= socialrails(:facebook [, options={}]) %>
|
59
|
+
```
|
60
|
+
|
61
|
+
Options must be an Hash of theses available options:
|
62
|
+
|
63
|
+
``` ruby
|
64
|
+
post_count # 1 by default
|
65
|
+
max_characters # 0 by default
|
66
|
+
```
|
67
|
+
|
68
|
+
Use the following generator command to override views.
|
69
|
+
|
70
|
+
``` ruby
|
71
|
+
rails g social_rails:views
|
72
|
+
```
|
data/Rakefile
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
begin
|
2
|
+
require 'bundler/setup'
|
3
|
+
rescue LoadError
|
4
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
5
|
+
end
|
6
|
+
|
7
|
+
require 'rdoc/task'
|
8
|
+
|
9
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
10
|
+
rdoc.rdoc_dir = 'rdoc'
|
11
|
+
rdoc.title = 'IxSocial'
|
12
|
+
rdoc.options << '--line-numbers'
|
13
|
+
rdoc.rdoc_files.include('README.md')
|
14
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
15
|
+
end
|
16
|
+
|
17
|
+
APP_RAKEFILE = File.expand_path("../test/dummy/Rakefile", __FILE__)
|
18
|
+
load 'rails/tasks/engine.rake'
|
19
|
+
|
20
|
+
|
21
|
+
load 'rails/tasks/statistics.rake'
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
require 'bundler/gem_tasks'
|
26
|
+
|
27
|
+
require 'rake/testtask'
|
28
|
+
|
29
|
+
Rake::TestTask.new(:test) do |t|
|
30
|
+
t.libs << 'lib'
|
31
|
+
t.libs << 'test'
|
32
|
+
t.pattern = 'test/**/*_test.rb'
|
33
|
+
t.verbose = false
|
34
|
+
end
|
35
|
+
|
36
|
+
|
37
|
+
task default: :test
|
@@ -0,0 +1,35 @@
|
|
1
|
+
window.SocialRails = () => {
|
2
|
+
|
3
|
+
const routes = {
|
4
|
+
facebook: '/social/facebook',
|
5
|
+
instagram: '/social/instagram',
|
6
|
+
twitter: '/social/twitter'
|
7
|
+
}
|
8
|
+
|
9
|
+
|
10
|
+
const placeholders = document.getElementsByClassName('js-social')
|
11
|
+
|
12
|
+
for( let i = 0, len = placeholders.length; i < len; i++ ) {
|
13
|
+
|
14
|
+
let namespace = placeholders[i].dataset.namespace
|
15
|
+
let node = document.getElementById('social-' + namespace)
|
16
|
+
let options = node.dataset.options
|
17
|
+
let xhr = new XMLHttpRequest();
|
18
|
+
|
19
|
+
// xhr.open('GET', `${routes[namespace]}?options=${options}`);
|
20
|
+
xhr.open('GET', routes[namespace])
|
21
|
+
|
22
|
+
xhr.onload = () => {
|
23
|
+
if (xhr.status === 200)
|
24
|
+
node.outerHTML = xhr.responseText
|
25
|
+
else
|
26
|
+
console.log(xhr.status)
|
27
|
+
}
|
28
|
+
|
29
|
+
xhr.send()
|
30
|
+
}
|
31
|
+
|
32
|
+
}
|
33
|
+
|
34
|
+
/* Auto init */
|
35
|
+
window.SocialRails();
|
@@ -0,0 +1,36 @@
|
|
1
|
+
'use strict';
|
2
|
+
|
3
|
+
window.SocialRails = function () {
|
4
|
+
|
5
|
+
var routes = {
|
6
|
+
facebook: '/social/facebook',
|
7
|
+
instagram: '/social/instagram',
|
8
|
+
twitter: '/social/twitter'
|
9
|
+
};
|
10
|
+
|
11
|
+
var placeholders = document.getElementsByClassName('js-social');
|
12
|
+
|
13
|
+
var _loop = function _loop(i, len) {
|
14
|
+
|
15
|
+
var namespace = placeholders[i].dataset.namespace;
|
16
|
+
var node = document.getElementById('social-' + namespace);
|
17
|
+
var options = node.dataset.options;
|
18
|
+
var xhr = new XMLHttpRequest();
|
19
|
+
|
20
|
+
// xhr.open('GET', `${routes[namespace]}?options=${options}`);
|
21
|
+
xhr.open('GET', routes[namespace]);
|
22
|
+
|
23
|
+
xhr.onload = function () {
|
24
|
+
if (xhr.status === 200) node.outerHTML = xhr.responseText;else console.log(xhr.status);
|
25
|
+
};
|
26
|
+
|
27
|
+
xhr.send();
|
28
|
+
};
|
29
|
+
|
30
|
+
for (var i = 0, len = placeholders.length; i < len; i++) {
|
31
|
+
_loop(i, len);
|
32
|
+
}
|
33
|
+
};
|
34
|
+
|
35
|
+
/* Auto init */
|
36
|
+
window.SocialRails();
|
@@ -0,0 +1,15 @@
|
|
1
|
+
/*
|
2
|
+
* This is a manifest file that'll be compiled into application.css, which will include all the files
|
3
|
+
* listed below.
|
4
|
+
*
|
5
|
+
* Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
|
6
|
+
* or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
|
7
|
+
*
|
8
|
+
* You're free to add application-wide styles to this file and they'll appear at the bottom of the
|
9
|
+
* compiled file so the styles you add here take precedence over styles defined in any other CSS/SCSS
|
10
|
+
* files in this directory. Styles in this file should be added after the last require_* statement.
|
11
|
+
* It is generally better to create a new file per style scope.
|
12
|
+
*
|
13
|
+
*= require_tree .
|
14
|
+
*= require_self
|
15
|
+
*/
|
@@ -0,0 +1,11 @@
|
|
1
|
+
<%
|
2
|
+
##
|
3
|
+
# Available locals
|
4
|
+
#
|
5
|
+
# {String} link - The link(url) attached to this post
|
6
|
+
# {String} message - The status message in the post.
|
7
|
+
%>
|
8
|
+
|
9
|
+
<a href="<%= link %>" target="_blank" class="socialrails__text socialrails__text--facebook">
|
10
|
+
<%= message %>
|
11
|
+
</a>
|
@@ -0,0 +1,15 @@
|
|
1
|
+
<%
|
2
|
+
##
|
3
|
+
# Available locals
|
4
|
+
#
|
5
|
+
# {Bool} has_full_picture - Some posts don't have a picture attached.
|
6
|
+
# {String} full_picture - The picture(url) scraped from any link included with the post.
|
7
|
+
# {String} link - The link(url) attached to this post
|
8
|
+
# {String} message - The status message in the post.
|
9
|
+
%>
|
10
|
+
|
11
|
+
<% if has_full_picture %>
|
12
|
+
<a href="<%= link %>" target="_blank">
|
13
|
+
<img src="<%= full_picture %>" class="socialrails__picture socialrails__picture--facebook" />
|
14
|
+
</a>
|
15
|
+
<% end %>
|
@@ -0,0 +1,11 @@
|
|
1
|
+
<%
|
2
|
+
##
|
3
|
+
# Available locals
|
4
|
+
#
|
5
|
+
# {String} link - The link(url) to the instagram post
|
6
|
+
# {String} picture - The posted image(url)
|
7
|
+
%>
|
8
|
+
|
9
|
+
<a href="<%= link %>" target="_blank">
|
10
|
+
<img src="<%= picture %>" class="socialrails__picture socialrails__picture--instagram" />
|
11
|
+
</a>
|
@@ -0,0 +1 @@
|
|
1
|
+
Erreur de configuration
|
data/config/routes.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
module SocialRails
|
2
|
+
# rails g social_rails:config
|
3
|
+
class ConfigGenerator < Rails::Generators::Base
|
4
|
+
source_root File.expand_path(File.join(File.dirname(__FILE__), 'templates'))
|
5
|
+
|
6
|
+
desc <<DESC
|
7
|
+
Description:
|
8
|
+
Copies social_rails configuration file to your application's initializer directory.
|
9
|
+
DESC
|
10
|
+
|
11
|
+
def copy_config_file
|
12
|
+
template 'social_rails_config.rb', 'config/initializers/social_rails.rb'
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
SocialRails::API::Facebook.configure do |config|
|
2
|
+
|
3
|
+
# Required
|
4
|
+
|
5
|
+
#config.app_id = ""
|
6
|
+
#config.app_secret = ""
|
7
|
+
#config.page_name = ""
|
8
|
+
end
|
9
|
+
|
10
|
+
SocialRails::API::Instagram.configure do |config|
|
11
|
+
|
12
|
+
# Required
|
13
|
+
|
14
|
+
#config.user_id = ""
|
15
|
+
#config.access_token = ""
|
16
|
+
end
|
17
|
+
|
18
|
+
SocialRails::API::Twitter.configure do |config|
|
19
|
+
|
20
|
+
# Required
|
21
|
+
|
22
|
+
#config.consumer_key = ""
|
23
|
+
#config.consumer_secret = ""
|
24
|
+
#config.access_token = ""
|
25
|
+
#config.access_token_secret = ""
|
26
|
+
#config.twitter_name = ""
|
27
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
module SocialRails
|
2
|
+
# rails g social_rails:views
|
3
|
+
class ViewsGenerator < Rails::Generators::Base
|
4
|
+
|
5
|
+
source_root File.expand_path('../../../../app/views/social_rails', __FILE__)
|
6
|
+
|
7
|
+
class_option :medias, type: :array
|
8
|
+
|
9
|
+
def copy_views
|
10
|
+
medias = options[:medias]
|
11
|
+
|
12
|
+
if medias.nil?
|
13
|
+
copy_all_views
|
14
|
+
else
|
15
|
+
FileUtils.mkdir app_views_path unless File.exists? app_views_path
|
16
|
+
medias.each { |m| copy_media(m) }
|
17
|
+
copy_media('shared')
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
def copy_all_views
|
24
|
+
FileUtils.copy_entry self.class.source_root, app_views_path
|
25
|
+
end
|
26
|
+
|
27
|
+
def copy_media media
|
28
|
+
FileUtils.copy_entry get_media_path(media), app_views_path(media)
|
29
|
+
end
|
30
|
+
|
31
|
+
def app_views_path media = ""
|
32
|
+
['app', 'views', 'social_rails', media].compact.join('/')
|
33
|
+
end
|
34
|
+
|
35
|
+
def get_media_path media
|
36
|
+
[self.class.source_root, media].compact.join('/')
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
module SocialRails
|
2
|
+
module API
|
3
|
+
|
4
|
+
class Base
|
5
|
+
|
6
|
+
include Cache
|
7
|
+
|
8
|
+
class << self
|
9
|
+
attr_accessor :uid
|
10
|
+
attr_accessor :required
|
11
|
+
end
|
12
|
+
|
13
|
+
# Allow for configure block
|
14
|
+
def self.configure
|
15
|
+
yield config
|
16
|
+
end
|
17
|
+
|
18
|
+
# Return module configurations
|
19
|
+
def self.config
|
20
|
+
@config ||= OpenStruct.new(SocialRails::DEFAULT_CONFIG)
|
21
|
+
end
|
22
|
+
|
23
|
+
# Standardize namespace
|
24
|
+
def self.namespace
|
25
|
+
"social_rails/#{self.uid}"
|
26
|
+
end
|
27
|
+
|
28
|
+
# Make sure that all required configs are set
|
29
|
+
def self.configured?
|
30
|
+
self.required.all? { |required_config| !self.config[required_config].nil? }
|
31
|
+
end
|
32
|
+
|
33
|
+
def self.set_public_options options
|
34
|
+
# Expire cache to allow api querying
|
35
|
+
if options.has_key?(:post_count) && options[:post_count] > self.config.public[:post_count]
|
36
|
+
self.expire
|
37
|
+
end
|
38
|
+
|
39
|
+
self.config.public = self.config.public.merge(options)
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'koala'
|
2
|
+
|
3
|
+
module SocialRails
|
4
|
+
|
5
|
+
module API
|
6
|
+
|
7
|
+
# Facebook module for SocialRails
|
8
|
+
class Facebook < Base
|
9
|
+
|
10
|
+
# Module unique identifier
|
11
|
+
self.uid = 'facebook'
|
12
|
+
|
13
|
+
# Facebook Graph Api requirements
|
14
|
+
# Required:
|
15
|
+
# * app_id
|
16
|
+
# * app_secret
|
17
|
+
# * page_name
|
18
|
+
self.required = %w(app_id app_secret page_name)
|
19
|
+
|
20
|
+
# @private
|
21
|
+
# Use Koala to fetch Facebook feed
|
22
|
+
def self.get
|
23
|
+
oAuth = Koala::Facebook::OAuth.new(self.config.app_id, self.config.app_secret)
|
24
|
+
client = Koala::Facebook::API.new(oAuth.get_app_access_token)
|
25
|
+
|
26
|
+
feed = client.get_connection(self.config.page_name, 'posts', {fields: ['link', 'message', 'full_picture'] })
|
27
|
+
self.find_posts(feed)
|
28
|
+
end
|
29
|
+
|
30
|
+
# Loop through Facebook feed to find posts with message
|
31
|
+
def self.find_posts feed
|
32
|
+
items = []
|
33
|
+
feed.each do |item|
|
34
|
+
items.push(item) if item.has_key?("message")
|
35
|
+
return items if items.count == self.config.public[:post_count]
|
36
|
+
end
|
37
|
+
feed.next_page
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'httparty'
|
2
|
+
|
3
|
+
module SocialRails
|
4
|
+
|
5
|
+
module API
|
6
|
+
# Instagram module for SocialRails
|
7
|
+
class Instagram < Base
|
8
|
+
|
9
|
+
# Module unique identifier
|
10
|
+
self.uid = 'instagram'
|
11
|
+
|
12
|
+
# Instagram api requirements.
|
13
|
+
# Required:
|
14
|
+
# * user_id
|
15
|
+
# * access_token
|
16
|
+
self.required = %w(user_id access_token)
|
17
|
+
|
18
|
+
# @private
|
19
|
+
# Query instagram api with HTTParty
|
20
|
+
def self.get
|
21
|
+
instagram_call = HTTParty.get("https://api.instagram.com/v1/users/#{self.config.user_id}/media/recent?count=#{self.config.public[:post_count]}&access_token=#{self.config.access_token}")
|
22
|
+
JSON.parse(instagram_call.body)['data']
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'twitter'
|
2
|
+
|
3
|
+
module SocialRails
|
4
|
+
|
5
|
+
module API
|
6
|
+
# Twitter module for SocialRails
|
7
|
+
class Twitter < Base
|
8
|
+
|
9
|
+
# Module unique identifier
|
10
|
+
self.uid = 'twitter'
|
11
|
+
|
12
|
+
# Twitter api requirements.
|
13
|
+
# Required:
|
14
|
+
# * consumer_key
|
15
|
+
# * consumer_secret
|
16
|
+
# * access_token
|
17
|
+
# * access_token_secret
|
18
|
+
self.required = %w(consumer_key consumer_secret access_token access_token_secret)
|
19
|
+
|
20
|
+
# @private
|
21
|
+
# Query twitter api with Twitter gem
|
22
|
+
def self.get
|
23
|
+
client = ::Twitter::REST::Client.new({
|
24
|
+
consumer_key: self.config.consumer_key,
|
25
|
+
consumer_secret: self.config.consumer_secret,
|
26
|
+
access_token: self.config.access_token,
|
27
|
+
access_token_secret: self.config.access_token_secret
|
28
|
+
})
|
29
|
+
|
30
|
+
latest_tweets = client.user_timeline(self.config.twitter_name, { exclude_replies: true, include_rts: false }).first(self.config.public[:post_count])
|
31
|
+
tweets = latest_tweets.map{ |t| client.oembed(t.id, {lang: 'fr'}) }
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module SocialRails
|
2
|
+
module Cache
|
3
|
+
|
4
|
+
def self.included base
|
5
|
+
base.extend CacheMethods
|
6
|
+
end
|
7
|
+
|
8
|
+
# Methods used to manage cache
|
9
|
+
module CacheMethods
|
10
|
+
|
11
|
+
# Manage cache state
|
12
|
+
#
|
13
|
+
# Write to namespace if namespace is nonexistent.
|
14
|
+
# Return cache namespace content.
|
15
|
+
# Returns nil if media isn't configured
|
16
|
+
def latest
|
17
|
+
if self.configured?
|
18
|
+
Rails
|
19
|
+
.cache
|
20
|
+
.fetch(self.namespace, {expires_in: self.config.cooldown}) { self.get }
|
21
|
+
.take(self.config.public[:post_count])
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
# Expire namespace from cache
|
26
|
+
def expire
|
27
|
+
Rails.cache.delete(self.namespace)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
module SocialRails
|
2
|
+
class Engine < ::Rails::Engine
|
3
|
+
isolate_namespace SocialRails
|
4
|
+
|
5
|
+
require 'social_rails/helpers/helper_methods'
|
6
|
+
initializer "social_rails.view_helpers" do
|
7
|
+
ActionView::Base.send :include, SocialRails::Helpers::HelperMethods
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module SocialRails
|
2
|
+
module Helpers
|
3
|
+
|
4
|
+
class Base < Tag
|
5
|
+
def initialize(template, content, scope, config)
|
6
|
+
|
7
|
+
@template, @content, @scope, @config = template, content, scope, config
|
8
|
+
# @tags ||= []
|
9
|
+
|
10
|
+
@tags.each do |tag|
|
11
|
+
eval <<-DEF, nil, __FILE__, __LINE__ + 1
|
12
|
+
def #{tag}_tag
|
13
|
+
#{tag.classify}.new @template, @content, @scope, @config
|
14
|
+
end
|
15
|
+
DEF
|
16
|
+
end
|
17
|
+
|
18
|
+
@output_buffer = if defined?(::ActionView::OutputBuffer)
|
19
|
+
::ActionView::OutputBuffer.new
|
20
|
+
else
|
21
|
+
ActiveSupport::SafeBuffer.new
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def render(&block)
|
26
|
+
instance_eval(&block)
|
27
|
+
@output_buffer
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
module SocialRails
|
2
|
+
module Helpers
|
3
|
+
|
4
|
+
class Facebook < Base
|
5
|
+
def initialize(template, content, scope, config)
|
6
|
+
@tags = %w(full_picture description)
|
7
|
+
super
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
module FacebookPostPart
|
12
|
+
def to_s(locals = {})
|
13
|
+
locals[:link] = @content["link"]
|
14
|
+
locals[:message] = truncate @content["message"]
|
15
|
+
super(locals)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
class FullPicture < Tag
|
20
|
+
include FacebookPostPart
|
21
|
+
def to_s(locals = {})
|
22
|
+
locals[:has_full_picture] = @content["full_picture"] && !@content["full_picture"].empty?
|
23
|
+
locals[:full_picture] = @content["full_picture"]
|
24
|
+
super(locals)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
class Description < Tag
|
29
|
+
include FacebookPostPart
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
require 'social_rails/apis'
|
2
|
+
|
3
|
+
module SocialRails
|
4
|
+
module Helpers
|
5
|
+
|
6
|
+
module HelperMethods
|
7
|
+
|
8
|
+
##
|
9
|
+
# What starts it all.
|
10
|
+
# Render a placeholder with a namespace.
|
11
|
+
# To use in an view.
|
12
|
+
# Ex.: <%= socialrails(:facebook [, {options}]) %>
|
13
|
+
#
|
14
|
+
# @param {Symbol} Must be one of these: `SocialRails::APIs`
|
15
|
+
# @param {Hash} (Optional)
|
16
|
+
#
|
17
|
+
def socialrails api, options = {}
|
18
|
+
if valid_media(api)
|
19
|
+
api = "SocialRails::API::#{api.to_s.capitalize}".constantize
|
20
|
+
api.set_public_options(options)
|
21
|
+
render partial: 'social_rails/shared/placeholder', locals: {namespace: api.uid}
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
##
|
26
|
+
# Build [api]_posts methods
|
27
|
+
# To use in a async rendered view
|
28
|
+
# Ex.: `facebook_posts.each do |post|`
|
29
|
+
#
|
30
|
+
SocialRails::APIs.each do |api|
|
31
|
+
eval <<-DEF, nil, __FILE__, __LINE__ + 1
|
32
|
+
def #{api}_posts
|
33
|
+
posts, the_content = [], "SocialRails::API::#{api.to_s.capitalize}".constantize.latest
|
34
|
+
|
35
|
+
if !the_content.nil?
|
36
|
+
config = "SocialRails::API::#{api.to_s.capitalize}".constantize.config
|
37
|
+
for i in 0..config.public[:post_count] - 1
|
38
|
+
posts.push("SocialRails::Helpers::#{api.to_s.capitalize}".constantize.new(
|
39
|
+
self,
|
40
|
+
the_content[i],
|
41
|
+
SocialRails::API::#{api.to_s.capitalize}.uid,
|
42
|
+
config
|
43
|
+
))
|
44
|
+
end
|
45
|
+
end
|
46
|
+
posts
|
47
|
+
end
|
48
|
+
DEF
|
49
|
+
end
|
50
|
+
|
51
|
+
protected
|
52
|
+
|
53
|
+
def valid_media(api)
|
54
|
+
SocialRails::APIs.include?(api.to_s)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module SocialRails
|
2
|
+
module Helpers
|
3
|
+
|
4
|
+
class Instagram < Base
|
5
|
+
def initialize(template, content, scope, config)
|
6
|
+
@tags = %w(picture caption)
|
7
|
+
super
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
module InstagramPostPart
|
12
|
+
def to_s(locals = {})
|
13
|
+
locals[:link] = @content["link"]
|
14
|
+
super(locals)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
class Caption < Tag
|
19
|
+
include InstagramPostPart
|
20
|
+
|
21
|
+
def to_s(locals = {})
|
22
|
+
locals[:caption] = truncate @content["caption"]["text"]
|
23
|
+
super(locals)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
class Picture < Tag
|
28
|
+
include InstagramPostPart
|
29
|
+
|
30
|
+
def to_s(locals = {})
|
31
|
+
locals[:picture] = @content["images"]["standard_resolution"]["url"]
|
32
|
+
super(locals)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
module SocialRails
|
2
|
+
module Helpers
|
3
|
+
class Tag
|
4
|
+
def initialize(template, content, scope, config)
|
5
|
+
@template, @content, @scope, @config, @options = template, content, scope, config
|
6
|
+
end
|
7
|
+
|
8
|
+
def to_s(locals = {})
|
9
|
+
@template.render partial: partial_path, locals: locals
|
10
|
+
end
|
11
|
+
|
12
|
+
def partial_path
|
13
|
+
[
|
14
|
+
"social_rails",
|
15
|
+
@scope,
|
16
|
+
self.class.name.demodulize.underscore
|
17
|
+
].compact.join("/")
|
18
|
+
end
|
19
|
+
|
20
|
+
def truncate(the_content)
|
21
|
+
content, max = the_content, @config.public[:max_characters]
|
22
|
+
if content.length > max && max > 0
|
23
|
+
content = content[0..(max - 1)]
|
24
|
+
content << "..."
|
25
|
+
end
|
26
|
+
content
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module SocialRails
|
2
|
+
module Helpers
|
3
|
+
|
4
|
+
class Twitter < Base
|
5
|
+
def initialize(template, content, scope, config)
|
6
|
+
@tags = %w(tweet)
|
7
|
+
super
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
class Tweet < Tag
|
12
|
+
def to_s(locals = {})
|
13
|
+
locals[:tweet] = @content.html
|
14
|
+
super(locals)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
end
|
data/lib/social_rails.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'social_rails/engine'
|
2
|
+
|
3
|
+
module SocialRails
|
4
|
+
|
5
|
+
DEFAULT_CONFIG = {
|
6
|
+
cooldown: 15.minutes,
|
7
|
+
public: {
|
8
|
+
max_characters: 0,
|
9
|
+
post_count: 1
|
10
|
+
}
|
11
|
+
}
|
12
|
+
|
13
|
+
end
|
14
|
+
|
15
|
+
require 'social_rails/cache'
|
16
|
+
require 'social_rails/apis/base'
|
17
|
+
require 'social_rails/apis/facebook'
|
18
|
+
require 'social_rails/apis/instagram'
|
19
|
+
require 'social_rails/apis/twitter'
|
20
|
+
require 'social_rails/helpers/tag'
|
21
|
+
require 'social_rails/helpers/base'
|
22
|
+
require 'social_rails/helpers/facebook'
|
23
|
+
require 'social_rails/helpers/instagram'
|
24
|
+
require 'social_rails/helpers/twitter'
|
metadata
ADDED
@@ -0,0 +1,144 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: social_rails
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 2.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Francis G
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-11-17 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rails
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '4.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '4.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: koala
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '2.4'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '2.4'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: httparty
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0.13'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0.13'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: twitter
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 6.0.0
|
62
|
+
- - "~>"
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: '6.0'
|
65
|
+
type: :runtime
|
66
|
+
prerelease: false
|
67
|
+
version_requirements: !ruby/object:Gem::Requirement
|
68
|
+
requirements:
|
69
|
+
- - ">="
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: 6.0.0
|
72
|
+
- - "~>"
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '6.0'
|
75
|
+
description: Includes a configurable cache to keep your app from tipping over api
|
76
|
+
limits.
|
77
|
+
email:
|
78
|
+
- francis@ixmedia.com
|
79
|
+
executables: []
|
80
|
+
extensions: []
|
81
|
+
extra_rdoc_files: []
|
82
|
+
files:
|
83
|
+
- MIT-LICENSE
|
84
|
+
- README.md
|
85
|
+
- Rakefile
|
86
|
+
- app/assets/config/social_rails_manifest.js
|
87
|
+
- app/assets/javascripts/app.js
|
88
|
+
- app/assets/javascripts/social_rails/social-rails.js
|
89
|
+
- app/assets/stylesheets/social_rails/application.css
|
90
|
+
- app/controllers/social_rails/social_controller.rb
|
91
|
+
- app/views/social_rails/facebook/_description.html.erb
|
92
|
+
- app/views/social_rails/facebook/_full_picture.html.erb
|
93
|
+
- app/views/social_rails/facebook/latest.html.erb
|
94
|
+
- app/views/social_rails/instagram/_caption.html.erb
|
95
|
+
- app/views/social_rails/instagram/_picture.html.erb
|
96
|
+
- app/views/social_rails/instagram/latest.html.erb
|
97
|
+
- app/views/social_rails/shared/_placeholder.html.erb
|
98
|
+
- app/views/social_rails/shared/config.html.erb
|
99
|
+
- app/views/social_rails/twitter/_tweet.html.erb
|
100
|
+
- app/views/social_rails/twitter/latest.html.erb
|
101
|
+
- config/routes.rb
|
102
|
+
- lib/generators/social_rails/config_generator.rb
|
103
|
+
- lib/generators/social_rails/templates/social_rails_config.rb
|
104
|
+
- lib/generators/social_rails/views_generator.rb
|
105
|
+
- lib/social_rails.rb
|
106
|
+
- lib/social_rails/apis.rb
|
107
|
+
- lib/social_rails/apis/base.rb
|
108
|
+
- lib/social_rails/apis/facebook.rb
|
109
|
+
- lib/social_rails/apis/instagram.rb
|
110
|
+
- lib/social_rails/apis/twitter.rb
|
111
|
+
- lib/social_rails/cache.rb
|
112
|
+
- lib/social_rails/engine.rb
|
113
|
+
- lib/social_rails/helpers/base.rb
|
114
|
+
- lib/social_rails/helpers/facebook.rb
|
115
|
+
- lib/social_rails/helpers/helper_methods.rb
|
116
|
+
- lib/social_rails/helpers/instagram.rb
|
117
|
+
- lib/social_rails/helpers/tag.rb
|
118
|
+
- lib/social_rails/helpers/twitter.rb
|
119
|
+
- lib/social_rails/version.rb
|
120
|
+
homepage: https://github.com/ixmedia/social_rails
|
121
|
+
licenses:
|
122
|
+
- MIT
|
123
|
+
metadata: {}
|
124
|
+
post_install_message:
|
125
|
+
rdoc_options: []
|
126
|
+
require_paths:
|
127
|
+
- lib
|
128
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
129
|
+
requirements:
|
130
|
+
- - ">="
|
131
|
+
- !ruby/object:Gem::Version
|
132
|
+
version: '0'
|
133
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
134
|
+
requirements:
|
135
|
+
- - ">="
|
136
|
+
- !ruby/object:Gem::Version
|
137
|
+
version: '0'
|
138
|
+
requirements: []
|
139
|
+
rubyforge_project:
|
140
|
+
rubygems_version: 2.6.13
|
141
|
+
signing_key:
|
142
|
+
specification_version: 4
|
143
|
+
summary: Ease the pain of integrating social media posts in a Rails app.
|
144
|
+
test_files: []
|