leipzig 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -31,7 +31,7 @@ require 'leipzig'
31
31
 
32
32
  key = 'my-key'
33
33
 
34
- client = Leipzig::Client.new(key)
34
+ client = Leipzig::Mediahandbook.new(key)
35
35
 
36
36
  companies = client.find_companies(:postcode => '04103')
37
37
  branches = client.find_branches(:limit => 5)
@@ -41,6 +41,13 @@ people = client.find_people(:offset => 100)
41
41
  All `find` methods except a hash of params which are used to change the result set. See [this link](http://www.apileipzig.de/wiki/show/allgemeineParameter) for more
42
42
  information about available params.
43
43
 
44
+ The result set is always an array containing OpenStruct objects so one can access entry data like properties:
45
+
46
+ ```ruby
47
+ client.find_companies(:limit => 10).first.name
48
+ # ==> First company's name
49
+ ```
50
+
44
51
  ## Note
45
52
 
46
53
  At the moment only the mediahandbook entries are supported.
@@ -62,6 +69,12 @@ $ API_KEY='my-key' rake
62
69
  6. Push to the branch
63
70
  7. Create new Pull Request
64
71
 
72
+ ## Todo
73
+
74
+ * Support for whole API and all available resources
75
+ * Refactoring
76
+ * Maybe model layer instead of OpenStruct?
77
+
65
78
  ## License
66
79
 
67
80
  (The MIT License)
@@ -4,28 +4,17 @@ require 'ostruct'
4
4
 
5
5
  module Leipzig
6
6
  class Client
7
- TYPES = [:companies, :branches, :people]
8
- API_URL = 'http://www.apileipzig.de/api/v1/mediahandbook/'
9
-
10
7
  attr_reader :api_key
11
8
 
12
- TYPES.each do |type|
13
- define_method "find_#{type}" do |*args|
14
- find(type, *args)
15
- end
16
- end
17
-
18
9
  def initialize(api_key)
19
10
  @api_key = api_key
20
- @options = { :limit => 10, :name => 'leipzig', :api_key => @api_key }
11
+ @options = {}
21
12
  end
22
13
 
23
- def find(type, conditions)
24
- raise "Type #{type} is invalid" unless TYPES.include?(type.to_sym)
25
-
26
- uri = "#{API_URL}#{type}/search"
14
+ private
27
15
 
28
- result = JSON.parse(RestClient.get(uri, :params => @options.merge(conditions)))
16
+ def request(uri, params)
17
+ result = JSON.parse RestClient.get(uri, :params => @options.merge(params))
29
18
 
30
19
  if result.has_key? "error"
31
20
  raise "Webservice returned error for uri '#{uri}': #{result['error']}"
@@ -0,0 +1,25 @@
1
+ require 'leipzig/client'
2
+
3
+ module Leipzig
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
24
+ end
25
+ end
@@ -1,3 +1,3 @@
1
1
  module Leipzig
2
- VERSION = '0.0.1'
2
+ VERSION = '0.0.2'
3
3
  end
data/lib/leipzig.rb CHANGED
@@ -1,6 +1,3 @@
1
1
  require 'leipzig/version'
2
2
  require 'leipzig/client'
3
-
4
- module Leipzig
5
-
6
- end
3
+ require 'leipzig/mediahandbook'
@@ -1,10 +1,10 @@
1
- require 'leipzig/client'
1
+ require 'leipzig/mediahandbook'
2
2
 
3
3
  KEY = ENV['API_KEY']
4
4
  LIMIT = 5
5
5
 
6
- describe 'Client' do
7
- let(:client) { Leipzig::Client.new(KEY) }
6
+ describe 'Mediahandbook' do
7
+ let(:client) { Leipzig::Mediahandbook.new(KEY) }
8
8
 
9
9
  it 'returns given API key' do
10
10
  client.api_key.should eq(KEY)
@@ -29,7 +29,7 @@ describe 'Client' do
29
29
  end
30
30
 
31
31
  it 'raises exception if invalid key is used' do
32
- lambda { Leipzig::Client.new('wrong').find_companies }.should raise_error
32
+ lambda { Leipzig::Mediahandbook.new('wrong').find_companies }.should raise_error
33
33
  end
34
34
 
35
35
  it 'raises exception if undefined method is called' do
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.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -75,8 +75,9 @@ files:
75
75
  - leipzig.gemspec
76
76
  - lib/leipzig.rb
77
77
  - lib/leipzig/client.rb
78
+ - lib/leipzig/mediahandbook.rb
78
79
  - lib/leipzig/version.rb
79
- - spec/leipzig/client_spec.rb
80
+ - spec/leipzig/mediahandbook_spec.rb
80
81
  - spec/leipzig/spec_helper.rb
81
82
  - spec/leipzig/version_spec.rb
82
83
  homepage: ''
@@ -93,7 +94,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
93
94
  version: '0'
94
95
  segments:
95
96
  - 0
96
- hash: -1631967839568932202
97
+ hash: -3565537231393772397
97
98
  required_rubygems_version: !ruby/object:Gem::Requirement
98
99
  none: false
99
100
  requirements:
@@ -102,7 +103,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
102
103
  version: '0'
103
104
  segments:
104
105
  - 0
105
- hash: -1631967839568932202
106
+ hash: -3565537231393772397
106
107
  requirements: []
107
108
  rubyforge_project:
108
109
  rubygems_version: 1.8.24
@@ -110,6 +111,6 @@ signing_key:
110
111
  specification_version: 3
111
112
  summary: Tiny client for API Leipzig
112
113
  test_files:
113
- - spec/leipzig/client_spec.rb
114
+ - spec/leipzig/mediahandbook_spec.rb
114
115
  - spec/leipzig/spec_helper.rb
115
116
  - spec/leipzig/version_spec.rb