hashblue 0.0.5 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -4,7 +4,9 @@ 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
- account = Hashblue::Account.authenticate('oauth2-access-token')
7
+ account = Hashblue::Account.authenticate(
8
+ Hashblue::AccessToken.new('oauth2-access-token')
9
+ )
8
10
 
9
11
  Finally, use that client to start to navigate the API:
10
12
 
data/hashblue.gemspec CHANGED
@@ -5,8 +5,8 @@ require "hashblue/version"
5
5
  Gem::Specification.new do |s|
6
6
  s.name = "hashblue"
7
7
  s.version = Hashblue::VERSION
8
- s.authors = ["Tom Ward"]
9
- s.email = ["tom@popdog.net"]
8
+ s.authors = ["Tom Ward","Richard Spence"]
9
+ s.email = ["tom@popdog.net","richard.spence@o2.com"]
10
10
  s.homepage = "https://api.hashblue.com"
11
11
  s.summary = %q{A simple client for the hashblue api}
12
12
  s.description = %q{A simple client for the hashblue api}
@@ -20,6 +20,7 @@ Gem::Specification.new do |s|
20
20
 
21
21
  s.add_dependency 'httparty', '~>0.7.8'
22
22
  s.add_dependency 'activesupport', '~>3.0'
23
+ s.add_dependency 'json'
23
24
 
24
25
  s.add_development_dependency 'rake', '~>0.9.2'
25
26
  s.add_development_dependency 'rspec', '~>2.6.0'
data/lib/hashblue.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  module Hashblue
2
+ autoload :AccessToken, 'hashblue/access_token'
2
3
  autoload :Account, 'hashblue/account'
3
4
  autoload :Client, 'hashblue/client'
4
5
  autoload :Collection, 'hashblue/collection'
@@ -0,0 +1,33 @@
1
+ module Hashblue
2
+ class AccessToken
3
+ def initialize(access_token)
4
+ @access_token = access_token
5
+ end
6
+
7
+ def to_s
8
+ @access_token
9
+ end
10
+
11
+ def valid?
12
+ client.head '/account'
13
+ true
14
+ rescue Client::RequestError
15
+ false
16
+ end
17
+
18
+ def refresh_with(client_identifier, client_secret, refresh_token)
19
+ response = client.post '/oauth/access_token', {
20
+ 'grant_type' => 'refresh_token',
21
+ 'refresh_token' => refresh_token,
22
+ 'client_id' => client_identifier,
23
+ 'client_secret' => client_secret
24
+ }, nil
25
+ @access_token = response["access_token"]
26
+ response
27
+ end
28
+
29
+ def client
30
+ @client ||= Client.new(self)
31
+ end
32
+ end
33
+ end
@@ -4,7 +4,7 @@ module Hashblue
4
4
  def authenticate(access_token)
5
5
  client = Hashblue::Client.new(access_token)
6
6
  response = client.get("/account")
7
- Account.new(client, response["account"])
7
+ Account.build(client, response["account"])
8
8
  end
9
9
  end
10
10
 
@@ -1,18 +1,26 @@
1
1
  require 'httparty'
2
2
  require 'active_support/ordered_hash'
3
- require 'active_support/json'
3
+ require 'json'
4
4
 
5
5
  module Hashblue
6
+
7
+ class JsonParser < HTTParty::Parser
8
+ def json
9
+ ::JSON.parse(body)
10
+ end
11
+ end
12
+
13
+
6
14
  class Client
7
15
  class RequestError < StandardError; end
8
16
 
9
17
  include HTTParty
18
+ parser JsonParser
10
19
 
11
20
  base_uri "https://api.hashblue.com"
12
21
  headers 'Accept' => 'application/json',
13
22
  'User-Agent' => "Hashblue Client Gem v#{Hashblue::VERSION} (https://github.com/freerange/hashblue-gem)"
14
23
 
15
-
16
24
  attr_reader :access_token
17
25
 
18
26
  def initialize(access_token)
@@ -27,6 +35,10 @@ module Hashblue
27
35
  Collection.new(self, Contact, get(uri, query), "contacts")
28
36
  end
29
37
 
38
+ def head(path, query = {})
39
+ request :head, path, query
40
+ end
41
+
30
42
  def get(path, query = {})
