leipzig 0.0.2 → 0.0.3
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.
- data/README.md +0 -1
- data/lib/leipzig/calendar.rb +7 -0
- data/lib/leipzig/client.rb +39 -4
- data/lib/leipzig/district.rb +7 -0
- data/lib/leipzig/mediahandbook.rb +1 -19
- data/lib/leipzig/version.rb +1 -1
- data/lib/leipzig.rb +2 -0
- data/spec/leipzig/mediahandbook_spec.rb +6 -0
- metadata +5 -3
data/README.md
CHANGED
data/lib/leipzig/client.rb
CHANGED
@@ -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
|
-
@
|
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
|
17
|
-
|
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 "
|
55
|
+
raise "API returned error for uri '#{uri}': #{result['error']}"
|
21
56
|
end
|
22
57
|
|
23
58
|
result['data'].map { |entry| OpenStruct.new(entry) }
|
@@ -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
|
data/lib/leipzig/version.rb
CHANGED
data/lib/leipzig.rb
CHANGED
@@ -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.
|
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: -
|
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: -
|
108
|
+
hash: -953058203614356815
|
107
109
|
requirements: []
|
108
110
|
rubyforge_project:
|
109
111
|
rubygems_version: 1.8.24
|