Skip to content

Commit

Permalink
refactor client requests
Browse files Browse the repository at this point in the history
  • Loading branch information
jkvoorhis committed Jun 22, 2015
1 parent 4c60628 commit 6d1fbde
Showing 1 changed file with 22 additions and 46 deletions.
68 changes: 22 additions & 46 deletions lib/urbanairship/client.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
require 'net/http'
require 'uri'
require 'unirest'

module Urbanairship
Expand All @@ -14,54 +12,33 @@ def initialize(key:, secret:)

def send_request(method:, body:, url:,
content_type: nil, version: nil, params: nil)
case method
req_type = case method
when 'GET'
response = Unirest.get(
url,
headers:{
"Content-type" => content_type,
"Accept" => "application/vnd.urbanairship+json; version=" + version.to_s
},
auth:{:user=>@key, :password=>@secret},
parameters: body
)
return {'body'=>response.body, 'code'=>response.code}
:get
when 'POST'
response = Unirest.post(
url,
headers:{
"Content-type" => content_type,
"Accept" => "application/vnd.urbanairship+json; version=" + version.to_s
},
auth:{:user=>@key, :password=>@secret},
parameters: body
)
return {'body'=>response.body, 'code'=>response.code}
when 'PUT'
response = Unirest.put(
url,
headers:{
"Content-type" => content_type,
"Accept" => "application/vnd.urbanairship+json; version=" + version.to_s
},
auth:{:user=>@key, :password=>@secret},
parameters: body
)
return {'body'=>response.body, 'code'=>response.code}
:post
when "PUT"
:put
when 'DELETE'
response = Unirest.delete(
url,
headers:{
"Content-type" => content_type,
"Accept" => "application/vnd.urbanairship+json; version=" + version.to_s
},
auth:{:user=>@key, :password=>@secret},
parameters: body
)
return {'body'=>nil, 'code'=>response.code}
:delete
else
fail 'Method was not "GET" "POST" "PUT" or "DELETE"'
end

response = Unirest.method(req_type).call(
url,
headers:{
"Content-type" => content_type,
"Accept" => "application/vnd.urbanairship+json; version=" + version.to_s
},
auth:{
:user=>@key,
:password=>@secret
},
parameters: body
)

{'body'=>response.body, 'code'=>response.code}
end

def create_push
Expand All @@ -71,6 +48,5 @@ def create_push
def create_scheduled_push
Push::ScheduledPush.new(self)
end

end
end
end

0 comments on commit 6d1fbde

Please sign in to comment.