Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
senenh committed May 24, 2016
0 parents commit bd9c3ed
Show file tree
Hide file tree
Showing 13 changed files with 254 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/.bundle/
/.yardoc
/Gemfile.lock
/_yardoc/
/coverage/
/doc/
/pkg/
/spec/reports/
/tmp/
.idea/*
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
source 'https://rubygems.org'

# Specify your gem's dependencies in instasent.gemspec
gemspec
21 changes: 21 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2016 Senen

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
45 changes: 45 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
Welcome to __Instasent Ruby SDK__. This repository contains Instasent's Python SDK and samples for REST API.

## Installation

The easiest way to install the instasent package is either via command line:

```
$ gem install instasent
```
And then execute:

$ bundle
or including the following in your Gemfile:

```
gem 'instasent'
```

## Example
### Send an SMS
You can check 'examples/send-sms.py' file.
```ruby
require 'instasent'

client = Instasent::Client.new('my-token')

response = client.send_sms('My company', '+34666666666', 'test message')

puts response['response_code']
puts response['response_body']
```
## Available functions
```
client.send_sms(sender, to, text)
client.get_sms(page, per_page)
client.get_sms_by_id(message_id)
```
## Documentation
Complete documentation at :
[http://docs.instasent.com/](http://docs.instasent.com/).

# Getting help

If you need help installing or using the library, please contact Instasent Support at [email protected] first.
If you've instead found a bug in the library or would like new features added, go ahead and open issues or pull requests against this repo!
2 changes: 2 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require "bundler/gem_tasks"
task :default => :spec
14 changes: 14 additions & 0 deletions bin/console
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/env ruby

require "bundler/setup"
require "instasent"

# You can add fixtures and/or initialization code here to make experimenting
# with your gem easier. You can also use a different console, if you like.

# (If you use this, don't forget to add pry to your Gemfile!)
# require "pry"
# Pry.start

require "irb"
IRB.start
8 changes: 8 additions & 0 deletions bin/setup
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'
set -vx

bundle install

# Do any other automated setup that you need to do here
8 changes: 8 additions & 0 deletions examples/get_sms.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
$LOAD_PATH << '.'
require 'instasent'

client = Instasent::Client.new('my-token')
response = client.get_sms(1, 50)

puts response['response_body']
puts response['response_code']
8 changes: 8 additions & 0 deletions examples/get_sms_by_id.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
$LOAD_PATH << '.'
require 'instasent'

client = Instasent::Client.new('my-token')
response = client.get_sms_by_id('id')

puts response['response_body']
puts response['response_code']
8 changes: 8 additions & 0 deletions examples/send_sms.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
$LOAD_PATH << '.'
require 'instasent'

client = Instasent::Client.new('my-token')
response = client.send_sms('My company', '+34666666666', 'test message')

puts response['response_body']
puts response['response_code']
32 changes: 32 additions & 0 deletions instasent.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# coding: utf-8
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'instasent/version'

Gem::Specification.new do |spec|
spec.name = "instasent"
spec.version = Instasent::VERSION
spec.authors = ["Instasent"]
spec.email = ["[email protected]"]

spec.summary = "Implementation of the Instasent SMS lib for Ruby"
spec.description = "Implementation of the Instasent SMS lib for Ruby"
spec.homepage = "https://github.com/instasent/instasent-ruby-lib"
spec.license = "MIT"

# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
# to allow pushing to a single host or delete this section to allow pushing to any host.
# if spec.respond_to?(:metadata)
# spec.metadata['allowed_push_host'] = "Set to 'http://mygemserver.com'"
# else
# raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
# end

spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
spec.bindir = "exe"
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
spec.require_paths = ["lib"]

spec.add_development_dependency "bundler", "~> 1.12"
spec.add_development_dependency "rake", "~> 10.0"
end
91 changes: 91 additions & 0 deletions lib/instasent.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
require "instasent/version"
require "net/https"
require "json"

module Instasent
class Client

@@rootEndpoint = "http://api.instasent.com/"
@@secureChannel = "https://api.instasent.com/"
@@use_secure_channel = true

def initialize(token, use_secure_channel=true)
@@token = token
@@use_secure_channel = use_secure_channel
end

def send_sms(sender, to, text, client_id='')

if @use_secure_channel
url = @@secureChannel + "sms/"
else
url = @@rootEndpoint + "sms/"
end

http_method = 'POST'

data = {'from' => sender, 'to' => to, 'text' => text}

response = self.execute_request(url, http_method, data)

return response

end

def get_sms(page=1, per_page=10)

if @use_secure_channel
url = @@secureChannel + "sms/?page=" + page.to_s + "per_page=" + per_page.to_s
else
url = @@rootEndpoint + "sms/?page=" + page.to_s + "per_page=" + per_page.to_s
end

http_method = 'GET'

response = self.execute_request(url, http_method)

return response

end

def get_sms_by_id(id)

if @@use_secure_channel
url = @@secureChannel + 'sms/' + id
else
url = @@rootEndpoint + 'sms/' + id
end

http_method = 'GET'

response = self.execute_request(url, http_method)

return response

end

def execute_request(url='', http_method='', data='')

url_parsed = URI.parse(url)

if http_method=='GET'
req = Net::HTTP::Get.new(url_parsed.to_s)
else
req = Net::HTTP::Post.new(url_parsed.to_s, initheader = {'Content-Type' => 'application/json'})
req.body = data.to_json
end

req.add_field("Authorization", "Bearer "+@@token.to_s)

res = Net::HTTP.start(url_parsed.host, url_parsed.port) { |http|
http.request(req)
}

return {'response_body' => res.body, 'response_code' => res.code}

end

end


end
3 changes: 3 additions & 0 deletions lib/instasent/version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module Instasent
VERSION = "0.1.3"
end

0 comments on commit bd9c3ed

Please sign in to comment.