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.
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) ⇒ 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) ⇒ Object
Initialize the Client
20 21 22 23 24 25 |
# File 'lib/urbanairship/client.rb', line 20 def initialize(key: required('key'), secret: nil, server: Urbanairship.configuration.server, token: nil) @key = key @secret = secret @server = server @token = token end |
Instance Attribute Details
#key ⇒ Object
Returns the value of attribute key.
8 9 10 |
# File 'lib/urbanairship/client.rb', line 8 def key @key end |
#secret ⇒ Object
Returns the value of attribute secret.
8 9 10 |
# File 'lib/urbanairship/client.rb', line 8 def secret @secret end |
Class Method Details
.build_response(response) ⇒ Hash
Build a hash from the response object
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/urbanairship/client.rb', line 118 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
104 105 106 |
# File 'lib/urbanairship/client.rb', line 104 def create_push Push::Push.new(self) end |
#create_scheduled_push ⇒ Object
Create a Scheduled Push Object
111 112 113 |
# File 'lib/urbanairship/client.rb', line 111 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
37 38 39 40 41 42 43 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 |
# File 'lib/urbanairship/client.rb', line 37 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? 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 |