hashblue 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -4,20 +4,20 @@ This gem provides an easy way to access the hashblue API (https://api.hashblue.c
4
4
 
5
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
6
 
7
- client = Hashblue::Client.new('oauth2-access-token')
7
+ account = Hashblue::Account.authenticate('oauth2-access-token')
8
8
 
9
9
  Finally, use that client to start to navigate the API:
10
10
 
11
- contacts = client.account.contacts
11
+ contacts = account.contacts
12
12
  first_contact_messages = contacts.first.messages
13
13
 
14
14
  You can also pass in query parameters to restrict the results:
15
15
 
16
- messages_abouts_pubs = client.messages(:q => 'pub')
16
+ messages_abouts_pubs = account.messages(:q => 'pub')
17
17
 
18
18
  And if there are additional pages of results, access them too:
19
19
 
20
- second_page = client.messages(:q => 'pub').next_page
20
+ second_page = account.messages(:q => 'pub').next_page
21
21
 
22
22
  It's possible to delete messages, and mark them as favourites:
23
23
 
data/hashblue.gemspec CHANGED
@@ -19,6 +19,7 @@ Gem::Specification.new do |s|
19
19
  s.require_paths = ["lib"]
20
20
 
21
21
  s.add_dependency 'httparty', '~>0.7.8'
22
+ s.add_dependency 'activesupport', '~>3.0'
22
23
 
23
24
  s.add_development_dependency 'rake', '~>0.9.2'
24
25
  s.add_development_dependency 'rspec', '~>2.6.0'
@@ -1,5 +1,13 @@
1
1
  module Hashblue
2
2
  class Account < Model
3
+ class << self
4
+ def authenticate(access_token)
5
+ client = Hashblue::Client.new(access_token)
6
+ response = client.get("/account")
7
+ Account.new(client, response["account"])
8
+ end
9
+ end
10
+
3
11
  attribute_methods :msisdn, :phone_number, :email
4
12
 
5
13
  def messages(query = {})
@@ -10,6 +18,10 @@ module Hashblue
10
18
  client.load_contacts(contacts_uri, query)
11
19
  end
12
20
 
21
+ def favourite_messages(query = {})
22
+ client.load_messages(favourite_messages_uri, query)
23
+ end
24
+
13
25
  def messages_uri
14
26
  @attributes["messages"]
15
27
  end
@@ -17,5 +29,9 @@ module Hashblue
17
29
  def contacts_uri
18
30
  @attributes["contacts"]
19
31
  end
32
+
33
+ def favourite_messages_uri
34
+ @attributes["favourite_messages"]
35
+ end
20
36
  end
21
37
  end
@@ -1,4 +1,6 @@
1
1
  require 'httparty'
2
+ require 'active_support/ordered_hash'
3
+ require 'active_support/json'
2
4
 
3
5
  module Hashblue
4
6
  class Client
@@ -7,28 +9,14 @@ module Hashblue
7
9
  include HTTParty
8
10
 
9
11
  base_uri "https://api.hashblue.com"
12
+ headers 'Accept' => 'application/json',
13
+ 'User-Agent' => "Hashblue Client Gem v#{Hashblue::VERSION} (https://github.com/freerange/hashblue-gem)"
10
14
 
11
- attr_reader :access_token, :refresh_token
12
15
 
13
- def initialize(access_token, refresh_token = nil)
14
- @access_token = access_token
15
- @refresh_token = refresh_token
16
- end
17
-
18
- def account
19
- @account ||= Account.new(self, get("/account")["account"])
20
- end
16
+ attr_reader :access_token
21
17
 
22
- def messages(query = {})
23
- load_messages("/messages", query)
24
- end
25
-
26
- def favourites(query = {})
27
- load_messages("/messages/favourites", query)
28
- end
29
-
30
- def contacts(query = {})
31
- load_contacts("/contacts", query)
18
+ def initialize(access_token)
19
+ @access_token = access_token
32
20
  end
33
21
 
34
22
  def load_messages(uri, query = {})
@@ -51,18 +39,30 @@ module Hashblue
51
39
  request :put, path, query, body
52
40
  end
53
41
 
42
+ def post(path, query, body)
43
+ request :post, path, query, body
44
+ end
45
+
54
46
  private
55
47
 
56
48
  def request(method, path, query, body = nil)
57
- response = self.class.send method, path, :query => query, :headers => request_headers
58
- case response.headers["status"]
59
- when "200" then response.to_hash
60
- else raise RequestError, "request unsuccessful: #{response.to_hash.inspect}"
49
+ options = {:query => query, :headers => self.class.headers.merge(authorization_header)}
50
+ if body
51
+ options[:headers]["Content-type"] = "application/json"
52
+ options[:body] = ActiveSupport::JSON.encode(body)
53
+ end
54
+
55
+ response = self.class.send method, path, options
56
+ case response.code
57
+ when 200, 201 then response.to_hash
58
+ else raise RequestError, "request unsuccessful: #{response.inspect}"
61
59
  end
62
60
  end
63
61
 
64
- def request_headers
65
- {"Authorization" => "OAuth #{access_token}", 'Accept' => 'application/json'}
62
+ def authorization_header
63
+ {
64
+ "Authorization" => "OAuth #{access_token}",
65
+ }
66
66
  end
67
67
  end
68
68
  end
@@ -9,5 +9,9 @@ module Hashblue
9
9
  def messages_uri
10
10
  @attributes["messages"]
11
11
  end
12
+
13
+ def send_message(content)
14
+ client.post messages_uri, {}, "message" => {"content" => content}
15
+ end
12
16
  end
13
17
  end
@@ -1,3 +1,3 @@
1
1
  module Hashblue
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
data/lib/hashblue.rb CHANGED
@@ -1,5 +1,3 @@
1
- require "hashblue/version"
2
-
3
1
  module Hashblue
4
2
  autoload :Account, 'hashblue/account'
5
3
  autoload :Client, 'hashblue/client'
@@ -8,10 +8,22 @@ describe Hashblue::Account do
8
8
  subject do
9
9
  Hashblue::Account.new(client, {
10
10
  'messages' => 'https://api.example.com/messages',
11
- 'contacts' => 'https://api.example.com/contacts'
11
+ 'contacts' => 'https://api.example.com/contacts',
12
+ 'favourite_messages' => 'https://api.example.com/messages/favourites'
12
13
  })
13
14
  end
14
15
 
16
+ describe '.authenticate' do
17
+ it "returns Account model built with response from GET /account" do
18
+ client.class.stubs(:new).with('access-token').returns(client)
19
+ attributes = {
20
+ "msisdn" => "4479001234567"
21
+ }
22
+ client.stubs(:get).with('/account').returns("account" => attributes)
23
+ Hashblue::Account.authenticate('access-token').should eql(Hashblue::Account.new(client, attributes))
24
+ end
25
+ end
26
+
15
27
  describe '#messages' do
16
28
  it "loads messages from its messages uri" do
17
29
  client.expects(:load_messages).with('https://api.example.com/messages', {}).returns([:some_messages])
@@ -25,4 +37,11 @@ describe Hashblue::Account do
25
37
  subject.contacts.should eql([:some_contacts])
26
38
  end
27
39
  end
40
+
41
+ describe '#favourite_messages' do
42
+ it "loads messages from its favourite messages uri" do
43
+ client.expects(:load_messages).with('https://api.example.com/messages/favourites', {}).returns([:some_messages])
44
+ subject.favourite_messages.should eql([:some_messages])
45
+ end
46
+ end
28
47
  end
@@ -5,78 +5,44 @@ describe Hashblue::Client do
5
5
  Hashblue::Client.new('hashblue-access-token')
6
6
  end
7
7
 
8
+ describe 'all requests' do
9
+ it 'point to api.hashblue.com' do
10
+ Hashblue::Client.base_uri.should == 'https://api.hashblue.com'
11
+ end
12
+
13
+ it 'request json responses' do
14
+ Hashblue::Client.headers['Accept'].should == 'application/json'
15
+ end
16
+
17
+ it 'include a user-agent' do
18
+ Hashblue::Client.headers['User-Agent'].should_not be_nil
19
+ end
20
+ end
21
+
8
22
  describe '#get' do
9
23
  it "makes requests with Authorization and Accept headers and passed query" do
10
24
  Hashblue::Client.expects(:get).with("/messages",
11
- :headers => {
12
- "Authorization" => "OAuth hashblue-access-token",
13
- "Accept" => "application/json"
14
- },
25
+ :headers => Hashblue::Client.headers.merge({
26
+ "Authorization" => "OAuth hashblue-access-token"
27
+ }),
15
28
  :query => {
16
29
  :since => '2011-01-14T14:30Z'
17
30
  }
18
- ).returns(mock(:headers => {"status" => "200"}, :to_hash => {}))
31
+ ).returns(mock(:code => 200, :to_hash => {}))
19
32
  subject.get "/messages", :since => '2011-01-14T14:30Z'
