crowdmob 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. data/lib/base.rb +12 -0
  2. data/lib/campaigns.rb +106 -0
  3. data/lib/crowdmob.rb +2 -0
  4. data/lib/installs.rb +103 -0
  5. metadata +48 -0
data/lib/base.rb ADDED
@@ -0,0 +1,12 @@
1
+ module CrowdMob
2
+ class << self
3
+ attr_accessor :base_url
4
+ end
5
+
6
+ # You can test against CrowdMob's staging server located at:
7
+ @base_url = 'http://deals.mobstaging.com'
8
+
9
+ # Eventually, you'll want to switch over to CrowdMob's production server
10
+ # located at:
11
+ # @base_url = 'https://deals.crowdmob.com'
12
+ end
data/lib/campaigns.rb ADDED
@@ -0,0 +1,106 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'digest'
4
+ require 'json'
5
+ require 'net/http'
6
+ require 'time'
7
+
8
+ load 'base.rb'
9
+
10
+
11
+
12
+ module CrowdMob::Campaigns
13
+ class << self
14
+ attr_accessor :organization_secret_key
15
+ attr_accessor :organization_permalink
16
+ end
17
+
18
+ # When you registered your organization with CrowdMob, you got an
19
+ # organization secret key and permalink:
20
+ @organization_secret_key = '9cbfbe10e13f2a30cb6509ef0e09445b'
21
+ @organization_permalink = 'crowdmob'
22
+
23
+ def self.create(active, params)
24
+ url = CrowdMob.base_url + '/organizations/' + @organization_permalink + '/sponsored_action_campaigns.json'
25
+ uri = URI.parse(url)
26
+ now, secret_hash = self.compute_secret_hash
27
+ params = {
28
+ 'datetime' => now,
29
+ 'secret_hash' => secret_hash,
30
+ 'active' => params[:active],
31
+ 'sponsored_action_campaign[bid_in_cents]' => params[:bid_in_cents],
32
+ 'sponsored_action_campaign[max_total_spend_in_cents]' => params[:max_total_spend_in_cents],
33
+ 'sponsored_action_campaign[max_spend_per_day_in_cents]' => params[:max_spend_per_day_in_cents],
34
+ 'sponsored_action_campaign[starts_at]' => params[:starts_at],
35
+ 'sponsored_action_campaign[ends_at]' => params[:ends_at],
36
+ 'sponsored_action_campaign[kind]' => 'install',
37
+ }
38
+ response, data = Net::HTTP.post_form(uri, params)
39
+ json = JSON.parse(response.body)
40
+ json
41
+ end
42
+
43
+ def self.edit(campaign_id, active, params)
44
+ url = CrowdMob.base_url + '/organizations/' + @organization_permalink + '/sponsored_action_campaigns/' + campaign_id.to_s + '.json'
45
+ now, secret_hash = self.compute_secret_hash
46
+ url += '?datetime=' + now + '&secret_hash=' + secret_hash + '&active=' + active.to_s
47
+ params.each { |key, value| url += '&sponsored_action_campaign[' + key.to_s + ']=' + value.to_s }
48
+ uri = URI.parse(url)
49
+ response = self.issue_http_request(uri, 'Put')
50
+ json = JSON.parse(response.body)
51
+ json
52
+ end
53
+
54
+ def self.delete(campaign_id)
55
+ url = CrowdMob.base_url + '/organizations/' + @organization_permalink + '/sponsored_action_campaigns/' + campaign_id.to_s + '.json'
56
+ now, secret_hash = self.compute_secret_hash
57
+ url += '?datetime=' + now + '&secret_hash=' + secret_hash
58
+ uri = URI.parse(url)
59
+ response = self.issue_http_request(uri, 'Delete')
60
+ end
61
+
62
+ def self.compute_secret_hash
63
+ now = DateTime.now.iso8601
64
+ secret_hash = @organization_secret_key + @organization_permalink + ',' + now
65
+ secret_hash = Digest::SHA2.hexdigest(secret_hash)
66
+ [now, secret_hash]
67
+ end
68
+
69
+ def self.issue_http_request(uri, http_method)
70
+ http = Net::HTTP.new(uri.host, uri.port)
71
+ http.use_ssl = uri.scheme == 'https'
72
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
73
+ request = Net::HTTP.const_get(http_method).new(uri.request_uri)
74
+ response = http.request(request)
75
+ response
76
+ end
77
+ end
78
+
79
+
80
+
81
+ # You can run this script from the command line to see a working example of
82
+ # server-to-server integration.
83
+ if __FILE__ == $0
84
+ CrowdMob.base_url = 'http://deals.mobstaging.com'
85
+ CrowdMob::Campaigns.organization_secret_key = '9cbfbe10e13f2a30cb6509ef0e09445b'
86
+ CrowdMob::Campaigns.organization_permalink = 'crowdmob'
87
+
88
+ # Create a campaign:
89
+ now = DateTime.now
90
+ one_week_from_now = now + 7
91
+ params = {
92
+ bid_in_cents: 1,
93
+ max_total_spend_in_cents: 100,
94
+ max_spend_per_day_in_cents: 10,
95
+ starts_at: now,
96
+ ends_at: one_week_from_now,
97
+ }
98
+ campaign = CrowdMob::Campaigns.create(true, params)
99
+
100
+ # Edit the campaign:
101
+ params = { bid_in_cents: 2 }
102
+ campaign = CrowdMob::Campaigns.edit(campaign['id'], false, params)
103
+
104
+ # Delete the campaign:
105
+ CrowdMob::Campaigns.delete(campaign['id'])
106
+ end
data/lib/crowdmob.rb ADDED
@@ -0,0 +1,2 @@
1
+ load 'installs.rb'
2
+ load 'campaigns.rb'
data/lib/installs.rb ADDED
@@ -0,0 +1,103 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'digest'
4
+ require 'json'
5
+ require 'net/http'
6
+
7
+ load 'base.rb'
8
+
9
+
10
+
11
+ module CrowdMob::Installs
12
+ class << self
13
+ attr_accessor :app_secret_key
14
+ attr_accessor :app_permalink
15
+ end
16
+
17
+ # When you registered your app with CrowdMob, you got an app secret key and
18
+ # a permalink:
19
+ @app_secret_key = '5bb75e8dd6300cadcdd07fa2c46a3c10'
20
+ @app_permalink = 'lulzio'
21
+
22
+ # If you didn't record your app's secret key and permalink when you regist
23
+ # registered your app with CrowdMob, you can find it on your app's page on
24
+ # CrowdMob's server. In this example, our app is located here on CrowdMob's
25
+ # staging server:
26
+ # http://deals.mobstaging.com/organizations/crowdmob/apps/lulzio
27
+ #
28
+ # In your case, if you registered your app on CrowdMob's production server,
29
+ # your app's homepage URL would correspond to:
30
+ # https://deals.crowdmob.com/organizations/[your organization permalink]/apps/[your app permalink]
31
+
32
+ # When you signed up for server-to-server installs tracking with CrowdMob,
33
+ # CrowdMob worked with you to determine a secure hashing algorithm, a salt,
34
+ # and a unique device identifier to meet your requirements. In this
35
+ # example, we're SHA256 hashing MAC addresses, salted with the string
36
+ # "salt". We typically recommend using your app's secret key as your salt,
37
+ # but we can use any string that meets your requirements as a salt.
38
+ @@salt = 'salt'
39
+
40
+ def self.report(mac_address)
41
+ url = CrowdMob.base_url + '/crave/verify_install.json'
42
+ uri = URI.parse(url)
43
+
44
+ # Hash the MAC address. If you already store the unique device
45
+ # identifiers hashed, then this step is unnecessary. If you store the
46
+ # device IDs hashed, you would've worked with CrowdMob's engineers to
47
+ # implement a custom server-to-server installs tracking integration
48
+ # solution.
49
+ hashed_mac_address = Digest::SHA2.hexdigest(@@salt + mac_address)
50
+
51
+ # Compute the secret hash. The secret hash is a required POST parameter
52
+ # which prevents forged POST requests. This secret hash consists of your
53
+ # app's permalink, a comma, the string "campaign_uuid", a comma, and the
54
+ # previously hashed MAC address - salted with your app's secret key, all
55
+ # SHA256 hashed. (Note that there's no comma between the secret key salt
56
+ # and the permalink.)
57
+ secret_hash = Digest::SHA2.hexdigest(@app_secret_key + @app_permalink + ',' + 'campaign_uuid' + ',' + hashed_mac_address)
58
+
59
+ # The POST parameters:
60
+ params = {
61
+ 'permalink' => @app_permalink,
62
+ 'uuid' => hashed_mac_address,
63
+ 'uuid_type' => 'campaign_uuid',
64
+ 'secret_hash' => secret_hash
65
+ }
66
+
67
+ # Finally, issue the POST request to CrowdMob's server:
68
+ response, data = Net::HTTP.post_form(uri, params)
69
+ json = JSON.parse(response.body)
70
+
71
+ # Check for a 200 HTTP status code. This code denotes successful install
72
+ # tracking.
73
+ puts "HTTP status code: #{response.code}"
74
+ puts "CrowdMob internal (action) status code: #{json['action_status']}"
75
+
76
+ # This table explains what the different status code combinations
77
+ # denote:
78
+ # HTTP Status Code CrowdMob Internal Status Code Meaning
79
+ # ---------------- ----------------------------- -------
80
+ # 400 1001 You didn't supply your app's permalink as an HTTP POST parameter.
81
+ # 400 1002 You didn't specify the unique device identifier type as an HTTP POST parameter. (In the case of server-to-server installs tracking, this parameter should be the string "campaign_uuid".)
82
+ # 400 1003 You didn't specify the unique device identifier as an HTTP POST parameter. (Typically a salted hashed MAC address, but could be some other unique device identifier that you collect on your server.)
83
+ # 404 1004 The app permalink that you specified doesn't correspond to any app registered on CrowdMob's server.
84
+ # 403 1005 The secret hash that you computed doesn't correspond to the secret hash that CrowdMob's server computed. (This could be a forged request?)
85
+ # 200 Any CrowdMob's server successfully tracked the install.
86
+ end
87
+ end
88
+
89
+
90
+
91
+ # You can run this script from the command line to see a working example of
92
+ # server-to-server integration.
93
+ if __FILE__ == $0
94
+ CrowdMob.base_url = 'http://deals.mobstaging.com'
95
+ CrowdMob::Installs.app_secret_key = '5bb75e8dd6300cadcdd07fa2c46a3c10'
96
+ CrowdMob::Installs.app_permalink = 'lulzio'
97
+
98
+ # This is an example MAC address, stored in your server's database, used to
99
+ # uniquely identify a device:
100
+ mac_address = '11:11:11:11:11:11'
101
+
102
+ CrowdMob::Installs.report(mac_address)
103
+ end
metadata ADDED
@@ -0,0 +1,48 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: crowdmob
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - CrowdMob, Inc.
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-14 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Server to server integration with CrowdMob
15
+ email: developers@crowdmob.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/base.rb
21
+ - lib/campaigns.rb
22
+ - lib/crowdmob.rb
23
+ - lib/installs.rb
24
+ homepage: http://rubygems.org/gems/bing_images
25
+ licenses: []
26
+ post_install_message:
27
+ rdoc_options: []
28
+ require_paths:
29
+ - lib
30
+ required_ruby_version: !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - ! '>='
34
+ - !ruby/object:Gem::Version
35
+ version: '0'
36
+ required_rubygems_version: !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ! '>='
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ requirements: []
43
+ rubyforge_project:
44
+ rubygems_version: 1.8.24
45
+ signing_key:
46
+ specification_version: 3
47
+ summary: Server to server integration with CrowdMob
48
+ test_files: []