mambanation 0.1.29 → 0.1.30
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION.yml +1 -1
- data/lib/mambanation.rb +111 -65
- metadata +3 -3
data/VERSION.yml
CHANGED
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
|
-
|
3
|
-
|
7
|
+
include HTTParty
|
8
|
+
API_VERSION = "2".freeze
|
9
|
+
|
10
|
+
class MambaNationError < StandardError
|
11
|
+
attr_reader :data
|
4
12
|
|
5
|
-
def
|
6
|
-
|
13
|
+
def initialize(data)
|
14
|
+
@data = data
|
15
|
+
super
|
7
16
|
end
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
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
|
-
|
53
|
-
|
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
|
-
|
57
|
-
|
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
|
-
|
61
|
-
|
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
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
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:
|
4
|
+
hash: 39
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 1
|
9
|
-
-
|
10
|
-
version: 0.1.
|
9
|
+
- 30
|
10
|
+
version: 0.1.30
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- "J\xC3\xA9r\xC3\xA9my Van de Wyngaert"
|