hash-blue 0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +15 -0
- data/LICENSE.LGPLv3 +165 -0
- data/README.md +104 -0
- data/Rakefile +2 -0
- data/hash-blue.gemspec +28 -0
- data/lib/hash-blue.rb +17 -0
- data/lib/hash_blue/account.rb +81 -0
- data/lib/hash_blue/client.rb +58 -0
- data/lib/hash_blue/contact.rb +109 -0
- data/lib/hash_blue/message.rb +147 -0
- data/lib/hash_blue/version.rb +13 -0
- data/spec/account_spec.rb +29 -0
- data/spec/contact_spec.rb +39 -0
- data/spec/message_spec.rb +41 -0
- metadata +77 -0
data/Gemfile
ADDED
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.
|
data/README.md
ADDED
@@ -0,0 +1,104 @@
|
|
1
|
+
O2 Labs has exposed the power of #blue to developers via a simple REST & JSON based API, combined with oAuth2 to developers who can create new ways for users to manage their texts and add combine the ubiquity of SMS with their applications, users simply grant an application access to their messages stream or just certain messages.
|
2
|
+
|
3
|
+
_#_blue is not a service designed for users to text into applications rather it is aimed at enriching the users existing text experience by building on top of their day to day text use.
|
4
|
+
|
5
|
+
This gem provides a smooth access to _#_blue API.
|
6
|
+
|
7
|
+
# How to install
|
8
|
+
|
9
|
+
gem install hash-blue
|
10
|
+
|
11
|
+
# Getting started
|
12
|
+
|
13
|
+
There're three main entities in _#_blue:
|
14
|
+
|
15
|
+
* message: models a SMS sent/received by the user
|
16
|
+
* contact: friend's list including phone number, name and email address.
|
17
|
+
* account: describes the user account
|
18
|
+
|
19
|
+
_#_blue client design is based on ActiveRecord model.
|
20
|
+
|
21
|
+
## Retrieve a valid user token
|
22
|
+
|
23
|
+
_#_blue API is based on oAuth secure mechanism so before using the API the user must grant permissions to your application. Follow the instructions on [the HashBlue documentation](https://api.hashblue.com/doc/Authentication)
|
24
|
+
|
25
|
+
### First step. Redirect user to HashBlue authorization page
|
26
|
+
|
27
|
+
redirect_to "https://hashblue.com/oauth/authorize?client_id=<app_client_id>&redirect_uri=<your_callback_url>"
|
28
|
+
|
29
|
+
### Second step. Get access token
|
30
|
+
|
31
|
+
uri = URI.parse("https://hashblue.com")
|
32
|
+
rest = Net::HTTP.new(uri.host, uri.port)
|
33
|
+
rest.use_ssl = true
|
34
|
+
code = <code_from_first_step>
|
35
|
+
|
36
|
+
query = "client_id=<app_client_id>&client_secret=<app_client_secret>&grant_type=authorization_code&code=#{code}&redirect_uri=<your_callback_url>"
|
37
|
+
access_token = rest.post( "/oauth/access_token?#{query}", nil)
|
38
|
+
|
39
|
+
## Configure the client with a valid access token
|
40
|
+
|
41
|
+
HashBlue::Client.user = <valid_access_token>
|
42
|
+
|
43
|
+
## Work with messages
|
44
|
+
|
45
|
+
### Retrieve all messages
|
46
|
+
|
47
|
+
messages = HashBlue::Message.find(:all)
|
48
|
+
messages = HashBlue::Message.find
|
49
|
+
|
50
|
+
### Retrieve a specific message
|
51
|
+
|
52
|
+
message = HashBlue::Message.find(<valid_message_id>)
|
53
|
+
|
54
|
+
### Retrieve a contact messages
|
55
|
+
|
56
|
+
messages = HashBlue::Message.find({:contact => <valid_contact_id>})
|
57
|
+
|
58
|
+
### Retrieve favourite messages
|
59
|
+
|
60
|
+
messages = HashBlue::Message.favourites
|
61
|
+
|
62
|
+
### Mark a message as favourite
|
63
|
+
|
64
|
+
message = HashBlue::Message.find(<valid_message_id>)
|
65
|
+
message.favourite!
|
66
|
+
|
67
|
+
### Unmark a message as favourite
|
68
|
+
|
69
|
+
message = HashBlue::Message.find(<valid_message_id>)
|
70
|
+
message.unfavourite!
|
71
|
+
|
72
|
+
### Create a message
|
73
|
+
|
74
|
+
HashBlue::Message.create!(<phone_number>, <content>)
|
75
|
+
|
76
|
+
## Work with contacts
|
77
|
+
|
78
|
+
### Retrieve all contacts
|
79
|
+
|
80
|
+
contacts = HashBlue::Contact.find(:all)
|
81
|
+
|
82
|
+
### Retrieve a specific contact
|
83
|
+
|
84
|
+
contact = HashBlue::Contact.find(<contact_id>)
|
85
|
+
|
86
|
+
### Crete a contact
|
87
|
+
|
88
|
+
HashBlue::Contact.create!(<phone_number>, <name>, <email>)
|
89
|
+
|
90
|
+
## Work with account
|
91
|
+
|
92
|
+
### Retrieve user account
|
93
|
+
|
94
|
+
HashBlue::Account.find
|
95
|
+
|
96
|
+
# Author
|
97
|
+
|
98
|
+
Juan de Bravo
|
99
|
+
|
100
|
+
juandebravo at gmail dot com
|
101
|
+
|
102
|
+
Ruby sensei @ [The Lab](http://thelab.o2.com)
|
103
|
+
|
104
|
+
|
data/Rakefile
ADDED
data/hash-blue.gemspec
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "hash_blue/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "hash-blue"
|
7
|
+
s.version = HashBlue::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Juan de Bravo"]
|
10
|
+
s.email = ["juandebravo@gmail.com"]
|
11
|
+
s.homepage = "https://api.hashblue.com/doc/"
|
12
|
+
s.summary = %q{This gem provides a smooth access to _#_blue API.}
|
13
|
+
s.description = %q{O2 Labs has exposed the power of #blue to developers via a
|
14
|
+
simple REST & JSON based API, combined with oAuth2 to developers who can
|
15
|
+
create new ways for users to manage their texts and add combine the ubiquity
|
16
|
+
of SMS with their applications, users simply grant an application access to
|
17
|
+
their messages stream or just certain messages.}
|
18
|
+
|
19
|
+
s.rubyforge_project = "hash-blue"
|
20
|
+
|
21
|
+
s.files = `git ls-files`.split("\n")
|
22
|
+
s.files.delete(".gitignore")
|
23
|
+
s.files.delete(".rspec")
|
24
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
25
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
26
|
+
s.require_paths = ["lib"]
|
27
|
+
|
28
|
+
end
|
data/lib/hash-blue.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'hash_blue/client'
|
2
|
+
require 'hash_blue/contact'
|
3
|
+
require 'hash_blue/message'
|
4
|
+
require 'hash_blue/account'
|
5
|
+
|
6
|
+
#
|
7
|
+
# O2 Labs has exposed the power of #blue to developers via a simple REST & JSON based API,
|
8
|
+
# enabling new ways for users to manage their texts and add combine the ubiquity of SMS
|
9
|
+
# with their applications, users simply grant an application access to their messages stream
|
10
|
+
# or just certain messages.
|
11
|
+
#
|
12
|
+
# Juan de Bravo (juandebravo@gmail.com)
|
13
|
+
# Ruby sensei at The Lab (http://thelab.o2.com)
|
14
|
+
#
|
15
|
+
|
16
|
+
module HashBlue
|
17
|
+
end
|
@@ -0,0 +1,81 @@
|
|
1
|
+
require 'hash_blue/client'
|
2
|
+
|
3
|
+
#
|
4
|
+
# O2 Labs has exposed the power of #blue to developers via a simple REST & JSON based API,
|
5
|
+
# enabling new ways for users to manage their texts and add combine the ubiquity of SMS
|
6
|
+
# with their applications, users simply grant an application access to their messages stream
|
7
|
+
# or just certain messages.
|
8
|
+
#
|
9
|
+
# Juan de Bravo (juandebravo@gmail.com)
|
10
|
+
# Ruby sensei at The Lab (http://thelab.o2.com)
|
11
|
+
#
|
12
|
+
|
13
|
+
module HashBlue
|
14
|
+
|
15
|
+
# This class models the Account entity, providing an easy way to CRUD operations
|
16
|
+
# using ActiveRecord as design model
|
17
|
+
#
|
18
|
+
# Examples of use:
|
19
|
+
#
|
20
|
+
# # Initialize client with a valid access token
|
21
|
+
# HashBlue::Client.user = <valid_access_token>
|
22
|
+
#
|
23
|
+
# # Retrieve all messages
|
24
|
+
# messages = HashBlue::Message.find(:all)
|
25
|
+
#
|
26
|
+
# # Retrieve a specific message
|
27
|
+
# message = HashBlue::Message.find(<valid_message_id>)
|
28
|
+
#
|
29
|
+
# # Retrieve a contact messages
|
30
|
+
# messages = HashBlue::Message.find({:contact => <valid_contact_id>})
|
31
|
+
#
|
32
|
+
# # Retrieve favourite messages
|
33
|
+
# messages = HashBlue::Message.favourites
|
34
|
+
#
|
35
|
+
# # Mark a message as favourite
|
36
|
+
# message = HashBlue::Message.find(<valid_message_id>)
|
37
|
+
# message.favourite!
|
38
|
+
#
|
39
|
+
# # Unmark a message as favourite
|
40
|
+
# message = HashBlue::Message.find(<valid_message_id>)
|
41
|
+
# message.unfavourite!
|
42
|
+
#
|
43
|
+
# # Create a message
|
44
|
+
# HashBlue::Message.create!(<phone_number>, <content>)
|
45
|
+
|
46
|
+
class Account < Client
|
47
|
+
|
48
|
+
attr_accessor :msisdn
|
49
|
+
attr_accessor :phone_number
|
50
|
+
attr_accessor :email
|
51
|
+
|
52
|
+
def to_s
|
53
|
+
"#{self.class.name} => {:phone_number => #{phone_number}, :msisdn => #{msisdn}, :email => #{email}}"
|
54
|
+
end
|
55
|
+
|
56
|
+
# Class methods
|
57
|
+
class << self
|
58
|
+
|
59
|
+
# Get user account
|
60
|
+
def find
|
61
|
+
parse_response(get "/account")
|
62
|
+
end
|
63
|
+
|
64
|
+
private
|
65
|
+
|
66
|
+
# Method used to parse a JSON response
|
67
|
+
def parse_response(data)
|
68
|
+
data = JSON::parse(data)
|
69
|
+
value = Account.new
|
70
|
+
if data.has_key?("account")
|
71
|
+
data = data["account"]
|
72
|
+
value.msisdn = data["msisdn"]
|
73
|
+
value.phone_number = data["phone_number"]
|
74
|
+
value.email = data["email"]
|
75
|
+
end
|
76
|
+
value
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
require 'uri'
|
2
|
+
require 'net/http'
|
3
|
+
require 'net/https'
|
4
|
+
require 'json'
|
5
|
+
|
6
|
+
#
|
7
|
+
# O2 Labs has exposed the power of #blue to developers via a simple REST & JSON based API,
|
8
|
+
# enabling new ways for users to manage their texts and add combine the ubiquity of SMS
|
9
|
+
# with their applications, users simply grant an application access to their messages stream
|
10
|
+
# or just certain messages.
|
11
|
+
#
|
12
|
+
# Juan de Bravo (juandebravo@gmail.com)
|
13
|
+
# Ruby sensei at The Lab (http://thelab.o2.com)
|
14
|
+
#
|
15
|
+
|
16
|
+
module HashBlue
|
17
|
+
# This class is in charge of the HTTP channel with HashBlue API
|
18
|
+
class Client
|
19
|
+
class << self
|
20
|
+
attr_accessor :user
|
21
|
+
|
22
|
+
def client
|
23
|
+
@rest ||= (uri = URI.parse("https://api.hashblue.com")
|
24
|
+
rest = Net::HTTP.new(uri.host, uri.port)
|
25
|
+
rest.use_ssl = true
|
26
|
+
rest)
|
27
|
+
end
|
28
|
+
|
29
|
+
# Send a HTTP GET request (should be used to retrieve available resources)
|
30
|
+
def get(path)
|
31
|
+
# get oAuth token directly from the class or the subclass
|
32
|
+
if user.nil?
|
33
|
+
token = self.superclass.user
|
34
|
+
else
|
35
|
+
token = self.user
|
36
|
+
end
|
37
|
+
client.get(path, {'Authorization' => "OAuth #{token}"}).body
|
38
|
+
end
|
39
|
+
|
40
|
+
# Send a HTTP POST request (should be used to create new resources)
|
41
|
+
def post(path, body)
|
42
|
+
client.post(path, body.to_json, {'Authorization' => "OAuth #{self.superclass.user}",
|
43
|
+
'Content-Type' => "application/json"}).body
|
44
|
+
end
|
45
|
+
|
46
|
+
# Send a HTTP PUT request (should be used to update available resources)
|
47
|
+
def put(path, body = nil)
|
48
|
+
client.put(path, body, {'Authorization' => "OAuth #{self.superclass.user}"})
|
49
|
+
end
|
50
|
+
|
51
|
+
# Send a HTTP DELETE request (should be used to delete available resources)
|
52
|
+
def delete(path)
|
53
|
+
client.delete(path, {'Authorization' => "OAuth #{self.superclass.user}"})
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,109 @@
|
|
1
|
+
require 'hash_blue/contact'
|
2
|
+
|
3
|
+
#
|
4
|
+
# O2 Labs has exposed the power of #blue to developers via a simple REST & JSON based API,
|
5
|
+
# enabling new ways for users to manage their texts and add combine the ubiquity of SMS
|
6
|
+
# with their applications, users simply grant an application access to their messages stream
|
7
|
+
# or just certain messages.
|
8
|
+
#
|
9
|
+
# Juan de Bravo (juandebravo@gmail.com)
|
10
|
+
# Ruby sensei at The Lab (http://thelab.o2.com)
|
11
|
+
#
|
12
|
+
|
13
|
+
module HashBlue
|
14
|
+
|
15
|
+
# This class models the Contact entity, providing an easy way to CRUD operations
|
16
|
+
# using ActiveRecord as design model
|
17
|
+
#
|
18
|
+
# Examples of use:
|
19
|
+
#
|
20
|
+
# # Initialize client with a valid access token
|
21
|
+
# HashBlue::Client.user = <valid_access_token>
|
22
|
+
#
|
23
|
+
# # Retrieve all contacts
|
24
|
+
# messages = HashBlue::Contact.find(:all)
|
25
|
+
#
|
26
|
+
# # Retrieve a specific contact
|
27
|
+
# message = HashBlue::Contact.find(<valid_contact_id>)
|
28
|
+
#
|
29
|
+
# # Create a contact
|
30
|
+
# HashBlue::Contact.create!(<phone_number>, <name>, <email>)
|
31
|
+
|
32
|
+
class Contact < Client
|
33
|
+
|
34
|
+
attr_accessor :id
|
35
|
+
attr_accessor :phone_number
|
36
|
+
attr_accessor :name
|
37
|
+
attr_accessor :email
|
38
|
+
|
39
|
+
def initialize(phone_number = nil, name = nil, email = nil)
|
40
|
+
@phone_number = phone_number
|
41
|
+
@name = name
|
42
|
+
@email = email
|
43
|
+
end
|
44
|
+
|
45
|
+
def save!
|
46
|
+
contact = {:phone_number => phone_number, :name => name, :email => email}
|
47
|
+
self.class.post "/contacts", {:contact => contact}
|
48
|
+
end
|
49
|
+
|
50
|
+
def to_s
|
51
|
+
"#{self.class.name} [#{id}] => {:phone_number => #{phone_number}, :name => #{name}, :email => #{email}}"
|
52
|
+
end
|
53
|
+
|
54
|
+
class << self
|
55
|
+
|
56
|
+
# Retrieve a specific contact or a set of contacts
|
57
|
+
# @param arg:
|
58
|
+
# => nil => retrieve all messages
|
59
|
+
# => :all => retrieve all messages
|
60
|
+
# => {:contact => <contact_id>} => retrieve a specific contact messages
|
61
|
+
# => id => retrieve a specific message using a valid unique identifier
|
62
|
+
|
63
|
+
def find(arg = nil)
|
64
|
+
if arg.nil?
|
65
|
+
parse_response(get "/contacts")
|
66
|
+
elsif arg.is_a? Symbol
|
67
|
+
if arg.eql?(:all)
|
68
|
+
parse_response(get "/contacts")
|
69
|
+
else
|
70
|
+
raise ArgumentError, "Invalid argument #{arg}"
|
71
|
+
end
|
72
|
+
elsif arg.is_a? String or arg.is_a? Fixnum
|
73
|
+
parse_response(get "/contacts/#{arg}")
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
# Create a new contact
|
78
|
+
def create!(phone_number, name, email)
|
79
|
+
contact = {:phone_number => phone_number, :name => name, :email => email}
|
80
|
+
post "/contacts", {:contact => contact}
|
81
|
+
end
|
82
|
+
|
83
|
+
private
|
84
|
+
|
85
|
+
def parse_response(data)
|
86
|
+
data = JSON::parse(data)
|
87
|
+
if data.has_key?("contact")
|
88
|
+
value = Contact.new
|
89
|
+
value.id = data["contact"]["uri"].split("/").last
|
90
|
+
value.phone_number = data["contact"]["phone_number"]
|
91
|
+
value.name = data["contact"]["name"]
|
92
|
+
value.email = data["contact"]["email"]
|
93
|
+
elsif data.has_key?("contacts")
|
94
|
+
value = []
|
95
|
+
data["contacts"].each do |contact|
|
96
|
+
elem = Contact.new
|
97
|
+
elem.id = contact["uri"].split("/").last
|
98
|
+
elem.phone_number = contact["phone_number"]
|
99
|
+
elem.name = contact["name"]
|
100
|
+
elem.email = contact["email"]
|
101
|
+
value.push elem
|
102
|
+
end
|
103
|
+
end
|
104
|
+
value
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
end
|
@@ -0,0 +1,147 @@
|
|
1
|
+
require 'hash_blue/client'
|
2
|
+
|
3
|
+
#
|
4
|
+
# O2 Labs has exposed the power of #blue to developers via a simple REST & JSON based API,
|
5
|
+
# enabling new ways for users to manage their texts and add combine the ubiquity of SMS
|
6
|
+
# with their applications, users simply grant an application access to their messages stream
|
7
|
+
# or just certain messages.
|
8
|
+
#
|
9
|
+
# Juan de Bravo (juandebravo@gmail.com)
|
10
|
+
# Ruby sensei at The Lab (http://thelab.o2.com)
|
11
|
+
#
|
12
|
+
|
13
|
+
module HashBlue
|
14
|
+
|
15
|
+
# This class models the Message entity, providing an easy way to CRUD operations
|
16
|
+
# using ActiveRecord as design model
|
17
|
+
#
|
18
|
+
# Examples of use:
|
19
|
+
#
|
20
|
+
# # Initialize client with a valid access token
|
21
|
+
# HashBlue::Client.user = <valid_access_token>
|
22
|
+
#
|
23
|
+
# # Retrieve all messages
|
24
|
+
# messages = HashBlue::Message.find(:all)
|
25
|
+
#
|
26
|
+
# # Retrieve a specific message
|
27
|
+
# message = HashBlue::Message.find(<valid_message_id>)
|
28
|
+
#
|
29
|
+
# # Retrieve a contact messages
|
30
|
+
# messages = HashBlue::Message.find({:contact => <valid_contact_id>})
|
31
|
+
#
|
32
|
+
# # Retrieve favourite messages
|
33
|
+
# messages = HashBlue::Message.favourites
|
34
|
+
#
|
35
|
+
# # Mark a message as favourite
|
36
|
+
# message = HashBlue::Message.find(<valid_message_id>)
|
37
|
+
# message.favourite!
|
38
|
+
#
|
39
|
+
# # Unmark a message as favourite
|
40
|
+
# message = HashBlue::Message.find(<valid_message_id>)
|
41
|
+
# message.unfavourite!
|
42
|
+
#
|
43
|
+
# # Create a message
|
44
|
+
# HashBlue::Message.create!(<phone_number>, <content>)
|
45
|
+
|
46
|
+
class Message < Client
|
47
|
+
attr_accessor :id
|
48
|
+
attr_accessor :content
|
49
|
+
attr_accessor :contact # HashBlue::Contact object
|
50
|
+
attr_accessor :sent # true|false
|
51
|
+
attr_accessor :timestamp
|
52
|
+
|
53
|
+
# Mark as favourite
|
54
|
+
def favourite!
|
55
|
+
self.class.put "/messages/#{id}/favourite"
|
56
|
+
end
|
57
|
+
|
58
|
+
# Unmark as favourite
|
59
|
+
def unfavourite!
|
60
|
+
self.class.delete "/messages/#{id}/favourite"
|
61
|
+
end
|
62
|
+
|
63
|
+
# Delete message
|
64
|
+
def delete!
|
65
|
+
self.class.delete "/messages/#{id}"
|
66
|
+
end
|
67
|
+
|
68
|
+
# Create message
|
69
|
+
def save!
|
70
|
+
self.class.create!(contact.is_a? HashBlue::Contact ? contact.phone_number : contact, content)
|
71
|
+
end
|
72
|
+
|
73
|
+
def to_s
|
74
|
+
"#{self.class.name} [#{id}] => {:content => #{content}, :contact => #{contact}, :sent => #{sent}, :timestamp => #{timestamp}}"
|
75
|
+
end
|
76
|
+
|
77
|
+
alias :destroy! :delete!
|
78
|
+
|
79
|
+
# Class methods
|
80
|
+
class << self
|
81
|
+
|
82
|
+
# Retrieve a specific message or a set of messages
|
83
|
+
# @param arg:
|
84
|
+
# => nil => retrieve all messages
|
85
|
+
# => :all => retrieve all messages
|
86
|
+
# => {:contact => <contact_id>} => retrieve a specific contact messages
|
87
|
+
# => id => retrieve a specific message using a valid unique identifier
|
88
|
+
|
89
|
+
def find(arg = nil)
|
90
|
+
if arg.nil?
|
91
|
+
messages = get "/messages"
|
92
|
+
elsif arg.is_a? Symbol
|
93
|
+
if arg.eql?(:all)
|
94
|
+
messages = get "/messages"
|
95
|
+
else
|
96
|
+
raise ArgumentError, "Invalid argument #{arg}"
|
97
|
+
end
|
98
|
+
elsif arg.is_a? String or arg.is_a? Fixnum
|
99
|
+
messages = get "/messages/#{arg}"
|
100
|
+
elsif arg.is_a? Hash
|
101
|
+
arg.has_key?(:contact) or raise ArgumentError, "Invalid argument #{arg}"
|
102
|
+
messages = get "/contacts/#{arg[:contact]}/messages"
|
103
|
+
end
|
104
|
+
parse_response(messages)
|
105
|
+
end
|
106
|
+
|
107
|
+
# Retrieve the messaged marked as favourites
|
108
|
+
def favourites
|
109
|
+
parse_response(get "/messages/favourites")
|
110
|
+
end
|
111
|
+
|
112
|
+
# Send a message to a specific number
|
113
|
+
def create!(phone_number, content)
|
114
|
+
message = {:phone_number => phone_number, :content => content}
|
115
|
+
post "/messages", {:message => message}
|
116
|
+
end
|
117
|
+
|
118
|
+
private
|
119
|
+
|
120
|
+
# Method used to parse a JSON response
|
121
|
+
def parse_response(data)
|
122
|
+
data = JSON::parse(data)
|
123
|
+
if data.has_key?("message")
|
124
|
+
value = Message.new
|
125
|
+
value.id = data["message"]["uri"].split("/").last
|
126
|
+
value.content = data["message"]["content"]
|
127
|
+
value.contact = Contact.new(data["message"]["contact"]["phone_number"], data["message"]["contact"]["name"], data["message"]["contact"]["email"])
|
128
|
+
value.timestamp = data["message"]["timestamp"]
|
129
|
+
value.sent = data["message"]["sent"]
|
130
|
+
elsif data.has_key?("messages")
|
131
|
+
value = []
|
132
|
+
data["messages"].each do |message|
|
133
|
+
elem = Message.new
|
134
|
+
elem.id = message["uri"].split("/").last
|
135
|
+
elem.content = message["content"]
|
136
|
+
elem.contact = Contact.new(message["contact"]["phone_number"], message["contact"]["name"], message["contact"]["email"])
|
137
|
+
elem.contact.id = message["contact"]["uri"].split("/").last
|
138
|
+
elem.timestamp = message["timestamp"]
|
139
|
+
elem.sent = message["sent"]
|
140
|
+
value.push elem
|
141
|
+
end
|
142
|
+
end
|
143
|
+
value
|
144
|
+
end
|
145
|
+
end
|
146
|
+
end
|
147
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
#
|
2
|
+
# O2 Labs has exposed the power of #blue to developers via a simple REST & JSON based API,
|
3
|
+
# enabling new ways for users to manage their texts and add combine the ubiquity of SMS
|
4
|
+
# with their applications, users simply grant an application access to their messages stream
|
5
|
+
# or just certain messages.
|
6
|
+
#
|
7
|
+
# Juan de Bravo (juandebravo@gmail.com)
|
8
|
+
# Ruby sensei at The Lab (http://thelab.o2.com)
|
9
|
+
#
|
10
|
+
|
11
|
+
module HashBlue
|
12
|
+
VERSION = "0.1"
|
13
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
$:.unshift File.join(File.dirname(__FILE__),'..','lib')
|
2
|
+
|
3
|
+
require 'hash-blue'
|
4
|
+
require 'webmock/rspec'
|
5
|
+
require 'json'
|
6
|
+
|
7
|
+
describe HashBlue::Account do
|
8
|
+
before(:all) do
|
9
|
+
HashBlue::Client.user = "XXXXX"
|
10
|
+
end
|
11
|
+
context "initialize" do
|
12
|
+
it "should be an instance of HashBlue::Account when instanciated" do
|
13
|
+
HashBlue::Account.new.should be_an_instance_of(HashBlue::Account)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
context "find account" do
|
18
|
+
it "should return a valid account when including valid header" do
|
19
|
+
stub_request(:get, "https://api.hashblue.com/account").
|
20
|
+
with(:headers => {'Accept'=>'*/*', 'Authorization'=>'OAuth XXXXX'}).
|
21
|
+
to_return(:status => 200, :body => '{"account":{"messages":"https://api.hashblue.com/messages","msisdn":"447711223344","phone_number":"07711223344","contacts":"https://api.hashblue.com/contacts","email":"foo@hashblue.com"}}', :headers => {})
|
22
|
+
account = HashBlue::Account.find
|
23
|
+
account.msisdn.should eql("447711223344")
|
24
|
+
account.phone_number.should eql("07711223344")
|
25
|
+
account.email.should eql("foo@hashblue.com")
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
$:.unshift File.join(File.dirname(__FILE__),'..','lib')
|
2
|
+
|
3
|
+
require 'hash-blue'
|
4
|
+
require 'webmock/rspec'
|
5
|
+
require 'json'
|
6
|
+
|
7
|
+
describe HashBlue::Contact do
|
8
|
+
before(:all) do
|
9
|
+
HashBlue::Client.user = "XXXXX"
|
10
|
+
end
|
11
|
+
context "initialize" do
|
12
|
+
it "should be an instance of HashBlue::Contact when instanciated" do
|
13
|
+
HashBlue::Contact.new.should be_an_instance_of(HashBlue::Contact)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
context "find a unique contact" do
|
18
|
+
it "should return a valid message when ID is valid" do
|
19
|
+
stub_request(:get, "https://api.hashblue.com/contacts/foo").
|
20
|
+
with(:headers => {'Accept'=>'*/*', 'Authorization'=>'OAuth XXXXX'}).
|
21
|
+
to_return(:status => 200, :body => '{"contact":{"name":"Juan","messages":"https://api.hashblue.com/contacts/dtjgcih62q5d2zl0p6k93xmw7/messages","uri":"https://api.hashblue.com/contacts/foo","msisdn":"447711223344","phone_number":"07711223344","email":"juan@foo.com"}}', :headers => {})
|
22
|
+
contact = HashBlue::Contact.find("foo")
|
23
|
+
contact.id.should eql("foo")
|
24
|
+
contact.phone_number.should eql("07711223344")
|
25
|
+
contact.name.should eql("Juan")
|
26
|
+
contact.email.should eql("juan@foo.com")
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
context "create a new contact" do
|
31
|
+
it "should create a contact successfully when phone_number, name and email provided" do
|
32
|
+
stub_request(:post, "https://api.hashblue.com/contacts").
|
33
|
+
with(:body => {:contact => {:phone_number => '123', :name => 'foo', :email => 'foo@bar.com'}}, :headers => {'Accept'=>'*/*', 'Authorization'=>'OAuth XXXXX'})
|
34
|
+
|
35
|
+
HashBlue::Contact.create!("123", "foo", "foo@bar.com")
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
$:.unshift File.join(File.dirname(__FILE__),'..','lib')
|
2
|
+
|
3
|
+
require 'hash-blue'
|
4
|
+
require 'webmock/rspec'
|
5
|
+
require 'json'
|
6
|
+
|
7
|
+
describe HashBlue::Message do
|
8
|
+
before(:all) do
|
9
|
+
HashBlue::Client.user = "XXXXX"
|
10
|
+
end
|
11
|
+
|
12
|
+
context "initialize" do
|
13
|
+
it "should be an instance of HashBlue::Message when instanciated" do
|
14
|
+
HashBlue::Message.new.should be_an_instance_of(HashBlue::Message)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
context "find a unique message" do
|
19
|
+
it "should return a valid message when ID is valid" do
|
20
|
+
stub_request(:get, "https://api.hashblue.com/messages/foo").
|
21
|
+
with(:headers => {'Accept'=>'*/*', 'Authorization'=>'OAuth XXXXX'}).
|
22
|
+
to_return(:status => 200, :body => '{"message":{"uri":"https://api.hashblue.com/messages/foo","timestamp":"2011-05-09T21:43:52Z","sent":true,"contact":{"name":"Mike","uri":"https://api.hashblue.com/contacts/mike","messages":"https://api.hashblue.com/contacts/mike/messages","phone_number":"07711223344","msisdn":"447711223344","email":"mike@foo.com"},"content":"test","favourite":false}}', :headers => {})
|
23
|
+
|
24
|
+
message = HashBlue::Message.find("foo")
|
25
|
+
message.id.should eql("foo")
|
26
|
+
message.sent.should eql(true)
|
27
|
+
message.contact.should be_an_instance_of(HashBlue::Contact)
|
28
|
+
message.contact.email.should eql("mike@foo.com")
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
context "create a new message" do
|
33
|
+
it "should create a message successfully when phone_number and content provided" do
|
34
|
+
stub_request(:post, "https://api.hashblue.com/messages").
|
35
|
+
with(:body => {:message => {:phone_number => 123, :content => 'yet another test'}}, :headers => {'Accept'=>'*/*', 'Authorization'=>'OAuth XXXXX'})
|
36
|
+
|
37
|
+
HashBlue::Message.create!(123, "yet another test")
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
metadata
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hash-blue
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: "0.1"
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Juan de Bravo
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2011-05-10 00:00:00 +02:00
|
14
|
+
default_executable:
|
15
|
+
dependencies: []
|
16
|
+
|
17
|
+
description: |-
|
18
|
+
O2 Labs has exposed the power of #blue to developers via a
|
19
|
+
simple REST & JSON based API, combined with oAuth2 to developers who can
|
20
|
+
create new ways for users to manage their texts and add combine the ubiquity
|
21
|
+
of SMS with their applications, users simply grant an application access to
|
22
|
+
their messages stream or just certain messages.
|
23
|
+
email:
|
24
|
+
- juandebravo@gmail.com
|
25
|
+
executables: []
|
26
|
+
|
27
|
+
extensions: []
|
28
|
+
|
29
|
+
extra_rdoc_files: []
|
30
|
+
|
31
|
+
files:
|
32
|
+
- Gemfile
|
33
|
+
- LICENSE.LGPLv3
|
34
|
+
- README.md
|
35
|
+
- Rakefile
|
36
|
+
- hash-blue.gemspec
|
37
|
+
- lib/hash-blue.rb
|
38
|
+
- lib/hash_blue/account.rb
|
39
|
+
- lib/hash_blue/client.rb
|
40
|
+
- lib/hash_blue/contact.rb
|
41
|
+
- lib/hash_blue/message.rb
|
42
|
+
- lib/hash_blue/version.rb
|
43
|
+
- spec/account_spec.rb
|
44
|
+
- spec/contact_spec.rb
|
45
|
+
- spec/message_spec.rb
|
46
|
+
has_rdoc: true
|
47
|
+
homepage: https://api.hashblue.com/doc/
|
48
|
+
licenses: []
|
49
|
+
|
50
|
+
post_install_message:
|
51
|
+
rdoc_options: []
|
52
|
+
|
53
|
+
require_paths:
|
54
|
+
- lib
|
55
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
56
|
+
none: false
|
57
|
+
requirements:
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: "0"
|
61
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
62
|
+
none: false
|
63
|
+
requirements:
|
64
|
+
- - ">="
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: "0"
|
67
|
+
requirements: []
|
68
|
+
|
69
|
+
rubyforge_project: hash-blue
|
70
|
+
rubygems_version: 1.6.2
|
71
|
+
signing_key:
|
72
|
+
specification_version: 3
|
73
|
+
summary: This gem provides a smooth access to _#_blue API.
|
74
|
+
test_files:
|
75
|
+
- spec/account_spec.rb
|
76
|
+
- spec/contact_spec.rb
|
77
|
+
- spec/message_spec.rb
|