Class: Connfu::Provisioning::Application

Inherits:
Object
  • Object
show all
Defined in:
lib/connfu/provisioning/application.rb

Overview

Application class defines the single entry point to fetch, create, update and delete any channel associated to the application

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key, endpoint = Connfu::CONNFU_ENDPOINT) ⇒ Application

Initializer

Parameters

  • api_key valid api_key that authenticates the application

  • endpoint connFu endpoint (host:port). Optional



20
21
22
23
24
25
# File 'lib/connfu/provisioning/application.rb', line 20

def initialize(api_key, endpoint = Connfu::CONNFU_ENDPOINT)
  @base = Base.new(api_key, endpoint)
  @name = nil
  @description = nil
  @stream_name = nil
end

Instance Attribute Details

#baseObject

Base instance to send HTTP requests to connFu API



12
13
14
# File 'lib/connfu/provisioning/application.rb', line 12

def base
  @base
end

Instance Method Details

#add_dtmf(voice, tone, message) ⇒ Object

Add a new dtmf action to the Voice channel

Parameters

  • voice: Voice channel identifier

  • dtmf: for the phone number to be allocated



300
301
302
# File 'lib/connfu/provisioning/application.rb', line 300

def add_dtmf(voice, tone, message)
  @base.post("channels/voice/#{voice}/dtmf", {:tone => tone, :message => message})
end

#add_phone(voice, country) ⇒ Object

Add a new phone number to the Voice channel

Parameters

  • voice: Voice channel identifier

  • country: for the phone number to be allocated



273
274
275
# File 'lib/connfu/provisioning/application.rb', line 273

def add_phone(voice, country)
  @base.post("channels/voice/#{voice}/phones", {:country => country})
end

#add_whitelist(*args) ⇒ Object

Create a new item in the whitelist

Parameters
  • *args:

    - First parameter: voice channel identifier
    - Second parameter can be either:
        - WhitelistUser object with the name and phone information
        - two arguments name, phone
    

Return

  • Whitelist object



338
339
340
341
342
343
344
345
346
# File 'lib/connfu/provisioning/application.rb', line 338

def add_whitelist(*args)
  voice_id = args[0]
  if args.length.eql?(2)
    whitelist = @base.post("channels/voice/#{voice_id}/whitelisted", {:name => args[1].name, :phone => args[1].phone})
  else
    whitelist = @base.post("channels/voice/#{voice_id}/whitelisted", {:name => args[1], :phone => args[2]})
  end
  WhitelistUser.unmarshal(ActiveSupport::JSON.decode(whitelist))
end

#create_rss_channel(name, uri) ⇒ Object

Create a new rss channel

Parameters:

  • name

    rss channel identifier

  • uri

    RSS endpoint



381
382
383
384
385
# File 'lib/connfu/provisioning/application.rb', line 381

def create_rss_channel(name, uri)
  data = @base.post("channels/rss", {:uid => name, :uri => uri})
  data = ActiveSupport::JSON.decode(data)
  Rss.unmarshal(data)
end

#create_twitter_channel(key, params = {:origin => [], :mentions => [], :hashtags => []}) ⇒ Object

Create a Twitter channel per each of the origin or recipients

Parameters

  • key Twitter channel identifier

  • params Twitter channel behavior

    • array object of origin twitter accounts

    • Hash object

      • origin: one or more twitter accounts

      • mentions: one or more twitter accounts

      • hashtags: one or more hashtags



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/connfu/provisioning/application.rb', line 114

def create_twitter_channel(key, params = {:origin => [], :mentions => [], :hashtags => []})

  if params.is_a?(Array)
    _params = {}
    _params[:origin] = params
    params = _params
  elsif params.is_a?(String)
    _params = {}
    _params[:origin] = params
    params = _params
  end

  unless params.has_key?(:origin) or params.has_key?(:mentions)
    raise "Invalid parameters. Either origin or mentions should be defined"
  end

  if params.has_key?(:origin) and params.has_key?(:mentions)
    raise "Invalid parameters. Only origin or mentions can be defined"
  end

  filter = []

  # filter = "tags:(tag1 AND tag2...)
  if params.has_key?(:hashtags) && params[:hashtags].respond_to?(:length) && params[:hashtags].length > 0
    filter << "tags:(#{params[:hashtags].map { |hashtag| "#{hashtag}" }.join(" AND ")})"
  end

  if params.has_key?(:origin)
    # retrieve all the tweets from/to that user
    if filter.empty?
      filter = ""
    else
      filter = filter.join(' AND ')
    end
    if params[:origin].is_a?(String)
      users = [params[:origin]]
    else
      users = params[:origin]
    end

    data = @base.post("channels/twitter",
               {:uid => key,
                :accounts => users.collect { |item| {:name => item} },
                :filter => filter
               })
    Twitter.unmarshal(ActiveSupport::JSON.decode(data))
  else
    # retrieve users mentions
    users = params[:mentions]
    locations = []
    # create a stream per each origin
    users.each { |user|
      user_filter = filter.dup
      user_filter << "recipients:#{user}"

      data = @base.post("channels/twitter",
                              {:uid => key,
                               :accounts => [{:name => user}],
                               :filter => "(#{user_filter.join(' AND ')})"
                              })
      locations << Twitter.unmarshal(ActiveSupport::JSON.decode(data))
    }
    locations
  end

