postcodeapi 0.0.1 → 0.0.2

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/.travis.yml CHANGED
@@ -1,6 +1,14 @@
1
1
  language: ruby
2
2
  script: "bundle exec rspec spec/"
3
+
3
4
  rvm:
5
+ - 1.9.2
4
6
  - 1.9.3
7
+ - rbx-19mode
8
+ - ruby-head
5
9
 
10
+ matrix:
11
+ allow_failures:
12
+ - rvm: rbx-19mode
13
+ - rvm: ruby-head
6
14
 
data/CHANGELOG.txt ADDED
@@ -0,0 +1,7 @@
1
+ # 0.0.2 - 2013-03-14
2
+
3
+ * Use plain old net/http instead of pulling in httparty [martijn]
4
+
5
+ # 0.0.1 - 2013-02-06
6
+
7
+ * Initial release [ariejan]
@@ -0,0 +1,7 @@
1
+ require 'rubygems'
2
+ require 'postcodeapi'
3
+
4
+ api = Postcode::API.new("your-api-key")
5
+ result = api.postcode("5505NB")
6
+
7
+ puts result.inspect
@@ -1,18 +1,23 @@
1
1
  module Postcode
2
2
  # You're required to sign up for an api key at http://postcodeapi.nu
3
3
  class API
4
- include HTTParty
5
-
6
- base_uri "http://api.postcodeapi.nu"
4
+ BASE_URI = "http://api.postcodeapi.nu"
7
5
 
8
6
  def initialize(api_key)
9
7
  @api_key = api_key
10
8
  end
11
9
 
12
- def postcode(postcode, options = {})
13
- options.merge!(:headers => { "Api-Key" => @api_key})
14
- response = self.class.get("/#{postcode}", options)
15
- Hashie::Mash.new(response.parsed_response)
10
+ def postcode(postcode, house_number = nil, options = {})
11
+ uri = URI.parse([BASE_URI, postcode, house_number].compact.join('/'))
12
+
13
+ req = Net::HTTP::Get.new(uri.path)
14
+ req.add_field('Api-Key', @api_key)
15
+
16
+ res = Net::HTTP.new(uri.host, uri.port).start do |http|
17
+ http.request(req)
18
+ end
19
+
20
+ Hashie::Mash.new(JSON.parse(res.body))
16
21
  end
17
22
  end
18
23
  end
@@ -1,3 +1,3 @@
1
1
  module Postcode
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/lib/postcodeapi.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  require 'rubygems'
2
2
 
3
- require 'httparty'
4
3
  require 'hashie'
4
+ require 'json'
5
5
 
6
6
  require "postcodeapi/version"
7
7
  require "postcodeapi/api"
data/postcodeapi.gemspec CHANGED
@@ -17,7 +17,6 @@ Gem::Specification.new do |gem|
17
17
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
18
  gem.require_paths = ["lib"]
19
19
 
20
- gem.add_dependency 'httparty', '~> 0.10.2'
21
20
  gem.add_dependency 'hashie', '~> 1.2.0'
22
21
 
23
22
  gem.add_development_dependency 'rake', '~> 10.0.3'
@@ -1,9 +1,7 @@
1
1
  require File.expand_path('../../spec_helper', __FILE__)
2
2
 
3
3
  describe Postcode::API do
4
-
5
4
  context "without a valid API key" do
6
-
7
5
  before do
8
6
  body = '{"success":false,"error":{"code":401,"message":"A valid \'Api-Key\' needs to be provided in the \'Headers\' in order to use this API."}}'
9
7
  stub_request(:get, "http://api.postcodeapi.nu/5041EB").
@@ -22,25 +20,48 @@ describe Postcode::API do
22
20
  end
23
21
 
24
22
  context "with a valid API key" do
23
+ context "with postcode only" do
24
+ before do
25
+ body = '{"success":true,"resource":{"street":"Wilhelminapark","postcode":"5041EB","town":"Tilburg","latitude":51.9401,"longitude":5.61531}}'
26
+ stub_request(:get, "http://api.postcodeapi.nu/5041EB").
27
+ with(:headers => {'Api-Key'=>'valid'}).
28
+ to_return(:status => 401, :body => body, :headers => {"Content-Type" => "application/json"})
29
+ end
25
30
 
