hashblue 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ -fn -c
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in hashblue.gemspec
4
+ gemspec
data/MIT-LICENCE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (C) 2011 by Go Free Range (http://gofreerange.com)
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new
5
+
6
+ task :default => :spec
data/hashblue.gemspec ADDED
@@ -0,0 +1,26 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "hashblue/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "hashblue"
7
+ s.version = Hashblue::VERSION
8
+ s.authors = ["Tom Ward"]
9
+ s.email = ["tom@popdog.net"]
10
+ s.homepage = "https://api.hashblue.com"
11
+ s.summary = %q{A simple client for the hashblue api}
12
+ s.description = %q{A simple client for the hashblue api}
13
+
14
+ s.rubyforge_project = "hashblue"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ s.add_dependency 'httparty', '~>0.7.8'
22
+
23
+ s.add_development_dependency 'rake', '~>0.9.2'
24
+ s.add_development_dependency 'rspec', '~>2.6.0'
25
+ s.add_development_dependency 'mocha'
26
+ end
@@ -0,0 +1,21 @@
1
+ module Hashblue
2
+ class Account < Model
3
+ attribute_methods :msisdn, :phone_number, :email
4
+
5
+ def messages(query = {})
6
+ client.load_messages(messages_uri, query)
7
+ end
8
+
9
+ def contacts(query = {})
10
+ client.load_contacts(contacts_uri, query)
11
+ end
12
+
13
+ def messages_uri
14
+ @attributes["messages"]
15
+ end
16
+
17
+ def contacts_uri
18
+ @attributes["contacts"]
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,56 @@
1
+ require 'httparty'
2
+
3
+ module Hashblue
4
+ class Client
5
+ class RequestError < StandardError; end
6
+
7
+ include HTTParty
8
+
9
+ base_uri "https://api.hashblue.com"
10
+
11
+ attr_reader :access_token, :refresh_token
12
+
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
21
+
22
+ def messages(query = {})
23
+ load_messages("/messages", query)
24
+ end
25
+
26
+ def contacts(query = {})
27
+ load_contacts("/contacts", query)
28
+ end
29
+
30
+ def load_messages(uri, query = {})
31
+ get(uri, query)["messages"].collect do |m|
32
+ Message.new(self, m)
33
+ end
34
+ end
35
+
36
+ def load_contacts(uri, query = {})
37
+ get(uri, query)["contacts"].collect do |m|
38
+ Contact.new(self, m)
39
+ end
40
+ end
41
+
42
+ def get(path, query = {})
43
+ response = self.class.get path, :query => query, :headers => request_headers
44
+ case response.headers["status"]
45
+ when "200" then response.to_hash
46
+ else raise RequestError, "request unsuccessful: #{response.to_hash.inspect}"
47
+ end
48
+ end
49
+
50
+ private
51
+
52
+ def request_headers
53
+ {"Authorization" => "OAuth #{access_token}", 'Accept' => 'application/json'}
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,13 @@
1
+ module Hashblue
2
+ class Contact < Model
3
+ attribute_methods :name, :phone_number, :msisdn, :email
4
+
5
+ def messages(query = {})
6
+ client.load_messages(messages_uri, query)
7
+ end
8
+
9
+ def messages_uri
10
+ @attributes["messages"]
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,17 @@
1
+ module Hashblue
2
+ class Message < Model
3
+ attribute_methods :uri, :timestamp, :content
4
+
5
+ def sent?
6
+ @attributes["sent"]
7
+ end
8
+
9
+ def favourite?
10
+ @attributes["favourite"]
11
+ end
12
+
13
+ def contact
14
+ @contact ||= Contact.new(client, @attributes["contact"])
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,26 @@
1
+ module Hashblue
2
+ class Model
3
+ attr_accessor :client
4
+
5
+ def initialize(client, attributes)
6
+ @client = client
7
+ @attributes = attributes
8
+ end
9
+
10
+ def attributes
11
+ @attributes.dup
12
+ end
13
+
14
+ def eql?(m)
15
+ m.is_a?(self.class) && m.attributes.eql?(attributes)
16
+ end
17
+
18
+ def self.attribute_methods(*names)
19
+ names.each do |name|
20
+ define_method(name) do
21
+ @attributes[name.to_s]
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,3 @@
1
+ module Hashblue
2
+ VERSION = "0.0.1"
3
+ end
data/lib/hashblue.rb ADDED
@@ -0,0 +1,11 @@
1
+ require "hashblue/version"
2
+
3
+ module Hashblue
4
+ autoload :Account, 'hashblue/account'
5
+ autoload :Client, 'hashblue/client'
6
+ autoload :Contact, 'hashblue/contact'
7
+ autoload :Message, 'hashblue/message'
8
+ autoload :Model, 'hashblue/model'
9
+ autoload :Version, 'hashblue/version'
10
+ # Your code goes here...
11
+ end
@@ -0,0 +1,28 @@
1
+ require 'spec_helper'
2
+
3
+ describe Hashblue::Account do
4
+ let :client do
5
+ Hashblue::Client.new('hashblue-access-token')
6
+ end
7
+
8
+ subject do
9
+ Hashblue::Account.new(client, {
10
+ 'messages' => 'https://api.example.com/messages',
11
+ 'contacts' => 'https://api.example.com/contacts'
12
+ })
13
+ end
14
+
15
+ describe '#messages' do
16
+ it "loads messages from its messages uri" do
17
+ client.expects(:load_messages).with('https://api.example.com/messages', {}).returns([:some_messages])
18
+ subject.messages.should eql([:some_messages])
19
+ end
20
+ end
21
+
22
+ describe '#contacts' do
23
+ it "loads contacts from its contacts uri" do
24
+ client.expects(:load_contacts).with('https://api.example.com/contacts', {}).returns([:some_contacts])
25
+ subject.contacts.should eql([:some_contacts])
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,67 @@
1
+ require 'spec_helper'
2
+
3
+ describe Hashblue::Client do
4
+ subject do
5
+ Hashblue::Client.new('hashblue-access-token')
6
+ end
7
+
8
+ describe '#get' do
9
+ it "makes requests with Authorization and Accept headers and passed query" do
10
+ Hashblue::Client.expects(:get).with("/messages",
11
+ :headers => {
12
+ "Authorization" => "OAuth hashblue-access-token",
13
+ "Accept" => "application/json"
14
+ },
15
+ :query => {
16
+ :since => '2011-01-14T14:30Z'
17
+ }
18
+ ).returns(mock(:headers => {"status" => "200"}, :to_hash => {}))
19
+ subject.get "/messages", :since => '2011-01-14T14:30Z'
20
+ end
21
+
22
+ it "raises RequestError if response status isn't 200" do
23
+ Hashblue::Client.expects(:get).with("/messages",
24
+ :headers => {
25
+ "Authorization" => "OAuth hashblue-access-token",
26
+ "Accept" => "application/json"
27
+ },
28
+ :query => {}
29
+ ).returns(mock(:headers => {"status" => "401"}, :to_hash => {}))
30
+ lambda {subject.get "/messages"}.should raise_error(Hashblue::Client::RequestError)
31
+ end
32
+ end
33
+
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
+ end
@@ -0,0 +1,20 @@
1
+ require 'spec_helper'
2
+
3
+ describe Hashblue::Contact do
4
+ let :client do
5
+ Hashblue::Client.new('hashblue-access-token')
6
+ end
7
+
8
+ subject do
9
+ Hashblue::Contact.new(client, {
10
+ 'messages' => 'https://api.example.com/contact/1/messages',
11
+ })
12
+ end
13
+
14
+ describe '#messages' do
15
+ it "loads messages from its messages uri" do
16
+ client.expects(:load_messages).with('https://api.example.com/contact/1/messages', {}).returns([:some_messages])
17
+ subject.messages.should eql([:some_messages])
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,25 @@
1
+ require 'spec_helper'
2
+
3
+ describe Hashblue::Message do
4
+ let :client do
5
+ Hashblue::Client.new('hashblue-access-token')
6
+ end
7
+
8
+ subject do
9
+ Hashblue::Message.new(client, {
10
+ 'sent' => true,
11
+ 'favourite' => false,
12
+ 'contact' => {
13
+ 'messages' => 'https://api.example.com/contacts/2/messages'
14
+ }
15
+ })
16
+ end
17
+
18
+ describe '#contact' do
19
+ it "returns Contact built with contact attributes" do
20
+ subject.contact.should eql(Hashblue::Contact.new(client, {
21
+ 'messages' => 'https://api.example.com/contacts/2/messages'
22
+ }))
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,6 @@
1
+ require 'bundler/setup'
2
+ require 'hashblue'
3
+
4
+ RSpec.configure do |config|
5
+ config.mock_with :mocha
6
+ end
metadata ADDED
@@ -0,0 +1,148 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hashblue
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Tom Ward
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-06-21 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ type: :runtime
22
+ requirement: &id001 !ruby/object:Gem::Requirement
23
+ none: false
24
+ requirements:
25
+ - - ~>
26
+ - !ruby/object:Gem::Version
27
+ hash: 19
28
+ segments:
29
+ - 0
30
+ - 7
31
+ - 8
32
+ version: 0.7.8
33
+ version_requirements: *id001
34
+ name: httparty
35
+ prerelease: false
36
+ - !ruby/object:Gem::Dependency
37
+ type: :development
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ hash: 63
44
+ segments:
45
+ - 0
46
+ - 9
47
+ - 2
48
+ version: 0.9.2
49
+ version_requirements: *id002
50
+ name: rake
51
+ prerelease: false
52
+ - !ruby/object:Gem::Dependency
53
+ type: :development
54
+ requirement: &id003 !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ~>
58
+ - !ruby/object:Gem::Version
59
+ hash: 23
60
+ segments:
61
+ - 2
62
+ - 6
63
+ - 0
64
+ version: 2.6.0
65
+ version_requirements: *id003
66
+ name: rspec
67
+ prerelease: false
68
+ - !ruby/object:Gem::Dependency
69
+ type: :development
70
+ requirement: &id004 !ruby/object:Gem::Requirement
71
+ none: false
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ hash: 3
76
+ segments:
77
+ - 0
78
+ version: "0"
79
+ version_requirements: *id004
80
+ name: mocha
81
+ prerelease: false
82
+ description: A simple client for the hashblue api
83
+ email:
84
+ - tom@popdog.net
85
+ executables: []
86
+
87
+ extensions: []
88
+
89
+ extra_rdoc_files: []
90
+
91
+ files:
92
+ - .gitignore
93
+ - .rspec
94
+ - Gemfile
95
+ - MIT-LICENCE
96
+ - Rakefile
97
+ - hashblue.gemspec
98
+ - lib/hashblue.rb
99
+ - lib/hashblue/account.rb
100
+ - lib/hashblue/client.rb
101
+ - lib/hashblue/contact.rb
102
+ - lib/hashblue/message.rb
103
+ - lib/hashblue/model.rb
104
+ - lib/hashblue/version.rb
105
+ - spec/models/account_spec.rb
106
+ - spec/models/client_spec.rb
107
+ - spec/models/contact_spec.rb
108
+ - spec/models/message_spec.rb
109
+ - spec/spec_helper.rb
110
+ homepage: https://api.hashblue.com
111
+ licenses: []
112
+
113
+ post_install_message:
114
+ rdoc_options: []
115
+
116
+ require_paths:
117
+ - lib
118
+ required_ruby_version: !ruby/object:Gem::Requirement
119
+ none: false
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ hash: 3
124
+ segments:
125
+ - 0
126
+ version: "0"
127
+ required_rubygems_version: !ruby/object:Gem::Requirement
128
+ none: false
129
+ requirements:
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ hash: 3
133
+ segments:
134
+ - 0
135
+ version: "0"
136
+ requirements: []
137
+
138
+ rubyforge_project: hashblue
139
+ rubygems_version: 1.8.5
140
+ signing_key:
141
+ specification_version: 3
142
+ summary: A simple client for the hashblue api
143
+ test_files:
144
+ - spec/models/account_spec.rb
145
+ - spec/models/client_spec.rb
146
+ - spec/models/contact_spec.rb
147
+ - spec/models/message_spec.rb
148
+ - spec/spec_helper.rb