tepco_usage_api 0.3.0 → 0.4.0

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.
@@ -2,39 +2,83 @@ require 'net/http'
2
2
  require 'json'
3
3
 
4
4
  class TepcoUsage
5
+ @@default_options = {
6
+ :proxy => nil
7
+ }
8
+
9
+ def initialize(options = {})
10
+ @options = @@default_options.merge(options.inject({}) do |opts, (key, value)|
11
+ opts[key.to_sym] = value
12
+ opts
13
+ end)
14
+ end
15
+
16
+ def latest
17
+ response = request('/latest.json')
18
+ JSON.parse(response.body)
19
+ end
20
+
21
+ def at(arg)
22
+ path = case arg
23
+ when Date
24
+ "/#{arg.year}/#{arg.month}/#{arg.day}.json"
25
+ when Time
26
+ "/#{arg.year}/#{arg.month}/#{arg.day}/#{arg.hour}.json"
27
+ end
28
+ response = request(path)
29
+ JSON.parse(response.body)
30
+ end
31
+
32
+ def in(arg)
33
+ path = "/#{arg.year}/#{arg.month}.json"
34
+ response = request(path)
35
+ JSON.parse(response.body)
36
+ end
37
+
38
+ private
39
+
40
+ def http_class
41
+ if proxy = @options[:proxy]
42
+ proxy_uri = proxy.is_a?(URI) ? proxy : URI.parse(proxy)
43
+ Net::HTTP.Proxy(proxy_uri.host, proxy_uri.port, proxy_uri.user, proxy_uri.password)
44
+ else
45
+ Net::HTTP
46
+ end
47
+ end
48
+
49
+ def create_http
50
+ host = 'tepco-usage-api.appspot.com'
51
+ http_class.new(host)
52
+ end
53
+
54
+ def http
55
+ @http ||= create_http
56
+ end
57
+
58
+ def request(path)
59
+ http.request_get(path)
60
+ end
61
+
5
62
  class << self
6
63
  def latest
7
- response = request('/latest.json')
8
- JSON.parse(response.body)
64
+ tepco.latest
9
65
  end
10
66
 
11
67
  def at(arg)
12
- path = case arg
13
- when Date
14
- "/#{arg.year}/#{arg.month}/#{arg.day}.json"
15
- when Time
16
- "/#{arg.year}/#{arg.month}/#{arg.day}/#{arg.hour}.json"
17
- end
18
- response = request(path)
19
- JSON.parse(response.body)
68
+ tepco.at(arg)
20
69
  end
21
70
 
22
71
  def in(arg)
23
- path = "/#{arg.year}/#{arg.month}.json"
24
- response = request(path)
25
- JSON.parse(response.body)
72
+ tepco.in(arg)
26
73
  end
27
74
 
28
75
  private
29
- def request(path)
30
- http.request_get(path)
31
- end
32
76
 
33
- def http
34
- unless @http
35
- @http = Net::HTTP.new('tepco-usage-api.appspot.com')
77
+ def tepco
78
+ unless @tepco
79
+ @tepco = TepcoUsage.new
36
80
  end
37
- @http
81
+ @tepco
38
82
  end
39
83
  end
40
84
  end
@@ -5,19 +5,20 @@ require 'date'
5
5
  describe TepcoUsage do
6
6
  describe "#latest" do
7
7
  before do
8
- TepcoUsage.stub!(:request).with('/latest.json').and_return do
8
+ tepco = TepcoUsage.send(:tepco)
9
+ tepco.stub!(:request).with('/latest.json').and_return do
9
10
  mock = mock(Object.new, :body => <<-JSON)
