hungry 0.1.0 → 0.1.5

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 4f382ff7047bd18e021c8c7320fb9a95542cf0ac
4
- data.tar.gz: a9bd20ca16e736a776b557b32d789987486e71c6
2
+ SHA256:
3
+ metadata.gz: eaa4a597035654c264470bf95a14b6f9f367e8133965d53601bf5e20273f8016
4
+ data.tar.gz: 8e64ce4240b44aa3d0d9d29266b2e0bda642a512f39ef029fa65d524f7e28eec
5
5
  SHA512:
6
- metadata.gz: 72012a98730db3ab4c92c5bb72994f0eca7cb25009e27ceb0e3be395cbc86d91d7d860b0719394fee02661cd3ab141c0760456e173caf0e62ad66e5c68fb8830
7
- data.tar.gz: ca2815e04c8a495f0f168826d49db1d4b9150fc2a0f4864e2568898c9f9ff28ec43a45d0381c6331dcc9cf578ab43c4fe14e09b15e5581b839948a8b799f6cc2
6
+ metadata.gz: d3f56ccca758706e7b5788820900b4418c4c9850238fe7e8f5bea78ab9e8b02a5719d02655f14a93c664aa75c87524f7890f7e9cc0480fea99a3c9d449abf1ac
7
+ data.tar.gz: 88e9c4d0df376812b7e51c705d6ebd3d47e63e99780d63f71b25699d172183400c92ff4749f46917c93d2247d04cefa46ba0685983c1ac95f5d9a89cc292fcbc
@@ -1,6 +1,3 @@
1
- # SSH Key to pull private repositories:
2
- source_key: "LS0tLS1CRUdJTiBSU0EgUFJJVkFURSBLRVktLS0tLQpNSUlFb3dJQkFBS0NBUUVBM0MySG13YUFIOTQrOW9pb1NSU2I4enk3OHlQemxuL0hxcEdEVjhWREllR1NseTFPCi9VUmVpRnpkcEY2eEN1Y3RRWFkvVHA3QTBxKzMrMWd5cnBuZW00SWNVYUZTZ1kxelYxZ2JQY0VWVzJlWDIxVlEKN1pDdVBIYzFkbDI1K2gvbzQ3Z0l6aWwrT3dyUTNvdFVjd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40,19 @@ GEM
40
40
  guard (~> 2.0)
41
41
  guard-compat (~> 1.0)
42
42
  spork (>= 0.8.4)
43
- httparty (0.13.7)
44
- json (~> 1.8)
43
+ httparty (0.17.3)
44
+ mime-types (~> 3.0)
45
45
  multi_xml (>= 0.5.2)
46
- json (1.8.3)
47
46
  listen (3.1.5)
48
47
  rb-fsevent (~> 0.9, >= 0.9.4)
49
48
  rb-inotify (~> 0.9, >= 0.9.7)
50
49
  ruby_dep (~> 1.2)
51
50
  lumberjack (1.0.10)
52
51
  method_source (0.8.2)
53
- multi_xml (0.5.5)
52
+ mime-types (3.3.1)
53
+ mime-types-data (~> 3.2015)
54
+ mime-types-data (3.2019.1009)
55
+ multi_xml (0.6.0)
54
56
  nenv (0.3.0)
55
57
  notiffany (0.1.0)
56
58
  nenv (~> 0.1)
@@ -101,4 +103,4 @@ DEPENDENCIES
101
103
  rspec
102
104
 
103
105
  BUNDLED WITH
104
- 1.12.5
106
+ 1.17.3
data/Rakefile CHANGED
@@ -5,11 +5,11 @@ task :console do
5
5
  require 'irb'
6
6
  require 'irb/completion'
7
7
  require_relative './lib/hungry'
8
-
8
+
9
9
  Hungry.logger = Logger.new(STDOUT).tap do |logger|
10
10
  logger.level = Logger::DEBUG
11
11
  end
12
-
12
+
13
13
  ARGV.clear
14
14
  IRB.start
15
15
  end
@@ -1,21 +1,18 @@
1
1
  # -*- encoding: utf-8 -*-
