citygrid_api 0.0.9 → 0.0.10

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.9
1
+ 0.0.10
data/citygrid_api.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "citygrid_api"
8
- s.version = "0.0.9"
8
+ s.version = "0.0.10"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Elpizo Choi", "Joseph Chen"]
12
- s.date = "2012-03-01"
12
+ s.date = "2012-03-08"
13
13
  s.description = "Ruby wrapper for CityGrid APIs"
14
14
  s.email = "Joseph.Chen@citygridmedia.com"
15
15
  s.extra_rdoc_files = [
@@ -80,6 +80,7 @@ Gem::Specification.new do |s|
80
80
  "lib/citygrid/api/accounts/account.rb",
81
81
  "lib/citygrid/api/accounts/authentication.rb",
82
82
  "lib/citygrid/api/accounts/method_of_payment.rb",
83
+ "lib/citygrid/api/accounts/temp_impersonation.rb",
83
84
  "lib/citygrid/api/accounts/user.rb",
84
85
  "lib/citygrid/api/advertising.rb",
85
86
  "lib/citygrid/api/advertising/account_manager.rb",
@@ -121,6 +122,7 @@ Gem::Specification.new do |s|
121
122
  "public/javascript/dashboard.js",
122
123
  "test/api/accounts/test_account.rb",
123
124
  "test/api/accounts/test_method_of_payment.rb",
125
+ "test/api/accounts/test_temp_impersonation.rb",
124
126
  "test/api/accounts/test_user.rb",
125
127
  "test/api/advertising/test_account_manager.rb",
126
128
  "test/api/advertising/test_ad_group.rb",
@@ -12,13 +12,16 @@ api:
12
12
  accounts:
13
13
  account:
14
14
  ssl: true
15
- endpoint: accounts/account/v2
15
+ endpoint: accounts/account/v2
16
16
  method_of_payment:
17
17
  ssl: true
18
18
  endpoint: accounts/mop/v2
19
19
  user:
20
20
  ssl: true
21
21
  endpoint: accounts/user/v2
22
+ temp_impersonation:
23
+ endpoint: accounts/user/v2
24
+ hostname: qa-prdmirror.citygrid.com
22
25
  advertising:
23
26
  account_manager:
24
27
  ssl: true
@@ -0,0 +1,9 @@
1
+ class CityGrid
2
+ class API
3
+ class Accounts
4
+ class TempImpersonation < Accounts
5
+ define_action :impersonate, :get, "impersonate", :auth_token => true, :format => false
6
+ end
7
+ end
8
+ end
9
+ end
data/lib/citygrid/api.rb CHANGED
@@ -85,24 +85,50 @@ class CityGrid
85
85
  "options" => Net::HTTP::Options
86
86
  }
87
87
 
88
- def strip_unsafe_params options
89
- puts "OPTIONS: #{options}"
88
+ def strip_unsafe_params method, options
89
+ safe_options = options.dup
90
+ to_merge = {}
90
91
  unsafe_params = {
91
- :password => "[FILTERED]", :securityCode => "[FILTERED]",
92
- :cardNumber => "[FILTERED]", :expirationMonth => "[FILTERED]",
93
- :expirationYear => "[FILTERED]"
92
+ :password => "FILTERED", "securityCode" => "FILTERED",
93
+ "cardNumber" => "FILTERED", "expirationMonth" => "FILTERED",
94
+ "expirationYear" => "FILTERED"
94
95
  }
95
- if !options[:query].nil?
96
- to_merge[:query] = options[:query].merge(unsafe_params.select { |k| options.keys.include? k })
97
- return options.merge(to_merge)
96
+ # If the parameters are contained in the :body node of the options hash
97
+ if options[:body] && !options[:body].nil?
98
+ # Convert the inner JSON before doing any work
99
+ safe_options[:body] = JSON.parse(safe_options[:body])
100
+
101
+ # This handles the mop query format
102
+ if safe_options[:body]["mutateOperationListResource"] &&
103
+ safe_options[:body]["mutateOperationListResource"][0] &&
104
+ safe_options[:body]["mutateOperationListResource"][0]["operand"]
105
+ # All the sensitive fields are located in this path
106
+ target_hash = safe_options[:body]["mutateOperationListResource"][0]["operand"]
107
+ # Strip any elements from the unsafe params hash that aren't in options and merge
108
+ to_merge = target_hash.merge(unsafe_params.select { |k| target_hash.keys.include?(k) })
109
+ # Merge the clean operand hash back into the body
110
+ safe_options[:body]["mutateOperationListResource"][0]["operand"].merge!(to_merge)
111
+ # Convert the body key back to json before returning
112
+ safe_options[:body] = safe_options[:body].to_json
113
+ return safe_options
114
+ # Any other format with :body node
115
+ else
116
+ to_merge = safe_options[:body].merge(unsafe_params.select { |k| safe_options[:body].keys.include? k })
117
+ return safe_options.merge({ :body => to_merge })
118
+ end
119
+ # If the parameters are contained in the :query node of the options hash
120
+ elsif options[:query] && !options[:query].nil?
121
+ to_merge = safe_options[:query].merge(unsafe_params.select { |k| safe_options[:query].keys.include? k })
122
+ return safe_options.merge({ :query => to_merge })
123
+ else
124
+ return options
98
125
  end