10
11
  {
11
- "saving": false,
12
- "hour": 0,
13
- "capacity_updated": "2011-03-26 09:30:00",
14
- "month": 3,
15
- "usage_updated": "2011-03-26 16:30:54",
16
- "entryfor": "2011-03-26 15:00:00",
17
- "capacity_peak_period": 18,
18
- "year": 2011,
19
- "usage": 2904,
20
- "capacity": 3700,
12
+ "saving": false,
13
+ "hour": 0,
14
+ "capacity_updated": "2011-03-26 09:30:00",
15
+ "month": 3,
16
+ "usage_updated": "2011-03-26 16:30:54",
17
+ "entryfor": "2011-03-26 15:00:00",
18
+ "capacity_peak_period": 18,
19
+ "year": 2011,
20
+ "usage": 2904,
21
+ "capacity": 3700,
21
22
  "day": 27
22
23
  }
23
24
  JSON
@@ -25,46 +26,47 @@ describe TepcoUsage do
25
26
  end
26
27
 
27
28
  subject{TepcoUsage}
28
- its(:latest) { should_not be_nil }
29
+ its(:latest) { should_not be_nil }
29
30
  it do
30
31
  TepcoUsage.latest['saving'].should be_false
31
32
  end
32
-
33
+
33
34
  end
34
35
  describe "#at" do
35
36
  context "Given Date object" do
36
37
  before do
37
38
  today = Date.today
38
39
  path = "/#{today.year}/#{today.month}/#{today.day}.json"
39
- TepcoUsage.stub!(:request).with(path).and_return do
40
+ tepco = TepcoUsage.send(:tepco)
41
+ tepco.stub!(:request).with(path).and_return do
40
42
  mock = mock(Object.new, :body => <<-JSON)
41
43
  [
42
44
  {
43
- "saving": false,
44
- "hour": 0,
45
- "capacity_updated": "2011-03-25 16:05:00",
46
- "month": 3,
47
- "usage_updated": "2011-03-25 16:30:49",
48
- "entryfor": "2011-03-25 15:00:00",
49
- "capacity_peak_period": null,
50
- "year": 2011,
51
- "usage": 2889,
52
- "capacity": 3750,
45
+ "saving": false,
46
+ "hour": 0,
47
+ "capacity_updated": "2011-03-25 16:05:00",
48
+ "month": 3,
49
+ "usage_updated": "2011-03-25 16:30:49",
50
+ "entryfor": "2011-03-25 15:00:00",
51
+ "capacity_peak_period": null,
52
+ "year": 2011,
53
+ "usage": 2889,
54
+ "capacity": 3750,
53
55
  "day": 26
54
- },
56
+ },
55
57
  {
56
- "saving": false,
57
- "hour": 1,
58
- "capacity_updated": "2011-03-25 16:05:00",
59
- "month": 3,
60
- "usage_updated": "2011-03-25 17:05:49",
61
- "entryfor": "2011-03-25 16:00:00",
62
- "capacity_peak_period": null,
63
- "year": 2011,
64
- "usage": 2758,
65
- "capacity": 3750,
58
+ "saving": false,
59
+ "hour": 1,
60
+ "capacity_updated": "2011-03-25 16:05:00",
61
+ "month": 3,
62
+ "usage_updated": "2011-03-25 17:05:49",
63
+ "entryfor": "2011-03-25 16:00:00",
64
+ "capacity_peak_period": null,
65
+ "year": 2011,
66
+ "usage": 2758,
67
+ "capacity": 3750,
66
68
  "day": 26
67
- }
69
+ }
68
70
  ]
69
71
  JSON
70
72
  end
@@ -72,39 +74,41 @@ describe TepcoUsage do
72
74
  subject{TepcoUsage.at(Date.today)}
73
75
  it { should_not be_nil }
74
76
  end
77
+
75
78
  context "Given Time object" do
76
79
  before do
77
80
  @now = Time.now
78
81
  path = "/#{@now.year}/#{@now.month}/#{@now.day}/#{@now.hour}.json"
79
- TepcoUsage.stub!(:request).with(path).and_return do
82
+ tepco = TepcoUsage.send(:tepco)
83
+ tepco.stub!(:request).with(path).and_return do
80
84
  mock = mock(Object.new, :body => <<-JSON)