end

#create_voice_channel(name, country, privacy = Voice::Privacy::WHITELIST) ⇒ Object

Create a new voice channel

Parameters

  • name voice channel identifier

  • country country to allocate a DID



235
236
237
# File 'lib/connfu/provisioning/application.rb', line 235

def create_voice_channel(name, country, privacy = Voice::Privacy::WHITELIST)
  Voice.unmarshal(ActiveSupport::JSON.decode(@base.post("channels/voice", {:uid => name, :country => country, :privacy => privacy})))
end

#delete_dtmf(voice, dtmf) ⇒ Object

Delete one dtmf action or the whole dtmf actions

Parameters

  • voice: Voice channel identifier

  • dtmf: specific dtmf tone to delete.



291
292
293
# File 'lib/connfu/provisioning/application.rb', line 291

def delete_dtmf(voice, dtmf)
  @base.delete("channels/voice/#{voice}/dtmf/#{dtmf}")
end

#delete_phone(voice, phone) ⇒ Object

Delete one phone number or the whole phone list

Parameters

  • voice: Voice channel identifier

  • phone: specific number to delete.



264
265
266
# File 'lib/connfu/provisioning/application.rb', line 264

def delete_phone(voice, phone)
  @base.delete("channels/voice/#{voice}/phones/#{phone}")
end

#delete_rss_channel(name) ⇒ Object

Delete a rss channel

Parameters:

  • name

    rss channel identifier



400
401
402
# File 'lib/connfu/provisioning/application.rb', line 400

def delete_rss_channel(name)
  @base.delete("channels/rss/#{name}")
end

#delete_twitter_channel(key) ⇒ Object

Delete a Twitter channel

Parameters

  • key twitter channel unique identifier

Return

  • nil if success

  • RestClient exception with relevant error information



189
190
191
# File 'lib/connfu/provisioning/application.rb', line 189

def delete_twitter_channel(key)
  @base.delete("channels/twitter/#{key}")
end

#delete_voice_channel(voice) ⇒ Object

Delete a voice channel

Parameters

  • name voice channel identifier



244
245
246
# File 'lib/connfu/provisioning/application.rb', line 244

def delete_voice_channel(voice)
  @base.delete("channels/voice/#{voice}")
end

#delete_whitelist(voice, number = "") ⇒ Object

Delete one whitelist number or the whole whitelist

Parameters:

  • voice

    Voice channel identifier

  • number (optional) (defaults to: "")

    specific number to delete. If not present, the whole whitelist is deleted



323
324
325
# File 'lib/connfu/provisioning/application.rb', line 323

def delete_whitelist(voice, number = "")
  @base.delete("channels/voice/#{voice}/whitelisted/#{number}")
end

#descriptionObject

Returns the app description. Lazy loading, it sends a request to retrieve application info only the first time this method is called, and the info is cached for next calls

Return

application description



60
61
62
63
# File 'lib/connfu/provisioning/application.rb', line 60

def description
  @name.nil? and get_info
  @description
end

#get_channelsObject

Retrieves the app channels

Return

Array of Channel instances



79
80
81
82
# File 'lib/connfu/provisioning/application.rb', line 79

def get_channels
  data = ActiveSupport::JSON.decode(@base.get("channels/"))
  Channel.unmarshal(data)
end

#get_dtmf(voice, dtmf = "") ⇒ Object

Retrieve a voice channel dtmf tones, that’s the list of actions associated to a IVR voice channel

Parameters

  • voice Voice channel identifier

  • dtmf (optional) specific Dtmf tone to retrieve. If it is not specified, all the list is fetched



282
283
284
# File 'lib/connfu/provisioning/application.rb', line 282

def get_dtmf(voice, dtmf = "")
  Dtmf.unmarshal(voice, ActiveSupport::JSON.decode(@base.get("channels/voice/#{voice}/dtmf/#{dtmf}")))
end

#get_infoObject

Retrieves the app information using the API

Return

  • self instance with name, description and stream_name updated

  • raise RestClient::Unauthorized if token is invalid



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/connfu/provisioning/application.rb', line 32

def get_info
  if @name.nil?
    data = ActiveSupport::JSON.decode(@base.get(""))
    unless data.nil? or !data.instance_of?(Hash)
      @name = data["name"]
      @description = data["description"]
      @stream_name = data["stream_name"]
    end
  end
  self
end

#get_phones(voice, phone = "") ⇒ Object

Retrieve a voice channel phone lists, that’s the list of phones assigned to that voice channel

Parameters

  • voice Voice channel identifier

  • phone (optional) specific Phone number to retrieve. If it is not specified, all the list is fetched



