Skip to content

Commit

Permalink
DRYing and simplifying
Browse files Browse the repository at this point in the history
  • Loading branch information
Nu-hin committed Mar 8, 2014
1 parent ec18960 commit 94795ed
Showing 1 changed file with 21 additions and 26 deletions.
47 changes: 21 additions & 26 deletions lib/pickpoint_api/session.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,7 @@ def make_label(invoice_id)
return nil
end

if invoice_id.kind_of?(Array)
data = invoice_id
elsif
data = [invoice_id]
end

data = make_invoice_ids(invoice_id)
data = attach_session_id(data,'Invoices')
response = execute_action(:make_label, data)

Expand All @@ -111,12 +106,7 @@ def make_zlabel(invoice_id)
return nil
end

if invoice_id.kind_of?(Array)
data = invoice_id
elsif
data = [invoice_id]
end

data = make_invoice_ids(invoice_id)
data = attach_session_id(data,'Invoices')
response = execute_action(:make_zlabel, data)

Expand All @@ -134,12 +124,7 @@ def make_reestr(invoice_id)
return nil
end

if invoice_id.kind_of?(Array)
data = invoice_id
elsif
data = [invoice_id]
end

data = make_invoice_ids(invoice_id)
data = attach_session_id(data,'Invoices')
response = execute_action(:make_reestr, data)

Expand Down Expand Up @@ -206,6 +191,14 @@ def get_invoices_change_state(state, date_from = nil, date_to = DateTime.now)

private

def make_invoice_ids(invoice_id)
if invoice_id.kind_of?(Array)
invoice_id
elsif
[invoice_id]
end
end

def api_path
if @test
::PickpointApi::Constants::API_TEST_PATH
Expand Down Expand Up @@ -250,22 +243,24 @@ def execute_action(action, data = {})

req.body = data.to_json
response = send_request(req)
log_response(response)
response.body
end

def send_request(req)
::Net::HTTP.start(::PickpointApi::Constants::API_HOST, ::PickpointApi::Constants::API_PORT) do |http|
http.request(req)
end
end

def log_response(response)
if !response.body.nil?
if response.body.start_with?('%PDF')
::PickpointApi.logger.debug("Response: #{response.code}; data: PDF")
end
::PickpointApi.logger.debug("Response: #{response.code}; data: #{response.body}")
else
end

response.body
end

def send_request(req)
::Net::HTTP.start(::PickpointApi::Constants::API_HOST, ::PickpointApi::Constants::API_PORT) do |http|
http.request(req)
end
end

end

0 comments on commit 94795ed

Please sign in to comment.