81
85
  [
82
86
  {
83
- "saving": false,
84
- "hour": 0,
85
- "capacity_updated": "2011-03-25 16:05:00",
86
- "month": 3,
87
- "usage_updated": "2011-03-25 16:30:49",
88
- "entryfor": "2011-03-25 15:00:00",
89
- "capacity_peak_period": null,
90
- "year": 2011,
91
- "usage": 2889,
92
- "capacity": 3750,
87
+ "saving": false,
88
+ "hour": 0,
89
+ "capacity_updated": "2011-03-25 16:05:00",
90
+ "month": 3,
91
+ "usage_updated": "2011-03-25 16:30:49",
92
+ "entryfor": "2011-03-25 15:00:00",
93
+ "capacity_peak_period": null,
94
+ "year": 2011,
95
+ "usage": 2889,
96
+ "capacity": 3750,
93
97
  "day": 26
94
- },
98
+ },
95
99
  {
96
- "saving": false,
97
- "hour": 1,
98
- "capacity_updated": "2011-03-25 16:05:00",
99
- "month": 3,
100
- "usage_updated": "2011-03-25 17:05:49",
101
- "entryfor": "2011-03-25 16:00:00",
102
- "capacity_peak_period": null,
103
- "year": 2011,
104
- "usage": 2758,
105
- "capacity": 3750,
100
+ "saving": false,
101
+ "hour": 1,
102
+ "capacity_updated": "2011-03-25 16:05:00",
103
+ "month": 3,
104
+ "usage_updated": "2011-03-25 17:05:49",
105
+ "entryfor": "2011-03-25 16:00:00",
106
+ "capacity_peak_period": null,
107
+ "year": 2011,
108
+ "usage": 2758,
109
+ "capacity": 3750,
106
110
  "day": 26
107
- }
111
+ }
108
112
  ]
109
113
  JSON
110
114
  end
@@ -117,12 +121,32 @@ describe TepcoUsage do
117
121
  before do
118
122
  @now = Time.now
119
123
  path = "/#{@now.year}/#{@now.month}.json"
120
- TepcoUsage.stub!(:request).with(path).and_return do
124
+ tepco = TepcoUsage.send(:tepco)
125
+ tepco.stub!(:request).with(path).and_return do
121
126
  mock(Object.new, :body => '{"body": "dummy"}')
122
127
  end
123
128
  end
124
129
 
125
130
  subject{TepcoUsage.in @now}
126
- it { should_not be_nil }
131
+ it { should_not be_nil }
132
+ end
133
+
134
+ context "Configuration" do
135
+ describe "About Proxy" do
136
+ before do
137
+ host = 'proxy.example.com'
138
+ port = 4567
139
+ user = 'proxy_user'
140
+ password = 'proxy_pass'
141
+ @proxy = URI.parse("http://#{user}:#{password}@#{host}:#{port}")
142
+ @tepco = TepcoUsage.new(:proxy => @proxy)
143
+ end
144
+ subject {@tepco.send :http_class}
145
+ it { should be_proxy_class }
146
+ its(:proxy_address) { should == @proxy.host }
147
+ its(:proxy_port) { should == @proxy.port }
148
+ its(:proxy_user) { should == @proxy.user }
149
+ its(:proxy_pass) { should == @proxy.password }
150
+ end
127
151
  end
128
152
  end
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 3
7
+ - 4
8
8
  - 0
9
- version: 0.3.0
9
+ version: 0.4.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Tatsuya Sato
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2011-06-01 00:00:00 +09:00
17
+ date: 2011-10-23 00:00:00 +09:00
18
18
  default_executable:
19
19
  dependencies: []
20
20
 
@@ -30,11 +30,11 @@ extensions: []
30
30
  extra_rdoc_files: []
31
31
 
32
32
  files:
33
- - README.rdoc
34
33
  - LICENCE.txt
34
+ - README.rdoc
35
35
  - lib/tepco_usage_api.rb
36
- - spec/tepco_usage_api_spec.rb
37
36
  - spec/spec_helper.rb
37
+ - spec/tepco_usage_api_spec.rb
38
38
  has_rdoc: true
39
39
  homepage: http://github.com/satoryu/tepco_usage_api/
40
40
  licenses: