ubersmith 1.0.0 → 1.0.1

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.
@@ -0,0 +1,91 @@
1
+ require 'net/https'
2
+ require 'uri'
3
+ require 'ubersmith/url_maker'
4
+
5
+ """
6
+ Example usage:
7
+
8
+ uc = Ubersmith::Client.new(:config_file_path => 'config/ubersmith.yml')
9
+
10
+ data = uc.client_get(:user_login => 'integrated_widgets')
11
+
12
+ uc.client_contact_add(:client_id => '123', :email => 'joe@someserver.com')
13
+
14
+ data = uc.client_contact_list
15
+ """
16
+
17
+ module Ubersmith
18
+ class Client
19
+ include UrlMaker
20
+
21
+ BASE_URL = "https://ubersmith.twistage.com/api/2.0/"
22
+
23
+ def initialize(options)
24
+ @config_file_path = options[:config_file_path]
25
+ @debug = options[:debug]
26
+ puts "debug: #{@debug}"
27
+ end
28
+
29
+ def method_missing(method, *args)
30
+ components = method.to_s.split('_', 2)
31
+ raise "invalid method: #{method}" if components.length < 2
32
+ raise "invalid category: #{components[0]}" unless %w(uber client device order sales support).include?(components[0])
33
+ options = args[0] if args
34
+ options ||= {}
35
+ cache = options.delete(:cache)
36
+ cache = true if cache.nil?
37
+ ret = do_get(build_url(BASE_URL, {
38
+ :method => components[0] + '.' + components[1],
39
+ }.merge(options)), :cache => cache)
40
+ JSON.parse(ret)['data']
41
+ end
42
+
43
+ private
44
+ def do_get(url, options = {})
45
+ options[:filename] ||= tmp_dir + "/" + safe_filename(url) if options[:cache]
46
+ if options[:filename] && File.exists?(options[:filename])
47
+ ret = File.read(options[:filename])
48
+ else
49
+ uri = URI.parse(url)
50
+ http = Net::HTTP.new(uri.host, uri.port)
51
+ http.use_ssl = (uri.scheme == 'https')
52
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
53
+ puts "HTTP GET #{uri.request_uri}" if @debug
54
+ req = Net::HTTP::Get.new(uri.request_uri)
55
+ req.basic_auth auth_params['api_login'], auth_params['api_pass']
56
+ res = http.start { |http| http.request(req) }
57
+ ret = res.body
58
+ if options[:filename]
59
+ FileUtils.makedirs(File.dirname(options[:filename]))
60
+ File.open(options[:filename], "wb") { |f| f.write(ret) }
61
+ end
62
+ end
63
+ ret
64
+ end
65
+
66
+ def config
67
+ return @config unless @config.nil?
68
+ @config = File.open(@config_file_path) { |f| YAML::load(f) } rescue nil
69
+ if @config.blank?
70
+ puts "Could not load ubersmith config from #{@config_file_path}"
71
+ return {}
72
+ end
73
+ @config
74
+ end
75
+
76
+ def auth_params
77
+ {
78
+ "api_login" => config["username"],
79
+ "api_pass" => config["password"],
80
+ }
81
+ end
82
+
83
+ def safe_filename(url)
84
+ url.gsub(/\W/, '_')
85
+ end
86
+
87
+ def tmp_dir
88
+ 'tmp/ubersmith'
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,33 @@
1
+ require 'cgi'
2
+ require 'uri'
3
+
4
+ module Ubersmith
5
+ module UrlMaker
6
+ def build_query_string(pars, escape_method = :cgi)
7
+ pars.map {|key, val| _escape(key, escape_method) + '=' + _escape(val, escape_method) }.join("&")
8
+ end
9
+
10
+ def build_url(url, pars, escape = :cgi)
11
+ append_url_param(url, build_query_string(pars, escape))
12
+ end
13
+
14
+ def append_url_param(url, param)
15
+ return url if param.blank?
16
+ joiner = url.index('?') ? '&' : '?'
17
+ [url, param].join(joiner)
18
+ end
19
+
20
+ def _escape(value, method)
21
+ case method.to_s
22
+ when 'cgi'
23
+ CGI.escape(value.to_s)
24
+ when 'uri'
25
+ URI.escape(value.to_s, Regexp.new("[^#{URI::REGEXP::PATTERN::UNRESERVED}]"))
26
+ when ''
27
+ value.to_s
28
+ else
29
+ raise ArgumentError.new("unknown escape method: #{method}")
30
+ end
31
+ end
32
+ end
33
+ end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 1
7
7
  - 0
8
- - 0
9
- version: 1.0.0
8
+ - 1
9
+ version: 1.0.1
10
10
  platform: ruby
11
11
  authors:
12
12
  - David Wegman
@@ -27,7 +27,8 @@ extensions: []
27
27
  extra_rdoc_files: []
28
28
 
29
29
  files:
30
- - lib/ubersmith.rb
30
+ - lib/ubersmith/client.rb
31
+ - lib/ubersmith/url_maker.rb
31
32
  has_rdoc: true
32
33
  homepage: http://rubygems.org/gems/ubersmith
33
34
  licenses: []
@@ -1,140 +0,0 @@
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