bluevia 1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,30 @@
1
+ require 'logger'
2
+
3
+ #
4
+ # BlueVia is a global iniciative of Telefonica delivered by Movistar and O2.
5
+ # Please, check out www.bluevia.com and if you need more information
6
+ # contact us at mailto:support@bluevia.com
7
+ #
8
+
9
+
10
+ module BlueviaLogger
11
+ def logger=(value)
12
+ $logger = value
13
+ end
14
+
15
+ def logger
16
+ $logger||=create_logger
17
+ end
18
+
19
+ def log_level=(level)
20
+ logger.level = level
21
+ end
22
+
23
+ def create_logger(output=nil)
24
+ output.nil? and output = STDOUT
25
+ logger = Logger.new(output)
26
+ logger.level = Logger::ERROR
27
+ logger.datetime_format = "%Y-%m-%d %H:%M:%S"
28
+ logger
29
+ end
30
+ end
@@ -0,0 +1,81 @@
1
+ #
2
+ # BlueVia is a global iniciative of Telefonica delivered by Movistar and O2.
3
+ # Please, check out www.bluevia.com and if you need more information
4
+ # contact us at mailto:support@bluevia.com
5
+
6
+ require 'json'
7
+ require 'net/http'
8
+ require 'bluevia/base_client'
9
+
10
+ module Bluevia
11
+ #
12
+ # This class is in charge of access Bluevia Directory API
13
+ #
14
+
15
+ class Directory < BaseClient
16
+
17
+ # Valid methods included in the API
18
+ @@valid_methods = %W[Profile AccessInfo TerminalInfo]
19
+ USER_PROFILE = @@valid_methods[0]
20
+ USER_ACCESS_INFO = @@valid_methods[1]
21
+ USER_TERMINAL_INFO = @@valid_methods[2]
22
+
23
+ # Base Path for Directory API
24
+ BASEPATH_API = "/Directory"
25
+
26
+ def initialize(params = nil)
27
+ super(params)
28
+ end
29
+
30
+ #
31
+ # This method is in charge of retrieving user information
32
+ # [*user*] User identifier, a valid access token
33
+ # [*type*] information to be retrieved. Can be one or more @@valid_methods.
34
+ # Raises RuntimeError if any error occurred while invoking the request
35
+ #
36
+
37
+ def get_user_info(user, type = nil)
38
+ Utils.check_attribute user, "User cannot be null"
39
+ unless type.nil?
40
+ if (type.instance_of?(Array))
41
+ if type.length == 1
42
+ if @@valid_methods.to_a.include?(type[0])
43
+ _type = "/" + type[0]
44
+ else
45
+ raise SyntaxError, "Type not allowed. #{@@valid_methods} only."
46
+ end
47
+ else
48
+ _type = ""
49
+ data_sets = type.join(',')
50
+ end
51
+
52
+ elsif (type.instance_of?(String))
53
+ if @@valid_methods.to_a.include?(type)
54
+ _type = "/User" + type
55
+ else
56
+ raise SyntaxError, "Type not allowed. #{@@valid_methods} only."
57
+ end
58
+ else
59
+ raise SyntaxError, "Invalid type"
60
+ end
61
+ else
62
+ _type = ""
63
+ end
64
+
65
+ # Include the alias prefix in URL
66
+ identifier = CGI::escape("alias:#{user}")
67
+
68
+ params = Hash.new
69
+
70
+ unless data_sets.nil?
71
+ params[:dataSets] = data_sets
72
+ end
73
+
74
+ # returns the response body
75
+ GET("#{get_basepath}/#{identifier}/UserInfo#{_type}", params, nil)
76
+ end
77
+
78
+
79
+ end
80
+ end
81
+
@@ -0,0 +1,13 @@
1
+ #
2
+ # BlueVia is a global iniciative of Telefonica delivered by Movistar and O2.
3
+ # Please, check out www.bluevia.com and if you need more information
4
+ # contact us at mailto:support@bluevia.com
5
+ #
6
+
7
+ require 'bluevia/errors/server_error'
8
+ require 'bluevia/errors/client_error'
9
+ require 'bluevia/errors/not_found_error'
10
+
11
+ module Bluevia::Errors
12
+
13
+ end
@@ -0,0 +1,6 @@
1
+
2
+ module Bluevia
3
+ class ClientError < StandardError
4
+
5
+ end
6
+ end
@@ -0,0 +1,5 @@
1
+
2
+ module Bluevia
3
+ class NotFoundError < StandardError
4
+ end
5
+ end
@@ -0,0 +1,6 @@
1
+
2
+ module Bluevia
3
+ class ServerError < StandardError
4
+
5
+ end
6
+ end
@@ -0,0 +1,64 @@
1
+ require 'nokogiri'
2
+
3
+ #
4
+ # BlueVia is a global iniciative of Telefonica delivered by Movistar and O2.
5
+ # Please, check out www.bluevia.com and if you need more information
6
+ # contact us at mailto:support@bluevia.com
7
+ #
8
+ # Extension of Hash to create a Hash table using as input an XML
9
+ # that should be decoded using Nokogiri library
10
+ #
11
+ class Hash
12
+ class << self
13
+ def from_xml(xml_io)
14
+ begin
15
+ result = Nokogiri::XML(xml_io)
16
+ return { result.root.name.to_sym => xml_node_to_hash(result.root)}
17
+ rescue Exception => e
18
+ raise e
19
+ end
20
+ end
21
+
22
+ def xml_node_to_hash(node)
23
+ # If we are at the root of the document, start the hash
24
+ if node.element?
25
+ result_hash = {}
26
+ if node.attributes != {}
27
+ result_hash[:attributes] = {}
28
+ node.attributes.keys.each do |key|
29
+ result_hash[:attributes][node.attributes[key].name.to_sym] = prepare(node.attributes[key].value)
30
+ end
31
+ end
32
+ if node.children.size > 0
33
+ node.children.each do |child|
34
+ result = xml_node_to_hash(child)
35
+
36
+ if child.name == "text"
37
+ unless child.next_sibling || child.previous_sibling
38
+ return prepare(result)
39
+ end
40
+ elsif result_hash[child.name.to_sym]
41
+ if result_hash[child.name.to_sym].is_a?(Object::Array)
42
+ result_hash[child.name.to_sym] << prepare(result)
43
+ else
44
+ result_hash[child.name.to_sym] = [result_hash[child.name.to_sym]] << prepare(result)
45
+ end
46
+ else
47
+ result_hash[child.name.to_sym] = prepare(result)
48
+ end
49
+ end
50
+
51
+ return result_hash
52
+ else
53
+ return result_hash
54
+ end
55
+ else
56
+ return prepare(node.content.to_s)
57
+ end
58
+ end
59
+
60
+ def prepare(data)
61
+ (data.class == String && data.to_i.to_s == data) ? data.to_i : data
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,96 @@
1
+ #
2
+ # BlueVia is a global iniciative of Telefonica delivered by Movistar and O2.
3
+ # Please, check out www.bluevia.com and if you need more information
4
+ # contact us at mailto:support@bluevia.com
5
+
6
+ module Bluevia
7
+ #
8
+ # This module is a mixin used by SMS client to get some basic messaging features
9
+ #
10
+
11
+ module Messaging
12
+
13
+
14
+ #
15
+ # This method is used to retrieve MO messages that belongs to a specific registration ID
16
+ # [*registration_id*] MO Registration Identifier Mandatory
17
+ # [*message_id*] message unique identifier
18
+ # [*attachments*] bool flag to get message attachments (if true)
19
+ # returns a collection of messages
20
+ #
21
+ def get_received_message(registration_id, message_id = nil, attachments = false)
22
+ Utils.check_attribute(registration_id, "Provisioning registration id must be provided")
23
+ unless message_id.nil?
24
+ message_id = "/#{message_id}"
25
+ unless attachments == false
26
+ attachments = "/attachments"
27
+ else
28
+ attachments = ""
29
+ end
30
+ else
31
+ message_id = ""
32
+ attachments = ""
33
+ end
34
+ GET("#{get_basepath}/inbound/#{registration_id}/messages#{message_id}#{attachments}")
35
+ end
36
+
37
+ #
38
+ # This method allows to retrieved a specific attachment belonging to a message
39
+ # [*registration_id*] MO Registration Identifier
40
+ # [*message_id*] message unique identifier
41
+ # [*attachment_id*] attachment identifier (corresponding to the specific message
42
+ #
43
+
44
+ def get_attachment(registration_id, message_id, attachment_id)
45
+ Utils.check_attribute(registration_id, "Registration id must be provided")
46
+ Utils.check_attribute(message_id, "Message id must be provided")
47
+ Utils.check_attribute(attachment_id, "Attachment id must be provided")
48
+ GET("#{get_basepath}/inbound/#{registration_id}/messages/#{message_id}/attachments/#{attachment_id}")
49
+ end
50
+
51
+ #
52
+ # This method returns the delivery status of a SMS sent by the user
53
+ # [*id*] message unique identifier
54
+ #
55
+
56
+ def get_delivery_status (id)
57
+ Utils.check_attribute id, "#{self.class.to_s.upcase} identifier cannot be null"
58
+ if is_url?(id)
59
+ id = id.split("/").last(2).first
60
+ end
61
+ response = GET("#{get_basepath}/outbound/requests/#{id}/deliverystatus", nil, nil)
62
+
63
+ # Clean duplicated key
64
+ if response.instance_of?(Hash)
65
+ if response.has_key?("smsDeliveryStatus")
66
+ return response["smsDeliveryStatus"]
67
+ elsif response.has_ley?("messageDeliveryStatus")
68
+ return response["messageDeliveryStatus"]
69
+ else
70
+ return response
71
+ end
72
+ end
73
+ end
74
+
75
+ #
76
+ # This method stop the message notification (MO)
77
+ # [*subscription_id*] The subscription identifier to be deleted
78
+ #
79
+
80
+ private
81
+
82
+ #
83
+ # Validates if a URL is defined properly
84
+ #
85
+ def is_url?(identifier)
86
+ if identifier.match(/^https?:\/\//i).nil?
87
+ # if identifier.match(/^https?:\/\/([a-z0-9-]+\.)+[a-z0-9]+(:[0-9]{2,5})?/i)
88
+ #if identifier.match(/^https?:\/\/([a-z]+\.)*[a-z]+$/i).nil?
89
+ #if identifier.match(/^https?:\/\/([a-z]+\.)*[a-z]+$/i).nil?
90
+ return false
91
+ else
92
+ return true
93
+ end
94
+ end
95
+ end
96
+ end
@@ -0,0 +1,117 @@
1
+ #
2
+ # BlueVia is a global iniciative of Telefonica delivered by Movistar and O2.
3
+ # Please, check out www.bluevia.com and if you need more information
4
+ # contact us at mailto:support@bluevia.com
5
+
6
+ require 'oauth'
7
+ require 'oauth/signature/hmac/sha1'
8
+ require 'oauth/client/helper'
9
+
10
+ module Bluevia
11
+ #
12
+ # This class can be used to launch oAuth authentication mechanism when
13
+ # a user is using the application for the first time
14
+ #
15
+ # User authentication is launched using oAuth protocol, so user is not required to use credentials in third party applications. If you want to learn more about oAuth please check this URL.
16
+ # When user wants to launch the oAuth process, once the Bluevia client has been created only the two lines below are required to retrieve a valid token for user:
17
+ #
18
+ # @service = @bc.get_service(:oAuth)
19
+ # token, secret, url = @service.get_request_token({:callback =>"http://foo.bar"})
20
+ #
21
+ # The retrieved parameter token and secret should be use during the oAuth process, and url is the endpoint where Bluevia shall authenticate the user. In case of a Rails application, the lines below could be used:
22
+ #
23
+ # token, token_secret, url = @service.get_request_token("http://juan.pollinimini.net/bluevia/get_access")
24
+ # cookies[:token] = "#{token}|#{token_secret}"
25
+ # redirect_to(url)
26
+ #
27
+ # Once user is authenticated and she authorized the application in BlueVia portal, she should be redirected to the URL used as parameter before. Now it's time to fetch the valid token and token secret that shall identify the new user during any call to BlueVia API. Lines below show an example using Rails:
28
+ #
29
+ # def get_access
30
+ # oauth_verifier = params[:oauth_verifier]
31
+ # get_token_from_cookie
32
+ # @bc = BlueviaClient.new(
33
+ # { :consumer_key => CONSUMER_KEY,
34
+ # :consumer_secret=> CONSUMER_SECRET
35
+ # })
36
+ # @service = @bc.get_service(:oAuth)
37
+ # @token, @token_secret = @service.get_access_token(@request_token, @request_secret, oauth_verifier)
38
+ # end
39
+ #
40
+ # private
41
+ # def get_token_from_cookie
42
+ # cookie_token = cookies[:token]
43
+ # unless cookie_token.nil?
44
+ # cookie_token = cookie_token.split("|")
45
+ # if cookie_token.size != 2
46
+ # raise SyntaxError, "The cookie is not valid"
47
+ # end
48
+ # @request_token = cookie_token[0]
49
+ # @request_secret = cookie_token[1]
50
+ # end
51
+ # end
52
+
53
+ class Oauth < BaseClient
54
+
55
+ AUTHORIZE_URI = "http://connect.bluevia.com/authorise/"
56
+
57
+ def initialize(params = nil)
58
+ super(params)
59
+ end
60
+
61
+ def get_request_token(_params)
62
+
63
+ consumer=OAuth::Consumer.new \
64
+ @consumer_key,
65
+ @consumer_secret,
66
+ {
67
+ :site => @@base_uri,
68
+ :signature_method => "HMAC-SHA1",
69
+ :request_token_path => "#{BASEPATH}/Oauth/getRequestToken",
70
+ :access_token_path => "#{BASEPATH}/Oauth/getAccessToken",
71
+ #:proxy => "http://localhost:8888",
72
+ :http_method => :post
73
+ }
74
+
75
+ params = Hash.new
76
+ specific_params = Hash.new
77
+ if _params.instance_of?(String)
78
+ params[:oauth_callback] = _params
79
+ uri = AUTHORIZE_URI
80
+ elsif _params.instance_of?(Hash)
81
+ if _params.has_key?(:callback)
82
+ params[:oauth_callback] = _params[:callback]
83
+ else
84
+ Raise SyntaxError, "Callback parameter must be provided"
85
+ end
86
+ if _params.has_key?(:uri)
87
+ uri = _params[:uri]
88
+ else
89
+ uri = AUTHORIZE_URI
90
+ end
91
+ end
92
+ request_token = consumer.get_request_token(params, {:v => "1"}, specific_params)
93
+ return request_token.token, request_token.secret, "#{uri}?oauth_token=#{request_token.token}"
94
+
95
+ end
96
+
97
+ def get_access_token(token, token_secret, oauth_verifier)
98
+ begin
99
+ consumer = OAuth::Consumer.new \
100
+ @consumer_key, @consumer_secret,
101
+ {
102
+ :site => BASEURI,
103
+ :signature_method => "HMAC-SHA1",
104
+ :request_token_path => "#{BASEPATH}/Oauth/getRequestToken",
105
+ :access_token_path => "#{BASEPATH}/Oauth/getAccessToken",
106
+ :http_method => :post
107
+ }
108
+ request_token = OAuth::RequestToken.new(consumer, token, token_secret)
109
+ access_token = request_token.get_access_token(:oauth_verifier => oauth_verifier)
110
+
111
+ return access_token.params[:oauth_token], access_token.params[:oauth_token_secret]
112
+ rescue => ex
113
+ return nil, nil # error
114
+ end
115
+ end
116
+ end
117
+ end
@@ -0,0 +1,48 @@
1
+ #
2
+ # BlueVia is a global iniciative of Telefonica delivered by Movistar and O2.
3
+ # Please, check out www.bluevia.com and if you need more information
4
+ # contact us at mailto:support@bluevia.com
5
+
6
+ module Bluevia
7
+ #
8
+ # Inner class that wraps the response
9
+ #
10
+
11
+ class Response
12
+ # HTTP response code
13
+ attr_accessor :code
14
+ # HTTP response body
15
+ attr_accessor :body
16
+ # HTTP headers (required when creating resources)
17
+ attr_accessor :headers
18
+
19
+ attr_accessor :message
20
+
21
+ def initialize
22
+ @body = ""
23
+ end
24
+
25
+ def to_s
26
+ value = String.new
27
+ self.instance_variables.each{ |var|
28
+ value << "#{var} : #{get_value(self.instance_variable_get(var))} \n"
29
+ }
30
+ value
31
+ end
32
+
33
+ def get_value(var)
34
+ if var.instance_of?(String)
35
+ var
36
+ elsif var.instance_of?(Hash)
37
+ var.to_a.join(" - ")
38
+ else
39
+ var
40
+ end
41
+ end
42
+
43
+ def [](value)
44
+ return self.instance_variable_get("@#{value}")
45
+ end
46
+
47
+ end
48
+ end