31
43
  request :get, path, query
32
44
  end
@@ -49,7 +61,7 @@ module Hashblue
49
61
  options = {:query => query, :headers => self.class.headers.merge(authorization_header)}
50
62
  if body
51
63
  options[:headers]["Content-type"] = "application/json"
52
- options[:body] = ActiveSupport::JSON.encode(body)
64
+ options[:body] = JSON::JSON.parse(body)
53
65
  end
54
66
 
55
67
  response = self.class.send method, path, options
@@ -5,7 +5,7 @@ class Hashblue::Collection
5
5
  @data = data
6
6
  @name = name
7
7
  @items = @data[name].collect do |item|
8
- model_class.new(@client, item)
8
+ model_class.build(client, item)
9
9
  end
10
10
  end
11
11
 
@@ -3,7 +3,7 @@ module Hashblue
3
3
  attribute_methods :uri, :timestamp, :content
4
4
 
5
5
  def contact
6
- @contact ||= Contact.new(client, @attributes["contact"])
6
+ @contact ||= Contact.build(client, @attributes["contact"])
7
7
  end
8
8
 
9
9
  def sent?
@@ -2,7 +2,7 @@ module Hashblue
2
2
  class Model
3
3
  attr_accessor :client
4
4
 
5
- def initialize(client, attributes)
5
+ def initialize(attributes)
6
6
  @client = client
7
7
  @attributes = attributes
8
8
  end
@@ -15,10 +15,16 @@ module Hashblue
15
15
  m.is_a?(self.class) && m.attributes.eql?(attributes)
16
16
  end
17
17
 
18
- def self.attribute_methods(*names)
19
- names.each do |name|
20
- define_method(name) do
21
- @attributes[name.to_s]
18
+ class << self
19
+ def build(client, attributes = {})
20
+ new(attributes).tap {|m| m.client = client}
21
+ end
22
+
23
+ def attribute_methods(*names)
24
+ names.each do |name|
25
+ define_method(name) do
26
+ @attributes[name.to_s]
27
+ end
22
28
  end
23
29
  end
24
30
  end
@@ -1,3 +1,3 @@
1
1
  module Hashblue
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.8"
3
3
  end
@@ -0,0 +1,52 @@
1
+ require 'spec_helper'
2
+
3
+ describe Hashblue::AccessToken do
4
+ subject do
5
+ Hashblue::AccessToken.new('access-token')
6
+ end
7
+
8
+ describe '#to_s' do
9
+ it 'returns the access token' do
10
+ subject.to_s.should eql('access-token')
11
+ end
12
+ end
13
+
14
+ describe 'valid?' do
15
+ it 'is true if a request to /account succeeds' do
16
+ subject.client.stubs(:head).returns
17
+ subject.valid?.should be_true
18
+ end
19
+
20
+ it 'is false if a request to /account raises a RequestError' do
21
+ subject.client.stubs(:head).raises(Hashblue::Client::RequestError)
22
+ subject.valid?.should be_false
23
+ end
24
+ end
25
+
26
+ describe 'refresh_with' do
27
+ before :each do
28
+ @response = {
29
+ "expires_in" => 2592000,
30
+ "refresh_token" => "new-refresh-token",
31
+ "access_token"=>"new-access-token"
32
+ }
33
+
34
+ subject.client.expects(:post).with('/oauth/access_token', {
35
+ 'grant_type' => 'refresh_token',
36
+ 'refresh_token' => 'provided-refresh-token',
37
+ 'client_id' => 'client-identifier',
38
+ 'client_secret' => 'client-secret'
39
+ }, nil).returns(@response)
40
+ end
41
+
42
+ it 'returns response' do
43
+ response = subject.refresh_with('client-identifier', 'client-secret', 'provided-refresh-token')
44
+ response.should eql(@response)
45
+ end
46
+
47
+ it 'updates its own access token' do
48
+ subject.refresh_with('client-identifier', 'client-secret', 'provided-refresh-token')
49
+ subject.to_s.should == @response["access_token"]
50
+ end
51
+ end
52
+ end
@@ -6,11 +6,11 @@ describe Hashblue::Account do
6
6
  end
7
7
 
8
8
  subject do
