Class: PocketMath::Advertiser::V1::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/pocketmath-advertiser.rb

Instance Method Summary collapse

Constructor Details

#initialize(pocketmath_api_key, pocketmath_api_base_url) ⇒ Client

Returns a new instance of Client.



21
22
23
24
# File 'lib/pocketmath-advertiser.rb', line 21

def initialize(pocketmath_api_key, pocketmath_api_base_url)
  @pocketmath_api_key = pocketmath_api_key
  @pocketmath_api_base_url = pocketmath_api_base_url
end

Instance Method Details

#closeObject



26
27
# File 'lib/pocketmath-advertiser.rb', line 26

def close
end

#create_gps_list(name) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/pocketmath-advertiser.rb', line 40

def create_gps_list(name)
  raise "name was nil" if name.nil?
  raise "name was blank" if name.empty? 
  
  add_list_json = JSON::generate(
     {
       "token" => "#{@pocketmath_api_key}",
       "list" =>
          {
             "name" => "#{name}"
          }
     })
     
  uri = URI.parse(@pocketmath_api_base_url)
  response = nil
  Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') do |http|
    request = Net::HTTP::Post.new("/v1/lists/gps/add_list.json")  
    request.body = add_list_json
    request.content_type = "application/json"
    response = http.request(request)
  end
  if !response.nil?
    data = response.body
    obj = JSON::parse(data)
    list_id = obj["id"]
    return list_id
  else
    return nil
  end
end

#create_insertion_order(opts = { "name" => nil, "start_datetime" => Date.now, "end_datetime" => Date.now + 7.days, "creative_type_ids" => [], "budget" => "1.00", "iab_catgories" => [], "bid_per_impression" => "2.00", "top_level_domain" => "pocketmath.com", "country" => "223", "image_url" => "https://s3.amazonaws.com/pocketmath/pocketmath_320x50.jpg", "landing_page_url" => "www.pocketmath.com", "size_id" => "11" }) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/pocketmath-advertiser.rb', line 155

def create_insertion_order( opts =
  {
    "name" => nil,
    "start_datetime" => Date.now,
    "end_datetime" => Date.now + 7.days,
    "creative_type_ids" => [],
    "budget" => "1.00",
    "iab_catgories" => [],
    "bid_per_impression" => "2.00",
    "top_level_domain" => "pocketmath.com",
    "country" => "223",
    "image_url" => "https://s3.amazonaws.com/pocketmath/pocketmath_320x50.jpg",
    "landing_page_url" => "www.pocketmath.com",
    "size_id" => "11"
  })

  o = deep_copy(opts)

  if o["name"].nil?
    raise "name was nil"
  end

  if o["iab_category_codes"].empty?
    raise "no iab_category_codes specified"
  end

  if o["start_datetime"].is_a?(Date)
    o["start_datetime"] = o["start_datetime"].strftime("%d %b %Y")
  elsif ! o["start_datetime"].is_a?(String)
    raise "unsupported type"
  end

  if o["end_datetime"].is_a?(Date)
    o["end_datetime"] = o["end_datetime"].strftime("%d %b %Y")
  elsif ! o["end_datetime"].is_a?(String)
    raise "unsupported type"
  end

  obj = { "token" => @pocketmath_api_key, "io" => o }

  create_io_json = JSON.generate(obj)

  uri = URI.parse(@pocketmath_api_base_url)
  response = nil
  Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') do |http|
    request = Net::HTTP::Post.new("/v1/io/create.json")  
    request.body = create_io_json
    request.content_type = "application/json"
    response = http.request(request)
  end
  if !response.nil?
    data = response.body
    p response
    p data
    obj = JSON::parse(data)
    io_id = obj["id"]
    return io_id
  else
    return nil
  end

end

#find_gps_list_id_by_name(name) ⇒ Object



29
30
31
32
33
34
35
36
37
38
# File 'lib/pocketmath-advertiser.rb', line 29

def find_gps_list_id_by_name(name)
  p "get_gps_list_id"
  url = "#{@pocketmath_api_base_url}/v1/lists/gps/list.json?token=#{@pocketmath_api_key}&limit=10000"
  response = Net::HTTP.get_response(URI.parse(url))
  data = response.body
  obj = JSON.parse(data)
  obj.each do |result|
    return result["id"] if result["name"] == name
  end
end

#get_insertion_order_stats(id, start_date = nil, end_date = nil) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/pocketmath-advertiser.rb', line 102

def get_insertion_order_stats(id, start_date = nil, end_date = nil)
  url = "#{@pocketmath_api_base_url}/v1/stats/id.json?token=#{@pocketmath_api_key}&id=#{URI.encode(id)}"
  start_date_string = to_date_string(start_date)
  end_date_string = to_date_string(end_date)
  url << "&start_date=#{URI.encode(start_date_string)}" if !start_date_string.nil?
  url << "&end_date=#{URI.encode(end_date_string)}" if !end_date_string.nil?
  uri = URI.parse(url)
  
  response = Net::HTTP.get(uri)
  p response

  if !response.nil?
    data = response
    obj = JSON::parse(data)
    stats = obj["stats"]
    return nil if stats.nil?
    io_totals = stats["io_totals"]
    return nil if io_totals.nil?
    p io_totals
    iostats = InsertionOrderStats.create({
      :impressions => io_totals["imp"].to_i,
      :clicks => io_totals["clk"].to_i,
      :conversions => io_totals["conv"].to_i,
      :spend => io_totals["spend"].gsub(/[^\d\.]/, '').to_f,
      :cpm => io_totals["cpm"].gsub(/[^\d\.]/, '').to_f,
      :cpc => io_totals["cpc"].gsub(/[^\d\.]/, '').to_f,
      :cpa => io_totals["cpa"].gsub(/[^\d\.]/, '').to_f,
      :ctr => io_totals["ctr"].to_f
    })
    return iostats
  else
    return nil
  end          
end

#upload_gps_list(id, coordinates = []) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/pocketmath-advertiser.rb', line 71

def upload_gps_list(id, coordinates = [])
  raise "id was nil" if id.nil?
  raise "coordinates was nil" if coordinates.nil?
  raise "coordiantes was empty" if coordinates.empty?

  c = Curl::Easy.new("#{@pocketmath_api_base_url}/v1/lists/gps/upload.json")
  c.multipart_form_post = true

  coordinates_string = ''
  coordinates.each do |coord|
    coordinates_string << "#{coord[:latitude]},#{coord[:longitude]}\r\n"
  end

  r = Random.new
  remote_file_name = "pm-advert-client-gps-list-#{r.rand(2*1000*1000*1000)}-#{r.rand(2*1000*1000*1000)}.txt"
  content_field = Curl::PostField.file('file', remote_file_name) do |field| 
    field.content = coordinates_string
  end

  success = nil

  success = c.http_post(
    content_field,
    Curl::PostField.content('token', @pocketmath_api_key),
    Curl::PostField.content('mode', 'append'),
    Curl::PostField.content('list_id', id.to_s )
  )       

  return success
end