Brewry 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 794dc054972a8ad20a14fdbddb8a54985d7f72b8
4
+ data.tar.gz: 39ab0e85495ef0afa31e96785e4b1dd76f61799e
5
+ SHA512:
6
+ metadata.gz: 3a008be39c6dd7dfeb84186473b98e3fb189990ead2e2041cdca4bd09f185ed84af64ea2078af09f396979d69090e8160b53331e95cd1ee802e4a280a197f223
7
+ data.tar.gz: 4f4aea34da4154ca7853db8461e16b888d74d43921a47a4773424ec1b0fae8fb2fcf16033b6a4c462080b71f36cde0a859d309bd35dc1310a3deebc2f0c17c22
data/.gitignore ADDED
@@ -0,0 +1 @@
1
+ Brewry-*
data/README.rdoc ADDED
@@ -0,0 +1,41 @@
1
+ == Brewry
2
+
3
+ Brewry gives you an API interface to talk to BreweryDB.
4
+
5
+ * It retuns everything as a hash so you can just pass it to ActiveRecord
6
+ * It recursively changes the hash to change the 'id' keys into 'guid' so it
7
+ doesn't interfere with your own ActiveRecord Ids. Changing it to whatever you
8
+ want is on the works.
9
+ * Because of metaprogramming, very little code is needed to touch most api
10
+ endoints for BreweryDB.
11
+
12
+ == Configuration
13
+
14
+ For Rails:
15
+
16
+ Create an initializer in config/initializers/brewry.rb
17
+
18
+ Brewry.configure do |config|
19
+ config.api_key = 'some api key'
20
+ end
21
+
22
+ == Examples
23
+
24
+ Fetch some beers with the name argument:
25
+
26
+ Brewry.search_beers(name: 'Corona Light')
27
+
28
+ Fetch some styles without arguments - this will return all beer styles:
29
+
30
+ Brewry.search_styles
31
+
32
+ == Releases
33
+
34
+ 0.0.1
35
+ * Initialize project
36
+
37
+ == TODO
38
+
39
+ * Change class so it can be instantiated.
40
+ * Add tests
41
+ * Allow changing of breweryDB id's into whatever the user needs.
data/brewry.gemspec ADDED
@@ -0,0 +1,21 @@
1
+ require './lib/brewry/version'
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = 'Brewry'
5
+ s.version = Brewry::VERSION
6
+ s.summary = 'Brewry provides an interface to the BreweryDB API.'
7
+ s.description = 'Brewry provides an interface to the BreweryDB API. Instead of returning a struct or some other Brewry instance, it returns a hash that can quickly be inserted into ActiveRecord. It also allows you to replace the keys for certain results such as key so you can keep track of them with your own id\'s.'
8
+ s.authors = ['Daniel Ochoa']
9
+ s.email = ['dannytenaglias@gmail.com']
10
+ s.homepage = 'https://github.com/DanyHunter/brewry'
11
+ s.license = 'MIT'
12
+
13
+ s.required_ruby_version = '>= 2.0'
14
+
15
+ s.add_dependency 'httparty', '~> 0.13'
16
+
17
+ s.files = `git ls-files`.split($\)
18
+ s.require_paths = ['lib']
19
+ s.executables = s.files.grep(%r{^bin/}).map {|f| File.basename(f)}
20
+ s.test_files = s.files.grep(%r{^(test|spec|features)/})
21
+ end
@@ -0,0 +1,3 @@
1
+ module Brewry
2
+ VERSION = '0.0.1'
3
+ end
data/lib/brewry.rb ADDED
@@ -0,0 +1,114 @@
1
+ require 'rubygems'
2
+ require 'httparty'
3
+ require_relative 'utils/brewry_string_utils'
4
+
5
+ class Brewry
6
+ # TODO: Change class so it needs to be instantiated
7
+ using BrewryStringUtils
8
+ include HTTParty
9
+
10
+ attr_accessor :search_hash
11
+
12
+ ### Configuration ###
13
+
14
+ base_uri 'http://api.brewerydb.com/v2'
15
+ @@api_key = nil
16
+
17
+ # Brewry.configure do |config|
18
+ # config.api_key = 'some_api_key'
19
+ # end
20
+ def self.configure
21
+ yield self
22
+ end
23
+
24
+ def self.api_key
25
+ @@api_key
26
+ end
27
+
28
+ def self.api_key=(apikey)
29
+ @@api_key = apikey
30
+ end
31
+
32
+ ### Methods ###
33
+
34
+ # override method_missing in order to create
35
+ # dynamic methods for searches. Ex.:
36
+ # BreweryDB.search_beers(name: 'Corona Light')
37
+ # BreweryDB.search_styles()
38
+ def self.method_missing(meth, *args, &block)
39
+ if meth.to_s =~ /^search_(.+)$/
40
+ search_for($1, *args)
41
+ else
42
+ super
43
+ end
44
+ end
45
+
46
+ # standard beer search - WILL DEPRECATE IN THE FUTURE
47
+ def self.beer_search(search)
48
+ query = build_query q: search, type: 'beer'
49
+ search = get('/search', query)
50
+ return_pretty_results(search)
51
+ end
52
+
53
+ # returns raw httparty query. See
54
+ # http://www.brewerydb.com/developers/docs
55
+ # for full range of options
56
+ def self.raw_query(path, options)
57
+ query = build_query(options)
58
+ get(path, query)
59
+ end
60
+
61
+ # To use BreweryDB search path. Options are:
62
+ # Ex. { q: 'Goosinator', type: 'beer' }
63
+ def self.search(options)
64
+ query = build_query options
65
+ search = get('/search', query)
66
+ return_pretty_results(search)
67
+ end
68
+
69
+ protected
70
+
71
+ # Used by the method_missing method for dynamic searches
72
+ def self.search_for(path, options = {})
73
+ query = build_query(options)
74
+ search = get("/#{path}", query)
75
+ return_pretty_results(search)
76
+ end
77
+
78
+ # underscore_and_symbolize is defined in utils/api_utilities.rb
79
+ def self.return_pretty_results(payload)
80
+ return query_error(payload) if payload['status'] == 'failure'
81
+ data = payload.parsed_response['data']
82
+ data.map do |obj|
83
+ obj = self.underscore_and_symbolize obj
84
+ end if data
85
+ end
86
+
87
+ def self.build_query(search = {})
88
+ search_hash = clean_search_hash
89
+ search_hash[:query].merge! search; search_hash
90
+ end
91
+
92
+ def self.query_error(search)
93
+ self.underscore_and_symbolize search
94
+ # TODO: throw some exception or special fail construct
95
+ end
96
+
97
+ def self.clean_search_hash
98
+ { query: { key: @@api_key } }
99
+ end
100
+
101
+ # TODO: User should be able to change the foreignkeys argument
102
+ def self.underscore_and_symbolize(obj, foreignkeys = :guid)
103
+ obj.inject({}) do |hash, (key, val)|
104
+ val = underscore_and_symbolize(val) if val.kind_of? Hash
105
+ if key == 'id'
106
+ hash.shift
107
+ hash[foreignkeys] = val
108
+ else
109
+ hash[key.underscore.to_sym] = val
110
+ end
111
+ hash
112
+ end
113
+ end
114
+ end
@@ -0,0 +1,14 @@
1
+ module BrewryStringUtils
2
+ # This is a ruby refinement. Only works in ruby >= 2.0
3
+ # This method is monkey patched into String when using Rails, but in this
4
+ # fashion it will always be scoped to the BrewryStringUtils module.
5
+ refine String do
6
+ def underscore
7
+ self.gsub(/::/, '/').
8
+ gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
9
+ gsub(/([a-z\d])([A-Z])/,'\1_\2').
10
+ tr("-", "_").
11
+ downcase
12
+ end
13
+ end
14
+ end
metadata ADDED
@@ -0,0 +1,67 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: Brewry
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Daniel Ochoa
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-05-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: httparty
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.13'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.13'
27
+ description: Brewry provides an interface to the BreweryDB API. Instead of returning
28
+ a struct or some other Brewry instance, it returns a hash that can quickly be inserted
29
+ into ActiveRecord. It also allows you to replace the keys for certain results such
30
+ as key so you can keep track of them with your own id's.
31
+ email:
32
+ - dannytenaglias@gmail.com
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - ".gitignore"
38
+ - README.rdoc
39
+ - brewry.gemspec
40
+ - lib/brewry.rb
41
+ - lib/brewry/version.rb
42
+ - lib/utils/brewry_string_utils.rb
43
+ homepage: https://github.com/DanyHunter/brewry
44
+ licenses:
45
+ - MIT
46
+ metadata: {}
47
+ post_install_message:
48
+ rdoc_options: []
49
+ require_paths:
50
+ - lib
51
+ required_ruby_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '2.0'
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ requirements: []
62
+ rubyforge_project:
63
+ rubygems_version: 2.2.1
64
+ signing_key:
65
+ specification_version: 4
66
+ summary: Brewry provides an interface to the BreweryDB API.
67
+ test_files: []