mms-api 0.1.1 → 0.1.2

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0f46973360a5bc90648f5db16ed68f2a715df24f
4
- data.tar.gz: 87a3cdbe52ea77dd5b1778d8df9318a2b34734e6
3
+ metadata.gz: 50ef9789119c25dc3d0061bd092475344eb30d4a
4
+ data.tar.gz: f85fb48393f03e4485984a27c0d04ca814397214
5
5
  SHA512:
6
- metadata.gz: 34e98f586c2a03de4ea3260a11f22fba361e93407de77f740f0f12b5c2a79144befb8ed1343ccff7143f3fed09238fd048c4415d7915a69115bb14ca4be823b1
7
- data.tar.gz: 2cbf2343574237b3814aae43a47815591d0b5cf773da2555ceb08271b4836cb50442c437b1a38b9a5af6f76b1589b6be88e73d7c92bc4abfb61eea1a7f26b2e5
6
+ metadata.gz: ba8ca38b105d5de0948607a673d4f6bb293982013f9b569dee5fc692c79c58d6831cc6b16557b6404c34ead8088aa830c21e096a8e426b18820706ef4d88e165
7
+ data.tar.gz: 1a8b3bb28a32f6c74a63d76a1cc4c413ed37cf56de64237f9e2207d3ed527cbd6b2ed51a374ef3c62489d5ced642f26be08b33617a3e7d106c7bf6fbc4a8ed32
@@ -68,12 +68,11 @@ module MMS
68
68
  raise MMS::ConfigError.new("Config option `#{key}` from file `#{config_file}` is not allowed!")
69
69
  end
70
70
  end
71
-
72
71
  end
73
72
 
74
73
  # @return [MMS::Agent]
75
74
  def agent
76
- @client = MMS::Client.new(@config.username, @config.apikey)
75
+ @client = MMS::Client.new(@config.username, @config.apikey, @config.apiurl)
77
76
  @agent = MMS::Agent.new(client)
78
77
  end
79
78
 
@@ -18,59 +18,26 @@ module MMS
18
18
  # @param [String] path
19
19
  # @return [Hash]
20
20
  def get(path)
21
- _get(@url + path, @username, @apikey)
21
+ _request(Net::HTTP::Get, @url + path, @username, @apikey, nil)
22
22
  end
23
23
 
24
24
  # @param [String] path
25
25
  # @param [Hash] data
26
26
  # @return [Hash]
27
27
  def post(path, data)
28
- _post(@url + path, data, @username, @apikey)
28
+ _request(Net::HTTP::Post, @url + path, @username, @apikey, data)
29
29
  end
30
30
 
31
31
  private
32
32
 
33
+ # @param [Net::HTTPRequest] http_method
33
34
  # @param [String] path
34
35
  # @param [String] username
35
36
  # @param [String] password
36
- def _get(path, username, password)
37
-
38
- digest_auth = Net::HTTP::DigestAuth.new
39
- digest_auth.next_nonce
40
-
41
- uri = URI.parse path
42
- uri.user= CGI.escape(username)
43
- uri.password= CGI.escape(password)
44
-
45
- http = Net::HTTP.new(uri.host, uri.port)
46
- http.use_ssl = true
47
-
48
- req = Net::HTTP::Get.new uri.request_uri
49
- res = http.request req
50
-
51
- auth = digest_auth.auth_header uri, res['WWW-Authenticate'], 'GET'
52
- req = Net::HTTP::Get.new uri.request_uri
53
- req.add_field 'Authorization', auth
54
-
55
- response = http.request(req)
56
- response_json = JSON.parse(response.body)
57
-
58
- unless response.code == 200 or response_json['error'].nil?
59
- msg = "http 'get' error for url `#{url}`"
60
- msg = response_json['detail'] unless response_json['detail'].nil?
61
-
62
- raise MMS::AuthError.new(msg, req, response) if response.code == '401'
63
- raise MMS::ApiError.new(msg, req, response)
64
- end
65
-
66
- (response_json.nil? or response_json['results'].nil?) ? response_json : response_json['results']
67
- end
68
-
69
- # @param [String] path
70
37
  # @param [Hash] data
71
- # @param [String] username
72
- # @param [String] password
73
- def _post(path, data, username, password)
38
+ # @return [Hash]
39
+ def _request(http_method, path, username, password, data = nil)
40
+
74
41
  digest_auth = Net::HTTP::DigestAuth.new
75
42
  digest_auth.next_nonce
76
43
 
@@ -79,13 +46,15 @@ module MMS
79
46
  uri.password= CGI.escape(password)
80
47
 
81
48
  http = Net::HTTP.new uri.host, uri.port
82
- http.use_ssl = true
49
+ http.use_ssl = (uri.scheme == 'https')
83
50
 
84
- req = Net::HTTP::Post.new uri.request_uri, {'Content-Type' => 'application/json'}
51
+ req = http_method.new(uri.request_uri, {'Content-Type' => 'application/json'})
85
52
  res = http.request req
86
53
 
87
- auth = digest_auth.auth_header uri, res['WWW-Authenticate'], 'POST'
88
- req = Net::HTTP::Post.new uri.request_uri, {'Content-Type' => 'application/json'}
54
+ raise 'Invalid method' unless http_method.kind_of? Class and http_method < Net::HTTPRequest
55
+ req = http_method.new(uri.request_uri, {'Content-Type' => 'application/json'})
56
+ method_name = http_method.name.split('::').last.upcase
57
+ auth = digest_auth.auth_header(uri, res['WWW-Authenticate'], method_name)
89
58
  req.add_field 'Authorization', auth
90
59
  req.body = data.to_json
91
60
 
@@ -1,3 +1,3 @@
1
1
  module MMS
2
- VERSION = '0.1.1'
2
+ VERSION = '0.1.2'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mms-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cargo Media
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2015-03-17 00:00:00.000000000 Z
13
+ date: 2015-04-17 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: net-http-digest_auth