99
126
  end
100
127
 
101
- #
102
128
  def parse_multiple_responses response
103
129
  parsing = response.values.select{ |x| x.is_a? Array }.first
104
130
  if parsing.nil? || parsing == []
105
- ap "Response was too hard to parse... letting it through..."
131
+ #ap "Response was too hard to parse... letting it through..."
106
132
  return parsing
107
133
  elsif parsing != nil && parsing != []
108
134
  if parsing[0]["response"]
@@ -110,7 +136,7 @@ class CityGrid
110
136
  return parsing
111
137
  else
112
138
  # this accomodates geocode response which does not contain a response node
113
- ap "Response was too hard to parse... letting it through..."
139
+ #ap "Response was too hard to parse... letting it through..."
114
140
  return nil
115
141
  end
116
142
  else
@@ -133,16 +159,12 @@ class CityGrid
133
159
  raise ConfigurationError.new "No endpoint defined" if !path || path.empty?
134
160
  raise ConfigurationError.new "No hostname defined" if !req_options[:base_uri] || req_options[:base_uri].empty?
135
161
 
136
- # prepare request and sanitized request for logs
137
- #puts "Options after strip unsafe: #{strip_unsafe_params(req_options)}"
138
- #puts "options before that: #{req_options}"
139
- #safe_req_options = strip_unsafe_params(req_options)
162
+ # prepare request
140
163
  req = HTTParty::Request.new http_method, path, req_options
141
- #req_to_output = HTTParty::Request.new http_method, path, safe_req_options
142
- # ap "HERE ARE THE INGREDIENTS OF THE REQUEST:"
143
- # ap "http_method is: #{http_method}"
144
- # ap "path is: #{path}"
145
- # ap "req_options is: #{req_options}"
164
+
165
+ # Sanitized request for logs
166
+ safe_req_options = strip_unsafe_params(http_method, req_options)
167
+ req_to_output = HTTParty::Request.new http_method, path, safe_req_options
146
168
 
147
169
  begin
148
170
  response = req.perform
@@ -150,21 +172,20 @@ class CityGrid
150
172
  raise CityGridExceptions::RequestError.new req, ex
151
173
  ensure
152
174
  if defined?(Rails.logger)
153
- Rails.logger.info req.to_curl
175
+ Rails.logger.info req_to_output.to_curl
154
176
  else
155
- puts req.to_curl
156
- ap response
177
+ puts req_to_output.to_curl
157
178
  end
158
179
  end
159
180
 
160
181
  begin
161
182
  # catch unparsable responses (html etc)
162
183
  if !response.parsed_response.is_a?(Hash)
163
- ap "[gem] the response was unparsable (response was not a hash)"
184
+ #ap "[gem] the response was unparsable (response was not a hash)"
164
185
  raise CityGridExceptions::ResponseParseError.new req, response
165
186
  # catch responses not in new response format
166
187
  elsif response["errors"]
167
- ap "[gem] An error in the old response format was caught. Raising a general response error..."
188
+ #ap "[gem] An error in the old response format was caught. Raising a general response error..."
168
189
  raise CityGridExceptions::ResponseError.new req, response["errors"], response
169
190
 
170
191
  # Parse and handle new response codes
@@ -172,23 +193,23 @@ class CityGrid
172
193
  (response["response"] && response["response"]["code"] != 200) &&
173
194
  (response["response"] && response["response"]["code"] != 400)
174
195
  error_code = response["response"]["code"]
