hashblue 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +26 -0
- data/lib/hashblue/client.rb +22 -10
- data/lib/hashblue/collection.rb +43 -0
- data/lib/hashblue/contact.rb +1 -1
- data/lib/hashblue/message.rb +18 -2
- data/lib/hashblue/version.rb +1 -1
- data/lib/hashblue.rb +1 -0
- data/spec/models/client_spec.rb +32 -0
- data/spec/models/collection_spec.rb +103 -0
- data/spec/models/contact_spec.rb +1 -0
- data/spec/models/message_spec.rb +56 -0
- metadata +7 -3
data/README.md
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
This gem provides an easy way to access the hashblue API (https://api.hashblue.com) from ruby programs. Currently the gem is limited to reading data from the API, but it is intended to add all API features shortly. It also doesn't handle pagination, instead only returning the first 50 results. This too should be rectified soon.
|
2
|
+
|
3
|
+
### Getting started
|
4
|
+
|
5
|
+
Using this gem is very straightforward. First, you need to obtain an OAuth2 access token (see https://api.hashblue.com/doc/Authentication). Then, create a new client:
|
6
|
+
|
7
|
+
client = Hashblue::Client.new('oauth2-access-token')
|
8
|
+
|
9
|
+
Finally, use that client to start to navigate the API:
|
10
|
+
|
11
|
+
contacts = client.account.contacts
|
12
|
+
first_contact_messages = contacts.first.messages
|
13
|
+
|
14
|
+
You can also pass in query parameters to restrict the results:
|
15
|
+
|
16
|
+
messages_abouts_pubs = client.messages(:q => 'pub')
|
17
|
+
|
18
|
+
And if there are additional pages of results, access them too:
|
19
|
+
|
20
|
+
second_page = client.messages(:q => 'pub').next_page
|
21
|
+
|
22
|
+
It's possible to delete messages, and mark them as favourites:
|
23
|
+
|
24
|
+
client.contacts.first.messages.first.delete!
|
25
|
+
client.messages.first.favourite!
|
26
|
+
client.messages.first.unfavourite!
|
data/lib/hashblue/client.rb
CHANGED
@@ -3,7 +3,7 @@ require 'httparty'
|
|
3
3
|
module Hashblue
|
4
4
|
class Client
|
5
5
|
class RequestError < StandardError; end
|
6
|
-
|
6
|
+
|
7
7
|
include HTTParty
|
8
8
|
|
9
9
|
base_uri "https://api.hashblue.com"
|
@@ -23,32 +23,44 @@ module Hashblue
|
|
23
23
|
load_messages("/messages", query)
|
24
24
|
end
|
25
25
|
|
26
|
+
def favourites(query = {})
|
27
|
+
load_messages("/messages/favourites", query)
|
28
|
+
end
|
29
|
+
|
26
30
|
def contacts(query = {})
|
27
31
|
load_contacts("/contacts", query)
|
28
32
|
end
|
29
33
|
|
30
34
|
def load_messages(uri, query = {})
|
31
|
-
get(uri, query)
|
32
|
-
Message.new(self, m)
|
33
|
-
end
|
35
|
+
Collection.new(self, Message, get(uri, query), "messages")
|
34
36
|
end
|
35
37
|
|
36
38
|
def load_contacts(uri, query = {})
|
37
|
-
get(uri, query)
|
38
|
-
Contact.new(self, m)
|
39
|
-
end
|
39
|
+
Collection.new(self, Contact, get(uri, query), "contacts")
|
40
40
|
end
|
41
41
|
|
42
42
|
def get(path, query = {})
|
43
|
-
|
43
|
+
request :get, path, query
|
44
|
+
end
|
45
|
+
|
46
|
+
def delete(path, query = {})
|
47
|
+
request :delete, path, query
|
48
|
+
end
|
49
|
+
|
50
|
+
def put(path, query, body)
|
51
|
+
request :put, path, query, body
|
52
|
+
end
|
53
|
+
|
54
|
+
private
|
55
|
+
|
56
|
+
def request(method, path, query, body = nil)
|
57
|
+
response = self.class.send method, path, :query => query, :headers => request_headers
|
44
58
|
case response.headers["status"]
|
45
59
|
when "200" then response.to_hash
|
46
60
|
else raise RequestError, "request unsuccessful: #{response.to_hash.inspect}"
|
47
61
|
end
|
48
62
|
end
|
49
63
|
|
50
|
-
private
|
51
|
-
|
52
64
|
def request_headers
|
53
65
|
{"Authorization" => "OAuth #{access_token}", 'Accept' => 'application/json'}
|
54
66
|
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
class Hashblue::Collection
|
2
|
+
def initialize(client, model_class, data, name)
|
3
|
+
@client = client
|
4
|
+
@model_class = model_class
|
5
|
+
@data = data
|
6
|
+
@name = name
|
7
|
+
@items = @data[name].collect do |item|
|
8
|
+
model_class.new(@client, item)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def respond_to?(method)
|
13
|
+
super || @items.respond_to?(method)
|
14
|
+
end
|
15
|
+
|
16
|
+
def method_missing(method, *args, &block)
|
17
|
+
@items.respond_to?(method) ? @items.send(method, *args, &block) : super
|
18
|
+
end
|
19
|
+
|
20
|
+
def next_page?
|
21
|
+
!!@data["next_page_uri"]
|
22
|
+
end
|
23
|
+
|
24
|
+
def next_page
|
25
|
+
if next_page?
|
26
|
+
self.class.new(@client, @model_class, @client.get(@data["next_page_uri"]), @name)
|
27
|
+
else
|
28
|
+
nil
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def previous_page?
|
33
|
+
!!@data["previous_page_uri"]
|
34
|
+
end
|
35
|
+
|
36
|
+
def previous_page
|
37
|
+
if previous_page?
|
38
|
+
self.class.new(@client, @model_class, @client.get(@data["previous_page_uri"]), @name)
|
39
|
+
else
|
40
|
+
nil
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
data/lib/hashblue/contact.rb
CHANGED
data/lib/hashblue/message.rb
CHANGED
@@ -2,6 +2,10 @@ module Hashblue
|
|
2
2
|
class Message < Model
|
3
3
|
attribute_methods :uri, :timestamp, :content
|
4
4
|
|
5
|
+
def contact
|
6
|
+
@contact ||= Contact.new(client, @attributes["contact"])
|
7
|
+
end
|
8
|
+
|
5
9
|
def sent?
|
6
10
|
@attributes["sent"]
|
7
11
|
end
|
@@ -10,8 +14,20 @@ module Hashblue
|
|
10
14
|
@attributes["favourite"]
|
11
15
|
end
|
12
16
|
|
13
|
-
def
|
14
|
-
|
17
|
+
def delete!
|
18
|
+
client.delete(uri)
|
19
|
+
end
|
20
|
+
|
21
|
+
def favourite!
|
22
|
+
client.put(uri + '/favourite', {}, {})
|
23
|
+
@attributes['favourite'] = true
|
24
|
+
true
|
25
|
+
end
|
26
|
+
|
27
|
+
def unfavourite!
|
28
|
+
client.delete(uri + '/favourite')
|
29
|
+
@attributes['favourite'] = false
|
30
|
+
true
|
15
31
|
end
|
16
32
|
end
|
17
33
|
end
|
data/lib/hashblue/version.rb
CHANGED
data/lib/hashblue.rb
CHANGED
@@ -3,6 +3,7 @@ require "hashblue/version"
|
|
3
3
|
module Hashblue
|
4
4
|
autoload :Account, 'hashblue/account'
|
5
5
|
autoload :Client, 'hashblue/client'
|
6
|
+
autoload :Collection, 'hashblue/collection'
|
6
7
|
autoload :Contact, 'hashblue/contact'
|
7
8
|
autoload :Message, 'hashblue/message'
|
8
9
|
autoload :Model, 'hashblue/model'
|
data/spec/models/client_spec.rb
CHANGED
@@ -64,4 +64,36 @@ describe Hashblue::Client do
|
|
64
64
|
subject.contacts(:anything => :here)
|
65
65
|
end
|
66
66
|
end
|
67
|
+
|
68
|
+
describe '#favourites(query = {})' do
|
69
|
+
it "loads messages from '/messages/favourites'" do
|
70
|
+
subject.expects(:load_messages).with('/messages/favourites', {}).returns([:favourite_messages])
|
71
|
+
subject.favourites.should eql([:favourite_messages])
|
72
|
+
end
|
73
|
+
|
74
|
+
it "passes query through to load_messages" do
|
75
|
+
subject.expects(:load_messages).with('/messages/favourites', {:anything => :here})
|
76
|
+
subject.favourites(:anything => :here)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
describe '#load_messages(uri, query)' do
|
81
|
+
it "builds a Collection of messages with the response from the uri" do
|
82
|
+
response = {"messages" => [{"content" => "hello"}]}
|
83
|
+
subject.stubs(:get).returns(response)
|
84
|
+
collection = mock()
|
85
|
+
Hashblue::Collection.expects(:new).with(subject, Hashblue::Message, response, "messages").returns(collection)
|
86
|
+
subject.load_messages("/messages")
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
describe '#load_contacts(uri, query)' do
|
91
|
+
it "builds a Collection of contacts with the response from the uri" do
|
92
|
+
response = {"contacts" => [{"msisdn" => "1234"}]}
|
93
|
+
subject.stubs(:get).returns(response)
|
94
|
+
collection = mock()
|
95
|
+
Hashblue::Collection.expects(:new).with(subject, Hashblue::Contact, response, "contacts").returns(collection)
|
96
|
+
subject.load_contacts("/contacts")
|
97
|
+
end
|
98
|
+
end
|
67
99
|
end
|
@@ -0,0 +1,103 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Hashblue::Collection do
|
4
|
+
let :client do
|
5
|
+
Hashblue::Client.new('hashblue-access-token')
|
6
|
+
end
|
7
|
+
|
8
|
+
describe "in general" do
|
9
|
+
subject do
|
10
|
+
Hashblue::Collection.new(client, Hashblue::Contact, {"contacts" => [
|
11
|
+
{'msisdn' => 1}, {'msisdn' => 2}, {'msisdn' => 3}
|
12
|
+
]}, "contacts")
|
13
|
+
end
|
14
|
+
|
15
|
+
it "passes array methods to an array of models built from provided data" do
|
16
|
+
subject[0].should eql(Hashblue::Contact.new(client, {'msisdn' => 1}))
|
17
|
+
subject.size.should eql(3)
|
18
|
+
subject.collect {|c| c.msisdn}.should eql([1, 2, 3])
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
describe "a collection without a next_page_uri" do
|
23
|
+
subject do
|
24
|
+
Hashblue::Collection.new(client, Hashblue::Contact, {"contacts" => []}, "contacts")
|
25
|
+
end
|
26
|
+
|
27
|
+
describe '#next_page?' do
|
28
|
+
it "returns false" do
|
29
|
+
subject.next_page?.should be_false
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
describe '#next_page' do
|
34
|
+
it "returns nil" do
|
35
|
+
subject.next_page.should be_false
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
describe "a collection with a next_page_uri" do
|
41
|
+
subject do
|
42
|
+
Hashblue::Collection.new(client, Hashblue::Contact, {"contacts" => [], "next_page_uri" => "https://api.example.com/contacts/more"}, "contacts")
|
43
|
+
end
|
44
|
+
|
45
|
+
describe '#next_page?' do
|
46
|
+
it "returns true" do
|
47
|
+
subject.next_page?.should be_true
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
describe '#next_page' do
|
52
|
+
it "returns a new collection built from the response from the next_page_uri" do
|
53
|
+
subject
|
54
|
+
response = {"contacts" => []}
|
55
|
+
client.stubs(:get).with("https://api.example.com/contacts/more").returns(response)
|
56
|
+
collection = mock()
|
57
|
+
Hashblue::Collection.expects(:new).with(client, Hashblue::Contact, response, "contacts").returns(collection)
|
58
|
+
subject.next_page.should eql(collection)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
describe "a collection without a previous_page_uri" do
|
64
|
+
subject do
|
65
|
+
Hashblue::Collection.new(client, Hashblue::Contact, {"contacts" => []}, "contacts")
|
66
|
+
end
|
67
|
+
|
68
|
+
describe '#previous_page?' do
|
69
|
+
it "returns false" do
|
70
|
+
subject.next_page?.should be_false
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
describe '#previous_page' do
|
75
|
+
it "returns nil" do
|
76
|
+
subject.next_page.should be_false
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
describe "a collection with a previous_page_uri" do
|
82
|
+
subject do
|
83
|
+
Hashblue::Collection.new(client, Hashblue::Contact, {"contacts" => [], "previous_page_uri" => "https://api.example.com/contacts/less"}, "contacts")
|
84
|
+
end
|
85
|
+
|
86
|
+
describe '#previous_page?' do
|
87
|
+
it "returns true" do
|
88
|
+
subject.previous_page?.should be_true
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
describe '#previous_page' do
|
93
|
+
it "returns a new collection built from the response from the next_page_uri" do
|
94
|
+
subject
|
95
|
+
response = {"contacts" => []}
|
96
|
+
client.stubs(:get).with("https://api.example.com/contacts/less").returns(response)
|
97
|
+
collection = mock()
|
98
|
+
Hashblue::Collection.expects(:new).with(client, Hashblue::Contact, response, "contacts").returns(collection)
|
99
|
+
subject.previous_page.should eql(collection)
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
data/spec/models/contact_spec.rb
CHANGED
data/spec/models/message_spec.rb
CHANGED
@@ -7,6 +7,7 @@ describe Hashblue::Message do
|
|
7
7
|
|
8
8
|
subject do
|
9
9
|
Hashblue::Message.new(client, {
|
10
|
+
'uri' => 'https://api.example.com/messages/abcdef123456',
|
10
11
|
'sent' => true,
|
11
12
|
'favourite' => false,
|
12
13
|
'contact' => {
|
@@ -15,6 +16,18 @@ describe Hashblue::Message do
|
|
15
16
|
})
|
16
17
|
end
|
17
18
|
|
19
|
+
describe 'favourite?' do
|
20
|
+
it "returns the 'favourite' attribute" do
|
21
|
+
subject.favourite?.should be_false
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
describe 'sent?' do
|
26
|
+
it "returns the 'sent' attribute" do
|
27
|
+
subject.sent?.should be_true
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
18
31
|
describe '#contact' do
|
19
32
|
it "returns Contact built with contact attributes" do
|
20
33
|
subject.contact.should eql(Hashblue::Contact.new(client, {
|
@@ -22,4 +35,47 @@ describe Hashblue::Message do
|
|
22
35
|
}))
|
23
36
|
end
|
24
37
|
end
|
38
|
+
|
39
|
+
describe '#delete!' do
|
40
|
+
it "sends a delete request to the message uri" do
|
41
|
+
client.expects(:delete).with('https://api.example.com/messages/abcdef123456')
|
42
|
+
subject.delete!
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
describe '#favourite!' do
|
47
|
+
it "sends a put request to the message favourite uri" do
|
48
|
+
client.expects(:put).with('https://api.example.com/messages/abcdef123456/favourite', {}, {})
|
49
|
+
subject.favourite!
|
50
|
+
end
|
51
|
+
|
52
|
+
it "returns true when request doesn't raise an error" do
|
53
|
+
client.stubs(:put)
|
54
|
+
subject.favourite!.should be_true
|
55
|
+
end
|
56
|
+
|
57
|
+
it "sets favourite attribute to true in local model" do
|
58
|
+
client.stubs(:put)
|
59
|
+
subject.favourite!
|
60
|
+
subject.favourite?.should be_true
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
describe '#unfavourite!' do
|
65
|
+
it "sends a delete request to the message favourite uri" do
|
66
|
+
client.expects(:delete).with('https://api.example.com/messages/abcdef123456/favourite')
|
67
|
+
subject.unfavourite!
|
68
|
+
end
|
69
|
+
|
70
|
+
it "returns true when request doesn't raise an error" do
|
71
|
+
client.stubs(:delete)
|
72
|
+
subject.unfavourite!.should be_true
|
73
|
+
end
|
74
|
+
|
75
|
+
it "sets favourite attribute to true in local model" do
|
76
|
+
client.stubs(:delete)
|
77
|
+
subject.unfavourite!
|
78
|
+
subject.favourite?.should be_false
|
79
|
+
end
|
80
|
+
end
|
25
81
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hashblue
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 27
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 2
|
10
|
+
version: 0.0.2
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Tom Ward
|
@@ -93,17 +93,20 @@ files:
|
|
93
93
|
- .rspec
|
94
94
|
- Gemfile
|
95
95
|
- MIT-LICENCE
|
96
|
+
- README.md
|
96
97
|
- Rakefile
|
97
98
|
- hashblue.gemspec
|
98
99
|
- lib/hashblue.rb
|
99
100
|
- lib/hashblue/account.rb
|
100
101
|
- lib/hashblue/client.rb
|
102
|
+
- lib/hashblue/collection.rb
|
101
103
|
- lib/hashblue/contact.rb
|
102
104
|
- lib/hashblue/message.rb
|
103
105
|
- lib/hashblue/model.rb
|
104
106
|
- lib/hashblue/version.rb
|
105
107
|
- spec/models/account_spec.rb
|
106
108
|
- spec/models/client_spec.rb
|
109
|
+
- spec/models/collection_spec.rb
|
107
110
|
- spec/models/contact_spec.rb
|
108
111
|
- spec/models/message_spec.rb
|
109
112
|
- spec/spec_helper.rb
|
@@ -143,6 +146,7 @@ summary: A simple client for the hashblue api
|
|
143
146
|
test_files:
|
144
147
|
- spec/models/account_spec.rb
|
145
148
|
- spec/models/client_spec.rb
|
149
|
+
- spec/models/collection_spec.rb
|
146
150
|
- spec/models/contact_spec.rb
|
147
151
|
- spec/models/message_spec.rb
|
148
152
|
- spec/spec_helper.rb
|