Module: CrowdMob::Campaigns

Defined in:
lib/campaigns.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

Returns the value of attribute organization_permalink.



15
16
17
# File 'lib/campaigns.rb', line 15

def organization_permalink
  @organization_permalink
end

.organization_secret_keyObject

Returns the value of attribute organization_secret_key.



14
15
16
# File 'lib/campaigns.rb', line 14

def organization_secret_key
  @organization_secret_key
end

Class Method Details

.compute_secret_hashObject



63
64
65
66
67
68
# File 'lib/campaigns.rb', line 63

def self.compute_secret_hash
  now = DateTime.now.iso8601
  secret_hash = @organization_secret_key + @organization_permalink + ',' + now
  secret_hash = Digest::SHA2.hexdigest(secret_hash)
  [now, secret_hash]
end

.create(app_store_url, active, params) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/campaigns.rb', line 23

def self.create(app_store_url, active, params)
  url = CrowdMob.base_url + '/organizations/' + @organization_permalink + '/sponsored_action_campaigns.json'
  uri = URI.parse(url)
  now, secret_hash = self.compute_secret_hash
  params = {
    'app_store_url' => app_store_url,
    'datetime' => now,
    'secret_hash' => secret_hash,
    'active' => active,
    'sponsored_action_campaign[bid_in_cents]' => params[:bid_in_cents],
    'sponsored_action_campaign[max_total_spend_in_cents]' => params[:max_total_spend_in_cents],
    'sponsored_action_campaign[max_spend_per_day_in_cents]' => params[:max_spend_per_day_in_cents],
    'sponsored_action_campaign[starts_at]' => params[:starts_at],
    'sponsored_action_campaign[ends_at]' => params[:ends_at],
    'sponsored_action_campaign[kind]' => 'install',
  }
  response, data = Net::HTTP.post_form(uri, params)
  json = JSON.parse(response.body)
  json
end

.delete(campaign_id) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/campaigns.rb', line 55

def self.delete(campaign_id)
  url = CrowdMob.base_url + '/organizations/' + @organization_permalink + '/sponsored_action_campaigns/' + campaign_id.to_s + '.json'
  now, secret_hash = self.compute_secret_hash
  url += '?datetime=' + now + '&secret_hash=' + secret_hash
  uri = URI.parse(url)
  response = self.issue_http_request(uri, 'Delete')
end

.edit(campaign_id, active, params) ⇒ Object



44
45
46
47
48
49
50
51
52
53
# File 'lib/campaigns.rb', line 44

def self.edit(campaign_id, active, params)
  url = CrowdMob.base_url + '/organizations/' + @organization_permalink + '/sponsored_action_campaigns/' + campaign_id.to_s + '.json'
  now, secret_hash = self.compute_secret_hash
  url += '?datetime=' + now + '&secret_hash=' + secret_hash + '&active=' + active.to_s
  params.each { |key, value| url += '&sponsored_action_campaign[' + key.to_s + ']=' + value.to_s }
  uri = URI.parse(url)
  response = self.issue_http_request(uri, 'Put')
  json = JSON.parse(response.body)
  json
end

.issue_http_request(uri, http_method) ⇒ Object



70
71
72
73
74
75
76
77
# File 'lib/campaigns.rb', line 70

def self.issue_http_request(uri, http_method)
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = uri.scheme == 'https'
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  request = Net::HTTP.const_get(http_method).new(uri.request_uri)
  response = http.request(request)
  response
end