custom_audience 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +1 -0
- data/custom_audience.gemspec +28 -0
- data/lib/custom_audience/version.rb +3 -0
- data/lib/custom_audience.rb +85 -0
- data/spec/custom_audience_spec.rb +135 -0
- data/spec/spec_helper.rb +6 -0
- metadata +160 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Jon Moses
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# CustomAudience
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'custom_audience'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install custom_audience
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'custom_audience/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "custom_audience"
|
8
|
+
spec.version = CustomAudience::VERSION
|
9
|
+
spec.authors = ["Jon Moses"]
|
10
|
+
spec.email = ["jon@burningbush.us"]
|
11
|
+
spec.description = %q{Easy FB custom audience creation}
|
12
|
+
spec.summary = %q{Because it's easy}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_dependency "koala", "~> 1.6.0"
|
22
|
+
spec.add_dependency 'activesupport', '~> 3'
|
23
|
+
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
25
|
+
spec.add_development_dependency "rspec"
|
26
|
+
spec.add_development_dependency "mocha"
|
27
|
+
spec.add_development_dependency "rake"
|
28
|
+
end
|
@@ -0,0 +1,85 @@
|
|
1
|
+
require 'koala'
|
2
|
+
require 'json'
|
3
|
+
require 'active_support/core_ext/object/blank'
|
4
|
+
require 'active_support/core_ext/hash/keys'
|
5
|
+
require 'custom_audience/version'
|
6
|
+
|
7
|
+
module CustomAudience
|
8
|
+
class CustomAudience
|
9
|
+
attr_accessor :users
|
10
|
+
attr_reader :attributes, :token
|
11
|
+
|
12
|
+
def initialize(name_or_hash = {})
|
13
|
+
if name_or_hash.is_a?(Hash)
|
14
|
+
@attributes = name_or_hash.stringify_keys
|
15
|
+
else
|
16
|
+
@attributes = {"name" => name_or_hash}
|
17
|
+
end
|
18
|
+
|
19
|
+
self.token = @attributes.delete('token')
|
20
|
+
fetch_attributes!
|
21
|
+
end
|
22
|
+
|
23
|
+
def exists?
|
24
|
+
id.present?
|
25
|
+
end
|
26
|
+
|
27
|
+
def save
|
28
|
+
# Should try and fetch attributes?
|
29
|
+
create if ! exists?
|
30
|
+
|
31
|
+
add_users
|
32
|
+
end
|
33
|
+
|
34
|
+
def account_id=(account_id)
|
35
|
+
attributes["account_id"] = account_id
|
36
|
+
fetch_attributes!
|
37
|
+
end
|
38
|
+
|
39
|
+
def token=(token)
|
40
|
+
@token = token
|
41
|
+
fetch_attributes!
|
42
|
+
end
|
43
|
+
|
44
|
+
def account_id
|
45
|
+
attributes['account_id'].to_s.gsub(/^act_/, '')
|
46
|
+
end
|
47
|
+
|
48
|
+
def name
|
49
|
+
attributes["name"]
|
50
|
+
end
|
51
|
+
|
52
|
+
def id
|
53
|
+
attributes["id"]
|
54
|
+
end
|
55
|
+
|
56
|
+
private
|
57
|
+
def graph
|
58
|
+
@graph ||= Koala::Facebook::API.new(token)
|
59
|
+
end
|
60
|
+
|
61
|
+
def create
|
62
|
+
id = graph.put_connections("act_" + account_id, 'customaudiences', name: name)["id"]
|
63
|
+
|
64
|
+
@attributes = graph.get_object(id)
|
65
|
+
end
|
66
|
+
|
67
|
+
def add_users
|
68
|
+
graph.put_connections id, 'users', users: JSON.dump(users.map {|user| {"id" => user } })
|
69
|
+
end
|
70
|
+
|
71
|
+
def all
|
72
|
+
@all ||= graph.get_connections("act_" + account_id, 'customaudiences').map do |audience_data|
|
73
|
+
CustomAudience.new audience_data
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
def fetch_attributes!
|
78
|
+
return unless account_id.present? && token.present?
|
79
|
+
|
80
|
+
if match = all.detect {|audience| audience.name == name }
|
81
|
+
@attributes = match.attributes
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
@@ -0,0 +1,135 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe CustomAudience::CustomAudience do
|
4
|
+
describe "#initialize" do
|
5
|
+
it "creates with a name" do
|
6
|
+
described_class.new("name").name.should eq("name")
|
7
|
+
end
|
8
|
+
|
9
|
+
it "creates with a hash" do
|
10
|
+
described_class.new('name' => "abc").name.should eq("abc")
|
11
|
+
end
|
12
|
+
|
13
|
+
it "fetches attributes with an account id" do
|
14
|
+
Koala::Facebook::API.stubs(:new).with('token').returns(api = stub)
|
15
|
+
api.expects(:get_connections).with("act_account_id", 'customaudiences').
|
16
|
+
returns([{"name" => "test", "id" => 5}, {"name" => "foo", "id" => 6}])
|
17
|
+
|
18
|
+
audience = described_class.new("name" => "test", "account_id" => "account_id", 'token' => 'token')
|
19
|
+
|
20
|
+
audience.id.should eq(5)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
describe "#account_id=" do
|
25
|
+
it "fetches the attributes" do
|
26
|
+
Koala::Facebook::API.stubs(:new).with('token').returns(api = stub)
|
27
|
+
api.expects(:get_connections).with("act_account_id", 'customaudiences').
|
28
|
+
returns([{"name" => "test", "id" => 5}, {"name" => "foo", "id" => 6}])
|
29
|
+
|
30
|
+
audience = described_class.new("name" => "test", 'token' => 'token')
|
31
|
+
audience.account_id = "account_id"
|
32
|
+
|
33
|
+
audience.id.should eq(5)
|
34
|
+
end
|
35
|
+
|
36
|
+
it "sets the account id" do
|
37
|
+
subject.account_id = 5
|
38
|
+
|
39
|
+
subject.account_id.should eq('5')
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
describe "#token=" do
|
44
|
+
it "fetches the attributes" do
|
45
|
+
Koala::Facebook::API.stubs(:new).with('token').returns(api = stub)
|
46
|
+
api.expects(:get_connections).with("act_account_id", 'customaudiences').
|
47
|
+
returns([{"name" => "test", "id" => 5}, {"name" => "foo", "id" => 6}])
|
48
|
+
|
49
|
+
audience = described_class.new("name" => "test", 'account_id' => 'account_id')
|
50
|
+
audience.token = 'token'
|
51
|
+
|
52
|
+
audience.id.should eq(5)
|
53
|
+
end
|
54
|
+
|
55
|
+
it "sets the token" do
|
56
|
+
subject.token = 'abc'
|
57
|
+
|
58
|
+
subject.token.should eq('abc')
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
describe "#exists?" do
|
63
|
+
it "is true when there is an id" do
|
64
|
+
subject.stubs(id: 1)
|
65
|
+
|
66
|
+
subject.should be_exists
|
67
|
+
end
|
68
|
+
|
69
|
+
it "is false when there is no id" do
|
70
|
+
subject.should_not be_exists
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
describe "#name" do
|
75
|
+
it "returns the name" do
|
76
|
+
subject.attributes['name'] = 'name'
|
77
|
+
subject.name.should eq('name')
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
describe "#id" do
|
82
|
+
it "returns the id" do
|
83
|
+
subject.attributes['id'] = 'id'
|
84
|
+
subject.id.should eq('id')
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
describe "#account_id" do
|
89
|
+
it "strips act_ from the front" do
|
90
|
+
subject.attributes['account_id'] = 'act_123'
|
91
|
+
|
92
|
+
subject.account_id.should eq('123')
|
93
|
+
end
|
94
|
+
|
95
|
+
it "handles non-string values" do
|
96
|
+
subject.attributes['account_id'] = 123
|
97
|
+
|
98
|
+
subject.account_id.should eq('123')
|
99
|
+
end
|
100
|
+
|
101
|
+
it "handles nil" do
|
102
|
+
subject.account_id.should eq('')
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
describe "#save" do
|
107
|
+
let(:api) { stub }
|
108
|
+
|
109
|
+
before do
|
110
|
+
Koala::Facebook::API.stubs(new: api)
|
111
|
+
|
112
|
+
subject.attributes.merge!('name' => 'name', 'account_id' => '123')
|
113
|
+
subject.users = [10]
|
114
|
+
end
|
115
|
+
|
116
|
+
it "creates if it doesn't exist, and adds users" do
|
117
|
+
subject.stubs(exists?: false)
|
118
|
+
|
119
|
+
api.expects(:put_connections).with('act_123', 'customaudiences', name: 'name').
|
120
|
+
returns({"id" => 1})
|
121
|
+
api.expects(:get_object).with(1).returns({"id" => 1})
|
122
|
+
api.expects(:put_connections).with(1, 'users', users: [{"id" => 10}].to_json)
|
123
|
+
|
124
|
+
subject.save
|
125
|
+
end
|
126
|
+
|
127
|
+
it "adds users if it already exists" do
|
128
|
+
subject.stubs(exists?: true, id: 1)
|
129
|
+
|
130
|
+
api.expects(:put_connections).with(1, 'users', users: [{"id" => 10}].to_json)
|
131
|
+
|
132
|
+
subject.save
|
133
|
+
end
|
134
|
+
end
|
135
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,160 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: custom_audience
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Jon Moses
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-05-08 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: koala
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 1.6.0
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 1.6.0
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: activesupport
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '3'
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '3'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: bundler
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ~>
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '1.3'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '1.3'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: rspec
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: mocha
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ! '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
type: :development
|
87
|
+
prerelease: false
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ! '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
- !ruby/object:Gem::Dependency
|
95
|
+
name: rake
|
96
|
+
requirement: !ruby/object:Gem::Requirement
|
97
|
+
none: false
|
98
|
+
requirements:
|
99
|
+
- - ! '>='
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '0'
|
102
|
+
type: :development
|
103
|
+
prerelease: false
|
104
|
+
version_requirements: !ruby/object:Gem::Requirement
|
105
|
+
none: false
|
106
|
+
requirements:
|
107
|
+
- - ! '>='
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
description: Easy FB custom audience creation
|
111
|
+
email:
|
112
|
+
- jon@burningbush.us
|
113
|
+
executables: []
|
114
|
+
extensions: []
|
115
|
+
extra_rdoc_files: []
|
116
|
+
files:
|
117
|
+
- .gitignore
|
118
|
+
- Gemfile
|
119
|
+
- LICENSE.txt
|
120
|
+
- README.md
|
121
|
+
- Rakefile
|
122
|
+
- custom_audience.gemspec
|
123
|
+
- lib/custom_audience.rb
|
124
|
+
- lib/custom_audience/version.rb
|
125
|
+
- spec/custom_audience_spec.rb
|
126
|
+
- spec/spec_helper.rb
|
127
|
+
homepage: ''
|
128
|
+
licenses:
|
129
|
+
- MIT
|
130
|
+
post_install_message:
|
131
|
+
rdoc_options: []
|
132
|
+
require_paths:
|
133
|
+
- lib
|
134
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
135
|
+
none: false
|
136
|
+
requirements:
|
137
|
+
- - ! '>='
|
138
|
+
- !ruby/object:Gem::Version
|
139
|
+
version: '0'
|
140
|
+
segments:
|
141
|
+
- 0
|
142
|
+
hash: 651806943190799798
|
143
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
144
|
+
none: false
|
145
|
+
requirements:
|
146
|
+
- - ! '>='
|
147
|
+
- !ruby/object:Gem::Version
|
148
|
+
version: '0'
|
149
|
+
segments:
|
150
|
+
- 0
|
151
|
+
hash: 651806943190799798
|
152
|
+
requirements: []
|
153
|
+
rubyforge_project:
|
154
|
+
rubygems_version: 1.8.24
|
155
|
+
signing_key:
|
156
|
+
specification_version: 3
|
157
|
+
summary: Because it's easy
|
158
|
+
test_files:
|
159
|
+
- spec/custom_audience_spec.rb
|
160
|
+
- spec/spec_helper.rb
|