elibom 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/elibom.rb +87 -0
  2. metadata +45 -0
data/lib/elibom.rb ADDED
@@ -0,0 +1,87 @@
1
+ require 'net/http'
2
+ require 'net/https'
3
+ require 'json'
4
+
5
+ # Ruby client of the Elibom API
6
+ module Elibom
7
+
8
+ class Client
9
+
10
+ def initialize(options={})
11
+ @host = options[:host] || "http://www.elibom.com"
12
+ @user = options[:user]
13
+ @api_password = options[:api_password]
14
+
15
+ raise ArgumentError, "Missing key ':user'" if @user.nil? || @user.empty?
16
+ raise ArgumentError, "Missing key ':api_password'" if @api_password.nil? || @api_password.empty?
17
+ end
18
+
19
+ def send_message(args={})
20
+ body = {}
21
+
22
+ required_args = [:destinations, :text]
23
+ required_args.each do |arg|
24
+ raise ArgumentError, "Missing key ':#{arg}'" if args[arg].nil?
25
+ body[arg] = args[arg]
26
+ end
27
+
28
+ post '/messages', body
29
+ end
30
+
31
+ def messages(delivery_id)
32
+ raise ArgumentError, "'delivery_id' cannot be nil or empty" if delivery_id.nil? || delivery_id.empty?
33
+ get "/messages/#{delivery_id}"
34
+ end
35
+ alias :list_messages :messages
36
+
37
+ def user(user_id)
38
+ raise ArgumentError, "'user_id' cannot be nil" if user_id.nil?
39
+ get "/users/#{user_id}"
40
+ end
41
+
42
+ def account
43
+ get '/account'
44
+ end
45
+
46
+ private
47
+
48
+ def get(resource)
49
+ uri = URI.parse("#{@host}#{resource}")
50
+ http = Net::HTTP.new(uri.host, uri.port)
51
+
52
+ request = Net::HTTP::Get.new(uri.request_uri)
53
+ request.basic_auth @user, @api_password
54
+ request['Accept'] = 'application/json'
55
+
56
+ response = http.request(request)
57
+
58
+ case response
59
+ when Net::HTTPSuccess
60
+ JSON.parse(response.body)
61
+ else
62
+ response.error!
63
+ end
64
+ end
65
+
66
+ def post(resource, body)
67
+ uri = URI.parse("#{@host}#{resource}")
68
+ http = Net::HTTP.new(uri.host, uri.port)
69
+
70
+ request = Net::HTTP::Post.new(uri.path)
71
+ request.basic_auth @user, @api_password
72
+ request['Content-Type'] = 'application/json'
73
+ request['Accept'] = 'application/json'
74
+ request.body = body.to_json
75
+
76
+ response = http.request(request)
77
+
78
+ case response
79
+ when Net::HTTPSuccess
80
+ JSON.parse(response.body)
81
+ else
82
+ response.error!
83
+ end
84
+ end
85
+
86
+ end
87
+ end
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: elibom
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - German Escobar
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-07-28 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: A client of the Elibom API
15
+ email: german.escobar@elibom.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/elibom.rb
21
+ homepage: http://rubygems.org/gems/elibom
22
+ licenses: []
23
+ post_install_message:
24
+ rdoc_options: []
25
+ require_paths:
26
+ - lib
27
+ required_ruby_version: !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ! '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements: []
40
+ rubyforge_project:
41
+ rubygems_version: 1.8.23
42
+ signing_key:
43
+ specification_version: 3
44
+ summary: Elibom API client
45
+ test_files: []