26
- before do
27
- body = '{"success":true,"resource":{"street":"Wilhelminapark","postcode":"5041EB","town":"Tilburg","latitude":51.9401,"longitude":5.61531}}'
28
- stub_request(:get, "http://api.postcodeapi.nu/5041EB").
29
- with(:headers => {'Api-Key'=>'valid'}).
30
- to_return(:status => 401, :body => body, :headers => {"Content-Type" => "application/json"})
31
+ subject(:api) { Postcode::API.new("valid") }
32
+
33
+ it 'returns an error response' do
34
+ result = api.postcode('5041EB')
35
+
36
+ result.success.should be_true
37
+ result.resource.street.should eq("Wilhelminapark")
38
+ result.resource.postcode.should eq("5041EB")
39
+ result.resource.town.should eq("Tilburg")
40
+ result.resource.latitude.should eq(51.9401)
41
+ result.resource.longitude.should eq(5.61531)
42
+ end
31
43
  end
32
44
 
33
- subject(:api) { Postcode::API.new("valid") }
45
+ context "with postcode and house number" do
46
+ before do
47
+ body = '{"success":true,"resource":{"street":"Wilhelminapark","postcode":"5041EB","town":"Tilburg","latitude":51.9401,"longitude":5.61531}}'
48
+ stub_request(:get, "http://api.postcodeapi.nu/5041EB/21").
49
+ with(:headers => {'Api-Key'=>'valid'}).
50
+ to_return(:status => 401, :body => body, :headers => {"Content-Type" => "application/json"})
51
+ end
34
52
 
35
- it 'returns an error response' do
36
- result = api.postcode('5041EB')
53
+ subject(:api) { Postcode::API.new("valid") }
54
+
55
+ it 'returns an error response' do
56
+ result = api.postcode('5041EB', 21)
37
57
 
38
- result.success.should be_true
39
- result.resource.street.should eq("Wilhelminapark")
40
- result.resource.postcode.should eq("5041EB")
41
- result.resource.town.should eq("Tilburg")
42
- result.resource.latitude.should eq(51.9401)
43
- result.resource.longitude.should eq(5.61531)
58
+ result.success.should be_true
59
+ result.resource.street.should eq("Wilhelminapark")
60
+ result.resource.postcode.should eq("5041EB")
61
+ result.resource.town.should eq("Tilburg")
62
+ result.resource.latitude.should eq(51.9401)
63
+ result.resource.longitude.should eq(5.61531)
64
+ end
44
65
  end
45
66
  end
46
67
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: postcodeapi
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:
@@ -9,24 +9,8 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-02-06 00:00:00.000000000 Z
12
+ date: 2013-03-14 00:00:00.000000000 Z
13
13
  dependencies:
14
- - !ruby/object:Gem::Dependency
15
- name: httparty
16
- requirement: !ruby/object:Gem::Requirement
17
- none: false
18
- requirements:
19
- - - ~>
20
- - !ruby/object:Gem::Version
21
- version: 0.10.2
22
- type: :runtime
23
- prerelease: false
24
- version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
- requirements:
27
- - - ~>
28
- - !ruby/object:Gem::Version
29
- version: 0.10.2
30
14
  - !ruby/object:Gem::Dependency
31
15
  name: hashie
32
16
  requirement: !ruby/object:Gem::Requirement
@@ -101,10 +85,12 @@ files:
101
85
  - .gitignore
102
86
  - .rspec
103
87
  - .travis.yml
88
+ - CHANGELOG.txt
104
89
  - Gemfile
105
90
  - LICENSE.txt
106
91
  - README.md
107
92
  - Rakefile
93
+ - examples/postcode-lookup.rb
108
94
  - lib/postcodeapi.rb
109
95
  - lib/postcodeapi/api.rb
110
96
  - lib/postcodeapi/version.rb
@@ -125,7 +111,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
125
111
  version: '0'
126
112
  segments:
127
113
  - 0
128
- hash: -20125056826090370
114
+ hash: -3892934662629252101
129
115
  required_rubygems_version: !ruby/object:Gem::Requirement
130
116
  none: false
131
117
  requirements:
@@ -134,7 +120,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
134
120
  version: '0'
135
121
  segments:
136
122
  - 0
137
- hash: -20125056826090370
123
+ hash: -3892934662629252101
138
124
  requirements: []
139
125
  rubyforge_project:
140
126
  rubygems_version: 1.8.25