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



80
81
82
83
84
85
# File 'lib/campaigns.rb', line 80

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(params, active) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/campaigns.rb', line 23

def self.create(params, active)
  url = CrowdMob.base_url + '/api/organizations/' + @organization_permalink + '/sponsored_action_campaigns.json'
  uri = URI.parse(url)
  now, secret_hash = self.compute_secret_hash

  form = {
    'ios_url' => params[:ios_url],
    'android_url' => params[:android_url],
    'datetime' => now,
    'secret_hash' => secret_hash,
    'active' => active,
    'campaign[max_total_spend_in_dollars]' => params[:max_total_spend_in_dollars],
    'campaign[max_spend_per_day_in_dollars]' => params[:max_spend_per_day_in_dollars],
    'campaign[starts_at]' => params[:starts_at],
    'campaign[ends_at]' => params[:ends_at],
  }
  form['campaign[android]'] = '1' if params[:android_bid]
  form['campaign[android_bid]'] = params[:android_bid] if params[:android_bid]
  form['campaign[ipad]'] = '1' if params[:ipad_bid]
  form['campaign[ipad_bid]'] = params[:ipad_bid] if params[:ipad_bid]
  form['campaign[iphone]'] = '1' if params[:iphone_bid]
  form['campaign[iphone_bid]'] = params[:iphone_bid] if params[:iphone_bid]
  form['campaign[ipod]'] = '1' if params[:ipod_bid]
  form['campaign[ipod_bid]'] = params[:ipod_bid] if params[:ipod_bid]

  response, data = Net::HTTP.post_form(uri, form)
  json = JSON.parse(response.body)
  json
end

.delete(campaign_id) ⇒ Object



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

def self.delete(campaign_id)
  url = CrowdMob.base_url + '/api/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



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

def self.edit(campaign_id, active, params)
  url = CrowdMob.base_url + '/api/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 += '&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



87
88
89
90
91
92
93
94
# File 'lib/campaigns.rb', line 87

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

.query(campaign_id) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/campaigns.rb', line 53

def self.query(campaign_id)
  url = CrowdMob.base_url + '/api/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, 'Get')
end