Skip to content

Commit 04c1e28

Browse files
author
Artur Khabibullin
authored
Merge pull request #16 from messagebird/add-ci
Add CI
2 parents d9d0855 + 0c1c307 commit 04c1e28

File tree

15 files changed

+441
-29
lines changed

15 files changed

+441
-29
lines changed

.rspec

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
--color
2+
--require spec_helper

.travis.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
language: ruby
2+
rvm:
3+
- 2.5.1
4+
- 2.0.0
5+
- 1.9.3
6+
- jruby
7+
matrix:
8+
allow_failures:
9+
- rvm: jruby
10+
install: gem install rspec
11+
script: rake spec

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ MessageBird's REST API for Ruby
22
===============================
33
This repository contains the open source Ruby client for MessageBird's REST API. Documentation can be found at: https://developers.messagebird.com/
44

5+
[![Build Status](https://travis-ci.org/messagebird/ruby-rest-api.svg?branch=master)](https://travis-ci.org/messagebird/ruby-rest-api)
6+
57
Requirements
68
------------
79
- [Sign up](https://dashboard.messagebird.com/app/en/sign-up) for a free MessageBird account

Rakefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
require 'rake/testtask'
2+
require 'rspec/core/rake_task'
3+
4+
RSpec::Core::RakeTask.new(:spec) do |t|
5+
t.pattern = Dir.glob('spec/**/*_spec.rb')
6+
end
27

38
namespace :gem do
49
desc 'Build the messagebird API gem'

lib/messagebird/client.rb

Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
require 'messagebird/balance'
66
require 'messagebird/error'
77
require 'messagebird/hlr'
8+
require 'messagebird/http_client'
89
require 'messagebird/verify'
910
require 'messagebird/message'
1011
require 'messagebird/voicemessage'
@@ -19,42 +20,19 @@ def initialize(errors)
1920
end
2021
end
2122

22-
class InvalidPhoneNumberException < TypeError; end
23-
2423
class Client
24+
2525
attr_reader :access_key
26+
attr_reader :http_client
2627

27-
def initialize(access_key = nil)
28+
def initialize(access_key = nil, http_client = nil)
2829
@access_key = access_key || ENV['MESSAGEBIRD_ACCESS_KEY']
30+
@http_client = http_client || HttpClient.new(@access_key)
2931
end
3032

3133
def request(method, path, params={})
32-
uri = URI.join(ENDPOINT, '/', path)
33-
34-
# Set up the HTTP object.
35-
http = Net::HTTP.new(uri.host, uri.port)
36-
http.use_ssl = true
37-
38-
# Construct the HTTP GET or POST request.
39-
request = Net::HTTP::Get.new(uri.request_uri) if method == :get
40-
request = Net::HTTP::Post.new(uri.request_uri) if method == :post
41-
request['Accept'] = 'application/json'
42-
request['Authorization'] = "AccessKey #{@access_key}"
43-
request['User-Agent'] = "MessageBird/ApiClient/#{CLIENT_VERSION} Ruby/#{RUBY_VERSION}"
44-
45-
# If present, add the HTTP POST parameters.
46-
request.set_form_data(params) if method == :post && !params.empty?
47-
48-
# Execute the request and fetch the response.
49-
response = http.request(request)
50-
51-
# Parse the HTTP response.
52-
case response.code.to_i
53-
when 200, 201, 204, 401, 404, 405, 422
54-
json = JSON.parse(response.body)
55-
else
56-
raise InvalidPhoneNumberException, 'Unknown response from server'
57-
end
34+
response_body = @http_client.request(method, path, params)
35+
json = JSON.parse(response_body)
5836

5937
# If the request returned errors, create Error objects and raise.
6038
if json.has_key?('errors')

lib/messagebird/http_client.rb

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
require 'net/https'
2+
require 'uri'
3+
4+
module MessageBird
5+
6+
class InvalidPhoneNumberException < TypeError; end
7+
class InvalidResponseException < StandardError; end
8+
class MethodNotAllowedException < ArgumentError; end
9+
10+
class HttpClient
11+
12+
attr_reader :access_key
13+
14+
def initialize(access_key)
15+
@access_key = access_key
16+
end
17+
18+
def request(method, path, params={}, check_json=true)
19+
uri = URI.join(ENDPOINT, '/', path)
20+
21+
# Set up the HTTP object.
22+
http = Net::HTTP.new(uri.host, uri.port)
23+
http.use_ssl = true
24+
25+
# Construct the HTTP GET or POST request.
26+
case method
27+
when :get
28+
request = Net::HTTP::Get.new(uri.request_uri)
29+
when :post
30+
request = Net::HTTP::Post.new(uri.request_uri)
31+
else
32+
raise MethodNotAllowedException
33+
end
34+
35+
request['Accept'] = 'application/json'
36+
request['Authorization'] = "AccessKey #{@access_key}"
37+
request['User-Agent'] = "MessageBird/ApiClient/#{CLIENT_VERSION} Ruby/#{RUBY_VERSION}"
38+
39+
# If present, add the HTTP POST parameters.
40+
request.set_form_data(params) if method == :post && !params.empty?
41+
42+
# Execute the request and fetch the response.
43+
response = http.request(request)
44+
45+
assert_valid_response_code(response.code.to_i)
46+
assert_json_response_type(response['Content-Type']) unless check_json
47+
48+
response.body
49+
end
50+
51+
# Throw an exception if the response code is not one we expect from the
52+
# MessageBird API.
53+
def assert_valid_response_code(code)
54+
# InvalidPhoneNumberException does not make a lot of sense here, but it's
55+
# needed to maintain backwards compatibility. See issue:
56+
# https://github.com/messagebird/ruby-rest-api/issues/17
57+
expected_codes = [200, 201, 204, 401, 404, 405, 422]
58+
raise InvalidPhoneNumberException, 'Unknown response from server' unless expected_codes.include? code
59+
end
60+
61+
# Throw an exception if the response's content type is not JSON. This only
62+
# checks the header: it doesn't inspect the actual body.
63+
def assert_json_response_type(content_type)
64+
# Check whether the header starts with application/json and don't check
65+
# for equality: some API's may append the charset to this header.
66+
raise InvalidResponseException, 'Response is not JSON' unless content_type.start_with? 'application/json'
67+
end
68+
69+
end
70+
71+
end

messagebird-rest.gemspec

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,6 @@ Gem::Specification.new do |s|
1818

1919
s.files = Dir.glob("lib/**/*") + %w(LICENSE README.md)
2020
s.require_path = 'lib'
21+
22+
s.add_development_dependency 'rspec', '~> 3.8'
2123
end

spec/balance_spec.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
describe 'Balance' do
2+
3+
it 'reads' do
4+
5+
http_client = double(MessageBird::HttpClient)
6+
client = MessageBird::Client.new('', http_client)
7+
8+
expect(http_client)
9+
.to receive(:request)
10+
.with(:get, 'balance', {})
11+
.and_return('{"payment":"prepaid","type":"credits","amount":12.34}')
12+
13+
balance = client.balance
14+
15+
expect(balance.payment).to eq 'prepaid'
16+
17+
end
18+
19+
end

spec/error_spec.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
describe 'Error' do
2+
3+
it 'raises returned errors' do
4+
5+
http_client = double(MessageBird::HttpClient)
6+
client = MessageBird::Client.new('', http_client)
7+
8+
expect(http_client)
9+
.to receive(:request)
10+
.and_return('{"errors":[{"code": 2,"description": "Request not allowed (incorrect access_key)","parameter": "access_key"}]}')
11+
12+
expect{ client.message('some-id') }.to raise_error(MessageBird::ErrorException)
13+
end
14+
15+
end

spec/hlr_spec.rb

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
describe 'HLR' do
2+
3+
it 'views an existing' do
4+
5+
http_client = double(MessageBird::HttpClient)
6+
client = MessageBird::Client.new('', http_client)
7+
8+
expect(http_client)
9+
.to receive(:request)
10+
.with(:get, 'hlr/hlr-id', {})
11+
.and_return('{"id":"hlr-id","href":"https://rest.messagebird.com/hlr/hlr-id","msisdn":31612345678,"network":20406,"reference":"MyReference","status": "sent","createdDatetime": "2015-01-04T13:14:08+00:00","statusDatetime": "2015-01-04T13:14:09+00:00"}')
12+
13+
hlr = client.hlr('hlr-id')
14+
15+
expect(hlr.id).to eq 'hlr-id'
16+
17+
end
18+
19+
it 'requests a HLR' do
20+
21+
http_client = double(MessageBird::HttpClient)
22+
client = MessageBird::Client.new('', http_client)
23+
24+
expect(http_client)
25+
.to receive(:request)
26+
.with(:post, 'hlr', {:msisdn => 31612345678, :reference => 'MyReference'})
27+
.and_return('{}')
28+
29+
client.hlr_create(31612345678, 'MyReference')
30+
31+
end
32+
33+
end

0 commit comments

Comments
 (0)