citygrid_api 0.0.14 → 0.0.15

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.14
1
+ 0.0.15
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.14"
8
+ s.version = "0.0.15"
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"]
12
- s.date = "2012-05-18"
12
+ s.date = "2012-06-25"
13
13
  s.description = "Ruby wrapper for CityGrid APIs"
14
14
  s.email = "fu7iin@gmail.com"
15
15
  s.extra_rdoc_files = [
data/lib/citygrid/api.rb CHANGED
@@ -128,24 +128,34 @@ class CityGrid
128
128
  end
129
129
  end
130
130
 
131
- def parse_multiple_responses response
132
- parsing = response.values.select{ |x| x.is_a? Array }.first
133
- if parsing.nil? || parsing == []
134
- #pp "Response was too hard to parse... letting it through..."
135
- return parsing
136
- elsif parsing != nil && parsing != []
137
- if parsing[0]["response"]
138
- parsing = [parsing[0]["response"]["code"], parsing[0]["response"]["message"]]
139
- return parsing
140
- else
141
- # this accomodates geocode response which does not contain a response node
142
- #pp "Response was too hard to parse... letting it through..."
143
- return nil
144
- end
131
+ def parse_response_status response_body
132
+ status = nil
133
+ if response_body["response"] || response_body["responseStatus"]
134
+ status = response_body["response"] || response_body["responseStatus"]
135
+ elsif response_body["errors"]
136
+ # make this throw some kind of content API error
137
+ errors = ""
138
+ response_body["errors"].each { |e| errors += "#{e["error"]} " }
139
+ status = { "code" => "CONTENT_API_ERROR", "message" => errors }
140
+ elsif response_body["code"] && response_body["message"]
141
+ status = response_body
145
142
  else
146
- # We should figure out a better way to do this
147
- raise CityGridExceptions::APIError.new "Received a JSON error code but it could not be parsed: #{response}"
143
+ response_body.each_value do |value|
144
+ case value
145
+ when Array
146
+ value.each do |inner_value|
147
+ if inner_value["response"] || inner_value["responseStatus"]
148
+ status = inner_value["response"] || inner_value["responseStatus"]
149
+ end
150
+ end
151
+ when Hash
152
+ if value["response"] || value["responseStatus"]
153
+ status = value["response"] || value["responseStatus"]
154
+ end
155
+ end
156
+ end
148
157
  end
158
+ return status
149
159
  end
150
160
 
151
161
  # Transform response into API::Response object
@@ -184,46 +194,23 @@ class CityGrid
184
194
  end
185
195
  end
186
196
 
187
-
197
+ response_status = parse_response_status response
198
+
188
199
  begin
189
200
  # catch unparsable responses (html etc)
190
201
  if !response.parsed_response.is_a?(Hash)
191
202
  #pp "[gem] the response was unparsable (response was not a hash)"
192
203
  raise CityGridExceptions::ResponseParseError.new req_for_airbrake, response
193
- # catch responses not in new response format
194
- elsif response["errors"]
195
- #pp "[gem] An error in the old response format was caught. Raising a general response error..."
196
- raise CityGridExceptions::ResponseError.new req_for_airbrake, response["errors"], response
197
-
198
- # Parse and handle new response codes
199
- elsif (response["response"] && response["response"]["code"] != "SUCCESS") &&
200
- (response["response"] && response["response"]["code"] != 200) &&
201
- (response["response"] && response["response"]["code"] != 400)
202
- error_code = response["response"]["code"]
203
- #pp "[gem] The response was contained in the first level of the response hash. Below:"
204
- #pp response
205
- #pp "found error code: #{error_code}"
206
- #pp "****************************************************************************"
207
- raise CityGridExceptions.appropriate_error(error_code).new req_for_airbrake, response, response["response"]["message"].to_s #+ " " + CityGridExceptions.print_superclasses(error_code)
208
- # if the response is a nested hash/nested hash containing arrays
209
- elsif response["totalNumEntries"] && response["response"].nil?
210
- #pp "[gem] now parsing a response with multiple entries: #{response}"
211
- error_code = parse_multiple_responses(response)
212
- #pp "the error code that came back is #{error_code}"
213
- if error_code.nil? || error_code == []
214
- #pp "[gem] passing over this for now"
215
- return CityGrid::API::Response.new response # pass over for now
216
- elsif error_code[0] == "SUCCESS" || error_code[0] == 200 || error_code[0] == 400
204
+ else
205
+ # Parse and handle new response codes
206
+ if !response_status.nil? && response_status["code"] != "SUCCESS" && response_status["code"] != 200
207
+ raise CityGridExceptions.appropriate_error(response_status["code"]).new req_for_airbrake, response, response_status["message"].to_s #+ " " + CityGridExceptions.print_superclasses(response_status["code"])
208
+ else
217
209
  return CityGrid::API::Response.new response
218
- else
219
- #pp "[gem] we found an error and it was #{error_code[1]}"
220
- raise CityGridExceptions.appropriate_error(error_code[0]).new req_for_airbrake, response, error_code[1].to_s + " "# + CityGridExceptions.print_superclasses(error_code[0])
221
210
  end
222
- else
223
- return CityGrid::API::Response.new response
224
211
  end
225
212
  rescue => ex
226
- pp "The gem threw an error: #{ex}"
213
+ pp "API ERROR: #{ex}"
227
214
  raise ex if CityGrid.raise_errors?
228
215
  end
229
216
  end
@@ -74,8 +74,10 @@ module CityGridExceptions
74
74
 
75
75
  # General Errors
76
76
  class SystemErrorTryAgainError < GeneralError; end
77
- class SystemErrorUnknownError < GeneralError; end
78
- class BadRequestTypeError < GeneralError; end
77
+ class SystemErrorUnknownError < GeneralError; end
78
+ class BadRequestTypeError < GeneralError; end
79
+ class InvalidRequestBodyError < GeneralError; end
80
+ class ContentApiError < GeneralError; end # this stands in for old format content api errors
79
81
 
80
82
 
81
83
 
@@ -95,19 +97,21 @@ module CityGridExceptions
95
97
 
96
98
  #Authorization Error
97
99
  class PermissionDeniedError < AuthorizationError; end
98
- class NoPermissionsError < AuthorizationError; end
100
+ class NoPermissionsError < AuthorizationError; end
99
101
 
100
102
  # Request Error
101
- class ParameterRequiredError < RequestError; end
102
- class ParameterRequiredConditionalError < RequestError; end
103
- class ParameterInvalidError < RequestError; end
104
- class ParameterFormatError < RequestError; end
105
- class ParameterNotSupportedError < RequestError; end
106
- class ParameterRangeTooLowError < RequestError; end
107
- class ParameterRangeTooHighError < RequestError; end
108
- class ParameterSizeLimitExceededError < RequestError; end
109
- class ParameterCannotBeZeroError < RequestError; end
110
- class ParameterOnlyOne < RequestError; end
103
+ class ParameterRequiredError < RequestError; end
104
+ class ParameterRequiredConditionalError < RequestError; end
105
+ class ParameterInvalidError < RequestError; end
106
+ class ParameterFormatError < RequestError; end
107
+ class ParameterNotSupportedError < RequestError; end
108
+ class ParameterRangeTooLowError < RequestError; end
109
+ class ParameterRangeTooHighError < RequestError; end
110
+ class ParameterSizeLimitExceededError < RequestError; end
111
+ class ParameterCannotBeZeroError < RequestError; end
112
+ class ParameterOnlyOneError < RequestError; end
113
+ class ParameterAssociationActionNotPerformedError < RequestError; end
114
+ class ParameterActionNotAllowedError < RequestError; end
111
115
 
112
116
  # Operator Error
113
117
  class OperatorInvalidError < OperatorError; end
@@ -137,38 +141,42 @@ module CityGridExceptions
137
141
  class MonthlyBudgetReachedError < SpecificDataError; end
138
142
  class QuotaExceededError < SpecificDataError; end
139
143
  class RateExceededError < SpecificDataError; end
144
+ class BatchLimitExceededError < SpecificDataError; end
140
145
 
141
146
  # unused errors
142
147
  #400 => RequestError,
143
148
  @possible_errors =
144
- {
145
- 0 => ResponseError, nil => ResponseParseError, "" => ResponseParseError,
146
- 401 => AuthenticationError, 403 => RequestError, 405 => RequestError, 406 => HeaderError,
147
- 500 => ResponseError, "SYSTEM_ERROR_TRY_AGAIN" => SystemErrorTryAgainError,
148
- "SYSTEM_ERROR_UNKNOWN" => SystemErrorUnknownError, "BAD_REQUEST_TYPE" => BadRequestTypeError,
149
- "HEADER_CONTENT_TYPE_IS_REQUIRED" => ContentTypeRequiredError, "HEADER_CONTENT_TYPE_INVALID" => ContentTypeInvalidError,
150
- "HEADER_ACCEPT_IS_REQUIRED" => AcceptRequiredError, "HEADER_ACCEPT_INVALID" => AcceptInvalidError,
151
- "AUTH_TOKEN_INVALID" => AuthTokenInvalidError, "AUTH_TOKEN_EXPIRED" => AuthTokenExpiredError,
152
- "AUTH_TOKEN_NONE" => AuthTokenNoneError,
153
- "USERNAME_IS_REQUIRED" => UsernameRequiredError, "PASSWORD_IS_REQUIRED" => PasswordRequiredError,
154
- "ACCOUNT_NOT_FOUND" => AccountNotFoundError, "PERMISSION_DENIED" => PermissionDeniedError,
155
- "NO_PERMISSIONS" => NoPermissionsError, "PARAMETER_REQUIRED" => ParameterRequiredError,
156
- "PARAMETER_REQUIRED_CONDITIONAL" => ParameterRequiredConditionalError, "PARAMETER_INVALID" => ParameterInvalidError,
157
- "PARAMETER_FORMAT" => ParameterFormatError, "PARAMETER_NOT_SUPPORTED" => ParameterNotSupportedError,
158
- "PARAMETER_RANGE_TOO_LOW" => ParameterRangeTooLowError, "PARAMETER_RANGE_TOO_HIGH" => ParameterRangeTooHighError,
159
- "PARAMETER_SIZE_LIMIT_EXCEEDED" => ParameterSizeLimitExceededError, "PARAMETER_CANNOT_BE_ZERO" => ParameterCannotBeZeroError,
160
- "PARAMETER_ONLY_ONE" => ParameterOnlyOne, "OPERATOR_INVALID" => OperatorInvalidError,
161
- "ENTITY_NOT_FOUND" => EntityNotFoundError, "ENTITY_EXISTS" => EntityExistsError,
162
- "ENTITY_LIMIT" => EntityLimitError, "ENTITY_ALREADY_IN_USE" => EntityAlreadyInUseError,
163
- "ENTITY_EXPIRED" => EntityExpiredError, "ENTITY_INACTIVE" => EntityInactiveError,
164
- "ENTITY_NOT_ELIGIBLE" => EntityNotEligibleError, "ENTITY_NOT_MODIFIED" => EntityNotModifiedError,
165
- "ENTITY_STATE_INVALID" => EntityStateInvalidError, "ENTITY_MISSING_DATA" => EntityMissingDataError,
166
- "DATA_NOT_FOUND" => DataNotFoundError, "ASSOCIATION_EXISTS" => AssociationExistsError,
167
- "NO_ASSOCIATION_EXISTS" => NoAssociationExistsError, "DUPLICATE" => DuplicateError,
168
- "DATE_BEFORE_DATE" => DateBeforeDateError, "REMOVE_NOT_ALLOWED" => RemoveNotAllowedError,
169
- "MOP_EXPIRED" => MopExpiredError, "ACCOUNT_INACTIVE" => AccountInactiveError,
170
- "ACCOUNT_DELINQUENT" => AccountDelinquentError, "MONTHLY_BUDGET_REACHED" => MonthlyBudgetReachedError,
171
- "QUOTA_EXCEEDED" => QuotaExceededError,"RATE_EXCEEDED" => RateExceededError
149
+ {
150
+ 0 => ResponseError, nil => ResponseParseError, "" => ResponseParseError, 400 => RequestError,
151
+ 401 => AuthenticationError, 403 => RequestError, 405 => RequestError, 406 => HeaderError,
152
+ 409 => RequestError, 410 => RequestError, 415 => RequestError, 413 => RequestError, 414 => RequestError,
153
+ 500 => ResponseError, "CONTENT_API_ERROR" => ContentApiError, "SYSTEM_ERROR_TRY_AGAIN" => SystemErrorTryAgainError,
154
+ "SYSTEM_ERROR_UNKNOWN" => SystemErrorUnknownError, "BAD_REQUEST_TYPE" => BadRequestTypeError,
155
+ "INVALID_REQUEST_BODY" => InvalidRequestBodyError,
156
+ "HEADER_CONTENT_TYPE_IS_REQUIRED" => ContentTypeRequiredError, "HEADER_CONTENT_TYPE_INVALID" => ContentTypeInvalidError,
157
+ "HEADER_ACCEPT_IS_REQUIRED" => AcceptRequiredError, "HEADER_ACCEPT_INVALID" => AcceptInvalidError,
158
+ "AUTH_TOKEN_INVALID" => AuthTokenInvalidError, "AUTH_TOKEN_EXPIRED" => AuthTokenExpiredError,
159
+ "AUTH_TOKEN_NONE" => AuthTokenNoneError,
160
+ "USERNAME_IS_REQUIRED" => UsernameRequiredError, "PASSWORD_IS_REQUIRED" => PasswordRequiredError,
161
+ "ACCOUNT_NOT_FOUND" => AccountNotFoundError, "PERMISSION_DENIED" => PermissionDeniedError,
162
+ "NO_PERMISSIONS" => NoPermissionsError, "PARAMETER_REQUIRED" => ParameterRequiredError,
163
+ "PARAMETER_REQUIRED_CONDITIONAL" => ParameterRequiredConditionalError, "PARAMETER_INVALID" => ParameterInvalidError,
164
+ "PARAMETER_FORMAT" => ParameterFormatError, "PARAMETER_NOT_SUPPORTED" => ParameterNotSupportedError,
165
+ "PARAMETER_RANGE_TOO_LOW" => ParameterRangeTooLowError, "PARAMETER_RANGE_TOO_HIGH" => ParameterRangeTooHighError,
166
+ "PARAMETER_SIZE_LIMIT_EXCEEDED" => ParameterSizeLimitExceededError, "PARAMETER_CANNOT_BE_ZERO" => ParameterCannotBeZeroError,
167
+ "PARAMETER_ONLY_ONE" => ParameterOnlyOneError, "PARAMETER_ASSOCIATION_ACTION_NOT_PERFORMED" => ParameterAssociationActionNotPerformedError,
168
+ "PARAMETER_ACTION_NOT_ALLOWED" => ParameterActionNotAllowedError, "OPERATOR_INVALID" => OperatorInvalidError,
169
+ "ENTITY_NOT_FOUND" => EntityNotFoundError, "ENTITY_EXISTS" => EntityExistsError,
170
+ "ENTITY_LIMIT" => EntityLimitError, "ENTITY_ALREADY_IN_USE" => EntityAlreadyInUseError,
171
+ "ENTITY_EXPIRED" => EntityExpiredError, "ENTITY_INACTIVE" => EntityInactiveError,
172
+ "ENTITY_NOT_ELIGIBLE" => EntityNotEligibleError, "ENTITY_NOT_MODIFIED" => EntityNotModifiedError,
173
+ "ENTITY_STATE_INVALID" => EntityStateInvalidError, "ENTITY_MISSING_DATA" => EntityMissingDataError,
174
+ "DATA_NOT_FOUND" => DataNotFoundError, "ASSOCIATION_EXISTS" => AssociationExistsError,
175
+ "NO_ASSOCIATION_EXISTS" => NoAssociationExistsError, "DUPLICATE" => DuplicateError,
176
+ "DATE_BEFORE_DATE" => DateBeforeDateError, "REMOVE_NOT_ALLOWED" => RemoveNotAllowedError,
177
+ "MOP_EXPIRED" => MopExpiredError, "ACCOUNT_INACTIVE" => AccountInactiveError,
178
+ "ACCOUNT_DELINQUENT" => AccountDelinquentError, "MONTHLY_BUDGET_REACHED" => MonthlyBudgetReachedError,
179
+ "QUOTA_EXCEEDED" => QuotaExceededError,"RATE_EXCEEDED" => RateExceededError, "BATCH_LIMIT_EXCEEDED" => BatchLimitExceededError
172
180
  }
173
181
 
174
182
  def CityGridExceptions.appropriate_error error_code
@@ -4,5 +4,5 @@ context "impersonate user" do
4
4
  setup do
5
5
  SessionHelper.gary_test.call_api CityGrid::API::Accounts::TempImpersonation, :impersonate, :customerId => 125902
6
6
  end
7
- should("have a different auth token"){ap topic.authToken}
7
+ should("have a different auth token"){pp topic.authToken}
8
8
  end
@@ -19,5 +19,5 @@ context "impersonate user" do
19
19
  setup do
20
20
  SessionHelper.gary_test.call_api CityGrid::API::Accounts::User, :impersonate, :customerId => 125902
21
21
  end
22
- should("have a different auth token"){ap topic.authToken}
22
+ should("have a different auth token"){pp topic.authToken}
23
23
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: citygrid_api
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.14
5
+ version: 0.0.15
6
6
  platform: ruby
7
7
  authors:
8
8
  - Elpizo Choi
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2012-05-18 00:00:00 Z
13
+ date: 2012-06-25 00:00:00 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: httparty