bluevia 1.0

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE.LGPLv3 ADDED
@@ -0,0 +1,165 @@
1
+ GNU LESSER GENERAL PUBLIC LICENSE
2
+ Version 3, 29 June 2007
3
+
4
+ Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
5
+ Everyone is permitted to copy and distribute verbatim copies
6
+ of this license document, but changing it is not allowed.
7
+
8
+
9
+ This version of the GNU Lesser General Public License incorporates
10
+ the terms and conditions of version 3 of the GNU General Public
11
+ License, supplemented by the additional permissions listed below.
12
+
13
+ 0. Additional Definitions.
14
+
15
+ As used herein, "this License" refers to version 3 of the GNU Lesser
16
+ General Public License, and the "GNU GPL" refers to version 3 of the GNU
17
+ General Public License.
18
+
19
+ "The Library" refers to a covered work governed by this License,
20
+ other than an Application or a Combined Work as defined below.
21
+
22
+ An "Application" is any work that makes use of an interface provided
23
+ by the Library, but which is not otherwise based on the Library.
24
+ Defining a subclass of a class defined by the Library is deemed a mode
25
+ of using an interface provided by the Library.
26
+
27
+ A "Combined Work" is a work produced by combining or linking an
28
+ Application with the Library. The particular version of the Library
29
+ with which the Combined Work was made is also called the "Linked
30
+ Version".
31
+
32
+ The "Minimal Corresponding Source" for a Combined Work means the
33
+ Corresponding Source for the Combined Work, excluding any source code
34
+ for portions of the Combined Work that, considered in isolation, are
35
+ based on the Application, and not on the Linked Version.
36
+
37
+ The "Corresponding Application Code" for a Combined Work means the
38
+ object code and/or source code for the Application, including any data
39
+ and utility programs needed for reproducing the Combined Work from the
40
+ Application, but excluding the System Libraries of the Combined Work.
41
+
42
+ 1. Exception to Section 3 of the GNU GPL.
43
+
44
+ You may convey a covered work under sections 3 and 4 of this License
45
+ without being bound by section 3 of the GNU GPL.
46
+
47
+ 2. Conveying Modified Versions.
48
+
49
+ If you modify a copy of the Library, and, in your modifications, a
50
+ facility refers to a function or data to be supplied by an Application
51
+ that uses the facility (other than as an argument passed when the
52
+ facility is invoked), then you may convey a copy of the modified
53
+ version:
54
+
55
+ a) under this License, provided that you make a good faith effort to
56
+ ensure that, in the event an Application does not supply the
57
+ function or data, the facility still operates, and performs
58
+ whatever part of its purpose remains meaningful, or
59
+
60
+ b) under the GNU GPL, with none of the additional permissions of
61
+ this License applicable to that copy.
62
+
63
+ 3. Object Code Incorporating Material from Library Header Files.
64
+
65
+ The object code form of an Application may incorporate material from
66
+ a header file that is part of the Library. You may convey such object
67
+ code under terms of your choice, provided that, if the incorporated
68
+ material is not limited to numerical parameters, data structure
69
+ layouts and accessors, or small macros, inline functions and templates
70
+ (ten or fewer lines in length), you do both of the following:
71
+
72
+ a) Give prominent notice with each copy of the object code that the
73
+ Library is used in it and that the Library and its use are
74
+ covered by this License.
75
+
76
+ b) Accompany the object code with a copy of the GNU GPL and this license
77
+ document.
78
+
79
+ 4. Combined Works.
80
+
81
+ You may convey a Combined Work under terms of your choice that,
82
+ taken together, effectively do not restrict modification of the
83
+ portions of the Library contained in the Combined Work and reverse
84
+ engineering for debugging such modifications, if you also do each of
85
+ the following:
86
+
87
+ a) Give prominent notice with each copy of the Combined Work that
88
+ the Library is used in it and that the Library and its use are
89
+ covered by this License.
90
+
91
+ b) Accompany the Combined Work with a copy of the GNU GPL and this license
92
+ document.
93
+
94
+ c) For a Combined Work that displays copyright notices during
95
+ execution, include the copyright notice for the Library among
96
+ these notices, as well as a reference directing the user to the
97
+ copies of the GNU GPL and this license document.
98
+
99
+ d) Do one of the following:
100
+
101
+ 0) Convey the Minimal Corresponding Source under the terms of this
102
+ License, and the Corresponding Application Code in a form
103
+ suitable for, and under terms that permit, the user to
104
+ recombine or relink the Application with a modified version of
105
+ the Linked Version to produce a modified Combined Work, in the
106
+ manner specified by section 6 of the GNU GPL for conveying
107
+ Corresponding Source.
108
+
109
+ 1) Use a suitable shared library mechanism for linking with the
110
+ Library. A suitable mechanism is one that (a) uses at run time
111
+ a copy of the Library already present on the user's computer
112
+ system, and (b) will operate properly with a modified version
113
+ of the Library that is interface-compatible with the Linked
114
+ Version.
115
+
116
+ e) Provide Installation Information, but only if you would otherwise
117
+ be required to provide such information under section 6 of the
118
+ GNU GPL, and only to the extent that such information is
119
+ necessary to install and execute a modified version of the
120
+ Combined Work produced by recombining or relinking the
121
+ Application with a modified version of the Linked Version. (If
122
+ you use option 4d0, the Installation Information must accompany
123
+ the Minimal Corresponding Source and Corresponding Application
124
+ Code. If you use option 4d1, you must provide the Installation
125
+ Information in the manner specified by section 6 of the GNU GPL
126
+ for conveying Corresponding Source.)
127
+
128
+ 5. Combined Libraries.
129
+
130
+ You may place library facilities that are a work based on the
131
+ Library side by side in a single library together with other library
132
+ facilities that are not Applications and are not covered by this
133
+ License, and convey such a combined library under terms of your
134
+ choice, if you do both of the following:
135
+
136
+ a) Accompany the combined library with a copy of the same work based
137
+ on the Library, uncombined with any other library facilities,
138
+ conveyed under the terms of this License.
139
+
140
+ b) Give prominent notice with the combined library that part of it
141
+ is a work based on the Library, and explaining where to find the
142
+ accompanying uncombined form of the same work.
143
+
144
+ 6. Revised Versions of the GNU Lesser General Public License.
145
+
146
+ The Free Software Foundation may publish revised and/or new versions
147
+ of the GNU Lesser General Public License from time to time. Such new
148
+ versions will be similar in spirit to the present version, but may
149
+ differ in detail to address new problems or concerns.
150
+
151
+ Each version is given a distinguishing version number. If the
152
+ Library as you received it specifies that a certain numbered version
153
+ of the GNU Lesser General Public License "or any later version"
154
+ applies to it, you have the option of following the terms and
155
+ conditions either of that published version or of any later version
156
+ published by the Free Software Foundation. If the Library as you
157
+ received it does not specify a version number of the GNU Lesser
158
+ General Public License, you may choose any version of the GNU Lesser
159
+ General Public License ever published by the Free Software Foundation.
160
+
161
+ If the Library as you received it specifies that a proxy can decide
162
+ whether future versions of the GNU Lesser General Public License shall
163
+ apply, that proxy's public statement of acceptance of any version is
164
+ permanent authorization for you to choose that version for the
165
+ Library.
@@ -0,0 +1,22 @@
1
+
2
+
3
+ #################################################################################
4
+ # #
5
+ # #
6
+ # This file is part of Bluevia Ruby SDK. #
7
+ # #
8
+ # Bluevia Ruby SDK is free software: you can redistribute it and/or modify #
9
+ # it under the terms of the GNU General Public License as published by #
10
+ # the Free Software Foundation, either version 3 of the License, or #
11
+ # (at your option) any later version. #
12
+ # #
13
+ # Bluevia Ruby SDK is distributed in the hope that it will be useful, #
14
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of #
15
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
16
+ # GNU General Public License for more details. #
17
+ # #
18
+ # You should have received a copy of the GNU General Public License #
19
+ # along with Bluevia Ruby SDK. If not, see <http://www.gnu.org/licenses/>. #
20
+ # #
21
+ # #
22
+ #################################################################################
data/README ADDED
@@ -0,0 +1,172 @@
1
+
2
+ Bluevia client module definition
3
+
4
+ Introduction
5
+
6
+ Bluevia Ruby SDK allows you to use the BueVia public API from your Ruby
7
+ application using just a few lines of code.
8
+ You only need to download the SDK, include it into your LOAD_PATH directory
9
+ and leave magic work for you. If you download the gem this should be
10
+ automatically included in your gem local repository.
11
+
12
+ When you want to use as developer Bluevia public APIs,
13
+ first of all you need to get a commercial or testing API Key.
14
+
15
+ Ruby SDK wraps any request to BlueVia endpoint by using a generic object
16
+ BlueviaClient. This object uses the Component Pattern to fetch any service
17
+ required by the developer (oAuth, SMS or Directory).
18
+
19
+ Getting Started
20
+
21
+ Find below the gem dependencies required by Bluevia SDK:
22
+
23
+ - httpclient: used to create HTTP requests
24
+ - oauth: generic gem to launch the oAuth process
25
+ - nokogiri: used to parse XML responses (Advertising)
26
+ - json_pure: used to parse JSON responses
27
+
28
+ This snippet shows the easier way to create a new object with valid credentials
29
+ that identify the application:
30
+
31
+ require 'rubygems'
32
+ require 'bluevia'
33
+ include Bluevia
34
+
35
+ @bc = BlueviaClient.new(
36
+ { :consumer_key => CONSUMER_KEY,
37
+ :consumer_secret=> CONSUMER_SECRET,
38
+ :uri => "https://api.bluevia.com"
39
+ })
40
+
41
+ Endpoints
42
+
43
+ BlueVia has two endpoints, commercial and sandbox.
44
+ Commercial is linked to Telefonica network, and sandbox is used just to
45
+ verify application behavior.
46
+ The developer can change the endpoint just setting the desired behavior
47
+ in the client:
48
+
49
+ @bc.set_commercial
50
+ @bc.set_sandbox
51
+
52
+ Use the method @bc.commercial? to verify if the client is accessing either
53
+ commercial or sandbox endpoint.
54
+
55
+ Using Oauth
56
+
57
+ User authentication is launched using oAuth protocol, so user is not
58
+ required to use credentials in third party applications.
59
+ If you want to learn more about oAuth please check this URL: http://oauth.net.
60
+
61
+ When user wants to launch the oAuth process, once the Bluevia client object
62
+ has been created only the two lines below are required to retrieve
63
+ a valid token for user:
64
+
65
+ @service = @bc.get_service(:oAuth)
66
+ token, secret, url = @service.get_request_token({:callback =>"http://foo.bar"})
67
+
68
+ The retrieved parameter token and secret should be used during the oAuth
69
+ process, and url is the endpoint where Bluevia shall authenticate the user.
70
+ In case of a Rails application, the lines below could be used:
71
+
72
+ token, token_secret, url = @service.get_request_token(
73
+ "http://foo.bar/bluevia/get_access")
74
+ cookies[:token] = "#{token}|#{token_secret}"
75
+ redirect_to(url)
76
+
77
+ Both tokend and token_secret must be saved by the application provider
78
+ because oAuth process will require it later.
79
+
80
+ Once user is authenticated and she has authorized the application in
81
+ BlueVia portal, she should be redirected to the URL used as parameter before.
82
+ Now it's time to fetch the valid token and token secret that shall identify
83
+ the new user during any call to BlueVia API. Lines below show an example using Rails:
84
+
85
+ def get_access
86
+ oauth_verifier = params[:oauth_verifier]
87
+ get_token_from_cookie
88
+ @bc = BlueviaClient.new(
89
+ { :consumer_key => CONSUMER_KEY,
90
+ :consumer_secret=> CONSUMER_SECRET
91
+ })
92
+ @service = @bc.get_service(:oAuth)
93
+ @token, @token_secret = @service.get_access_token(@request_token, @request_secret, oauth_verifier)
94
+ end
95
+
96
+ private
97
+ def get_token_from_cookie
98
+ cookie_token = cookies[:token]
99
+ unless cookie_token.nil?
100
+ cookie_token = cookie_token.split("|")
101
+ if cookie_token.size != 2
102
+ raise SyntaxError, "The cookie is not valid"
103
+ end
104
+ @request_token = cookie_token[0]
105
+ @request_secret = cookie_token[1]
106
+ end
107
+ end
108
+
109
+ Using BlueviaClient to launch requests
110
+
111
+ Most of requests when accessing Bluevia API are associated to a specific user,
112
+ so when a BlueviaClient object is created both user token and
113
+ user token secret must be provided to identify user on behalf of
114
+ whom the application wants to access BlueVia APIs.
115
+
116
+ @bc = BlueviaClient.new(
117
+ { :consumer_key => CONSUMER_KEY,
118
+ :consumer_secret=> CONSUMER_SECRET,
119
+ :token => USER_TOKEN,
120
+ :token_secret => TOKEN_SECRET,
121
+ :uri => "https://api.bluevia.com"
122
+ })
123
+
124
+ Send a SMS
125
+
126
+ First of all, create the BlueviaClient object as shown before.
127
+
128
+ Any operation available in BlueviaClient object is associated to
129
+ a specific service. Each service represents a specific enabler provided by Bluevia.
130
+
131
+ In case of SMS, these two lines are required to send a SMS on behalf of the user:
132
+
133
+ @service = @bc.get_service(:Sms)
134
+ info = @service.send_sms("11111",
135
+ USER_TOKEN,
136
+ "Yet another SMS Text")
137
+
138
+
139
+
140
+ Request Directory Info
141
+
142
+ @service = @bc.get_service(:Directory)
143
+ response = @service.get_user_info(USER_TOKEN, Directory::USER_IDENTITIES)
144
+
145
+ Get Ad
146
+
147
+ With Advertising API a developer can fetch an ad to include in her application.
148
+
149
+ There are some required parameters:
150
+ - user_agent
151
+ - ad_request_id
152
+ - ad_space
153
+
154
+ And also optional parameters:
155
+ - ad_presentation
156
+ - ad_presentation_size
157
+ - keywords
158
+ - protection_policy
159
+
160
+ All parameters should be include in a single Hash object:
161
+
162
+ @service = @bc.get_service(:Advertising)
163
+
164
+ params = {
165
+ :user_agent => "Mozilla 5.0",
166
+ :ad_request_id => "a1x4zasg58",
167
+ :ad_space => "1200",
168
+ :keywords => "bar"
169
+ }
170
+
171
+ ad = @service.request(params)
172
+
data/lib/bluevia.rb ADDED
@@ -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/bluevia_client'
8
+
9
+
10
+ # Main Bluevia module definition
11
+ module Bluevia
12
+ VERSION = "1.0"
13
+ end
@@ -0,0 +1,35 @@
1
+
2
+ #
3
+ # BlueVia is a global iniciative of Telefonica delivered by Movistar and O2.
4
+ # Please, check out www.bluevia.com and if you need more information
5
+ # contact us at mailto:support@bluevia.com
6
+ #
7
+
8
+ module Bluevia
9
+ #
10
+ # Wrapper to fetch in an easy way the image or the text ad
11
+ #
12
+ class AdResponse < Array
13
+ #
14
+ # Fetch the image ad
15
+ #
16
+ def image
17
+ val = self.find_all{|item| item["type_name"].eql?("image")}
18
+ if val.instance_of?(Array) and val.length > 0
19
+ val = val[0]
20
+ end
21
+ val
22
+ end
23
+
24
+ #
25
+ # Fetch the text ad
26
+ #
27
+ def text
28
+ val = self.find_all{|item| item["type_name"].eql?("text")}
29
+ if val.instance_of?(Array) and val.length > 0
30
+ val = val[0]
31
+ end
32
+ val
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,108 @@
1
+ require 'bluevia/ad_response'
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
+ module Bluevia
9
+ #
10
+ # This class fetches information related to Advertising API.
11
+ #
12
+
13
+ class Advertising < BaseClient
14
+
15
+ # optional parameters in the request
16
+ OPTIONAL_PARAMS = [:ad_presentation, :ad_presentation_size, :keywords, :protection_policy]
17
+
18
+ # required parameters in the request
19
+ REQUIRED_PARAMS = [:user_agent, :ad_request_id, :ad_space]
20
+
21
+ VALID_PARAMS = OPTIONAL_PARAMS + REQUIRED_PARAMS
22
+
23
+ def initialize(params = nil)
24
+ super(params)
25
+ end
26
+
27
+ # Base Path for Advertising API
28
+ BASEPATH_API = "/Advertising"
29
+
30
+ #
31
+ # This method fetchs an Ad from the server.
32
+ # [*params*] Hash object that must include at least the REQUIRED_PARAMS and
33
+ # cero or more OPTIONAL_PARAMS
34
+ # i.e.
35
+ #
36
+ # response = @service.request(
37
+ # {:user_agent => "Mozilla 5.0",
38
+ # :ad_request_id => "a1x4zasg58",
39
+ # :ad_space => "1200",
40
+ # :keywords => "bar"
41
+ # })
42
+ #
43
+ def request(params)
44
+ Utils.check_attribute(params, "params cannot be null")
45
+
46
+ REQUIRED_PARAMS.each { |param|
47
+ Utils.check_attribute(params[param], "#{param} cannot be null")
48
+ }
49
+
50
+ # delete any parameter that is neither optional nor mandatory
51
+ params.delete_if{|key,value| VALID_PARAMS.index(key).nil?}
52
+
53
+ # include in the request all the parameters
54
+ message = params.collect{|k,v| "#{k}=#{CGI::escape(v.to_s)}"}.join('&')
55
+
56
+ # this should be sent without content-Type because fails to validate signature
57
+ path = set_path("#{get_basepath}/simple/requests")
58
+ path = include_params(path, DEFAULT_PARAMS)
59
+ response = authorized_client.post(
60
+ path,
61
+ message,
62
+ {"Accept" => "application/json"})
63
+
64
+ resp = handle_response(create_response(response)).body
65
+
66
+ filter_response(resp)
67
+ end
68
+
69
+ private
70
+
71
+ def filter_response(response)
72
+ if response.nil? or !response.instance_of?(Hash)
73
+ p "empty!!"
74
+ end
75
+
76
+ return AdResponse.new if response.nil? or !response.instance_of?(Hash)
77
+
78
+ result = AdResponse.new
79
+ #begin
80
+
81
+ id = response[:adResponse][:ad][:resource][:attributes][:ad_presentation]
82
+ resources = response[:adResponse][:ad][:resource][:creative_element]
83
+ item = Hash.new
84
+ if resources.instance_of?(Array)
85
+ resources.each{|resource|
86
+ result << assign_item(resource, id)
87
+ }
88
+ else
89
+ result << assign_item(resources, id)
90
+ end
91
+ #rescue
92
+
93
+ #end
94
+ result
95
+ end
96
+
97
+ def assign_item(resource, id)
98
+ item = Hash.new
99
+ item["type_id"] = id
100
+ item["type_name"] = resource[:attributes][:type]
101
+ item["value"] = resource[:attribute].instance_of?(Array) ? resource[:attribute][1] : resource[:attribute]
102
+ item["interaction"] = resource[:interaction][:attribute]
103
+
104
+ item
105
+ end
106
+
107
+ end
108
+ end