facebook_dialog 0.0.3 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ rvm:
2
+ - 1.9.2
3
+ - 1.9.3
4
+
data/Gemfile CHANGED
@@ -2,3 +2,12 @@ source 'http://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in facebook_dialog.gemspec
4
4
  gemspec
5
+
6
+ platforms :ruby do
7
+ gem 'rb-readline'
8
+ end
9
+
10
+ group :development do
11
+ gem 'growl'
12
+ end
13
+
data/Guardfile ADDED
@@ -0,0 +1,6 @@
1
+ guard 'rspec', cli: "--colour" do
2
+ watch(%r{^spec/.+_spec\.rb$})
3
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
4
+ watch('spec/spec_helper.rb') { "spec" }
5
+ end
6
+
data/Rakefile CHANGED
@@ -8,6 +8,8 @@ begin
8
8
  RSpec::Core::RakeTask.new do |t|
9
9
  end
10
10
 
11
+ task default: :spec
12
+
11
13
  rescue LoadError
12
14
  puts "RSpec is not installed"
13
15
  end
data/Readme.md CHANGED
@@ -2,24 +2,32 @@
2
2
 
3
3
  A utility that makes it easy to integrate progressively enhanced facebook dialogs
4
4
 
5
- ```@ruby
5
+ [![Build Status](https://secure.travis-ci.org/EnlightSolutions/facebook_dialog.png)](http://travis-ci.org/enlightsolutions/facebook_dialog)
6
+
7
+ This is available for Rails >= 3.0 and Ruby >= 1.9.2
8
+
9
+ ```ruby
6
10
  FacebookDialog.api_key = "<your app_id/api key>"
7
11
 
8
12
  #in a Rails View
9
- <%= link_to "Share on Facebook" FacebookDialog::Feed.new({
13
+
14
+ <%- facebook_dialog = FacebookDialog::Feed.new({
10
15
  redirect_uri: "http://www.example.com",
11
16
  link: "http://www.example.com",
12
17
  name: "Want to know?",
13
18
  caption: "Something pretty awesome",
14
19
  description: "Zomg! You won't believe what I found on the web."
15
- }),
16
- id: "share_on_facebook" %>
20
+ }) -%>
21
+
22
+ <%= link_to "Share on Facebook", facebook_dialog.url, id: "share_on_facebook" %>
23
+
24
+ #at the bottom of your page
17
25
 
18
26
  <%= facebook_js %>
19
27
  <%= javascript_tag do %>
20
28
  <%= fb_init_js %>
21
29
  $("#share_on_facebook").click(function(e){
22
- FB.ui(<%== Facebook::TopicShareUrl.new(resource).to_json %>);
30
+ FB.ui(<%== facebook_dialog.to_json %>);
23
31
  });
24
32
  <%- end -%>
