subtledata 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/general_api.rb +56 -0
- data/lib/locations_api.rb +644 -0
- data/lib/monkey.rb +90 -0
- data/lib/swagger/configuration.rb +19 -0
- data/lib/swagger/request.rb +186 -0
- data/lib/swagger/response.rb +70 -0
- data/lib/swagger/version.rb +5 -0
- data/lib/swagger.rb +84 -0
- data/lib/users_api.rb +265 -0
- metadata +56 -0
data/lib/monkey.rb
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
# module Swagger
|
2
|
+
class Object
|
3
|
+
|
4
|
+
unless Object.method_defined? :blank?
|
5
|
+
def blank?
|
6
|
+
respond_to?(:empty?) ? empty? : !self
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
unless Object.method_defined? :present?
|
11
|
+
def present?
|
12
|
+
!blank?
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
|
18
|
+
class String
|
19
|
+
|
20
|
+
unless String.method_defined? :underscore
|
21
|
+
def underscore
|
22
|
+
self.gsub(/::/, '/').
|
23
|
+
gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
|
24
|
+
gsub(/([a-z\d])([A-Z])/,'\1_\2').
|
25
|
+
tr("-", "_").
|
26
|
+
downcase
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
unless String.method_defined? :camelize
|
31
|
+
def camelize(first_letter_in_uppercase = true)
|
32
|
+
if first_letter_in_uppercase != :lower
|
33
|
+
self.to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
|
34
|
+
else
|
35
|
+
self.to_s[0].chr.downcase + camelize(self)[1..-1]
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
41
|
+
|
42
|
+
class Hash
|
43
|
+
|
44
|
+
unless Hash.method_defined? :stringify_keys
|
45
|
+
def stringify_keys
|
46
|
+
inject({}) do |options, (key, value)|
|
47
|
+
options[key.to_s] = value
|
48
|
+
options
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
unless Hash.method_defined? :stringify_keys!
|
54
|
+
def stringify_keys!
|
55
|
+
self.replace(self.stringify_keys)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
unless Hash.method_defined? :symbolize_keys
|
60
|
+
def symbolize_keys
|
61
|
+
inject({}) do |options, (key, value)|
|
62
|
+
options[(key.to_sym rescue key) || key] = value
|
63
|
+
options
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
unless Hash.method_defined? :symbolize_keys!
|
69
|
+
def symbolize_keys!
|
70
|
+
self.replace(self.symbolize_keys)
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
unless Hash.method_defined? :symbolize_and_underscore_keys
|
75
|
+
def symbolize_and_underscore_keys
|
76
|
+
inject({}) do |options, (key, value)|
|
77
|
+
options[(key.to_s.underscore.to_sym rescue key) || key] = value
|
78
|
+
options
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
unless Hash.method_defined? :symbolize_and_underscore_keys!
|
84
|
+
def symbolize_and_underscore_keys!
|
85
|
+
self.replace(self.symbolize_and_underscore_keys)
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
end
|
90
|
+
# end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module Swagger
|
2
|
+
|
3
|
+
class Configuration
|
4
|
+
require 'swagger/version'
|
5
|
+
|
6
|
+
attr_accessor :format, :api_key, :username, :password, :auth_token, :scheme, :host, :base_path, :user_agent, :logger
|
7
|
+
|
8
|
+
# Defaults go in here..
|
9
|
+
def initialize
|
10
|
+
@format = 'json'
|
11
|
+
@scheme = 'http'
|
12
|
+
@host = 'api.wordnik.com'
|
13
|
+
@base_path = '/v4'
|
14
|
+
@user_agent = "ruby-#{Swagger::VERSION}"
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
@@ -0,0 +1,186 @@
|
|
1
|
+
module Swagger
|
2
|
+
|
3
|
+
class Request
|
4
|
+
require 'uri'
|
5
|
+
require 'addressable/uri'
|
6
|
+
require 'typhoeus'
|
7
|
+
require "swagger/version"
|
8
|
+
|
9
|
+
attr_accessor :host, :path, :format, :params, :body, :http_method, :headers
|
10
|
+
|
11
|
+
|
12
|
+
# All requests must have an HTTP method and a path
|
13
|
+
# Optionals parameters are :params, :headers, :body, :format, :host
|
14
|
+
#
|
15
|
+
def initialize(http_method, path, attributes={})
|
16
|
+
attributes[:format] ||= Swagger.configuration.format
|
17
|
+
attributes[:params] ||= {}
|
18
|
+
|
19
|
+
# Set default headers
|
20
|
+
default_headers = {
|
21
|
+
'Content-Type' => "application/#{attributes[:format].downcase}",
|
22
|
+
:api_key => Swagger.configuration.api_key
|
23
|
+
}
|
24
|
+
|
25
|
+
# api_key from headers hash trumps the default, even if its value is blank
|
26
|
+
if attributes[:headers].present? && attributes[:headers].has_key?(:api_key)
|
27
|
+
default_headers.delete(:api_key)
|
28
|
+
end
|
29
|
+
|
30
|
+
# api_key from params hash trumps all others (headers and default_headers)
|
31
|
+
if attributes[:params].present? && attributes[:params].has_key?(:api_key)
|
32
|
+
default_headers.delete(:api_key)
|
33
|
+
attributes[:headers].delete(:api_key) if attributes[:headers].present?
|
34
|
+
end
|
35
|
+
|
36
|
+
# Merge argument headers into defaults
|
37
|
+
attributes[:headers] = default_headers.merge(attributes[:headers] || {})
|
38
|
+
|
39
|
+
# Stick in the auth token if there is one
|
40
|
+
if Swagger.authenticated?
|
41
|
+
attributes[:headers].merge!({:auth_token => Swagger.configuration.auth_token})
|
42
|
+
end
|
43
|
+
|
44
|
+
self.http_method = http_method.to_sym
|
45
|
+
self.path = path
|
46
|
+
attributes.each do |name, value|
|
47
|
+
send("#{name.to_s.underscore.to_sym}=", value)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
# Construct a base URL
|
52
|
+
#
|
53
|
+
def url(options = {})
|
54
|
+
u = Addressable::URI.new(
|
55
|
+
:scheme => Swagger.configuration.scheme,
|
56
|
+
:host => Swagger.configuration.host,
|
57
|
+
:path => self.interpreted_path,
|
58
|
+
:query => self.query_string.sub(/\?/, '')
|
59
|
+
).to_s
|
60
|
+
|
61
|
+
# Drop trailing question mark, if present
|
62
|
+
u.sub! /\?$/, ''
|
63
|
+
|
64
|
+
# Obfuscate API key?
|
65
|
+
u.sub! /api\_key=\w+/, 'api_key=YOUR_API_KEY' if options[:obfuscated]
|
66
|
+
|
67
|
+
u
|
68
|
+
end
|
69
|
+
|
70
|
+
# Iterate over the params hash, injecting any path values into the path string
|
71
|
+
#
|
72
|
+
# e.g. /word.{format}/{word}/entries => /word.json/cat/entries
|
73
|
+
def interpreted_path
|
74
|
+
p = self.path.dup
|
75
|
+
|
76
|
+
# Fill in the path params
|
77
|
+
self.params.each_pair do |key, value|
|
78
|
+
p = p.gsub("{#{key}}", value.to_s)
|
79
|
+
end
|
80
|
+
|
81
|
+
# Stick a .{format} placeholder into the path if there isn't
|
82
|
+
# one already or an actual format like json or xml
|
83
|
+
# e.g. /words/blah => /words.{format}/blah
|
84
|
+
unless ['.json', '.xml', '{format}'].any? {|s| p.downcase.include? s }
|
85
|
+
p = p.sub(/^(\/?\w+)/, "\\1.#{format}")
|
86
|
+
end
|
87
|
+
|
88
|
+
p = p.sub("{format}", self.format.to_s)
|
89
|
+
|
90
|
+
URI.encode [Swagger.configuration.base_path, p].join("/").gsub(/\/+/, '/')
|
91
|
+
end
|
92
|
+
|
93
|
+
# Massage the request body into a state of readiness
|
94
|
+
# If body is a hash, camelize all keys then convert to a json string
|
95
|
+
#
|
96
|
+
def body=(value)
|
97
|
+
if value.is_a?(Hash)
|
98
|
+
value = value.inject({}) do |memo, (k,v)|
|
99
|
+
memo[k.to_s.camelize(:lower).to_sym] = v
|
100
|
+
memo
|
101
|
+
end
|
102
|
+
end
|
103
|
+
@body = value
|
104
|
+
end
|
105
|
+
|
106
|
+
# If body is an object, JSONify it before making the actual request.
|
107
|
+
#
|
108
|
+
def outgoing_body
|
109
|
+
body.is_a?(String) ? body : body.to_json
|
110
|
+
end
|
111
|
+
|
112
|
+
# Construct a query string from the query-string-type params
|
113
|
+
def query_string
|
114
|
+
|
115
|
+
# Iterate over all params,
|
116
|
+
# .. removing the ones that are part of the path itself.
|
117
|
+
# .. stringifying values so Addressable doesn't blow up.
|
118
|
+
query_values = {}
|
119
|
+
self.params.each_pair do |key, value|
|
120
|
+
next if self.path.include? "{#{key}}" # skip path params
|
121
|
+
next if value.blank? && value.class != FalseClass # skip empties
|
122
|
+
key = key.to_s.camelize(:lower).to_sym unless key.to_sym == :api_key # api_key is not a camelCased param
|
123
|
+
query_values[key] = value.to_s
|
124
|
+
end
|
125
|
+
|
126
|
+
# We don't want to end up with '?' as our query string
|
127
|
+
# if there aren't really any params
|
128
|
+
return "" if query_values.blank?
|
129
|
+
|
130
|
+
# Addressable requires query_values to be set after initialization..
|
131
|
+
qs = Addressable::URI.new
|
132
|
+
qs.query_values = query_values
|
133
|
+
qs.to_s
|
134
|
+
end
|
135
|
+
|
136
|
+
def make
|
137
|
+
logger = Logger.new STDOUT
|
138
|
+
logger.debug self.url
|
139
|
+
response = case self.http_method.to_sym
|
140
|
+
when :get,:GET
|
141
|
+
Typhoeus::Request.get(
|
142
|
+
self.url,
|
143
|
+
:headers => self.headers.stringify_keys,
|
144
|
+
)
|
145
|
+
|
146
|
+
when :post,:POST
|
147
|
+
Typhoeus::Request.post(
|
148
|
+
self.url,
|
149
|
+
:body => self.outgoing_body,
|
150
|
+
:headers => self.headers.stringify_keys,
|
151
|
+
)
|
152
|
+
|
153
|
+
when :put,:PUT
|
154
|
+
Typhoeus::Request.put(
|
155
|
+
self.url,
|
156
|
+
:body => self.outgoing_body,
|
157
|
+
:headers => self.headers.stringify_keys,
|
158
|
+
)
|
159
|
+
|
160
|
+
when :delete,:DELETE
|
161
|
+
Typhoeus::Request.delete(
|
162
|
+
self.url,
|
163
|
+
:body => self.outgoing_body,
|
164
|
+
:headers => self.headers.stringify_keys,
|
165
|
+
)
|
166
|
+
end
|
167
|
+
Response.new(response)
|
168
|
+
end
|
169
|
+
|
170
|
+
def response
|
171
|
+
self.make
|
172
|
+
end
|
173
|
+
|
174
|
+
def response_code_pretty
|
175
|
+
return unless @response.present?
|
176
|
+
@response.code.to_s
|
177
|
+
end
|
178
|
+
|
179
|
+
def response_headers_pretty
|
180
|
+
return unless @response.present?
|
181
|
+
# JSON.pretty_generate(@response.headers).gsub(/\n/, '<br/>') # <- This was for RestClient
|
182
|
+
@response.headers.gsub(/\n/, '<br/>') # <- This is for Typhoeus
|
183
|
+
end
|
184
|
+
|
185
|
+
end
|
186
|
+
end
|
@@ -0,0 +1,70 @@
|
|
1
|
+
module Swagger
|
2
|
+
|
3
|
+
class Response
|
4
|
+
require 'json'
|
5
|
+
|
6
|
+
attr_accessor :raw
|
7
|
+
|
8
|
+
def initialize(raw)
|
9
|
+
self.raw = raw
|
10
|
+
|
11
|
+
case self.code
|
12
|
+
when 500..510 then raise(ServerError, self.error_message)
|
13
|
+
when 299..426 then raise(ClientError, self.error_message)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def code
|
18
|
+
raw.code
|
19
|
+
end
|
20
|
+
|
21
|
+
# Account for error messages that take different forms...
|
22
|
+
def error_message
|
23
|
+
body['message']
|
24
|
+
rescue
|
25
|
+
body
|
26
|
+
end
|
27
|
+
|
28
|
+
# If body is JSON, parse it
|
29
|
+
# Otherwise return raw string
|
30
|
+
def body
|
31
|
+
JSON.parse raw.body
|
32
|
+
rescue
|
33
|
+
raw.body
|
34
|
+
end
|
35
|
+
|
36
|
+
# `headers_hash` is a Typhoeus-specific extension of Hash,
|
37
|
+
# so simplify it back into a regular old Hash.
|
38
|
+
def headers
|
39
|
+
h = {}
|
40
|
+
raw.headers_hash.each {|k,v| h[k] = v }
|
41
|
+
h
|
42
|
+
end
|
43
|
+
|
44
|
+
# Extract the response format from the header hash
|
45
|
+
# e.g. {'Content-Type' => 'application/json'}
|
46
|
+
def format
|
47
|
+
headers['Content-Type'].split("/").last.downcase
|
48
|
+
end
|
49
|
+
|
50
|
+
def json?
|
51
|
+
format == 'json'
|
52
|
+
end
|
53
|
+
|
54
|
+
def xml?
|
55
|
+
format == 'xml'
|
56
|
+
end
|
57
|
+
|
58
|
+
def pretty_body
|
59
|
+
return unless body.present?
|
60
|
+
case format
|
61
|
+
when 'json' then JSON.pretty_generate(body).gsub(/\n/, '<br/>')
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
def pretty_headers
|
66
|
+
JSON.pretty_generate(headers).gsub(/\n/, '<br/>')
|
67
|
+
end
|
68
|
+
|
69
|
+
end
|
70
|
+
end
|
data/lib/swagger.rb
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
require 'monkey'
|
2
|
+
require 'swagger/configuration'
|
3
|
+
require 'swagger/request'
|
4
|
+
require 'swagger/response'
|
5
|
+
require 'swagger/version'
|
6
|
+
require 'logger'
|
7
|
+
|
8
|
+
module Swagger
|
9
|
+
|
10
|
+
class << self
|
11
|
+
attr_accessor :logger
|
12
|
+
|
13
|
+
# A Swagger configuration object. Must act like a hash and return sensible
|
14
|
+
# values for all Swagger configuration options. See Swagger::Configuration.
|
15
|
+
attr_accessor :configuration
|
16
|
+
|
17
|
+
attr_accessor :resources
|
18
|
+
|
19
|
+
# Call this method to modify defaults in your initializers.
|
20
|
+
#
|
21
|
+
# @example
|
22
|
+
# Swagger.configure do |config|
|
23
|
+
# config.api_key = '1234567890abcdef' # required
|
24
|
+
# config.username = 'wordlover' # optional, but needed for user-related functions
|
25
|
+
# config.password = 'i<3words' # optional, but needed for user-related functions
|
26
|
+
# config.format = 'json' # optional, defaults to 'json'
|
27
|
+
# end
|
28
|
+
#
|
29
|
+
def configure
|
30
|
+
self.configuration ||= Configuration.new
|
31
|
+
yield(configuration) if block_given?
|
32
|
+
|
33
|
+
# Configure logger. Default to use Rails
|
34
|
+
self.logger ||= configuration.logger || (defined?(Rails) ? Rails.logger : Logger.new(STDOUT))
|
35
|
+
|
36
|
+
# remove :// from scheme
|
37
|
+
configuration.scheme.sub!(/:\/\//, '')
|
38
|
+
|
39
|
+
# remove http(s):// and anything after a slash
|
40
|
+
configuration.host.sub!(/https?:\/\//, '')
|
41
|
+
configuration.host = configuration.host.split('/').first
|
42
|
+
|
43
|
+
# Add leading and trailing slashes to base_path
|
44
|
+
configuration.base_path = "/#{configuration.base_path}".gsub(/\/+/, '/')
|
45
|
+
configuration.base_path = "" if configuration.base_path == "/"
|
46
|
+
end
|
47
|
+
|
48
|
+
def authenticated?
|
49
|
+
Swagger.configuration.auth_token.present?
|
50
|
+
end
|
51
|
+
|
52
|
+
def de_authenticate
|
53
|
+
Swagger.configuration.auth_token = nil
|
54
|
+
end
|
55
|
+
|
56
|
+
def authenticate
|
57
|
+
return if Swagger.authenticated?
|
58
|
+
|
59
|
+
if Swagger.configuration.username.blank? || Swagger.configuration.password.blank?
|
60
|
+
raise ClientError, "Username and password are required to authenticate."
|
61
|
+
end
|
62
|
+
|
63
|
+
request = Swagger::Request.new(
|
64
|
+
:get,
|
65
|
+
"account/authenticate/{username}",
|
66
|
+
:params => {
|
67
|
+
:username => Swagger.configuration.username,
|
68
|
+
:password => Swagger.configuration.password
|
69
|
+
}
|
70
|
+
)
|
71
|
+
|
72
|
+
response_body = request.response.body
|
73
|
+
Swagger.configuration.auth_token = response_body['token']
|
74
|
+
end
|
75
|
+
|
76
|
+
end
|
77
|
+
|
78
|
+
end
|
79
|
+
|
80
|
+
class ServerError < StandardError
|
81
|
+
end
|
82
|
+
|
83
|
+
class ClientError < StandardError
|
84
|
+
end
|