Class: PushIo::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/push_io/client.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Client

Returns a new instance of Client.



6
7
8
9
10
11
12
13
14
15
# File 'lib/push_io/client.rb', line 6

def initialize(attributes = {})
  @config = PushIo::Client.config.dup
  self.httpclient = HTTPClient.new
  attributes.each do |name, value|
    @config.send("#{name}=", value) if @config.respond_to?("#{name}=")
  end
  if (@config.app_guid.nil? || @config.app_guid == '' || @config.sender_secret.nil? || @config.sender_secret == '')
    raise UnconfiguredClientError, "You must specify a Push IO service app_guid and sender_secret either in an initializer or when creating your PushIo::Client instance"
  end
end

Class Attribute Details

.configurationObject

Returns the value of attribute configuration.



18
19
20
# File 'lib/push_io/client.rb', line 18

def configuration
  @configuration
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



3
4
5
# File 'lib/push_io/client.rb', line 3

def config
  @config
end

#httpclientObject

Returns the value of attribute httpclient.



4
5
6
# File 'lib/push_io/client.rb', line 4

def httpclient
  @httpclient
end

Class Method Details

.configObject



20
21
22
# File 'lib/push_io/client.rb', line 20

def config
  self.configuration ||= Configuration.new
end

.configure {|config| ... } ⇒ Object

Yields:



24
25
26
# File 'lib/push_io/client.rb', line 24

def configure
  yield(config)
end

Instance Method Details

#deliver(*args) ⇒ Object



29
30
31
32
# File 'lib/push_io/client.rb', line 29

def deliver(*args)
  opts = args.extract_options!
  send_post(opts) if ready_to_deliver?(opts)
end

#deliver_broadcast(*args) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/push_io/client.rb', line 34

def deliver_broadcast(*args)
  opts = args.extract_options!
  opts.delete(:audience) if opts[:audience]
  opts.delete(:tag_query) if opts[:tag_query]
  opts[:audience] = 'broadcast'
  send_post(opts) if ready_to_deliver?(opts)
end

#deliver_to_audience(audience, *args) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/push_io/client.rb', line 42

def deliver_to_audience(audience, *args)
  opts = args.extract_options!
  opts.delete(:tag_query) if opts[:tag_query]
  opts[:audience] = audience
  raise PushIo::MissingOptionsError, "An audience was not specified" unless opts[:audience]
  send_post(opts) if ready_to_deliver?(opts)
end

#deliver_to_ids(api_key, device_id_array, *args) ⇒ Object



58
59
60
61
62
63
64
65
66
# File 'lib/push_io/client.rb', line 58

def deliver_to_ids(api_key, device_id_array, *args)
  opts = args.extract_options!
  opts.delete(:audience) if opts[:audience]
  opts.delete(:tag_query) if opts[:tag_query]
  raise PushIo::MissingOptionsError, "A valid API Key was not specified" unless api_key =~ PushIo::API_KEY_REGEX
  raise PushIo::MissingOptionsError, "Device IDs need to be specified in an array" unless device_id_array.is_a? Array
  opts[:recipient_ids] = {api_key => device_id_array}
  send_devices_post(opts) if ready_to_deliver_to_devices?(opts)
end

#deliver_to_query(query, *args) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/push_io/client.rb', line 50

def deliver_to_query(query, *args)
  opts = args.extract_options!
  opts.delete(:audience) if opts[:audience]
  opts[:tag_query] = query
  raise PushIo::MissingOptionsError, "A query was not specified" unless opts[:tag_query]
  send_post(opts) if ready_to_deliver?(opts)
end

#deliver_to_tokens(api_key, device_token_array, *args) ⇒ Object



68
69
70
71
72
73
74
75
76
# File 'lib/push_io/client.rb', line 68

def deliver_to_tokens(api_key, device_token_array, *args)
  opts = args.extract_options!
  opts.delete(:audience) if opts[:audience]
  opts.delete(:tag_query) if opts[:tag_query]
  raise PushIo::MissingOptionsError, "A valid API Key was not specified" unless api_key =~ PushIo::API_KEY_REGEX
  raise PushIo::MissingOptionsError, "Device tokens need to be specified in an array" unless device_token_array.is_a? Array
  opts[:recipient_tokens] = {api_key => device_token_array}
  send_devices_post(opts) if ready_to_deliver_to_devices?(opts)
end