2
- $:.push File.expand_path('../lib', __FILE__)
3
- require 'hungry'
4
-
5
2
  Gem::Specification.new do |s|
6
3
  s.name = 'hungry'
7
- s.version = Hungry::VERSION
4
+ s.version = '0.1.5'
8
5
  s.authors = ['Tom-Eric Gerritsen']
9
6
  s.email = ['tomeric@eet.nu']
10
7
  s.homepage = 'http://github.com/eet-nu/hungry'
11
8
  s.summary = %q{An interface to the Eet.nu API.}
12
9
  s.description = %q{An interface to the Eet.nu API.}
13
-
10
+
14
11
  s.files = `git ls-files`.split("\n")
15
12
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
16
13
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
17
14
  s.require_paths = ['lib']
18
-
15
+
19
16
  # specify any dependencies here:
20
17
  s.add_dependency 'httparty'
21
18
  end
@@ -1,32 +1,31 @@
1
1
  $LOAD_PATH << File.expand_path('..', __FILE__)
2
-
2
+
3
3
  require 'support/presence'
4
4
  require 'support/symbolize_keys'
5
5
 
6
6
  module Hungry
7
- VERSION = '0.1.0'
8
-
9
7
  class << self
10
- attr_accessor :api_url, :json_parser, :logger
8
+ attr_accessor :api_url, :credentials, :json_parser, :logger
11
9
  end
12
-
13
- self.api_url = 'https://api.eet.nu/'
14
- self.json_parser = lambda do |json|
15
- require 'json'
16
- JSON.parse(json)
17
- end
18
-
10
+
11
+ def self.credentials=(new_credentials = {})
12
+ @credentials = new_credentials.symbolize_keys
13
+
14
+ Resource.basic_auth credentials[:username], credentials[:password]
15
+ Collection.basic_auth credentials[:username], credentials[:password]
16
+ end
17
+
19
18
  ### LIBRARY:
20
-
19
+
21
20
  # Utility:
22
21
  autoload :Collection, 'hungry/collection'
23
22
  autoload :Resource, 'hungry/resource'
24
23
  autoload :Util, 'hungry/util'
25
-
24
+
26
25
  # Helpers:
27
26
  autoload :Geolocation, 'hungry/geolocation'
28
27
  autoload :Location, 'hungry/location'
29
-
28
+
30
29
  # Models:
31
30
  autoload :City, 'hungry/city'
32
31
  autoload :Country, 'hungry/country'
@@ -38,12 +37,21 @@ module Hungry
38
37
  autoload :Tag, 'hungry/tag'
39
38
  autoload :User, 'hungry/user'
40
39
  autoload :Venue, 'hungry/venue'
41
-
40
+
42
41
  ### EXCEPTIONS:
43
-
42
+
44
43
  # Exception raised when a geolocation is required, but it is not given:
45
44
  class GeolocationNotGiven < StandardError; end
46
-
45
+
47
46
  # Exception raised when an endpoint is not specified for a resource:
48
47
  class EndpointNotSpecified < StandardError; end
48
+
49
+ ### CONFIGURATION:
50
+
51
+ self.credentials = { username: nil, password: nil }
52
+ self.api_url = 'https://api.eet.nu/'
53
+ self.json_parser = lambda do |json|
54
+ require 'json'
55
+ JSON.parse(json)
56
+ end
49
57
  end
@@ -1,7 +1,7 @@
1
1
  module Hungry
2
2
  class City < Location
3
-
3
+
4
4
  self.default_criteria = { type: 'City' }
5
-
5
+
6
6
  end
7
7
  end
@@ -4,41 +4,42 @@ module Hungry
4
4
  class Collection
5
5
  include Enumerable
6
6
  include HTTParty
7
-
7
+
8
8
  autoload :Pagination, 'hungry/collection/pagination'
9
-
9
+
10
10
  attr_reader :klass, :endpoint, :criteria
11
-
11
+
12
12
  ### CLASS METHODS:
13
-
13
+
14
14
  def self.get(*args)
15
15
  self.base_uri Hungry.api_url
