Class: Urbanairship::Client
- Inherits:
-
Object
- Object
- Urbanairship::Client
- Defined in:
- lib/urbanairship/client.rb
Constant Summary
Constants included from Common
Urbanairship::Common::CONTENT_TYPE
Instance Attribute Summary collapse
-
#key ⇒ Object
Returns the value of attribute key.
-
#secret ⇒ Object
Returns the value of attribute secret.
-
#token ⇒ Object
Returns the value of attribute token.
Class Method Summary collapse
-
.build_response(response) ⇒ Hash
Build a hash from the response object.
Instance Method Summary collapse
-
#create_push ⇒ Object
Create a Push Object.
-
#create_scheduled_push ⇒ Object
Create a Scheduled Push Object.
-
#initialize(key: required('key'), secret: nil, server: Urbanairship.configuration.server, token: nil, oauth: nil) ⇒ Object
constructor
Initialize the Client.
-
#send_request(method: required('method'), path: nil, url: nil, body: nil, content_type: nil, encoding: nil, auth_type: :basic) ⇒ Object
Send a request to Airship’s API.
Methods included from Loggable
create_logger, logger, #logger
Methods included from Common
#apid_path, #channel_path, #compact_helper, #create_and_send_path, #custom_events_path, #device_token_path, #experiments_path, #lists_path, #named_users_path, #open_channel_path, #pipelines_path, #push_path, #reports_path, #required, #schedules_path, #segments_path, #tag_lists_path, #try_helper
Constructor Details
#initialize(key: required('key'), secret: nil, server: Urbanairship.configuration.server, token: nil, oauth: nil) ⇒ Object
Initialize the Client
22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/urbanairship/client.rb', line 22 def initialize(key: required('key'), secret: nil, server: Urbanairship.configuration.server, token: nil, oauth: nil) @key = key @secret = secret @server = server @token = token @oauth = oauth if @oauth != nil && @token != nil raise ArgumentError.new("oauth and token can't both be used at the same time.") end end |
Instance Attribute Details
#key ⇒ Object
Returns the value of attribute key.
9 10 11 |
# File 'lib/urbanairship/client.rb', line 9 def key @key end |
#secret ⇒ Object
Returns the value of attribute secret.
9 10 11 |
# File 'lib/urbanairship/client.rb', line 9 def secret @secret end |
#token ⇒ Object
Returns the value of attribute token.
9 10 11 |
# File 'lib/urbanairship/client.rb', line 9 def token @token end |
Class Method Details
.build_response(response) ⇒ Hash
Build a hash from the response object
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/urbanairship/client.rb', line 135 def self.build_response(response) response_hash = {'code'=>response.code.to_s, 'headers'=>response.headers} begin body = JSON.parse(response.body) rescue JSON::ParserError if response.body.nil? || response.body.empty? body = {} else body = response.body response_hash['error'] = 'could not parse response JSON' end end response_hash['body'] = body response_hash end |
Instance Method Details
#create_push ⇒ Object
Create a Push Object
121 122 123 |
# File 'lib/urbanairship/client.rb', line 121 def create_push Push::Push.new(self) end |
#create_scheduled_push ⇒ Object
Create a Scheduled Push Object
128 129 130 |
# File 'lib/urbanairship/client.rb', line 128 def create_scheduled_push Push::ScheduledPush.new(self) end |
#send_request(method: required('method'), path: nil, url: nil, body: nil, content_type: nil, encoding: nil, auth_type: :basic) ⇒ Object
Send a request to Airship’s API
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/urbanairship/client.rb', line 44 def send_request(method: required('method'), path: nil, url: nil, body: nil, content_type: nil, encoding: nil, auth_type: :basic) req_type = case method when 'GET' :get when 'POST' :post when 'PUT' :put when 'DELETE' :delete else fail 'Method was not "GET" "POST" "PUT" or "DELETE"' end raise ArgumentError.new("path and url can't be both nil") if path.nil? && url.nil? headers = {'User-Agent' => 'UARubyLib/' + Urbanairship::VERSION + ' ' + @key} headers['Accept'] = 'application/vnd.urbanairship+json; version=3' headers['Content-Type'] = content_type unless content_type.nil? headers['Content-Encoding'] = encoding unless encoding.nil? unless @oauth.nil? begin @token = @oauth.get_token rescue RestClient::Exception => e new_error = RestClient::Exception.new(e.response, e.response.code) new_error. = "error while getting oauth token: #{e.}" raise new_error end end if @token != nil auth_type = :bearer end if auth_type == :bearer raise ArgumentError.new('token must be provided as argument if auth_type=bearer') if @token.nil? headers['X-UA-Appkey'] = @key headers['Authorization'] = "Bearer #{@token}" end url = "https://#{@server}/api#{path}" unless path.nil? debug = "Making #{method} request to #{url}.\n"+ "\tHeaders:\n" debug += "\t\tcontent-type: #{content_type}\n" unless content_type.nil? debug += "\t\tcontent-encoding: gzip\n" unless encoding.nil? debug += "\t\taccept: application/vnd.urbanairship+json; version=3\n" debug += "\tBody:\n#{body}" unless body.nil? logger.debug(debug) params = { method: method, url: url, headers: headers, payload: body, timeout: Urbanairship.configuration.timeout } if auth_type == :basic raise ArgumentError.new('secret must be provided as argument if auth_type=basic') if @secret.nil? params[:user] = @key params[:password] = @secret end response = RestClient::Request.execute(params) logger.debug("Received #{response.code} response. Headers:\n\t#{response.headers}\nBody:\n\t#{response.body}") Response.check_code(response.code, response) self.class.build_response(response) end |