mambanation 0.1.29 → 0.1.30

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 (3) hide show
  1. data/VERSION.yml +1 -1
  2. data/lib/mambanation.rb +111 -65
  3. metadata +3 -3
data/VERSION.yml CHANGED
@@ -2,4 +2,4 @@
2
2
  :major: 0
3
3
  :build:
4
4
  :minor: 1
5
- :patch: 29
5
+ :patch: 30
data/lib/mambanation.rb CHANGED
@@ -1,74 +1,120 @@
1
+ require "forwardable"
2
+ require "hashie"
3
+ require "httparty"
4
+ require "yajl"
5
+
1
6
  module MambaNation
2
- class Request
3
- extend Forwardable
7
+ include HTTParty
8
+ API_VERSION = "2".freeze
9
+
10
+ class MambaNationError < StandardError
11
+ attr_reader :data
4
12
 
5
- def self.get(client, path, options={})
6
- new(client, :get, path, options).perform
13
+ def initialize(data)
14
+ @data = data
15
+ super
7
16
  end
8
-
9
- def self.post(client, path, options={})
10
- new(client, :post, path, options).perform
11
- end
12
-
13
- def self.put(client, path, options={})
14
- options[:body] ||= ""
15
- new(client, :put, path, options).perform
16
- end
17
-
18
- def self.delete(client, path, options={})
19
- new(client, :delete, path, options).perform
20
- end
21
-
22
- attr_reader :client, :method, :path, :options
23
-
24
- def_delegators :client, :get, :post, :put, :delete
25
-
26
- def initialize(client, method, path, options={})
27
- @client, @method, @path, @options = client, method, path, options
28
- end
29
-
30
- def uri
31
- @uri ||= begin
32
- uri = URI.parse(path)
33
-
34
- if options[:query] && options[:query] != {}
35
- uri.query = to_query(options[:query])
36
- end
37
-
38
- uri.to_s
39
- end
40
- end
41
-
42
- def perform
43
- MambaNation.make_friendly(send("perform_#{method}"))
44
- end
45
-
46
- private
47
-
48
- def perform_get
49
- get(uri, options[:headers])
17
+ end
18
+
19
+ class RateLimitExceeded < MambaNationError; end
20
+ class Unauthorized < MambaNationError; end
21
+ class General < MambaNationError; end
22
+
23
+ class Unavailable < StandardError; end
24
+ class InformMambaNation < StandardError; end
25
+ class NotFound < StandardError; end
26
+
27
+ def self.api_endpoint
28
+ @api_endpoint ||= "api.mambanation.com"
29
+ end
30
+
31
+ def self.api_endpoint=(value)
32
+ @api_endpoint = value
33
+ end
34
+
35
+ private
36
+
37
+ def self.perform_get(uri, options = {})
38
+ base_uri(self.api_endpoint)
39
+ make_friendly(get(uri, options))
40
+ end
41
+
42
+ def self.make_friendly(response)
43
+ raise_errors(response)
44
+ data = parse(response)
45
+ # Don't mash arrays of integers
46
+ if data && data.is_a?(Array) && data.first.is_a?(Integer)
47
+ data
48
+ else
49
+ mash(data)
50
50
  end
51
-
52
- def perform_post
53
- post(uri, options[:body], options[:headers])
51
+ end
52
+
53
+ def self.raise_errors(response)
54
+ case response.code.to_i
55
+ when 400
56
+ data = parse(response)
57
+ raise RateLimitExceeded.new(data), "(#{response.code}): #{response.message} - #{data['error'] if data}"
58
+ when 401
59
+ data = parse(response)
60
+ raise Unauthorized.new(data), "(#{response.code}): #{response.message} - #{data['error'] if data}"
61
+ when 403
62
+ data = parse(response)
63
+ raise General.new(data), "(#{response.code}): #{response.message} - #{data['error'] if data}"
64
+ when 404
65
+ raise NotFound, "(#{response.code}): #{response.message}"
66
+ when 500
67
+ raise InformMambaNation, "MambaNation had an internal error. Please let them know in the group. (#{response.code}): #{response.message}"
68
+ when 502..503
69
+ raise Unavailable, "(#{response.code}): #{response.message}"
54
70
  end
55
-
56
- def perform_put
57
- put(uri, options[:body], options[:headers])
71
+ end
72
+
73
+ def self.parse(response)
74
+ Yajl::Parser.parse(response.body)
75
+ end
76
+
77
+ def self.mash(obj)
78
+ if obj.is_a?(Array)
79
+ obj.map{|item| make_mash_with_consistent_hash(item)}
80
+ elsif obj.is_a?(Hash)
81
+ make_mash_with_consistent_hash(obj)
82
+ else
83
+ obj
58
84
  end
59
-
60
- def perform_delete
61
- delete(uri, options[:headers])
85
+ end
86
+
87
+ # Lame workaround for the fact that mash doesn't hash correctly
88
+ def self.make_mash_with_consistent_hash(obj)
89
+ m = Hashie::Mash.new(obj)
90
+ def m.hash
91
+ inspect.hash
62
92
  end
63
-
64
- def to_query(options)
65
- options.inject([]) do |collection, opt|
66
- collection << case opt[1].class.to_s
67
- when "Array" then opt[1].map { |value| "#{opt[0]}[]=#{value}" }.join('&')
68
- else "#{opt[0]}=#{opt[1]}"
69
- end
70
- collection
71
- end * '&'
93
+ return m
94
+ end
95
+
96
+ end
97
+
98
+ module Hashie
99
+ class Mash
100
+
101
+ # Converts all of the keys to strings, optionally formatting key name
102
+ def rubyify_keys!
103
+ keys.each{|k|
104
+ v = delete(k)
105
+ new_key = k.to_s.underscore
106
+ self[new_key] = v
107
+ v.rubyify_keys! if v.is_a?(Hash)
108
+ v.each{|p| p.rubyify_keys! if p.is_a?(Hash)} if v.is_a?(Array)
109
+ }
110
+ self
72
111
  end
112
+
73
113
  end
74
- end
114
+ end
115
+
116
+ directory = File.expand_path(File.dirname(__FILE__))
117
+
118
+ require File.join(directory, "mambanation", "base")
119
+ require File.join(directory, "mambanation", "httpauth")
120
+ require File.join(directory, "mambanation", "request")
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mambanation
3
3
  version: !ruby/object:Gem::Version
4
- hash: 33
4
+ hash: 39
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 29
10
- version: 0.1.29
9
+ - 30
10
+ version: 0.1.30
11
11
  platform: ruby
12
12
  authors:
13
13
  - "J\xC3\xA9r\xC3\xA9my Van de Wyngaert"