25
33
  ```
@@ -17,9 +17,12 @@ Gem::Specification.new do |gem|
17
17
 
18
18
  gem.add_dependency "rack"
19
19
  gem.add_dependency "activemodel"
20
+ gem.add_dependency "activesupport"
20
21
  gem.add_dependency "configatron"
21
22
  gem.add_dependency "rake"
22
23
 
23
24
  gem.add_development_dependency "rspec"
25
+ gem.add_development_dependency "guard-rspec"
26
+ gem.add_development_dependency "mocha"
24
27
  end
25
28
 
@@ -0,0 +1,54 @@
1
+ module FacebookDialog
2
+ class Dialog
3
+ include ActiveModel::Serializers::JSON
4
+ self.include_root_in_json = false
5
+
6
+ class_attribute :defaults
7
+ class_attribute :resource_name
8
+ self.resource_name = nil
9
+ self.defaults = {}
10
+
11
+ def initialize(options = {})
12
+ @options = options
13
+ @options[:app_id] = FacebookDialog.api_key
14
+ end
15
+
16
+ def self.base_uri
17
+ raise ResourceNameNotDefined if resource_name.nil?
18
+ "http://www.facebook.com/dialog/#{resource_name}/"
19
+ end
20
+
21
+ def url
22
+ "#{self.class.base_uri}?#{Rack::Utils.build_query(query)}"
23
+ end
24
+
25
+ def query
26
+ to_h.merge({
27
+ redirect_uri: @options[:redirect_uri] || @options[:link],
28
+ display: @options[:display] || "popup"
29
+ })
30
+ end
31
+
32
+ def serializable_hash(options = {})
33
+ to_h.merge({method: self.class.resource_name})
34
+ end
35
+
36
+ HTML_ONLY_KEYS = [:redirect_uri, :display]
37
+ def to_h
38
+ validate_options!
39
+ options = @options.merge(self.class.defaults)
40
+ options = options.reject{|key, value| HTML_ONLY_KEYS.include?(key) }
41
+ serialize_options(options)
42
+ end
43
+
44
+ protected
45
+ def validate_options!
46
+ FacebookDialog::Validators::Display.new(@options).validate
47
+ end
48
+
49
+ def serialize_options(options)
50
+ FacebookDialog::OptionSerialization.new(options).serialized_hash
51
+ end
52
+ end
53
+ end
54
+
@@ -1,10 +1,8 @@
1
1
  module FacebookDialog
2
- class Feed
3
- FACEBOOK_BASE_URL = "http://www.facebook.com/dialog/feed"
4
- include ActiveModel::Serializers::JSON
5
- self.include_root_in_json = false
2
+ class Feed < Dialog
3
+ self.resource_name = :feed
6
4
 
7
- DEFAULTS = {
5
+ self.defaults = {
8
6
  display: "popup"
9
7
  }
10
8
 
@@ -22,31 +20,7 @@ module FacebookDialog
22
20
  #description
23
21
  #properties
24
22
  #actions
25
- #ref
26
- def initialize(options = {})
27
- @options = options
28
- @options[:app_id] = FacebookDialog.api_key
29
- end
30
-
31
- def url
32
- "#{FACEBOOK_BASE_URL}?#{Rack::Utils.build_query(query)}"
33
- end
34
-
35
- def query
36
- to_h.merge({
37
- redirect_uri: @options[:redirect_uri] || @options[:link],
38
- display: @options[:display] || "popup"
39
- })
40
- end
41
-
42
- def serializable_hash(options = {})
43
- to_h.merge({method: :feed})
44
- end
45
-
46
- HTML_ONLY_KEYS = [:redirect_uri, :display]
47
- def to_h
48
- @options.reject{|key, value| HTML_ONLY_KEYS.include?(key) }
49
- end
23
+ #ref
50
24
  end
51
25
  end
52
26
 
@@ -0,0 +1,11 @@
1
+ module FacebookDialog
2
+ class Oauth < Dialog
3
+ self.resource_name = :oauth
4
+
5
+ def initialize(options= {})
6
+ super
7
+ @options[:client_id] = @options.delete(:app_id)
8
+ end
9
+ end
10
+ end
11
+
@@ -0,0 +1,26 @@
1
+ module FacebookDialog
2
+ class OptionSerialization
3
+ def initialize(options)
4
+ @options = options
5
+ end
6
+
7
+ def serialized_hash
8
+ @serialized_options = @options.dup
9
+ options_to_serialize.each do |option_key|
10
+ if @serialized_options[option_key] && @serialized_options[option_key].kind_of?(Array)
11
+ @serialized_options[option_key] = @serialized_options[option_key].map{|i| i.to_s}.join(",")
12
+ end
13
+ end
14
+
15
+ @serialized_options
16
+ end
17
+ protected
18
+ def options_to_serialize
19
+ [
20
+ :response_type,
21
+ :scope
22
+ ]
23
+ end
24
+ end
25
+ end
26
+
@@ -0,0 +1,37 @@
1
+ module FacebookDialog
2
+ module Validators
3
+ class Display
4
+ def initialize(options = {})
5
+ @options = options
6
+ @value = options[:display]
7
+ end
8
+
9
+ def validate
10
+ validate_against_whitelist
11
+ validate_access_key
12
+ end
13
+
14
+ protected
15
+ def validate_against_whitelist
16
+ unless @value.nil? || whitelist.include?(@value.to_s)
17
+ raise error_to_raise, "#{@value} is not a valid display type"
18
+ end
19
+ end
20
+
21
+ def validate_access_key
22
+ if @value && @value == "wap" && @options[:access_key].nil?
23
+ raise error_to_raise, "You must specify an access key for wap displays"
24
+ end
25
+ end
26
+
27
+ def whitelist
28
+ FacebookDialog.display_modes
29
+ end
30
+
31
+ def error_to_raise
32
+ FacebookDialog::InvalidDisplay
33
+ end
34
+ end
35
+ end
36
+ end
37
+
@@ -0,0 +1,7 @@
1
+ require "facebook_dialog/validators/display"
2
+
3
+ module FacebookDialog
4
+ module Validators
5
+ end
6
+ end
7
+
@@ -1,4 +1,4 @@
1
1
  module FacebookDialog
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
4
4
 
@@ -1,8 +1,14 @@
1
1
  require "rack/utils"
2
2
  require "active_model"
3
+ require "active_support/core_ext/class"
3
4
  require "configatron"
4
5
 
5
6
  require "facebook_dialog/version"
7
+ require "facebook_dialog/dialog"
8
+ require "facebook_dialog/validators"
9
+ require "facebook_dialog/option_serialization"
10
+
11
+ require "facebook_dialog/oauth"
6
12
  require "facebook_dialog/feed"
7
13
 
8
14
  module FacebookDialog
@@ -13,6 +19,23 @@ module FacebookDialog
13
19
  def self.api_key=(api_key)
14
20
  configatron.facebook_dialog.api_key = api_key
15
21
  end
22
+
23
+ #set of available display modes from facebook
24
+ #if you specify wap, you must supply an access_token
25
+ def self.display_modes
26
+ [
27
+ "page",
28
+ "popup",
29
+ "iframe",
30
+ "touch",
31
+ "wap"
32
+ ]
33
+ end
34
+
35
+
36
+ class ResourceNameNotDefined < Exception; end;
37
+ class InvalidDisplay < Exception; end;
16
38
  end
17
39
 
18
40
  require "facebook_dialog/railtie" if defined?(Rails)
41
+
@@ -0,0 +1,23 @@
1
+ require 'spec_helper'
2
+
3
+ describe FacebookDialog::Dialog do
4
+ class DummyDialog < FacebookDialog::Dialog; end
5
+
6
+ it "includes the api key as an app id argument" do
7
+ FacebookDialog.api_key = "something"
8
+ FacebookDialog::Feed.new.url.should include("app_id=#{Rack::Utils.escape(FacebookDialog.api_key)}")
9
+ end
10
+
11
+ it "raises an error when a resource name is not defined when getting base uri" do
12
+ lambda { DummyDialog.base_uri }.should raise_error(
13
+ FacebookDialog::ResourceNameNotDefined
14
+ )
15
+ end
16
+
17
+ it "validates the display" do
18
+ mock_validator = mock(:validate => nil)
19
+ FacebookDialog::Validators::Display.expects(:new).returns(mock_validator)
20
+
21
+ DummyDialog.new.to_h
22
+ end
23
+ end
@@ -22,7 +22,7 @@ describe FacebookDialog::Feed do
22
22
  end
23
23
 
24
24
  its(:url) { should include(Rack::Utils.escape(redirect_uri)) }
25
- its(:url) { should include(Rack::Utils.escape(title)) }
25
+ its(:url) { should include(Rack::Utils.escape(name)) }
26
26
  its(:url) { should include(Rack::Utils.escape(description)) }
27
27
  its(:url) { should include(Rack::Utils.escape(caption)) }
28
28
 
@@ -0,0 +1,24 @@
1
+ require 'spec_helper'
2
+
3
+ describe FacebookDialog::Oauth do
4
+ let(:redirect_uri) { "http://www.example.com" }
5
+ let(:scope) { ["email", "offline_access"] }
6
+ let(:state) { "something_passed_through" }
7
+ let(:response_type) { "code" }
8
+ let(:display) { "popup" }
9
+
10
+ let(:options) do
11
+ {
12
+ redirect_uri: redirect_uri,
13
+ scope: scope,
14
+ state: state,
15
+ response_type: response_type,
16
+ display: display
17
+ }
18
+ end
19
+
20
+ subject { FacebookDialog::Oauth.new(options) }
21
+
22
+ its(:url) { should include("redirect_uri=#{Rack::Utils.escape(redirect_uri)}")}
23
+ end
24
+
@@ -0,0 +1,17 @@
1
+ require 'spec_helper'
2
+
3
+ describe FacebookDialog::OptionSerialization do
4
+ subject do
5
+ FacebookDialog::OptionSerialization.new({
6
+ response_type: [:code, :token],
7
+ scope: [:offline_access, :email]
8
+ })
9
+ end
10
+ it "breaks an array of response types into a comma delimited list" do
11
+ subject.serialized_hash[:response_type].should eql("code,token")
12
+ end
13
+
14
+ it "breaks an array of scopes into a comma delimited list" do
15
+ subject.serialized_hash[:scope].should eql("offline_access,email")
16
+ end
17
+ end
@@ -0,0 +1,21 @@
1
+ require 'spec_helper'
2
+
3
+ describe FacebookDialog::Validators::Display do
4
+ subject { FacebookDialog::Validators::Display }
5
+ it "does not raise an error when I specify a valid display" do
6
+ lambda { subject.new({display: "popup"}).validate }.should_not raise_error
7
+ end
8
+
9
+ it "raises an error when an invalid display is specified" do
10
+ lambda { subject.new({display: "bogus"}).validate }.should raise_error FacebookDialog::InvalidDisplay
11
+ end
12
+
13
+ it "raises an error when wap is specified without an access key" do
14
+ lambda { subject.new({display: "wap"}).validate }.should raise_error FacebookDialog::InvalidDisplay
15
+ end
16
+
17
+ it "does not raise an error when wap is specified along with an access key" do
18
+ lambda { subject.new({display: "wap", access_key: "blah"}).validate }.should_not raise_error
19
+ end
20
+ end
21
+
data/spec/spec_helper.rb CHANGED
@@ -1,3 +1,8 @@
1
1
  require 'rspec'
2
2
  require 'facebook_dialog'
3
+ require 'mocha'
4
+
5
+ RSpec.configure do |config|
6
+ config.mock_with :mocha
7
+ end
3
8
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: facebook_dialog
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-12-16 00:00:00.000000000Z
12
+ date: 2011-12-20 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rack
16
- requirement: &2156166720 !ruby/object:Gem::Requirement
16
+ requirement: &2156417100 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *2156166720
24
+ version_requirements: *2156417100
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: activemodel
27
- requirement: &2156166140 !ruby/object:Gem::Requirement
27
+ requirement: &2156416260 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,21 @@ dependencies:
32
32
  version: '0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *2156166140
35
+ version_requirements: *2156416260
36
+ - !ruby/object:Gem::Dependency
37
+ name: activesupport
38
+ requirement: &2156415640 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :runtime
45
+ prerelease: false
46
+ version_requirements: *2156415640
36
47
  - !ruby/object:Gem::Dependency
37
48
  name: configatron
38
- requirement: &2156165640 !ruby/object:Gem::Requirement
49
+ requirement: &2156414700 !ruby/object:Gem::Requirement
39
50
  none: false
40
51
  requirements:
41
52
  - - ! '>='
@@ -43,10 +54,10 @@ dependencies:
43
54
  version: '0'
44
55
  type: :runtime
45
56
  prerelease: false
46
- version_requirements: *2156165640
57
+ version_requirements: *2156414700
47
58
  - !ruby/object:Gem::Dependency
48
59
  name: rake
49
- requirement: &2156165100 !ruby/object:Gem::Requirement
60
+ requirement: &2156414160 !ruby/object:Gem::Requirement
50
61
  none: false
51
62
  requirements:
52
63
  - - ! '>='
@@ -54,10 +65,32 @@ dependencies:
54
65
  version: '0'
55
66
  type: :runtime
56
67
  prerelease: false
57
- version_requirements: *2156165100
68
+ version_requirements: *2156414160
58
69
  - !ruby/object:Gem::Dependency
59
70
  name: rspec
60
- requirement: &2156164600 !ruby/object:Gem::Requirement
71
+ requirement: &2156413280 !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: *2156413280
80
+ - !ruby/object:Gem::Dependency
81
+ name: guard-rspec
82
+ requirement: &2156412560 !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ! '>='
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ type: :development
89
+ prerelease: false
90
+ version_requirements: *2156412560
91
+ - !ruby/object:Gem::Dependency
92
+ name: mocha
93
+ requirement: &2156411920 !ruby/object:Gem::Requirement
61
94
  none: false
62
95
  requirements:
63
96
  - - ! '>='
@@ -65,7 +98,7 @@ dependencies:
65
98
  version: '0'
66
99
  type: :development
67
100
  prerelease: false
68
- version_requirements: *2156164600
101
+ version_requirements: *2156411920
69
102
  description: A utility for building progressively enhanced Facebook dialogs
70
103
  email:
71
104
  - dpickett@enlightsolutions.com
@@ -76,16 +109,27 @@ files:
76
109
  - .gitignore
77
110
  - .rspec
78
111
  - .rvmrc
112
+ - .travis.yml
79
113
  - Gemfile
114
+ - Guardfile
80
115
  - Rakefile
81
116
  - Readme.md
82
117
  - facebook_dialog.gemspec
83
118
  - lib/facebook_dialog.rb
119
+ - lib/facebook_dialog/dialog.rb
84
120
  - lib/facebook_dialog/feed.rb
121
+ - lib/facebook_dialog/oauth.rb
122
+ - lib/facebook_dialog/option_serialization.rb
85
123
  - lib/facebook_dialog/railtie.rb
86
124
  - lib/facebook_dialog/script_helper.rb
125
+ - lib/facebook_dialog/validators.rb
126
+ - lib/facebook_dialog/validators/display.rb
87
127
  - lib/facebook_dialog/version.rb
128
+ - spec/facebook_dialog/dialog_spec.rb
88
129
  - spec/facebook_dialog/feed_spec.rb
130
+ - spec/facebook_dialog/oauth_spec.rb
131
+ - spec/facebook_dialog/option_serialization_spec.rb
132
+ - spec/facebook_dialog/validators/display_spec.rb
89
133
  - spec/spec_helper.rb
90
134
  homepage: ''
91
135
  licenses: []
@@ -101,7 +145,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
101
145
  version: '0'
102
146
  segments:
103
147
  - 0
104
- hash: -4243053102014288206
148
+ hash: -1035554407844364285
105
149
  required_rubygems_version: !ruby/object:Gem::Requirement
106
150
  none: false
107
151
  requirements:
@@ -110,7 +154,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
110
154
  version: '0'
111
155
  segments:
112
156
  - 0
113
- hash: -4243053102014288206
157
+ hash: -1035554407844364285
114
158
  requirements: []
115
159
  rubyforge_project:
116
160
  rubygems_version: 1.8.6
@@ -118,5 +162,9 @@ signing_key:
118
162
  specification_version: 3
119
163
  summary: Use ActiveModel serialization to keep html and javascript FB dialogs DRY
120
164
  test_files:
165
+ - spec/facebook_dialog/dialog_spec.rb
121
166
  - spec/facebook_dialog/feed_spec.rb
167
+ - spec/facebook_dialog/oauth_spec.rb
168
+ - spec/facebook_dialog/option_serialization_spec.rb
169
+ - spec/facebook_dialog/validators/display_spec.rb
122
170
  - spec/spec_helper.rb