datafiniti 0.2.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,287 @@
1
+ # Copyright (c) 2008-2009 The Rubyists, LLC (effortless systems) <rubyists@rubyists.com>
2
+ # Distributed under the terms of the MIT license.
3
+ # See the LICENSE file that accompanied this software for the full MIT License text
4
+ #
5
+ # Add inflection methods to String, which allows the easy transformation of
6
+ # words from singular to plural,class names to table names, modularized class
7
+ # names to ones without, and class names to foreign keys.
8
+
9
+ class String
10
+ # This module acts as a singleton returned/yielded by String.inflections,
11
+ # which is used to override or specify additional inflection rules. Examples:
12
+ #
13
+ # String.inflections do |inflect|
14
+ # inflect.plural /^(ox)$/i, '\1\2en'
15
+ # inflect.singular /^(ox)en/i, '\1'
16
+ #
17
+ # inflect.irregular 'octopus', 'octopi'
18
+ #
19
+ # inflect.uncountable "equipment"
20
+ # end
21
+ #
22
+ # New rules are added at the top. So in the example above, the irregular rule for octopus will now be the first of the
23
+ # pluralization and singularization rules that is runs. This guarantees that your rules run before any of the rules that may
24
+ # already have been loaded.
25
+ module Inflections
26
+ @plurals, @singulars, @uncountables = [], [], []
27
+
28
+ class << self
29
+ attr_reader :plurals, :singulars, :uncountables
30
+ end
31
+
32
+ # Clears the loaded inflections within a given scope (default is :all). Give the scope as a symbol of the inflection type,
33
+ # the options are: :plurals, :singulars, :uncountables
34
+ #
35
+ # Examples:
36
+ # clear :all
37
+ # clear :plurals
38
+ def self.clear(scope = :all)
39
+ case scope
40
+ when :all
41
+ @plurals, @singulars, @uncountables = [], [], []
42
+ else
43
+ instance_variable_set("@#{scope}", [])
44
+ end
45
+ end
46
+
47
+ # Specifies a new irregular that applies to both pluralization and singularization at the same time. This can only be used
48
+ # for strings, not regular expressions. You simply pass the irregular in singular and plural form.
49
+ #
50
+ # Examples:
51
+ # irregular 'octopus', 'octopi'
52
+ # irregular 'person', 'people'
53
+ def self.irregular(singular, plural)
54
+ plural(Regexp.new("(#{singular[0,1]})#{singular[1..-1]}$", "i"), '\1' + plural[1..-1])
55
+ singular(Regexp.new("(#{plural[0,1]})#{plural[1..-1]}$", "i"), '\1' + singular[1..-1])
56
+ end
57
+
58
+ # Specifies a new pluralization rule and its replacement. The rule can either be a string or a regular expression.
59
+ # The replacement should always be a string that may include references to the matched data from the rule.
60
+ #
61
+ # Example:
62
+ # plural(/(x|ch|ss|sh)$/i, '\1es')
63
+ def self.plural(rule, replacement)
64
+ @plurals.insert(0, [rule, replacement])
65
+ end
66
+
67
+ # Specifies a new singularization rule and its replacement. The rule can either be a string or a regular expression.
68
+ # The replacement should always be a string that may include references to the matched data from the rule.
69
+ #
70
+ # Example:
71
+ # singular(/([^aeiouy]|qu)ies$/i, '\1y')
72
+ def self.singular(rule, replacement)
73
+ @singulars.insert(0, [rule, replacement])
74
+ end
75
+
76
+ # Add uncountable words that shouldn't be attempted inflected.
77
+ #
78
+ # Examples:
79
+ # uncountable "money"
80
+ # uncountable "money", "information"
81
+ # uncountable %w( money information rice )
82
+ def self.uncountable(*words)
83
+ (@uncountables << words).flatten!
84
+ end
85
+
86
+ # Setup the default inflections
87
+ plural(/$/, 's')
88
+ plural(/s$/i, 's')
89
+ plural(/(ax|test)is$/i, '\1es')
90
+ plural(/(octop|vir)us$/i, '\1i')
91
+ plural(/(alias|status)$/i, '\1es')
92
+ plural(/(bu)s$/i, '\1ses')
93
+ plural(/(buffal|tomat)o$/i, '\1oes')
94
+ plural(/([ti])um$/i, '\1a')
95
+ plural(/sis$/i, 'ses')
96
+ plural(/(?:([^f])fe|([lr])f)$/i, '\1\2ves')
97
+ plural(/(hive)$/i, '\1s')
98
+ plural(/([^aeiouy]|qu)y$/i, '\1ies')
99
+ plural(/(x|ch|ss|sh)$/i, '\1es')
100
+ plural(/(matr|vert|ind)ix|ex$/i, '\1ices')
101
+ plural(/([m|l])ouse$/i, '\1ice')
102
+ plural(/^(ox)$/i, '\1en')
103
+ plural(/(quiz)$/i, '\1zes')
104
+
105
+ singular(/s$/i, '')
106
+ singular(/(n)ews$/i, '\1ews')
107
+ singular(/([ti])a$/i, '\1um')
108
+ singular(/((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/i, '\1\2sis')
109
+ singular(/(^analy)ses$/i, '\1sis')
110
+ singular(/([^f])ves$/i, '\1fe')
111
+ singular(/(hive)s$/i, '\1')
112
+ singular(/(tive)s$/i, '\1')
113
+ singular(/([lr])ves$/i, '\1f')
114
+ singular(/([^aeiouy]|qu)ies$/i, '\1y')
115
+ singular(/(s)eries$/i, '\1eries')
116
+ singular(/(m)ovies$/i, '\1ovie')
117
+ singular(/(x|ch|ss|sh)es$/i, '\1')
118
+ singular(/([m|l])ice$/i, '\1ouse')
119
+ singular(/(bus)es$/i, '\1')
120
+ singular(/(o)es$/i, '\1')
121
+ singular(/(shoe)s$/i, '\1')
122
+ singular(/(cris|ax|test)es$/i, '\1is')
123
+ singular(/(octop|vir)i$/i, '\1us')
124
+ singular(/(alias|status)es$/i, '\1')
125
+ singular(/^(ox)en/i, '\1')
126
+ singular(/(vert|ind)ices$/i, '\1ex')
127
+ singular(/(matr)ices$/i, '\1ix')
128
+ singular(/(quiz)zes$/i, '\1')
129
+
130
+ irregular('person', 'people')
131
+ irregular('man', 'men')
132
+ irregular('child', 'children')
133
+ irregular('sex', 'sexes')
134
+ irregular('move', 'moves')
135
+
136
+ uncountable(%w(equipment information rice money species series fish sheep))
137
+ end
138
+
139
+ # Yield the Inflections module if a block is given, and return
140
+ # the Inflections module.
141
+ def self.inflections
142
+ yield Inflections if block_given?
143
+ Inflections
144
+ end
145
+
146
+ # By default, camelize converts the string to UpperCamelCase. If the argument to camelize
147
+ # is set to :lower then camelize produces lowerCamelCase.
148
+ #
149
+ # camelize will also convert '/' to '::' which is useful for converting paths to namespaces
150
+ #
151
+ # Examples
152
+ # "active_record".camelize #=> "ActiveRecord"
153
+ # "active_record".camelize(:lower) #=> "activeRecord"
154
+ # "active_record/errors".camelize #=> "ActiveRecord::Errors"
155
+ # "active_record/errors".camelize(:lower) #=> "activeRecord::Errors"
156
+ def camelize(first_letter_in_uppercase = :upper)
157
+ s = gsub(/\/(.?)/){|x| "::#{x[-1..-1].upcase unless x == '/'}"}.gsub(/(^|_)(.)/){|x| x[-1..-1].upcase}
158
+ s[0...1] = s[0...1].downcase unless first_letter_in_uppercase == :upper
159
+ s
160
+ end
161
+ alias_method :camelcase, :camelize
162
+
163
+ # Singularizes and camelizes the string. Also strips out all characters preceding
164
+ # and including a period (".").
165
+ #
166
+ # Examples
167
+ # "egg_and_hams".classify #=> "EggAndHam"
168
+ # "post".classify #=> "Post"
169
+ # "schema.post".classify #=> "Post"
170
+ def classify
171
+ sub(/.*\./, '').singularize.camelize
172
+ end
173
+
174
+ # Constantize tries to find a declared constant with the name specified
175
+ # in the string. It raises a NameError when the name is not in CamelCase
176
+ # or is not initialized.
177
+ #
178
+ # Examples
179
+ # "Module".constantize #=> Module
180
+ # "Class".constantize #=> Class
181
+ def constantize
182
+ raise(NameError, "#{inspect} is not a valid constant name!") unless m = /\A(?:::)?([A-Z]\w*(?:::[A-Z]\w*)*)\z/.match(self)
183
+ Object.module_eval("::#{m[1]}", __FILE__, __LINE__)
184
+ end
185
+
186
+ # Replaces underscores with dashes in the string.
187
+ #
188
+ # Example
189
+ # "puni_puni".dasherize #=> "puni-puni"
190
+ def dasherize
191
+ gsub(/_/, '-')
192
+ end
193
+
194
+ # Removes the module part from the expression in the string
195
+ #
196
+ # Examples
197
+ # "ActiveRecord::CoreExtensions::String::Inflections".demodulize #=> "Inflections"
198
+ # "Inflections".demodulize #=> "Inflections"
199
+ def demodulize
200
+ gsub(/^.*::/, '')
201
+ end
202
+
203
+ # Creates a foreign key name from a class name.
204
+ # +use_underscore+ sets whether the method should put '_' between the name and 'id'.
205
+ #
206
+ # Examples
207
+ # "Message".foreign_key #=> "message_id"
208
+ # "Message".foreign_key(false) #=> "messageid"
209
+ # "Admin::Post".foreign_key #=> "post_id"
210
+ def foreign_key(use_underscore = true)
211
+ "#{demodulize.underscore}#{'_' if use_underscore}id"
212
+ end
213
+
214
+ # Capitalizes the first word and turns underscores into spaces and strips _id.
215
+ # Like titleize, this is meant for creating pretty output.
216
+ #
217
+ # Examples
218
+ # "employee_salary" #=> "Employee salary"
219
+ # "author_id" #=> "Author"
220
+ def humanize
221
+ gsub(/_id$/, "").gsub(/_/, " ").capitalize
222
+ end
223
+
224
+ # Returns the plural form of the word in the string.
225
+ #
226
+ # Examples
227
+ # "post".pluralize #=> "posts"
228
+ # "octopus".pluralize #=> "octopi"
229
+ # "sheep".pluralize #=> "sheep"
230
+ # "words".pluralize #=> "words"
231
+ # "the blue mailman".pluralize #=> "the blue mailmen"
232
+ # "CamelOctopus".pluralize #=> "CamelOctopi"
233
+ def pluralize
234
+ result = dup
235
+ Inflections.plurals.each{|(rule, replacement)| break if result.gsub!(rule, replacement)} unless Inflections.uncountables.include?(downcase)
236
+ result
237
+ end
238
+
239
+ # The reverse of pluralize, returns the singular form of a word in a string.
240
+ #
241
+ # Examples
242
+ # "posts".singularize #=> "post"
243
+ # "octopi".singularize #=> "octopus"
244
+ # "sheep".singluarize #=> "sheep"
245
+ # "word".singluarize #=> "word"
246
+ # "the blue mailmen".singularize #=> "the blue mailman"
247
+ # "CamelOctopi".singularize #=> "CamelOctopus"
248
+ def singularize
249
+ result = dup
250
+ Inflections.singulars.each{|(rule, replacement)| break if result.gsub!(rule, replacement)} unless Inflections.uncountables.include?(downcase)
251
+ result
252
+ end
253
+
254
+ # Underscores and pluralizes the string.
255
+ #
256
+ # Examples
257
+ # "RawScaledScorer".tableize #=> "raw_scaled_scorers"
258
+ # "egg_and_ham".tableize #=> "egg_and_hams"
259
+ # "fancyCategory".tableize #=> "fancy_categories"
260
+ def tableize
261
+ underscore.pluralize
262
+ end
263
+
264
+ # Capitalizes all the words and replaces some characters in the string to create
265
+ # a nicer looking title. Titleize is meant for creating pretty output.
266
+ #
267
+ # titleize is also aliased as as titlecase
268
+ #
269
+ # Examples
270
+ # "man from the boondocks".titleize #=> "Man From The Boondocks"
271
+ # "x-men: the last stand".titleize #=> "X Men: The Last Stand"
272
+ def titleize
273
+ underscore.humanize.gsub(/\b([a-z])/){|x| x[-1..-1].upcase}
274
+ end
275
+ alias_method :titlecase, :titleize
276
+
277
+ # The reverse of camelize. Makes an underscored form from the expression in the string.
278
+ # Also changes '::' to '/' to convert namespaces to paths.
279
+ #
280
+ # Examples
281
+ # "ActiveRecord".underscore #=> "active_record"
282
+ # "ActiveRecord::Errors".underscore #=> active_record/errors
283
+ def underscore
284
+ gsub(/::/, '/').gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
285
+ gsub(/([a-z\d])([A-Z])/,'\1_\2').tr("-", "_").downcase
286
+ end
287
+ end
@@ -0,0 +1,33 @@
1
+ require_relative 'api'
2
+
3
+ module Datafiniti
4
+
5
+ class Listings < Api
6
+
7
+ attr_accessor :format
8
+ attr_accessor :response
9
+ attr_reader :records
10
+ attr_reader :total
11
+ attr_reader :preview
12
+
13
+ def initialize(args={})
14
+ @format = "json"
15
+ super(args)
16
+ end
17
+
18
+ def where(query)
19
+ query = build_new_query(query)
20
+ @response = get("/v2/data/locations/preview?view=portal_business_listings_json&q=#{query}")
21
+ update_records
22
+ return @response
23
+ end
24
+
25
+ def solr_query(query)
26
+ query = build_new_query(query, true)
27
+ @response = get("/v2/data/locations/preview?view=portal_business_listings_json&q=#{query}")
28
+ update_records
29
+ return @response
30
+ end
31
+ end
32
+
33
+ end
@@ -0,0 +1,19 @@
1
+ require_relative 'api'
2
+
3
+ module Datafiniti
4
+
5
+ class Locations < Api
6
+
7
+ attr_accessor :format
8
+ attr_accessor :response
9
+ attr_reader :records
10
+ attr_reader :total
11
+ attr_reader :preview
12
+
13
+ def initialize(args={})
14
+ @format = "json"
15
+ super(args)
16
+ end
17
+ end
18
+
19
+ end
@@ -0,0 +1,19 @@
1
+ require_relative 'api'
2
+
3
+ module Datafiniti
4
+
5
+ class Products < Api
6
+
7
+ attr_accessor :format
8
+ attr_accessor :response
9
+ attr_reader :records
10
+ attr_reader :total
11
+ attr_reader :preview
12
+
13
+ def initialize(args={})
14
+ @format = "json"
15
+ super(args)
16
+ end
17
+ end
18
+
19
+ end
@@ -0,0 +1,32 @@
1
+ module Datafiniti
2
+
3
+ class User
4
+ attr_accessor :token
5
+
6
+ def initialize(api_token=ENV['DATAFINITI_API_KEY'])
7
+ self.token = api_token
8
+ end
9
+
10
+ include HTTParty
11
+
12
+ base_uri "https://#{ENV['DATAFINITI_API_KEY']}:@api.datafiniti.co"
13
+ basic_auth ENV['DATAFINITI_API_KEY'], ''
14
+
15
+ def profile(force=false)
16
+ force ? @profile = get_profile : @profile ||= get_profile
17
+ end
18
+
19
+ def method_missing(name, *args, &block)
20
+ profile.has_key?(name.to_s) ? profile[name.to_s] : super
21
+ # profile.has_key?(name.to_s) ? profile.send(name.to_sym) : super
22
+ end
23
+
24
+ private
25
+
26
+ def get_profile
27
+ JSON.parse(self.class.get "/v2/users/#{self.token}")
28
+ end
29
+
30
+ end#user
31
+
32
+ end#datafiniti
@@ -0,0 +1,3 @@
1
+ module Datafiniti
2
+ VERSION = "0.2.8"
3
+ end
@@ -0,0 +1,48 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: get
5
+ uri: https://jj4dk6jp3rejqfdo8jrzmqqyltablx0j:@api.datafiniti.co/v2/users/jj4dk6jp3rejqfdo8jrzmqqyltablx0j
6
+ body:
7
+ encoding: US-ASCII
8
+ string: ''
9
+ headers:
10
+ Accept-Encoding:
11
+ - gzip;q=1.0,deflate;q=0.6,identity;q=0.3
12
+ Accept:
13
+ - "*/*"
14
+ User-Agent:
15
+ - Ruby
16
+ response:
17
+ status:
18
+ code: 200
19
+ message: OK
20
+ headers:
21
+ Server:
22
+ - nginx
23
+ Date:
24
+ - Thu, 15 Jan 2015 20:28:23 GMT
25
+ Content-Type:
26
+ - application/json
27
+ Transfer-Encoding:
28
+ - chunked
29
+ Connection:
30
+ - keep-alive
31
+ Vary:
32
+ - Accept-Encoding
33
+ Access-Control-Allow-Origin:
34
+ - "*"
35
+ Access-Control-Allow-Methods:
36
+ - PUTPOSTDELETEGET, OPTIONS
37
+ Access-Control-Allow-Headers:
38
+ - Origin, X-Requested-With, Authorization, Content-Type, Accept
39
+ Access-Control-Allow-Credentials:
40
+ - 'true'
41
+ Access-Control-Max-Age:
42
+ - '1000'
43
+ body:
44
+ encoding: UTF-8
45
+ string: '{"token":"jj4dk6jp3rejqfdo8jrzmqqyltablx0j","organization":"undefined","email":"nick+1@datafiniti.net","first_name":"undefined","last_name":"undefined","phone_number":"undefined","stripe_customer_id":"cus_4Poistq7iRTtaA","plan_id":"100000M","type":"user","active":1,"monthly_downloads":100000,"yearly_downloads":1100730,"date_registered":"2014-7-16"}'
46
+ http_version:
47
+ recorded_at: Thu, 15 Jan 2015 20:22:35 GMT
48
+ recorded_with: VCR 2.9.3