ubersmith 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. data/lib/ubersmith.rb +140 -0
  2. metadata +64 -0
data/lib/ubersmith.rb ADDED
@@ -0,0 +1,140 @@
1
+ require 'net/https'
2
+ require 'uri'
3
+
4
+ class Ubersmith
5
+ def base_url_20
6
+ "https://ubersmith.twistage.com/api/2.0/"
7
+ end
8
+
9
+ def client_list_contacts
10
+ url = build_url("https://ubersmith.twistage.com/api/2.0/", {
11
+ :method => "client.contact_list",
12
+ :client_id => "1098",
13
+ })
14
+ puts "url: #{url}"
15
+
16
+ uri = URI.parse(url)
17
+ http = Net::HTTP.new(uri.host, uri.port)
18
+ http.use_ssl = (uri.scheme == 'https')
19
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
20
+ req = Net::HTTP::Get.new(uri.request_uri)
21
+ req.basic_auth auth_params['api_login'], auth_params['api_pass']
22
+ res = http.start {|http| http.request(req) }
23
+ data = JSON.parse(res.body)
24
+ puts data['data'].keys
25
+
26
+ # res = Net::HTTP::Get.new()get(URI.parse(url))
27
+ # puts "res: #{res.body}"
28
+ end
29
+
30
+ def tmp_dir
31
+ 'tmp/ubersmith'
32
+ end
33
+
34
+ def do_get(url, options = {})
35
+ options[:filename] ||= tmp_dir + "/" + safe_filename(url) if options[:cache]
36
+ if options[:filename] && File.exists?(options[:filename])
37
+ ret = File.read(options[:filename])
38
+ else
39
+ uri = URI.parse(url)
40
+ http = Net::HTTP.new(uri.host, uri.port)
41
+ http.use_ssl = (uri.scheme == 'https')
42
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
43
+ req = Net::HTTP::Get.new(uri.request_uri)
44
+ req.basic_auth auth_params['api_login'], auth_params['api_pass']
45
+ res = http.start { |http| http.request(req) }
46
+ ret = res.body
47
+ if options[:filename]
48
+ FileUtils.makedirs(File.dirname(options[:filename]))
49
+ File.open(options[:filename], "wb") { |f| f.write(ret) }
50
+ end
51
+ end
52
+ ret
53
+ end
54
+
55
+ def support_ticket_list(options)
56
+ url = build_url(base_url_20, {
57
+ :method => "support.ticket_list",
58
+ }.merge(options))
59
+ ret = do_get(url, :cache => true)
60
+ JSON.parse(ret)['data']
61
+ end
62
+
63
+ def recent_tickets
64
+ support_ticket_list(:order_by => "timestamp", :direction => "desc", :limit => 10)
65
+ end
66
+ =begin
67
+ def cache_tickets
68
+ ret = do_get(build_url(base_url_20, {
69
+ :method => "support.ticket_list",
70
+ :order_by => "timestamp",
71
+ :direction => "desc",
72
+ :limit => 3,
73
+ }))
74
+ # puts "got ret: #{ret}"
75
+ ret = JSON.parse(ret)
76
+ authors = []
77
+ ret['data'].each do |id, data|
78
+ authors << data['author']
79
+ end
80
+ authors.uniq.each do |author|
81
+ puts "look for author: #{author}"
82
+ if match = /(.*) <([^>]+)>/.match(author)
83
+ puts "got name: " + match[1]
84
+ puts "got email: " + match[2]
85
+ end
86
+
87
+ end
88
+ # puts "got parsed ret: #{ret}"
89
+ end
90
+ =end
91
+
92
+ def safe_filename(url)
93
+ url.gsub(/\W/, '_')
94
+ end
95
+
96
+ def client_list
97
+ url = build_url(base_url_20, {
98
+ :method => "client.list",
99
+ })
100
+ ret = do_get(url, :cache => true)
101
+ JSON.parse(ret)['data']
102
+ end
103
+
104
+ def find_client_id_by_login(login)
105
+ puts "got list:"
106
+ data = client_list.detect { |id, entry| entry['login'] == login }
107
+ return data[0] if data
108
+ end
109
+
110
+ def support_ticket_update(options)
111
+ do_get(build_url(base_url_20, {
112
+ :method => "support.ticket_update",
113
+ }.merge(options)))
114
+ end
115
+
116
+ def client_contact_add(options)
117
+ do_get(build_url(base_url_20, {
118
+ :method => "client.contact_add",
119
+ }.merge(options)))
120
+ end
121
+
122
+ private
123
+ def config
124
+ return @config unless @config.nil?
125
+ file = File.join(::Rails.root.to_s, "config/ubersmith.yml")
126
+ @config = File.open(file) { |f| YAML::load(f) } rescue nil
127
+ if @config.blank?
128
+ puts "Could not load ubersmith config"
129
+ return {}
130
+ end
131
+ @config
132
+ end
133
+
134
+ def auth_params
135
+ {
136
+ "api_login" => config["username"],
137
+ "api_pass" => config["password"],
138
+ }
139
+ end
140
+ end
metadata ADDED
@@ -0,0 +1,64 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ubersmith
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 1
7
+ - 0
8
+ - 0
9
+ version: 1.0.0
10
+ platform: ruby
11
+ authors:
12
+ - David Wegman
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-12-02 00:00:00 -08:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description: Provides a ruby interface for interacting with an Ubersmith 2.0 instance
22
+ email: dwegman@twistage.com
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files: []
28
+
29
+ files:
30
+ - lib/ubersmith.rb
31
+ has_rdoc: true
32
+ homepage: http://rubygems.org/gems/ubersmith
33
+ licenses: []
34
+
35
+ post_install_message:
36
+ rdoc_options: []
37
+
38
+ require_paths:
39
+ - lib
40
+ required_ruby_version: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ segments:
46
+ - 0
47
+ version: "0"
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ segments:
54
+ - 0
55
+ version: "0"
56
+ requirements: []
57
+
58
+ rubyforge_project:
59
+ rubygems_version: 1.3.7
60
+ signing_key:
61
+ specification_version: 3
62
+ summary: Ubersmith 2.0 API
63
+ test_files: []
64
+