forked from ruckus/quickbooks-ruby
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
381 additions
and
174 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
module Quickbooks | ||
module Model | ||
class BaseModelJSON | ||
include Definition | ||
include ActiveModel::Validations | ||
include Validator | ||
include ROXML | ||
|
||
xml_convention :camelcase | ||
|
||
def initialize(attributes={}) | ||
attributes.each {|key, value| public_send("#{key}=", value) } | ||
end | ||
|
||
def to_json | ||
params = {} | ||
attributes.each_pair do |k, v| | ||
next if v.blank? | ||
params[k.camelize] = v.is_a?(Array) ? v.inject([]){|mem, item| mem << item.to_json; mem} : v | ||
end | ||
params.to_json | ||
end | ||
|
||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
module Quickbooks | ||
module Service | ||
class BaseServiceJSON < BaseService | ||
include ServiceCrudJSON | ||
HTTP_CONTENT_TYPE = 'application/json' | ||
HTTP_ACCEPT = 'application/json' | ||
attr_reader :last_response_json | ||
|
||
def url_for_resource(resource) | ||
"#{url_for_base}/#{resource}" | ||
end | ||
|
||
def url_for_base | ||
raise MissingRealmError.new unless @company_id | ||
"#{@base_uri}/#{@company_id}" | ||
end | ||
|
||
def default_model_query | ||
"SELECT * FROM #{self.class.name.split("::").last}" | ||
end | ||
|
||
def url_for_query(query = nil, start_position = 1, max_results = 20) | ||
query ||= default_model_query | ||
query = "#{query} STARTPOSITION #{start_position} MAXRESULTS #{max_results}" | ||
|
||
"#{url_for_base}/query?query=#{URI.encode_www_form_component(query)}" | ||
end | ||
|
||
private | ||
|
||
def parse_json(json) | ||
@last_response_json = json | ||
end | ||
|
||
def do_http(method, url, body, headers) # throws IntuitRequestException | ||
if @oauth.nil? | ||
raise "OAuth client has not been initialized. Initialize with setter access_token=" | ||
end | ||
unless headers.has_key?('Content-Type') | ||
headers['Content-Type'] = HTTP_CONTENT_TYPE | ||
end | ||
unless headers.has_key?('Accept') | ||
headers['Accept'] = HTTP_ACCEPT | ||
end | ||
unless headers.has_key?('Accept-Encoding') | ||
headers['Accept-Encoding'] = HTTP_ACCEPT_ENCODING | ||
end | ||
|
||
log "------ QUICKBOOKS-RUBY REQUEST ------" | ||
log "METHOD = #{method}" | ||
log "RESOURCE = #{url}" | ||
log "REQUEST BODY:" | ||
log(log_xml(body)) | ||
log "REQUEST HEADERS = #{headers.inspect}" | ||
|
||
response = case method | ||
when :get | ||
@oauth.get(url, headers) | ||
when :post | ||
@oauth.post(url, body, headers) | ||
when :upload | ||
@oauth.post_with_multipart(url, body, headers) | ||
else | ||
raise "Do not know how to perform that HTTP operation" | ||
end | ||
check_response(response, :request_xml => body) | ||
end | ||
|
||
def add_query_string_to_url(url, params) | ||
if params.is_a?(Hash) && !params.empty? | ||
url + "?" + params.collect { |k| "#{k.first}=#{k.last}" }.join("&") | ||
else | ||
url | ||
end | ||
end | ||
|
||
def check_response(response, options = {}) | ||
log "------ QUICKBOOKS-RUBY RESPONSE ------" | ||
log "RESPONSE CODE = #{response.code}" | ||
log "RESPONSE BODY:" | ||
log(log_xml(response.plain_body)) | ||
parse_xml(response.plain_body) | ||
status = response.code.to_i | ||
case status | ||
when 200 | ||
# even HTTP 200 can contain an error, so we always have to peek for an Error | ||
if response_is_error? | ||
parse_and_raise_exception(options) | ||
else | ||
response | ||
end | ||
when 302 | ||
raise "Unhandled HTTP Redirect" | ||
when 401 | ||
raise Quickbooks::AuthorizationFailure | ||
when 403 | ||
raise Quickbooks::Forbidden | ||
when 400, 500 | ||
parse_and_raise_exception(options) | ||
when 503, 504 | ||
raise Quickbooks::ServiceUnavailable | ||
else | ||
raise "HTTP Error Code: #{status}, Msg: #{response.plain_body}" | ||
end | ||
end | ||
|
||
def parse_and_raise_exception(options = {}) | ||
err = parse_intuit_error | ||
ex = Quickbooks::IntuitRequestException.new("#{err[:message]}:\n\t#{err[:detail]}") | ||
ex.code = err[:code] | ||
ex.detail = err[:detail] | ||
ex.type = err[:type] | ||
ex.request_xml = options[:request_xml] | ||
raise ex | ||
end | ||
|
||
def response_is_error? | ||
@last_response_json['Fault'].present? | ||
end | ||
|
||
def parse_intuit_error | ||
error = {:message => "", :detail => "", :type => nil, :code => 0} | ||
fault = @last_response_json['Fault'] | ||
if fault.present? | ||
error[:type] = fault['type'] if fault.has_key?('type') | ||
if fault_error = fault['Error'].first | ||
error[:message] = fault_error['Message'] | ||
error[:detail] = fault_error['Detail'] | ||
error[:code] = fault_error['code'] | ||
end | ||
end | ||
error | ||
rescue Exception => exception | ||
error[:detail] = @last_response_json.to_s | ||
error | ||
end | ||
|
||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
module Quickbooks | ||
module Service | ||
module ServiceCrudJSON | ||
|
||
def create(entity, options = {}) | ||
raise Quickbooks::InvalidModelException.new(entity.errors.full_messages.join(',')) unless entity.valid? | ||
response = do_http(:post, url_for_resource, entity.to_json, options) | ||
if response.code.to_i == 200 | ||
response.plain_body | ||
else | ||
nil | ||
end | ||
end | ||
|
||
end | ||
end | ||
end | ||
|
Oops, something went wrong.