20
33
  end
21
34
 
22
- it "raises RequestError if response status isn't 200" do
35
+ it "raises RequestError if response code isn't 200" do
23
36
  Hashblue::Client.expects(:get).with("/messages",
24
- :headers => {
25
- "Authorization" => "OAuth hashblue-access-token",
26
- "Accept" => "application/json"
27
- },
37
+ :headers => Hashblue::Client.headers.merge({
38
+ "Authorization" => "OAuth hashblue-access-token"
39
+ }),
28
40
  :query => {}
29
- ).returns(mock(:headers => {"status" => "401"}, :to_hash => {}))
41
+ ).returns(mock(:code => 500, :inspect => ""))
30
42
  lambda {subject.get "/messages"}.should raise_error(Hashblue::Client::RequestError)
31
43
  end
32
44
  end
33
45
 
34
- describe '#account' do
35
- it "returns Account model built with response from GET /account" do
36
- attributes = {
37
- "msisdn" => "4479001234567"
38
- }
39
- subject.stubs(:get).returns("account" => attributes)
40
- subject.account.should eql(Hashblue::Account.new(subject, attributes))
41
- end
42
- end
43
-
44
- describe '#messages(query = {})' do
45
- it "loads messages from '/messages'" do
46
- subject.expects(:load_messages).with('/messages', {}).returns([:some_messages])
47
- subject.messages.should eql([:some_messages])
48
- end
49
-
50
- it "passes query through to load_messages" do
51
- subject.expects(:load_messages).with('/messages', {:anything => :here})
52
- subject.messages(:anything => :here)
53
- end
54
- end
55
-
56
- describe '#contacts(query = {})' do
57
- it "loads contacts from '/contacts'" do
58
- subject.expects(:load_contacts).with('/contacts', {}).returns([:some_contacts])
59
- subject.contacts.should eql([:some_contacts])
60
- end
61
-
62
- it "passes query through to load_contacts" do
63
- subject.expects(:load_contacts).with('/contacts', {:anything => :here})
64
- subject.contacts(:anything => :here)
65
- end
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
46
  describe '#load_messages(uri, query)' do
