hoi 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (11) hide show
  1. data/.gitignore +18 -0
  2. data/Gemfile +10 -0
  3. data/LICENSE +22 -0
  4. data/README +6 -0
  5. data/Rakefile +20 -0
  6. data/VERSION +1 -0
  7. data/hoi.gemspec +22 -0
  8. data/lib/hoi.rb +111 -0
  9. data/test/helper.rb +16 -0
  10. data/test/test_hoi.rb +106 -0
  11. metadata +115 -0
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .rvmrc
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem "json", "> 1.4.0"
4
+ gem "httparty", "> 0.6.0"
5
+
6
+ group :development, :test do
7
+ gem "shoulda", ">= 0"
8
+ gem "bundler"
9
+ gem "mocha", "> 0.9.11"
10
+ end
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Espen Antonsen
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,6 @@
1
+ Hoi - Ruby API for Hoiio
2
+
3
+ == Usage ==
4
+
5
+ sms = Hoi::SMS.new(app_id, access_token)
6
+ sms.send( :msg => "Hoi!", :dest => "123" )
data/Rakefile ADDED
@@ -0,0 +1,20 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ require 'bundler/gem_tasks'
4
+ begin
5
+ Bundler.setup(:default, :development)
6
+ rescue Bundler::BundlerError => e
7
+ $stderr.puts e.message
8
+ $stderr.puts "Run `bundle install` to install missing gems"
9
+ exit e.status_code
10
+ end
11
+ require 'rake'
12
+
13
+ require 'rake/testtask'
14
+ Rake::TestTask.new(:test) do |test|
15
+ test.libs << 'lib' << 'test'
16
+ test.pattern = 'test/**/test_*.rb'
17
+ test.verbose = true
18
+ end
19
+
20
+ task :default => :test
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.3
data/hoi.gemspec ADDED
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/hoi', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Espen Antonsen"]
6
+ gem.email = ["espen@inspired.no"]
7
+ gem.description = %q{API for Hoiio}
8
+ gem.summary = %q{}
9
+ gem.homepage = "https://github.com/espen/hoi"
10
+
11
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
12
+ gem.files = `git ls-files`.split("\n")
13
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
14
+ gem.name = "hoi"
15
+ gem.require_paths = ["lib"]
16
+ gem.add_runtime_dependency(%q<json>, ["> 1.4.0"])
17
+ gem.add_runtime_dependency(%q<httparty>, ["> 0.6.0"])
18
+ gem.add_development_dependency(%q<shoulda>, [">= 0"])
19
+ gem.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
20
+ gem.add_development_dependency(%q<mocha>, ["> 0.9.11"])
21
+ gem.version = Hoi::VERSION
22
+ end
data/lib/hoi.rb ADDED
@@ -0,0 +1,111 @@
1
+ require 'httparty'
2
+ require 'json'
3
+ require 'cgi'
4
+
5
+ class Hoi
6
+ VERSION = '0.0.3'
7
+
8
+ include HTTParty
9
+ format :plain
10
+ default_timeout 30
11
+ @throws_exceptions = false
12
+
13
+ base_uri "https://secure.hoiio.com/open/"
14
+
15
+ attr_accessor :app_id, :access_token, :timeout, :throws_exceptions, :test_mode
16
+
17
+ @@app_id = nil
18
+ @@access_token = nil
19
+ @@test_mode = false
20
+
21
+ def initialize(app_id = nil, access_token = nil, extra_params = {})
22
+ @@app_id = app_id || ENV['HOIIO_APP_ID'] || @@app_id
23
+ @@access_token = access_token || ENV['HOIIO_ACCESS_TOKEN'] || @@access_token
24
+ end
25
+
26
+ def app_id
27
+ @@app_id
28
+ end
29
+
30
+ def app_id=(value)
31
+ @@app_id = value
32
+ end
33
+
34
+ def access_token
35
+ @@access_token
36
+ end
37
+
38
+ def access_token=(value)
39
+ @@access_token = value
40
+ end
41
+
42
+ def test_mode
43
+ @@test_mode
44
+ end
45
+
46
+ def test_mode=(value)
47
+ @@test_mode = value
48
+ end
49
+
50
+ protected
51
+
52
+ class << self
53
+ attr_accessor :app_id, :access_token, :test_mode
54
+
55
+ def app_id=(value)
56
+ @@app_id = value
57
+ end
58
+
59
+ def app_id
60
+ @@app_id
61
+ end
62
+
63
+ def access_token=(value)
64
+ @@access_token = value
65
+ end
66
+
67
+ def access_token
68
+ @@access_token
69
+ end
70
+
71
+ def test_mode=(value)
72
+ @@test_mode = value
73
+ end
74
+
75
+ def test_mode
76
+ @@test_mode
77
+ end
78
+
79
+ end
80
+
81
+ private
82
+
83
+ def default_params(params)
84
+ {:app_id => @@app_id, :access_token => self.access_token}.merge(params)
85
+ end
86
+
87
+ def handle_response(response)
88
+ response_body = JSON.parse('['+response.body+']').first
89
+ if @throws_exceptions && response_body.is_a?(Hash) && response_body["status"] != "success_ok"
90
+ raise "Error from Hoiio API: #{response_body["status"]}"
91
+ end
92
+ response_body
93
+ end
94
+
95
+ def call(url, params)
96
+ return {:status => 'success_ok'}.to_json if @@test_mode
97
+ handle_response( self.class.post('/sms/send', {:body => default_params(params) }) )
98
+ end
99
+
100
+ end
101
+
102
+
103
+ class Hoi::SMS < Hoi
104
+ def send(params)
105
+ call('/sms/send', params)
106
+ end
107
+
108
+ def get_rate(params)
109
+ call('/sms/get_rate', params)
110
+ end
111
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,16 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'test/unit'
11
+ require 'shoulda'
12
+ require 'mocha'
13
+
14
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
15
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
16
+ require 'hoi'
data/test/test_hoi.rb ADDED
@@ -0,0 +1,106 @@
1
+ require 'helper'
2
+ require 'cgi'
3
+
4
+ class TestHoi < Test::Unit::TestCase
5
+
6
+ context "attributes" do
7
+
8
+ setup do
9
+ @app_id = "singapore"
10
+ @access_token = "spg"
11
+ end
12
+
13
+ should "aaaa - have no API by default" do
14
+ Hoi.app_id = nil
15
+ Hoi.access_token = nil
16
+ @hoi = Hoi.new
17
+ assert_equal(nil, @hoi.app_id)
18
+ assert_equal(nil, @hoi.access_token)
19
+ end
20
+
21
+ should "set an credentials in constructor" do
22
+ @hoi = Hoi.new(@app_id, @access_token)
23
+ assert_equal(@app_id, @hoi.app_id)
24
+ assert_equal(@access_token, @hoi.access_token)
25
+ end
26
+
27
+ should "set an APP id from the ENV variables" do
28
+ ENV['HOIIO_APP_ID'] = @app_id
29
+ ENV['HOIIO_ACCESS_TOKEN'] = @access_token
30
+ @hoi = Hoi.new
31
+ assert_equal(@app_id, @hoi.app_id)
32
+ assert_equal(@access_token, @hoi.access_token)
33
+ ENV.delete('HOIIO_APP_ID')
34
+ ENV.delete('HOIIO_ACCESS_TOKEN')
35
+ end
36
+
37
+ should "set an API id via setter" do
38
+ @hoi = Hoi.new
39
+ @hoi.app_id = @app_id
40
+ @hoi.access_token = @access_token
41
+ assert_equal(@app_id, @hoi.app_id)
42
+ assert_equal(@access_token, @hoi.access_token)
43
+ end
44
+
45
+ should "set timeout and get" do
46
+ @hoi = Hoi.new
47
+ timeout = 30
48
+ @hoi.timeout = timeout
49
+ assert_equal(timeout, @hoi.timeout)
50
+ end
51
+ end
52
+
53
+ context "Hoi instances" do
54
+ setup do
55
+ @app_id = "mylittleid"
56
+ @access_token = "yodawg"
57
+ @hoi = Hoi::SMS.new(@app_id, @access_token)
58
+ @msg = "hello"
59
+ @dest = "123"
60
+ @url = "https://secure.hoiio.com/open/sms/send/"
61
+ @body = {"app_id" => @app_id, "access_token" => @access_token}
62
+ end
63
+
64
+ should "throw exception if configured to and the API replies with a JSON hash not containing 'success_ok'" do
65
+ Hoi::SMS.stubs(:post).returns(Struct.new(:body).new({'status' => 'error_X_param_missing'}.to_json))
66
+ @hoi.throws_exceptions = true
67
+ assert_raise RuntimeError do
68
+ @hoi.send(:msg => @msg, :dest => @dest)
69
+ end
70
+ end
71
+ end
72
+
73
+ context "SMS API" do
74
+ setup do
75
+ @app_id = "smsid"
76
+ @access_token = "yodawg"
77
+ @hoi = Hoi::SMS.new(@key)
78
+ @url = "https://secure.hoiio.com/open/sms/send/"
79
+ @msg = "hello"
80
+ @dest = "123"
81
+ @body = {:app_id => @app_id, :access_token => @access_token}
82
+ end
83
+
84
+ should "set an API id via main setter" do
85
+ Hoi.app_id = @app_id
86
+ Hoi.access_token = @access_token
87
+ @hoi = Hoi::SMS.new()
88
+ assert_equal(@app_id, @hoi.app_id)
89
+ assert_equal(@access_token, @hoi.access_token)
90
+ end
91
+
92
+
93
+ should "send sms" do
94
+ Hoi::SMS.stubs(:post).returns(Struct.new(:body).new({'status' => 'success_ok'}.to_json))
95
+ assert @hoi.send(:msg => @msg, :dest => @dest)
96
+ end
97
+
98
+ should "send not send sms in test mode" do
99
+ @hoi.test_mode = true
100
+ assert @hoi.send(:msg => @msg, :dest => @dest)
101
+ @hoi.test_mode = false
102
+ end
103
+
104
+ end
105
+
106
+ end
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hoi
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Espen Antonsen
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-03-19 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: json
16
+ requirement: &70357433780040 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>'
20
+ - !ruby/object:Gem::Version
21
+ version: 1.4.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70357433780040
25
+ - !ruby/object:Gem::Dependency
26
+ name: httparty
27
+ requirement: &70357433779520 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>'
31
+ - !ruby/object:Gem::Version
32
+ version: 0.6.0
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *70357433779520
36
+ - !ruby/object:Gem::Dependency
37
+ name: shoulda
38
+ requirement: &70357433778980 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *70357433778980
47
+ - !ruby/object:Gem::Dependency
48
+ name: bundler
49
+ requirement: &70357433778500 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 1.0.0
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *70357433778500
58
+ - !ruby/object:Gem::Dependency
59
+ name: mocha
60
+ requirement: &70357433778020 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>'
64
+ - !ruby/object:Gem::Version
65
+ version: 0.9.11
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: *70357433778020
69
+ description: API for Hoiio
70
+ email:
71
+ - espen@inspired.no
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - Gemfile
78
+ - LICENSE
79
+ - README
80
+ - Rakefile
81
+ - VERSION
82
+ - hoi.gemspec
83
+ - lib/hoi.rb
84
+ - test/helper.rb
85
+ - test/test_hoi.rb
86
+ homepage: https://github.com/espen/hoi
87
+ licenses: []
88
+ post_install_message:
89
+ rdoc_options: []
90
+ require_paths:
91
+ - lib
92
+ required_ruby_version: !ruby/object:Gem::Requirement
93
+ none: false
94
+ requirements:
95
+ - - ! '>='
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ segments:
99
+ - 0
100
+ hash: -4372762421911532507
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ none: false
103
+ requirements:
104
+ - - ! '>='
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ requirements: []
108
+ rubyforge_project:
109
+ rubygems_version: 1.8.17
110
+ signing_key:
111
+ specification_version: 3
112
+ summary: ''
113
+ test_files:
114
+ - test/helper.rb
115
+ - test/test_hoi.rb