From bd9c3ed30c91288c22b1dedf543260f38d041754 Mon Sep 17 00:00:00 2001 From: Senen Date: Tue, 24 May 2016 12:10:34 +0200 Subject: [PATCH] Initial commit --- .gitignore | 10 +++++ Gemfile | 4 ++ LICENSE.txt | 21 +++++++++ README.md | 45 +++++++++++++++++++ Rakefile | 2 + bin/console | 14 ++++++ bin/setup | 8 ++++ examples/get_sms.rb | 8 ++++ examples/get_sms_by_id.rb | 8 ++++ examples/send_sms.rb | 8 ++++ instasent.gemspec | 32 ++++++++++++++ lib/instasent.rb | 91 +++++++++++++++++++++++++++++++++++++++ lib/instasent/version.rb | 3 ++ 13 files changed, 254 insertions(+) create mode 100644 .gitignore create mode 100644 Gemfile create mode 100644 LICENSE.txt create mode 100644 README.md create mode 100644 Rakefile create mode 100755 bin/console create mode 100755 bin/setup create mode 100644 examples/get_sms.rb create mode 100644 examples/get_sms_by_id.rb create mode 100644 examples/send_sms.rb create mode 100644 instasent.gemspec create mode 100644 lib/instasent.rb create mode 100644 lib/instasent/version.rb diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..da3ed3a --- /dev/null +++ b/.gitignore @@ -0,0 +1,10 @@ +/.bundle/ +/.yardoc +/Gemfile.lock +/_yardoc/ +/coverage/ +/doc/ +/pkg/ +/spec/reports/ +/tmp/ +.idea/* diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..67149d7 --- /dev/null +++ b/Gemfile @@ -0,0 +1,4 @@ +source 'https://rubygems.org' + +# Specify your gem's dependencies in instasent.gemspec +gemspec diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..cc02032 --- /dev/null +++ b/LICENSE.txt @@ -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. diff --git a/README.md b/README.md new file mode 100644 index 0000000..bc754c9 --- /dev/null +++ b/README.md @@ -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 support@instasent.com 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! \ No newline at end of file diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..43022f7 --- /dev/null +++ b/Rakefile @@ -0,0 +1,2 @@ +require "bundler/gem_tasks" +task :default => :spec diff --git a/bin/console b/bin/console new file mode 100755 index 0000000..a59ae08 --- /dev/null +++ b/bin/console @@ -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 diff --git a/bin/setup b/bin/setup new file mode 100755 index 0000000..dce67d8 --- /dev/null +++ b/bin/setup @@ -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 diff --git a/examples/get_sms.rb b/examples/get_sms.rb new file mode 100644 index 0000000..8bcbfda --- /dev/null +++ b/examples/get_sms.rb @@ -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'] \ No newline at end of file diff --git a/examples/get_sms_by_id.rb b/examples/get_sms_by_id.rb new file mode 100644 index 0000000..c95952b --- /dev/null +++ b/examples/get_sms_by_id.rb @@ -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'] \ No newline at end of file diff --git a/examples/send_sms.rb b/examples/send_sms.rb new file mode 100644 index 0000000..2d9f703 --- /dev/null +++ b/examples/send_sms.rb @@ -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'] \ No newline at end of file diff --git a/instasent.gemspec b/instasent.gemspec new file mode 100644 index 0000000..e12ced8 --- /dev/null +++ b/instasent.gemspec @@ -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 = ["support@instasent.com"] + + 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 diff --git a/lib/instasent.rb b/lib/instasent.rb new file mode 100644 index 0000000..0f0c38f --- /dev/null +++ b/lib/instasent.rb @@ -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 diff --git a/lib/instasent/version.rb b/lib/instasent/version.rb new file mode 100644 index 0000000..1b91d9d --- /dev/null +++ b/lib/instasent/version.rb @@ -0,0 +1,3 @@ +module Instasent + VERSION = "0.1.3" +end