leipzig 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -71,7 +71,6 @@ $ API_KEY='my-key' rake
71
71
 
72
72
  ## Todo
73
73
 
74
- * Support for whole API and all available resources
75
74
  * Refactoring
76
75
  * Maybe model layer instead of OpenStruct?
77
76
 
@@ -0,0 +1,7 @@
1
+ require 'leipzig/client'
2
+
3
+ module Leipzig
4
+ class Calendar < Leipzig::Client
5
+ TYPES = [:events, :hosts, :venues]
6
+ end
7
+ end
@@ -4,20 +4,55 @@ require 'ostruct'
4
4
 
5
5
  module Leipzig
6
6
  class Client
7
+ API_URL = 'http://www.apileipzig.de/api/v1'
8
+ KEYWORDS = [:limit, :offset, :format]
9
+
7
10
  attr_reader :api_key
8
11
 
9
12
  def initialize(api_key)
10
13
  @api_key = api_key
11
- @options = {}
14
+ @conditions = { :limit => 10, :name => 'leipzig', :api_key => @api_key }
15
+ register_methods
12
16
  end
13
17
 
14
18
  private
15
19
 
16
- def request(uri, params)
17
- result = JSON.parse RestClient.get(uri, :params => @options.merge(params))
20
+ def types
21
+ self.singleton_class.const_get(:TYPES)
22
+ end
23
+
24
+ def find(type, conditions = {})
25
+ raise "Type #{type} is invalid" unless types.include?(type.to_sym)
26
+ uri = "#{API_URL}/#{resource}/#{type}"
27
+
28
+ if has_search_param?(conditions)
29
+ uri += '/search'
30
+ end
31
+
32
+ request(uri, conditions)
33
+ end
34
+
35
+ def has_search_param?(conditions)
36
+ conditions.keys.select { |key| !KEYWORDS.include?(key) }.any?
37
+ end
38
+
39
+ def register_methods
40
+ types.each do |type|
41
+ self.class.send(:define_method, "find_#{type}".to_sym) do |*args|
42
+ find(type, *args)
43
+ end
44
+ end
45
+ end
46
+
47
+ def resource
48
+ self.class.to_s.split('::').last.downcase
49
+ end
50
+
51
+ def request(uri, conditions)
52
+ result = JSON.parse RestClient.get(uri, :params => @conditions.merge(conditions))
18
53
 
19
54
  if result.has_key? "error"
20
- raise "Webservice returned error for uri '#{uri}': #{result['error']}"
55
+ raise "API returned error for uri '#{uri}': #{result['error']}"
21
56
  end
22
57
 
23
58
  result['data'].map { |entry| OpenStruct.new(entry) }
@@ -0,0 +1,7 @@
1
+ require 'leipzig/client'
2
+
3
+ module Leipzig
4
+ class District < Leipzig::Client
5
+ TYPES = [:districts, :streets, :statistics, :ihkcompanies]
6
+ end
7
+ end
@@ -2,24 +2,6 @@ require 'leipzig/client'
2
2
 
3
3
  module Leipzig
4
4
  class Mediahandbook < Leipzig::Client
5
-
6
- TYPES = [:companies, :branches, :people]
7
- API_URL = 'http://www.apileipzig.de/api/v1/mediahandbook/'
8
-
9
- TYPES.each do |type|
10
- define_method "find_#{type}" do |*args|
11
- find(type, *args)
12
- end
13
- end
14
-
15
- def initialize(api_key)
16
- super
17
- @options = { :limit => 10, :name => 'leipzig', :api_key => @api_key }
18
- end
19
-
20
- def find(type, conditions)
21
- raise "Type #{type} is invalid" unless TYPES.include?(type.to_sym)
22
- request("#{API_URL}#{type}/search", conditions)
23
- end
5
+ TYPES = [:companies, :branches, :people]
24
6
  end
25
7
  end
@@ -1,3 +1,3 @@
1
1
  module Leipzig
2
- VERSION = '0.0.2'
2
+ VERSION = '0.0.3'
3
3
  end
data/lib/leipzig.rb CHANGED
@@ -1,3 +1,5 @@
1
1
  require 'leipzig/version'
2
2
  require 'leipzig/client'
3
3
  require 'leipzig/mediahandbook'
4
+ require 'leipzig/calendar'
5
+ require 'leipzig/district'
@@ -17,6 +17,12 @@ describe 'Mediahandbook' do
17
17
  end
18
18
 
19
19
  it 'returns json representation of resource with default limit' do
20
+ data = client.find_companies
21
+ data.should be_instance_of(Array)
22
+ data.size.should eq(10)
23
+ end
24
+
25
+ it 'returns json representation of resource with given limit' do
20
26
  data = client.find_companies(:limit => LIMIT)
21
27
  data.should be_instance_of(Array)
22
28
  data.size.should eq(LIMIT)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: leipzig
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -74,7 +74,9 @@ files:
74
74
  - Rakefile
75
75
  - leipzig.gemspec
76
76
  - lib/leipzig.rb
77
+ - lib/leipzig/calendar.rb
77
78
  - lib/leipzig/client.rb
79
+ - lib/leipzig/district.rb
78
80
  - lib/leipzig/mediahandbook.rb
79
81
  - lib/leipzig/version.rb
80
82
  - spec/leipzig/mediahandbook_spec.rb
@@ -94,7 +96,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
94
96
  version: '0'
95
97
  segments:
96
98
  - 0
97
- hash: -3565537231393772397
99
+ hash: -953058203614356815
98
100
  required_rubygems_version: !ruby/object:Gem::Requirement
99
101
  none: false
100
102
  requirements:
@@ -103,7 +105,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
103
105
  version: '0'
104
106
  segments:
105
107
  - 0
106
- hash: -3565537231393772397
108
+ hash: -953058203614356815
107
109
  requirements: []
108
110
  rubyforge_project:
109
111
  rubygems_version: 1.8.24