81
47
  it "builds a Collection of messages with the response from the uri" do
82
48
  response = {"messages" => [{"content" => "hello"}]}
@@ -14,8 +14,15 @@ describe Hashblue::Contact do
14
14
 
15
15
  describe '#messages' do
16
16
  it "loads messages from its messages uri" do
17
- client.expects(:load_messages).with('https://api.example.com/contact/1/messages', {}).returns([:some_messages])
17
+ client.expects(:load_messages).with(subject.messages_uri, {}).returns([:some_messages])
18
18
  subject.messages.should eql([:some_messages])
19
19
  end
20
20
  end
21
+
22
+ describe '#send_message(content)' do
23
+ it "posts message to messages_uri" do
24
+ client.expects(:post).with(subject.messages_uri, {}, {"message" => {"content" => "Hello!"}})
25
+ subject.send_message "Hello!"
26
+ end
27
+ end
21
28
  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: 27
4
+ hash: 25
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 2
10
- version: 0.0.2
9
+ - 3
10
+ version: 0.0.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - Tom Ward
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-06-21 00:00:00 Z
18
+ date: 2011-06-24 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  type: :runtime
@@ -34,8 +34,23 @@ dependencies:
34
34
  name: httparty
35
35
  prerelease: false
36
36
  - !ruby/object:Gem::Dependency
37
- type: :development
37
+ type: :runtime
38
38
  requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ hash: 7
44
+ segments:
45
+ - 3
46
+ - 0
47
+ version: "3.0"
48
+ version_requirements: *id002
49
+ name: activesupport
50
+ prerelease: false
51
+ - !ruby/object:Gem::Dependency
52
+ type: :development
53
+ requirement: &id003 !ruby/object:Gem::Requirement
39
54
  none: false
40
55
  requirements:
41
56
  - - ~>
@@ -46,12 +61,12 @@ dependencies:
46
61
  - 9
47
62
  - 2
48
63
  version: 0.9.2
49
- version_requirements: *id002
64
+ version_requirements: *id003
50
65
  name: rake
51
66
  prerelease: false
52
67
  - !ruby/object:Gem::Dependency
53
68
  type: :development
54
- requirement: &id003 !ruby/object:Gem::Requirement
69
+ requirement: &id004 !ruby/object:Gem::Requirement
55
70
  none: false
56
71
  requirements:
57
72
  - - ~>
@@ -62,12 +77,12 @@ dependencies:
62
77
  - 6
63
78
  - 0
64
79
  version: 2.6.0
65
- version_requirements: *id003
80
+ version_requirements: *id004
66
81
  name: rspec
67
82
  prerelease: false
68
83
  - !ruby/object:Gem::Dependency
69
84
  type: :development
70
- requirement: &id004 !ruby/object:Gem::Requirement
85
+ requirement: &id005 !ruby/object:Gem::Requirement
71
86
  none: false
72
87
  requirements:
73
88
  - - ">="
@@ -76,7 +91,7 @@ dependencies:
76
91
  segments:
77
92
  - 0
78
93
  version: "0"
79
- version_requirements: *id004
94
+ version_requirements: *id005
80
95
  name: mocha
81
96
  prerelease: false
82
97
  description: A simple client for the hashblue api