175
- ap "[gem] The response was contained in the first level of the response hash. Below:"
176
- ap response
177
- ap "found error code: #{error_code}"
178
- ap "****************************************************************************"
196
+ #ap "[gem] The response was contained in the first level of the response hash. Below:"
197
+ #ap response
198
+ #ap "found error code: #{error_code}"
199
+ #ap "****************************************************************************"
179
200
  raise CityGridExceptions.appropriate_error(error_code).new req, response, response["response"]["message"].to_s #+ " " + CityGridExceptions.print_superclasses(error_code)
180
201
  # if the response is a nested hash/nested hash containing arrays
181
202
  elsif response["totalNumEntries"] && response["response"].nil?
182
- ap "[gem] now parsing a response with multiple entries: #{response}"
203
+ #ap "[gem] now parsing a response with multiple entries: #{response}"
183
204
  error_code = parse_multiple_responses(response)
184
- ap "the error code that came back is #{error_code}"
205
+ #ap "the error code that came back is #{error_code}"
185
206
  if error_code.nil? || error_code == []
186
- ap "[gem] passing over this for now"
207
+ #ap "[gem] passing over this for now"
187
208
  return CityGrid::API::Response.new response # pass over for now
188
209
  elsif error_code[0] == "SUCCESS" || error_code[0] == 200 || error_code[0] == 400
189
210
  return CityGrid::API::Response.new response
190
211
  else
191
- ap "[gem] we found an error and it was #{error_code[1]}"
212
+ #ap "[gem] we found an error and it was #{error_code[1]}"
192
213
  raise CityGridExceptions.appropriate_error(error_code[0]).new req, response, error_code[1].to_s + " "# + CityGridExceptions.print_superclasses(error_code[0])
193
214
  end
194
215
  else
@@ -0,0 +1,8 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'helper'))
2
+
3
+ context "impersonate user" do
4
+ setup do
5
+ SessionHelper.gary_test.call_api CityGrid::API::Accounts::TempImpersonation, :impersonate, :customerId => 125902
6
+ end
7
+ should("have a different auth token"){ap topic.authToken}
8
+ end
@@ -13,4 +13,11 @@ context "user log in with session" do
13
13
  end
14
14
  should("have an auth_token"){ topic.auth_token }
15
15
  should("be logged in"){ topic.logged_in? }
16
+ end
17
+
18
+ context "impersonate user" do
19
+ setup do
20
+ SessionHelper.gary_test.call_api CityGrid::API::Accounts::User, :impersonate, :customerId => 125902
21
+ end
22
+ should("have a different auth token"){ap topic.authToken}
16
23
  end
data/test/helper.rb CHANGED
@@ -21,7 +21,7 @@ unless defined? IN_DASHBOARD
21
21
  require "publisher_helper"
22
22
 
23
23
  # load default config
24
- CityGrid.load_config File.expand_path(File.join(File.dirname(__FILE__), '..', 'citygrid_api.yml.sample'))
24
+ CityGrid.load_config File.expand_path(File.join(File.dirname(__FILE__), '..', 'citygrid_api.yml'))
25
25
 
26
26
  # CityGrid.load_config File.expand_path(File.join(File.dirname(__FILE__), '..', 'citygrid_api.yml.sandbox'))
27
27
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: citygrid_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.9
4
+ version: 0.0.10
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,11 +10,11 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2012-03-01 00:00:00.000000000Z
13
+ date: 2012-03-08 00:00:00.000000000Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: httparty
17
- requirement: &70321245070140 !ruby/object:Gem::Requirement
17
+ requirement: &70187366267200 !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ~>
@@ -22,10 +22,10 @@ dependencies:
22
22
  version: 0.8.1
23
23
  type: :runtime
24
24
  prerelease: false
25
- version_requirements: *70321245070140
25
+ version_requirements: *70187366267200
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: json
28
- requirement: &70321245069540 !ruby/object:Gem::Requirement
28
+ requirement: &70187366266180 !ruby/object:Gem::Requirement
29
29
  none: false
30
30
  requirements:
31
31
  - - =
@@ -33,10 +33,10 @@ dependencies:
33
33
  version: 1.5.3
34
34
  type: :runtime
35
35
  prerelease: false
36
- version_requirements: *70321245069540
36
+ version_requirements: *70187366266180
37
37
  - !ruby/object:Gem::Dependency
38
38
  name: riot
39
- requirement: &70321245068820 !ruby/object:Gem::Requirement
39
+ requirement: &70187366264980 !ruby/object:Gem::Requirement
40
40
  none: false
41
41
  requirements:
42
42
  - - ~>
@@ -44,10 +44,10 @@ dependencies:
44
44
  version: 0.12.4
45
45
  type: :runtime