16
+
16
17
  super
17
18
  end
18
-
19
+
19
20
  ### INSTANCE METHODS:
20
-
21
+
21
22
  def initialize(klass, endpoint, criteria = {})
22
23
  @klass = klass
23
24
  @endpoint = endpoint
24
25
  @criteria = criteria.symbolize_keys
25
26
  end
26
-
27
+
27
28
  def from_url(url)
28
29
  uri = URI.parse(url)
29
30
  options = Util.params_from_uri(uri) || klass.default_criteria
30
-
31
+
31
32
  self.class.new(klass, uri.path, options)
32
33
  end
33
-
34
+
34
35
  def all(new_criteria = {})
35
36
  self.class.new(klass, endpoint, criteria.merge(new_criteria))
36
37
  end
37
-
38
+
38
39
  def [](index)
39
40
  build_resource results[index]
40
41
  end
41
-
42
+
42
43
  def first(n = 1)
43
44
  if n == 1 && (value = results.first)
44
45
  build_resource value
@@ -48,7 +49,7 @@ module Hungry
48
49
  end
49
50
  end
50
51
  end
51
-
52
+
52
53
  def count(*args)
53
54
  if args.present?
54
55
  super
@@ -56,40 +57,40 @@ module Hungry
56
57
  results.count
57
58
  end
58
59
  end
59
-
60
+
60
61
  def each(&block)
61
62
  results.each do |result|
62
63
  yield build_resource(result)
63
64
  end
64
65
  end
65
-
66
+
66
67
  def results
67
68
  @results ||= json['results']
68
69
  end
69
-
70
+
70
71
  def results=(results)
71
72
  @results = results
72
73
  end
73
-
74
+
74
75
  protected
75
-
76
+
76
77
  def build_resource(result)
77
78
  resource = klass.new(result)
78
79
  resource.data_source = data_source
79
80
  resource
80
81
  end
81
-
82
+
82
83
  def data_source
83
84
  Util.uri_with_params(endpoint, criteria)
84
85
  end
85
-
86
+
86
87
  def json
87
88
  @json ||= Util.parse_json(response.body)
88
89
  end
89
-
90
+
90
91
  def response
91
92
  raise NoEndpointSpecified unless endpoint
92
-
93
+
93
94
  @response ||= begin
94
95
  Util.log "GET: #{data_source}"
95
96
  self.class.get data_source
@@ -2,15 +2,15 @@ module Hungry
2
2
  class Collection
3
3
  module Pagination
4
4
  class NotPaginatedError < StandardError; end
5
-
5
+
6
6
  def paginate(page, options = {})
7
7
  all options.merge(page: page)
8
8
  end
9
-
9
+
10
10
  def first(n = 1)
11
11
  scope = all(per_page: n, page: 1)
12
12
  results = scope.results
13
-
13
+
14
14
  if n == 1 && (value = results.first)
15
15
  resource = klass.new results.first
16
16
  resource.data_source = scope.data_source
@@ -23,11 +23,11 @@ module Hungry
23
23
  end
24
24
  end
25
25
  end
26
-
26
+
27
27
  def paginated?
28
28
  pagination.present?
29
29
  end
30
-
30
+
31
31
  def per_page
32
32
  if paginated?
33
33
  pagination['per_page'].to_i
@@ -35,7 +35,7 @@ module Hungry
35
35
  response['results'].length
36
36
  end
37
37
  end
38
-
38
+
39
39
  def total_entries
40
40
  if paginated?
41
41
  pagination['total_entries'].to_i
@@ -43,7 +43,7 @@ module Hungry
43
43
  response['results'].length
44
44
  end
45
45
  end
46
-
46
+
47
47
  def total_pages
48
48
  if paginated?
49
49
  pagination['total_pages'].to_i
@@ -51,7 +51,7 @@ module Hungry
51
51
  1
52
52
  end
53
53
  end
54
-
54
+
55
55
  def current_page
56
56
  if paginated?
57
57
  pagination['current_page'].to_i
@@ -59,27 +59,27 @@ module Hungry
59
59
  1
60
60
  end