9
- Hashblue::Account.new(client, {
9
+ Hashblue::Account.new(
10
10
  'messages' => 'https://api.example.com/messages',
11
11
  'contacts' => 'https://api.example.com/contacts',
12
12
  'favourite_messages' => 'https://api.example.com/messages/favourites'
13
- })
13
+ ).tap {|a| a.client = client}
14
14
  end
15
15
 
16
16
  describe '.authenticate' do
@@ -20,7 +20,7 @@ describe Hashblue::Account do
20
20
  "msisdn" => "4479001234567"
21
21
  }
22
22
  client.stubs(:get).with('/account').returns("account" => attributes)
23
- Hashblue::Account.authenticate('access-token').should eql(Hashblue::Account.new(client, attributes))
23
+ Hashblue::Account.authenticate('access-token').should eql(Hashblue::Account.new(attributes))
24
24
  end
25
25
  end
26
26
 
@@ -13,10 +13,14 @@ describe Hashblue::Collection do
13
13
  end
14
14
 
15
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}))
16
+ subject[0].should eql(Hashblue::Contact.new('msisdn' => 1))
17
17
  subject.size.should eql(3)
18
18
  subject.collect {|c| c.msisdn}.should eql([1, 2, 3])
19
19
  end
20
+
21
+ it "sets client on each model in array" do
22
+ subject.collect{|c| c.client}.uniq.should eql([client])
23
+ end
20
24
  end
21
25
 
22
26
  describe "a collection without a next_page_uri" do
@@ -6,10 +6,10 @@ describe Hashblue::Contact do
6
6
  end
7
7
 
8
8
  subject do
9
- Hashblue::Contact.new(client, {
9
+ Hashblue::Contact.new(
10
10
  'uri' => 'https://api.example.com/contacts/abcdef123456',
11
- 'messages' => 'https://api.example.com/contact/1/messages',
12
- })
11
+ 'messages' => 'https://api.example.com/contact/1/messages'
12
+ ).tap {|c| c.client = client }
13
13
  end
14
14
 
15
15
  describe '#messages' do
@@ -6,14 +6,14 @@ describe Hashblue::Message do
6
6
  end
7
7
 
8
8
  subject do
9
- Hashblue::Message.new(client, {
9
+ Hashblue::Message.new(
10
10
  'uri' => 'https://api.example.com/messages/abcdef123456',
11
11
  'sent' => true,
12
12
  'favourite' => false,
13
13
  'contact' => {
14
14
  'messages' => 'https://api.example.com/contacts/2/messages'
15
15
  }
16
- })
16
+ ).tap {|m| m.client = client }
17
17
  end
18
18
 
19
19
  describe 'favourite?' do
@@ -30,9 +30,9 @@ describe Hashblue::Message do
30
30
 
31
31
  describe '#contact' do
32
32
  it "returns Contact built with contact attributes" do
33
- subject.contact.should eql(Hashblue::Contact.new(client, {
33
+ subject.contact.should eql(Hashblue::Contact.new(
34
34
  'messages' => 'https://api.example.com/contacts/2/messages'
35
- }))
35
+ ))
36
36
  end
37
37
  end
38
38
 
metadata CHANGED
@@ -1,24 +1,26 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hashblue
3
3
  version: !ruby/object:Gem::Version
4
- hash: 21
4
+ hash: 15
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 5
10
- version: 0.0.5
9
+ - 8
10
+ version: 0.0.8
11
11
  platform: ruby
12
12
  authors:
13
13
  - Tom Ward
14
+ - Richard Spence
14
15
  autorequire:
15
16
  bindir: bin
16
17
  cert_chain: []
17
18
 
18
- date: 2011-06-24 00:00:00 Z
19
+ date: 2011-09-29 00:00:00 Z
19
20
  dependencies:
20
21
  - !ruby/object:Gem::Dependency
21
- type: :runtime
22
+ name: httparty
23
+ prerelease: false
22
24
  requirement: &id001 !ruby/object:Gem::Requirement
23
25
  none: false
24
26
  requirements:
@@ -30,11 +32,11 @@ dependencies:
30
32
  - 7
31
33
  - 8
32
34
  version: 0.7.8
35
+ type: :runtime
33
36
  version_requirements: *id001
