mambanation 0.1.27 → 0.1.28

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.
data/VERSION.yml CHANGED
@@ -2,4 +2,4 @@
2
2
  :major: 0
3
3
  :build:
4
4
  :minor: 1
5
- :patch: 27
5
+ :patch: 28
data/lib/mambanation.rb CHANGED
@@ -1,120 +1,74 @@
1
- require "forwardable"
2
- require "hashie"
3
- require "httparty"
4
- require "yajl"
5
-
6
1
  module MambaNation
7
- include HTTParty
8
- API_VERSION = "2".freeze
9
-
10
- class MambaNationError < StandardError
11
- attr_reader :data
2
+ class Request
3
+ extend Forwardable
12
4
 
13
- def initialize(data)
14
- @data = data
15
- super
5
+ def self.get(client, path, options={})
6
+ new(client, :get, path, options).perform
16
7
  end
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)
8
+
9
+ def self.post(client, path, options={})
10
+ new(client, :post, path, options).perform
50
11
  end
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}"
12
+
13
+ def self.put(client, path, options={})
14
+ options[:body] ||= ""
15
+ new(client, :put, path, options).perform
70
16
  end
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
17
+
18
+ def self.delete(client, path, options={})
19
+ new(client, :delete, path, options).perform
84
20
  end
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
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
92
28
  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
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])
50
+ end
51
+
52
+ def perform_post
53
+ post(uri, options[:body], options[:headers])
54
+ end
55
+
56
+ def perform_put
57
+ put(uri, options[:body], options[:headers])
58
+ end
59
+
60
+ def perform_delete
61
+ delete(uri, options[:headers])
62
+ 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 * '&'
111
72
  end
112
-
113
73
  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")
74
+ end
@@ -154,8 +154,6 @@ module MambaNation
154
154
  # options:
155
155
  # application_id => Integer, Array
156
156
  def user_posts(id, query = {})
157
- query[:application_id] = urlencode_array({:application_id => query[:application_id]}) if query.is_a?(Hash) && query[:application_id]
158
-
159
157
  perform_get("/users/#{id.to_i}/posts.json", :query => query)
160
158
  end
161
159
 
@@ -241,18 +239,6 @@ module MambaNation
241
239
 
242
240
  private
243
241
 
244
- def urlencode_array(value_hash)
245
- return_string = ''
246
- value_hash.each_pair do |k,v|
247
- return_string = case v.class.to_s
248
- when "Hash" then urlencode_array(v)
249
- when "Array" then v.map { |value| value.is_a?(Hash) ? urlencode_array(value) : "#{k}[]=#{value}" }.join('&')
250
- else "#{k}[]=#{v}"
251
- end
252
- end
253
- return_string
254
- end
255
-
256
242
  def request_options(opts={})
257
243
  opts[:headers] ||= {}
258
244
  opts[:headers].merge! "X-FB-COOKIES" => @fbs_cookies if @fbs_cookies
@@ -276,4 +262,4 @@ module MambaNation
276
262
  end
277
263
 
278
264
  end
279
- end
265
+ end
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: 45
4
+ hash: 35
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 27
10
- version: 0.1.27
9
+ - 28
10
+ version: 0.1.28
11
11
  platform: ruby
12
12
  authors:
13
13
  - "J\xC3\xA9r\xC3\xA9my Van de Wyngaert"