255
256
257
# File 'lib/connfu/provisioning/application.rb', line 255

def get_phones(voice, phone = "")
  Phone.unmarshal(voice, ActiveSupport::JSON.decode(@base.get("channels/voice/#{voice}/phones/#{phone}")))
end

#get_rss_channel(name = "") ⇒ Object

RSS channel management

Retrieve the rss channel

Parameters:

  • name (defaults to: "")

    RSS channel identifier. It it is not specified, all the RSS channels are fetched



370
371
372
373
374
# File 'lib/connfu/provisioning/application.rb', line 370

def get_rss_channel(name = "")
  data = @base.get("channels/rss/#{name}")
  data = ActiveSupport::JSON.decode(data)
  Rss.unmarshal(data)
end

#get_twitter_channel(key = "") ⇒ Object

Twitter channel management

Retrieve the Twitter channel

Parameters:

  • key (defaults to: "")

    Twitter channel identifier. It it is not specified, all the twitter channels are fetched



90
91
92
93
# File 'lib/connfu/provisioning/application.rb', line 90

def get_twitter_channel(key = "")
  data = ActiveSupport::JSON.decode(@base.get("channels/twitter/#{key}"))
  Twitter.unmarshal(data)
end

#get_voice_channel(voice = "") ⇒ Object

Voice channel management

Retrieve the voice channel

Parameters:

  • voice (defaults to: "")

    Voice channel identifier. It it is not specified, all the voice channels are fetched



199
200
201
202
203
# File 'lib/connfu/provisioning/application.rb', line 199

def get_voice_channel(voice = "")
  data = @base.get("channels/voice/#{voice}")
  data = ActiveSupport::JSON.decode(data)
  Voice.unmarshal(data)
end

#get_whitelist(voice, number = "") ⇒ Object

Voice channel Whitelist management

Retrieve the whitelist

Parameters:

  • voice

    Voice channel identifier

  • number (optional) (defaults to: "")

    specific whitelist number to retrieve. If it is not specified, all the list is fetched



310
311
312
313
314
315
316
317
# File 'lib/connfu/provisioning/application.rb', line 310

def get_whitelist(voice, number = "")
  if number.empty?
    Whitelist.unmarshal(voice, ActiveSupport::JSON.decode(@base.get("channels/voice/#{voice}/whitelisted/#{number}")))
  else
    user = ActiveSupport::JSON.decode(@base.get("channels/voice/#{voice}/whitelisted/#{number}"))
    WhitelistUser.new(*user.values)
  end
end

#nameObject

Returns the app name. Lazy loading, it sends a request to retrieve application info only the first time this method is called, and the info is cached for next calls

Return

application name



49
50
51
52
# File 'lib/connfu/provisioning/application.rb', line 49

def name
  @name.nil? and get_info
  @name
end

#stream_nameObject

Returns the app stream name. Lazy loading, it sends a request to retrieve application info only the first time this method is called, and the info is cached for next calls

Return

application stream name



70
71
72
73
# File 'lib/connfu/provisioning/application.rb', line 70

def stream_name
  @name.nil? and get_info
  @stream_name
end

#update_rss_channel(name, uri) ⇒ Object

Update the rss channel URI

Parameters:

  • name

    RSS channel identifier

  • uri

    new uri



392
393
394
# File 'lib/connfu/provisioning/application.rb', line 392

def update_rss_channel(name, uri)
  @base.put("channels/rss/#{name}", {:uri => uri})
end

#update_twitter_channelObject



98
99
100
# File 'lib/connfu/provisioning/application.rb', line 98

def update_twitter_channel()

end

#update_voice_channel(voice, params) ⇒ Object

Update the voice channel topic

Parameters:

  • voice

    Voice channel identifier

  • params

    String: topic new topic Hash:

    topic new topic
    welcome_message new welcome_message
    rejected_message new rejected_message
    


215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/connfu/provisioning/application.rb', line 215

def update_voice_channel(voice, params)
  attributes = {}
  if params.is_a?(String)
    attributes[:topic] = params
  elsif params.is_a?(Hash)
    Voice::UPDATE_ATTRIBUTES.each { |attr|
      params.has_key?(attr) and attributes[attr] = params[attr]
    }
  else
    # do nothing, raise exception...
  end
  @base.put("channels/voice/#{voice}", attributes)
end

#update_whitelist(*args) ⇒ Object

Update a whitelist item

Parameters:

  • *args:
    • First parameter: voice channel identifier

    • Second parameter can be either:

      - WhitelistUser object with the name and phone information
      - two arguments name, phone
      


356
357
358
359
360
361
362
# File 'lib/connfu/provisioning/application.rb', line 356

def update_whitelist(*args)
  if args.length.eql?(2)
    @base.put("channels/voice/#{args[0]}/whitelisted/#{args[1].phone}", {:name => args[1].name})
  else
    @base.put("channels/voice/#{args[0]}/whitelisted/#{args[2]}", {:name => args[1]})
  end
end