61
61
  end
62
-
62
+
63
63
  def previous_page
64
64
  current_page - 1 if paginated? && current_page > 1
65
65
  end
66
-
66
+
67
67
  def next_page
68
68
  current_page + 1 if paginated? && current_page < total_pages
69
69
  end
70
-
70
+
71
71
  def previous
72
72
  raise NotPaginatedError unless paginated?
73
73
  all page: previous_page if previous_page
74
74
  end
75
-
75
+
76
76
  def next
77
77
  raise NotPaginatedError unless paginated?
78
78
  all page: next_page if next_page
79
79
  end
80
-
80
+
81
81
  private
82
-
82
+
83
83
  def pagination
84
84
  response['pagination']
85
85
  end
@@ -1,7 +1,7 @@
1
1
  module Hungry
2
2
  class Country < Location
3
-
3
+
4
4
  self.default_criteria = { type: 'Country' }
5
-
5
+
6
6
  end
7
7
  end
@@ -1,27 +1,27 @@
1
1
  module Hungry
2
2
  class Geolocation
3
3
  attr_accessor :latitude, :longitude
4
-
4
+
5
5
  def self.parse(input)
6
6
  # input is already a Geolocation, so we can return it early:
7
7
  return input if input.is_a?(self)
8
8
 
9
9
  if input.respond_to?(:geolocation)
10
10
  # input has a geolocation attribute, so try to use that one first:
11
- geolocation = parse(input.geolocation)
11
+ geolocation = parse(input.geolocation)
12
12
  return geolocation if geolocation
13
13
  end
14
-
14
+
15
15
  coordinates = []
16
-
16
+
17
17
  if input.respond_to?(:latitude) && input.respond_to?(:longitude)
18
18
  # input has latitude and longitude attributes, so use those:
19
19
  coordinates = [input.latitude, input.longitude]
20
-
20
+
21
21
  elsif input.respond_to?(:lat) && input.respond_to?(:lng)
22
22
  # input has lat and lng attributes, so use those:
23
23
  coordinates = [input.lat, input.lng]
24
-
24
+
25
25
  elsif input.respond_to?(:match)
26
26
  # Example: "50.8469397,5.6927505"
27
27
  #
@@ -30,40 +30,40 @@ module Hungry
30
30
  if match = input.match(/^([0-9\.]+),\s?([0-9\.]+)$/)
31
31
  coordinates = [match[1], match[2]]
32
32
  end
33
-
33
+
34
34
  elsif input.respond_to?(:keys)
35
35
  # Example: { latitude: 50.8469397, longitude: 5.6927505 }
36
36
  #
37
37
  # input is a Hash, so we can extract values with the keys:
38
38
  coordinates = [input[:latitude] || input[:lat], input[:longitude] || input[:lng]]
39
-
39
+
40
40
  elsif input.respond_to?(:[])
41
41
  # Example: [50.8469397, 5.6927505]
42
42
  #
43
43
  # input is an Array, so we need the first and second value:
44
44
  coordinates = input[0], input[1]
45
45
  end
46
-
46
+
47
47
  coordinates = coordinates.map(&:presence).compact
48
-
48
+
49
49
  if coordinates.length == 2 && coordinates.all? { |coordinate| Util.is_numeric?(coordinate) }
50
50
  new(*coordinates)
51
51
  end
52
52
  end
53
-
53
+
54
54
  def initialize(latitude, longitude)
55
55
  self.latitude = latitude.to_f
56
56
  self.longitude = longitude.to_f
57
57
  end
58
-
58
+
59
59
  def to_s
60
60
  [latitude, longitude].join(',')
61
61
  end
62
-
62
+
63
63
  def [](key)
64
64
  to_h.send(:[], key)
65
65
  end
66
-
66
+
67
67
  def []=(key, value)
68
68
  case key.to_sym
69
69
  when :latitude
@@ -72,7 +72,7 @@ module Hungry
72
72
  self.longitude = value.to_f
73
73
  end
74
74
  end
75
-
75
+
76
76
  def to_h
77
77
  {
78
78
  latitude: latitude,