34
- name: httparty
35
- prerelease: false
36
37
  - !ruby/object:Gem::Dependency
37
- type: :runtime
38
+ name: activesupport
39
+ prerelease: false
38
40
  requirement: &id002 !ruby/object:Gem::Requirement
39
41
  none: false
40
42
  requirements:
@@ -45,12 +47,26 @@ dependencies:
45
47
  - 3
46
48
  - 0
47
49
  version: "3.0"
50
+ type: :runtime
48
51
  version_requirements: *id002
49
- name: activesupport
50
- prerelease: false
51
52
  - !ruby/object:Gem::Dependency
52
- type: :development
53
+ name: json
54
+ prerelease: false
53
55
  requirement: &id003 !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ hash: 3
61
+ segments:
62
+ - 0
63
+ version: "0"
64
+ type: :runtime
65
+ version_requirements: *id003
66
+ - !ruby/object:Gem::Dependency
67
+ name: rake
68
+ prerelease: false
69
+ requirement: &id004 !ruby/object:Gem::Requirement
54
70
  none: false
55
71
  requirements:
56
72
  - - ~>
@@ -61,12 +77,12 @@ dependencies:
61
77
  - 9
62
78
  - 2
63
79
  version: 0.9.2
64
- version_requirements: *id003
65
- name: rake
66
- prerelease: false
67
- - !ruby/object:Gem::Dependency
68
80
  type: :development
69
- requirement: &id004 !ruby/object:Gem::Requirement
81
+ version_requirements: *id004
82
+ - !ruby/object:Gem::Dependency
83
+ name: rspec
84
+ prerelease: false
85
+ requirement: &id005 !ruby/object:Gem::Requirement
70
86
  none: false
71
87
  requirements:
72
88
  - - ~>
@@ -77,12 +93,12 @@ dependencies:
77
93
  - 6
78
94
  - 0
79
95
  version: 2.6.0
80
- version_requirements: *id004
81
- name: rspec
82
- prerelease: false
83
- - !ruby/object:Gem::Dependency
84
96
  type: :development
85
- requirement: &id005 !ruby/object:Gem::Requirement
97
+ version_requirements: *id005
98
+ - !ruby/object:Gem::Dependency
99
+ name: mocha
100
+ prerelease: false
101
+ requirement: &id006 !ruby/object:Gem::Requirement
86
102
  none: false
87
103
  requirements:
88
104
  - - ">="
@@ -91,12 +107,12 @@ dependencies:
91
107
  segments:
92
108
  - 0
93
109
  version: "0"
94
- version_requirements: *id005
95
- name: mocha
96
- prerelease: false
110
+ type: :development
111
+ version_requirements: *id006
97
112
  description: A simple client for the hashblue api
98
113
  email:
99
114
  - tom@popdog.net
115
+ - richard.spence@o2.com
100
116
  executables: []
101
117
 
102
118
  extensions: []
@@ -112,6 +128,7 @@ files:
112
128
  - Rakefile
113
129
  - hashblue.gemspec
114
130
  - lib/hashblue.rb
131
+ - lib/hashblue/access_token.rb
115
132
  - lib/hashblue/account.rb
116
133
  - lib/hashblue/client.rb
117
134
  - lib/hashblue/collection.rb
@@ -119,6 +136,7 @@ files:
119
136
  - lib/hashblue/message.rb
120
137
  - lib/hashblue/model.rb
121
138
  - lib/hashblue/version.rb
139
+ - spec/models/access_token_spec.rb
122
140
  - spec/models/account_spec.rb
123
141
  - spec/models/client_spec.rb
124
142
  - spec/models/collection_spec.rb
@@ -154,11 +172,12 @@ required_rubygems_version: !ruby/object:Gem::Requirement
154
172
  requirements: []
155
173
 
156
174
  rubyforge_project: hashblue
157
- rubygems_version: 1.8.5
175
+ rubygems_version: 1.8.10
158
176
  signing_key:
159
177
  specification_version: 3
160
178
  summary: A simple client for the hashblue api
161
179
  test_files:
180
+ - spec/models/access_token_spec.rb
162
181
  - spec/models/account_spec.rb
163
182
  - spec/models/client_spec.rb
164
183
  - spec/models/collection_spec.rb