pocketmath-advertise 0.0.1.pre.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/lib/pocketmath-advertise.rb +2 -0
- data/lib/pocketmath-advertiser.rb +162 -0
- data/lib/pocketmath-geocode.rb +73 -0
- metadata +60 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: efe5b188b48025ef1b46539be236f94382f607ec
|
4
|
+
data.tar.gz: 205b800ffc3b3b25671364b531dc3e1892817fd2
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4d5648e57f93ef13de4a3e13dede9922ebcfc898d169a34d9273bd20d9f7106f5b3db092d389783b39bb8504afdaac96bd9d6d70a699b25402de0a8b5b3a088c
|
7
|
+
data.tar.gz: 23829929b6886391e12f5b99aeb15ed7342c51e0c852e70033d876d31c25240bbbeca938044dce40e8c70c9dc9f73f1f07788c215a5145723024b761ba70b1b1
|
@@ -0,0 +1,162 @@
|
|
1
|
+
require 'json'
|
2
|
+
require 'open-uri'
|
3
|
+
require 'net/http'
|
4
|
+
require 'net/https'
|
5
|
+
require 'uri'
|
6
|
+
|
7
|
+
require 'rubygems'
|
8
|
+
require 'curb'
|
9
|
+
|
10
|
+
module PocketMath
|
11
|
+
module PocketMath::Advertiser
|
12
|
+
module PocketMath::Advertiser::V1
|
13
|
+
|
14
|
+
API_BASE_URL = "https://api.pocketmath.com"
|
15
|
+
|
16
|
+
def self.find_gps_list_id_by_name(name)
|
17
|
+
p "get_gps_list_id"
|
18
|
+
url = "#{API_BASE_URL}/v1/lists/gps/list.json?token=#{POCKETMATH_API_KEY}&limit=10000"
|
19
|
+
response = Net::HTTP.get_response(URI.parse(url))
|
20
|
+
data = response.body
|
21
|
+
obj = JSON.parse(data)
|
22
|
+
obj.each do |result|
|
23
|
+
return result["id"] if result["name"] == name
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.create_gps_list(name)
|
28
|
+
raise "name was nil" if name.nil?
|
29
|
+
raise "name was blank" if name.empty?
|
30
|
+
|
31
|
+
add_list_json = JSON::generate(
|
32
|
+
{
|
33
|
+
"token" => "#{POCKETMATH_API_KEY}",
|
34
|
+
"list" =>
|
35
|
+
{
|
36
|
+
"name" => "#{name}"
|
37
|
+
}
|
38
|
+
})
|
39
|
+
|
40
|
+
uri = URI.parse(API_BASE_URL)
|
41
|
+
response = nil
|
42
|
+
Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') do |http|
|
43
|
+
request = Net::HTTP::Post.new("/v1/lists/gps/add_list.json")
|
44
|
+
request.body = add_list_json
|
45
|
+
request.content_type = "application/json"
|
46
|
+
response = http.request(request)
|
47
|
+
end
|
48
|
+
if !response.nil?
|
49
|
+
data = response.body
|
50
|
+
obj = JSON::parse(data)
|
51
|
+
list_id = obj["id"]
|
52
|
+
return list_id
|
53
|
+
else
|
54
|
+
return nil
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
def self.upload_gps_list(id, coordinates = [])
|
59
|
+
raise "id was nil" if id.nil?
|
60
|
+
raise "coordinates was nil" if coordinates.nil?
|
61
|
+
raise "coordiantes was empty" if coordinates.empty?
|
62
|
+
|
63
|
+
c = Curl::Easy.new("#{API_BASE_URL}/v1/lists/gps/upload.json")
|
64
|
+
c.multipart_form_post = true
|
65
|
+
|
66
|
+
coordinates_string = ''
|
67
|
+
coordinates.each do |coord|
|
68
|
+
coordinates_string << "#{coord[:latitude]},#{coord[:longitude]}\r\n"
|
69
|
+
end
|
70
|
+
|
71
|
+
r = Random.new
|
72
|
+
remote_file_name = "pm-advert-client-gps-list-#{r.rand(2*1000*1000*1000)}-#{r.rand(2*1000*1000*1000)}.txt"
|
73
|
+
content_field = Curl::PostField.file('file', remote_file_name) do |field|
|
74
|
+
field.content = coordinates_string
|
75
|
+
end
|
76
|
+
|
77
|
+
success = nil
|
78
|
+
|
79
|
+
success = c.http_post(
|
80
|
+
content_field,
|
81
|
+
Curl::PostField.content('token', POCKETMATH_API_KEY),
|
82
|
+
Curl::PostField.content('mode', 'append'),
|
83
|
+
Curl::PostField.content('list_id', id.to_s )
|
84
|
+
)
|
85
|
+
|
86
|
+
return success
|
87
|
+
end
|
88
|
+
|
89
|
+
private
|
90
|
+
|
91
|
+
def self.deep_copy(o)
|
92
|
+
Marshal.load(Marshal.dump(o))
|
93
|
+
end
|
94
|
+
|
95
|
+
public
|
96
|
+
|
97
|
+
def self.create_insertion_order( opts =
|
98
|
+
{
|
99
|
+
"name" => nil,
|
100
|
+
"start_datetime" => Date.now,
|
101
|
+
"end_datetime" => Date.now + 7.days,
|
102
|
+
"creative_type_ids" => [],
|
103
|
+
"budget" => "1.00",
|
104
|
+
"iab_catgories" => [],
|
105
|
+
"bid_per_impression" => "2.00",
|
106
|
+
"top_level_domain" => "pocketmath.com",
|
107
|
+
"country" => "223",
|
108
|
+
"image_url" => "https://s3.amazonaws.com/pocketmath/pocketmath_320x50.jpg",
|
109
|
+
"landing_page_url" => "www.pocketmath.com",
|
110
|
+
"size_id" => "11"
|
111
|
+
})
|
112
|
+
|
113
|
+
o = deep_copy(opts)
|
114
|
+
|
115
|
+
if o["name"].nil?
|
116
|
+
raise "name was nil"
|
117
|
+
end
|
118
|
+
|
119
|
+
if o["iab_category_codes"].empty?
|
120
|
+
raise "no iab_category_codes specified"
|
121
|
+
end
|
122
|
+
|
123
|
+
if o["start_datetime"].is_a?(Date)
|
124
|
+
o["start_datetime"] = o["start_datetime"].strftime("%d %b %Y")
|
125
|
+
elsif ! o["start_datetime"].is_a?(String)
|
126
|
+
raise "unsupported type"
|
127
|
+
end
|
128
|
+
|
129
|
+
if o["end_datetime"].is_a?(Date)
|
130
|
+
o["end_datetime"] = o["end_datetime"].strftime("%d %b %Y")
|
131
|
+
elsif ! o["end_datetime"].is_a?(String)
|
132
|
+
raise "unsupported type"
|
133
|
+
end
|
134
|
+
|
135
|
+
obj = { "token" => POCKETMATH_API_KEY, "io" => o }
|
136
|
+
|
137
|
+
create_io_json = JSON.generate(obj)
|
138
|
+
|
139
|
+
uri = URI.parse(API_BASE_URL)
|
140
|
+
response = nil
|
141
|
+
Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') do |http|
|
142
|
+
request = Net::HTTP::Post.new("/v1/io/create.json")
|
143
|
+
request.body = create_io_json
|
144
|
+
request.content_type = "application/json"
|
145
|
+
response = http.request(request)
|
146
|
+
end
|
147
|
+
if !response.nil?
|
148
|
+
data = response.body
|
149
|
+
p response
|
150
|
+
p data
|
151
|
+
obj = JSON::parse(data)
|
152
|
+
io_id = obj["id"]
|
153
|
+
return io_id
|
154
|
+
else
|
155
|
+
return nil
|
156
|
+
end
|
157
|
+
|
158
|
+
end
|
159
|
+
|
160
|
+
end # module PocketMath::Advertiser::V1
|
161
|
+
end # module PocketMath::Advertiser
|
162
|
+
end # module PocketMath
|
@@ -0,0 +1,73 @@
|
|
1
|
+
require 'json'
|
2
|
+
require 'open-uri'
|
3
|
+
require 'net/http'
|
4
|
+
require 'uri'
|
5
|
+
|
6
|
+
GEOCODING_PROVIDER = :open # May also use :google (see Google Map API Terms of Service to determine licensing requirements)
|
7
|
+
|
8
|
+
module PocketMath
|
9
|
+
module PocketMath::Geocode
|
10
|
+
|
11
|
+
private
|
12
|
+
|
13
|
+
def self.get_gps_coordinates_open(location, max_results = 1)
|
14
|
+
url = "http://open.mapquestapi.com/geocoding/v1/address?key=#{MAPQUEST_API_KEY}&location=#{URI.encode(location)}&maxResults=#{max_results.to_s}&inFormat=kvp&outFormat=json&json=true"
|
15
|
+
response = Net::HTTP.get_response(URI.parse(url))
|
16
|
+
data = response.body
|
17
|
+
obj = JSON.parse(data)
|
18
|
+
p url
|
19
|
+
p obj
|
20
|
+
gps_coordinates = []
|
21
|
+
obj["results"].each do |result|
|
22
|
+
result["locations"].each do |location|
|
23
|
+
lat_lng = location["latLng"]
|
24
|
+
gps_coordinates << { :latitude => lat_lng["lat"], :longitude => lat_lng["lng"] }
|
25
|
+
end
|
26
|
+
end
|
27
|
+
return nil if gps_coordinates.empty?
|
28
|
+
return gps_coordinates
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
|
33
|
+
#
|
34
|
+
# Use of the Google Map API requires licensing from Google.
|
35
|
+
#
|
36
|
+
def self.get_gps_coordinates_google(location, max_results = 1)
|
37
|
+
url = "http://maps.googleapis.com/maps/api/geocode/json?address=#{URI.encode(location)}&sensor=true"
|
38
|
+
response = Net::HTTP.get_response(URI.parse(url))
|
39
|
+
data = response.body
|
40
|
+
obj = JSON.parse(data)
|
41
|
+
p url
|
42
|
+
p obj
|
43
|
+
gps_coordinates = []
|
44
|
+
n = 0
|
45
|
+
obj["results"].each do |result|
|
46
|
+
break if n >= max_results
|
47
|
+
geometry = result["geometry"]
|
48
|
+
raise "geometry was unavailable" if geometry.nil?
|
49
|
+
location = geometry["location"]
|
50
|
+
raise "location was unavailable" if location.nil?
|
51
|
+
lat = location["lat"]
|
52
|
+
lng = location["lng"]
|
53
|
+
gps_coordinates << { :latitude => lat, :longitude => lng }
|
54
|
+
n += 1
|
55
|
+
end
|
56
|
+
return nil if gps_coordinates.empty?
|
57
|
+
return gps_coordinates
|
58
|
+
end
|
59
|
+
|
60
|
+
public
|
61
|
+
|
62
|
+
def self.get_gps_coordinates(location, max_results = 1)
|
63
|
+
if GEOCODING_PROVIDER == :google
|
64
|
+
return get_gps_coordinates_google(location, max_results)
|
65
|
+
elsif GEOCODING_PROVIDER == :open
|
66
|
+
return get_gps_coordinates_open(location, max_results)
|
67
|
+
else
|
68
|
+
raise "map provider was not specified"
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
end
|
73
|
+
end
|
metadata
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: pocketmath-advertise
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1.pre.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Eric Tucker
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-01-05 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: curb
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
description: A client to connect to the PocketMath advertising API.
|
28
|
+
email: eric@pocketmath.com
|
29
|
+
executables: []
|
30
|
+
extensions: []
|
31
|
+
extra_rdoc_files: []
|
32
|
+
files:
|
33
|
+
- lib/pocketmath-advertise.rb
|
34
|
+
- lib/pocketmath-advertiser.rb
|
35
|
+
- lib/pocketmath-geocode.rb
|
36
|
+
homepage: http://github.com/pocketmath/pocketmath-advertise
|
37
|
+
licenses:
|
38
|
+
- Apache2
|
39
|
+
metadata: {}
|
40
|
+
post_install_message:
|
41
|
+
rdoc_options: []
|
42
|
+
require_paths:
|
43
|
+
- lib
|
44
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - '>='
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '0'
|
49
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - '>'
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: 1.3.1
|
54
|
+
requirements: []
|
55
|
+
rubyforge_project:
|
56
|
+
rubygems_version: 2.2.0
|
57
|
+
signing_key:
|
58
|
+
specification_version: 4
|
59
|
+
summary: PocketMath Advertiser Client
|
60
|
+
test_files: []
|