46
46
  prerelease: false
47
- version_requirements: *70321245068820
47
+ version_requirements: *70187366264980
48
48
  - !ruby/object:Gem::Dependency
49
49
  name: awesome_print
50
- requirement: &70321245068080 !ruby/object:Gem::Requirement
50
+ requirement: &70187366263960 !ruby/object:Gem::Requirement
51
51
  none: false
52
52
  requirements:
53
53
  - - ~>
@@ -55,10 +55,10 @@ dependencies:
55
55
  version: 0.4.0
56
56
  type: :runtime
57
57
  prerelease: false
58
- version_requirements: *70321245068080
58
+ version_requirements: *70187366263960
59
59
  - !ruby/object:Gem::Dependency
60
60
  name: bundler
61
- requirement: &70321245067160 !ruby/object:Gem::Requirement
61
+ requirement: &70187366262020 !ruby/object:Gem::Requirement
62
62
  none: false
63
63
  requirements:
64
64
  - - ~>
@@ -66,10 +66,10 @@ dependencies:
66
66
  version: 1.0.0
67
67
  type: :development
68
68
  prerelease: false
69
- version_requirements: *70321245067160
69
+ version_requirements: *70187366262020
70
70
  - !ruby/object:Gem::Dependency
71
71
  name: jeweler
72
- requirement: &70321245063220 !ruby/object:Gem::Requirement
72
+ requirement: &70187366260260 !ruby/object:Gem::Requirement
73
73
  none: false
74
74
  requirements:
75
75
  - - ~>
@@ -77,10 +77,10 @@ dependencies:
77
77
  version: 1.6.2
78
78
  type: :development
79
79
  prerelease: false
80
- version_requirements: *70321245063220
80
+ version_requirements: *70187366260260
81
81
  - !ruby/object:Gem::Dependency
82
82
  name: rcov
83
- requirement: &70321245062700 !ruby/object:Gem::Requirement
83
+ requirement: &70187366259520 !ruby/object:Gem::Requirement
84
84
  none: false
85
85
  requirements:
86
86
  - - ! '>='
@@ -88,10 +88,10 @@ dependencies:
88
88
  version: '0'
89
89
  type: :development
90
90
  prerelease: false
91
- version_requirements: *70321245062700
91
+ version_requirements: *70187366259520
92
92
  - !ruby/object:Gem::Dependency
93
93
  name: vcr
94
- requirement: &70321245061660 !ruby/object:Gem::Requirement
94
+ requirement: &70187366248660 !ruby/object:Gem::Requirement
95
95
  none: false
96
96
  requirements:
97
97
  - - ! '>='
@@ -99,10 +99,10 @@ dependencies:
99
99
  version: '0'
100
100
  type: :development
101
101
  prerelease: false
102
- version_requirements: *70321245061660
102
+ version_requirements: *70187366248660
103
103
  - !ruby/object:Gem::Dependency
104
104
  name: webmock
105
- requirement: &70321245059820 !ruby/object:Gem::Requirement
105
+ requirement: &70187366247360 !ruby/object:Gem::Requirement
106
106
  none: false
107
107
  requirements:
108
108
  - - ! '>='
@@ -110,7 +110,7 @@ dependencies:
110
110
  version: '0'
111
111
  type: :development
112
112
  prerelease: false
113
- version_requirements: *70321245059820
113
+ version_requirements: *70187366247360
114
114
  description: Ruby wrapper for CityGrid APIs
115
115
  email: Joseph.Chen@citygridmedia.com
116
116
  executables: []
@@ -182,6 +182,7 @@ files:
182
182
  - lib/citygrid/api/accounts/account.rb
183
183
  - lib/citygrid/api/accounts/authentication.rb
184
184
  - lib/citygrid/api/accounts/method_of_payment.rb
185
+ - lib/citygrid/api/accounts/temp_impersonation.rb
185
186
  - lib/citygrid/api/accounts/user.rb
186
187
  - lib/citygrid/api/advertising.rb
187
188
  - lib/citygrid/api/advertising/account_manager.rb
@@ -223,6 +224,7 @@ files:
223
224
  - public/javascript/dashboard.js
224
225
  - test/api/accounts/test_account.rb
225
226
  - test/api/accounts/test_method_of_payment.rb
227
+ - test/api/accounts/test_temp_impersonation.rb
226
228
  - test/api/accounts/test_user.rb
227
229
  - test/api/advertising/test_account_manager.rb
228
230
  - test/api/advertising/test_ad_group.rb