twoffein-client 0.1.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.
- data/.gitignore +7 -0
- data/.rspec +2 -0
- data/.rvmrc +1 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +207 -0
- data/README.md.erb +52 -0
- data/Rakefile +67 -0
- data/VERSION +1 -0
- data/bin/twoffein-client +84 -0
- data/features/step_definitions/twoffein-client_steps.rb +1 -0
- data/features/support/env.rb +16 -0
- data/features/twoffein-client.feature +13 -0
- data/fixtures/vcr_cassettes/cookie.yml +234 -0
- data/fixtures/vcr_cassettes/drink.yml +60 -0
- data/fixtures/vcr_cassettes/drinks.yml +461 -0
- data/fixtures/vcr_cassettes/profile.yml +69 -0
- data/fixtures/vcr_cassettes/tweet.yml +103 -0
- data/lib/twoffein-client.rb +12 -0
- data/lib/twoffein-client/constants.rb +9 -0
- data/lib/twoffein-client/cookie.rb +23 -0
- data/lib/twoffein-client/credentials.rb +5 -0
- data/lib/twoffein-client/drink.rb +43 -0
- data/lib/twoffein-client/drinks.rb +96 -0
- data/lib/twoffein-client/exceptions.rb +15 -0
- data/lib/twoffein-client/http.rb +96 -0
- data/lib/twoffein-client/profile.rb +61 -0
- data/lib/twoffein-client/tweet.rb +31 -0
- data/lib/twoffein-client/util.rb +7 -0
- data/lib/twoffein-client/version.rb +7 -0
- data/spec/constants_spec.rb +11 -0
- data/spec/cookie_spec.rb +25 -0
- data/spec/drink_spec.rb +49 -0
- data/spec/drinks_spec.rb +154 -0
- data/spec/exceptions_spec.rb +14 -0
- data/spec/http_spec.rb +94 -0
- data/spec/profile_spec.rb +58 -0
- data/spec/spec_helper.rb +25 -0
- data/spec/tweet_spec.rb +41 -0
- data/spec/util_spec.rb +20 -0
- data/twoffein-client.gemspec +28 -0
- metadata +245 -0
@@ -0,0 +1,61 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# encoding: utf-8
|
3
|
+
|
4
|
+
require_relative "http"
|
5
|
+
require_relative "constants"
|
6
|
+
|
7
|
+
module Twoffein
|
8
|
+
Profile = Struct.new(:quest,
|
9
|
+
:drink,
|
10
|
+
:rank,
|
11
|
+
:rank_title,
|
12
|
+
:drunken,
|
13
|
+
:bluttwoffeinkonzentration,
|
14
|
+
:first_login,
|
15
|
+
:screen_name)
|
16
|
+
|
17
|
+
class Profile
|
18
|
+
def initialize(hash=nil)
|
19
|
+
return super(*hash) if hash.nil?
|
20
|
+
hash.each { |key,val| self[key] = val if members.include? key }
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.get(profile="")
|
24
|
+
new(HTTP.get("profile", :profile => profile))
|
25
|
+
end
|
26
|
+
|
27
|
+
def to_s
|
28
|
+
hash = instance_hash
|
29
|
+
max_length = hash.keys.map { |k| k.length }.max
|
30
|
+
|
31
|
+
hash.map { |attr, value|
|
32
|
+
attr = attr.to_sym
|
33
|
+
|
34
|
+
if attr == :first_login
|
35
|
+
value = human_readable_time(value)
|
36
|
+
end
|
37
|
+
|
38
|
+
postfix = ":"
|
39
|
+
attr = human_readable_key(attr) + postfix
|
40
|
+
|
41
|
+
"#{attr.to_s.ljust(max_length+postfix.length+1)}#{value}"
|
42
|
+
}.join("\n")
|
43
|
+
end
|
44
|
+
|
45
|
+
private
|
46
|
+
|
47
|
+
def instance_hash
|
48
|
+
members.reduce({}) do |hash, ivar|
|
49
|
+
hash.merge({ivar.to_sym => self[ivar]})
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def human_readable_key(key)
|
54
|
+
key.to_s.gsub('_', ' ').split(/(\W)/).map(&:capitalize).join
|
55
|
+
end
|
56
|
+
|
57
|
+
def human_readable_time(value)
|
58
|
+
Time.at(value.to_i).strftime("%Y-%m-%d %H:%M")
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require_relative "http"
|
2
|
+
require_relative "exceptions"
|
3
|
+
require_relative "drinks"
|
4
|
+
|
5
|
+
module Twoffein
|
6
|
+
class Tweet
|
7
|
+
attr_accessor :drink,
|
8
|
+
:target_screen_name
|
9
|
+
|
10
|
+
def initialize drink_key, target_screen_name=nil
|
11
|
+
@drink = drink_key.to_sym
|
12
|
+
@target_screen_name = target_screen_name
|
13
|
+
end
|
14
|
+
|
15
|
+
def post
|
16
|
+
info = HTTP.post("tweet", {
|
17
|
+
:drink => @drink,
|
18
|
+
:target_screen_name => @target_screen_name
|
19
|
+
})
|
20
|
+
raise Server::Error.new(info[:code], info[:error]) if info.has_key? :error
|
21
|
+
info
|
22
|
+
end
|
23
|
+
alias publish post
|
24
|
+
|
25
|
+
def to_s
|
26
|
+
s = "Ich trinke gerade #{Drinks[@drink].name}"
|
27
|
+
s << " mit #{@target_screen_name}" if @target_screen_name
|
28
|
+
s << "."
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
data/spec/cookie_spec.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
require_relative "spec_helper"
|
2
|
+
include SpecHelper
|
3
|
+
|
4
|
+
describe Cookie do
|
5
|
+
it "#init" do
|
6
|
+
cookie = Cookie.new "TargetUser"
|
7
|
+
cookie.target_screen_name.should == "TargetUser"
|
8
|
+
end
|
9
|
+
|
10
|
+
it "#post" do
|
11
|
+
VCR.use_cassette("cookie") do
|
12
|
+
cookie = Cookie.new "UserDoesNotExistTest"
|
13
|
+
expect { cookie.post }.to raise_error(Server::Error, "Profile not found.")
|
14
|
+
cookie = Cookie.new SCREEN_NAME
|
15
|
+
expect { cookie.post }.to raise_error(Server::Error, "Lol.")
|
16
|
+
cookie = Cookie.new "BakeRolls"
|
17
|
+
cookie.post.should include(:code, :info)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
it "#to_s" do
|
22
|
+
cookie = Cookie.new "TargetUser"
|
23
|
+
cookie.to_s.should == "Ich gebe TargetUser einen Keks!"
|
24
|
+
end
|
25
|
+
end
|
data/spec/drink_spec.rb
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require_relative "spec_helper"
|
3
|
+
include SpecHelper
|
4
|
+
|
5
|
+
describe Drink do
|
6
|
+
context "with default values" do
|
7
|
+
subject { Drink.new("Test") }
|
8
|
+
|
9
|
+
it "should get some variables" do
|
10
|
+
subject.name.should == "Test"
|
11
|
+
subject.key.should == :test
|
12
|
+
subject.brand.should be_false
|
13
|
+
end
|
14
|
+
|
15
|
+
it "#to_s" do
|
16
|
+
subject.to_s.should eq "Test (test)"
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
context "with explicit values" do
|
21
|
+
subject { Drink.new("Test", "key", true) }
|
22
|
+
|
23
|
+
it "should be initialized" do
|
24
|
+
subject.name.should == "Test"
|
25
|
+
subject.key.should == :key
|
26
|
+
subject.brand.should be_true
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should be initialized with special chars" do
|
30
|
+
drink = Drink.new("Club-Mate")
|
31
|
+
drink.name.should == "Club-Mate"
|
32
|
+
drink.key.should == :clubmate
|
33
|
+
drink.brand.should be_false
|
34
|
+
|
35
|
+
drink = Drink.new("Ärzte")
|
36
|
+
drink.name.should == "Ärzte"
|
37
|
+
drink.key.should == :aerzte
|
38
|
+
drink.brand.should be_false
|
39
|
+
end
|
40
|
+
|
41
|
+
it "#to_s" do
|
42
|
+
subject.to_s.should eq "Test (key)"
|
43
|
+
end
|
44
|
+
|
45
|
+
it "#==" do
|
46
|
+
subject.should eq Drink.new("Test", "key", true)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
data/spec/drinks_spec.rb
ADDED
@@ -0,0 +1,154 @@
|
|
1
|
+
require_relative "spec_helper"
|
2
|
+
include SpecHelper
|
3
|
+
|
4
|
+
describe Drinks do
|
5
|
+
subject { Drinks.new }
|
6
|
+
|
7
|
+
it "::get" do
|
8
|
+
VCR.use_cassette("drinks") do
|
9
|
+
kaffee = {
|
10
|
+
:drink => "Kaffee",
|
11
|
+
:key => "kaffee",
|
12
|
+
:brand => "0"
|
13
|
+
}
|
14
|
+
drinks = Drinks.get
|
15
|
+
drinks.length > 10
|
16
|
+
drinks.first.should eq kaffee
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
it "::all" do
|
21
|
+
VCR.use_cassette("drinks") do
|
22
|
+
kaffee = {
|
23
|
+
:drink => "Kaffee",
|
24
|
+
:key => "kaffee",
|
25
|
+
:brand => "0"
|
26
|
+
}
|
27
|
+
drinks = Drinks.get
|
28
|
+
drinks.length > 10
|
29
|
+
drinks.first.should eq kaffee
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
it "::names" do
|
34
|
+
VCR.use_cassette("drinks") do
|
35
|
+
names = Drinks.names
|
36
|
+
names.should include "Kaffee"
|
37
|
+
names.should_not include :kaffee
|
38
|
+
names.should_not include false
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
it "::keys" do
|
43
|
+
VCR.use_cassette("drinks") do
|
44
|
+
keys = Drinks.keys
|
45
|
+
keys.should_not include "Kaffee"
|
46
|
+
keys.should include :kaffee
|
47
|
+
keys.should_not include false
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
it "::brands" do
|
52
|
+
VCR.use_cassette("drinks") do
|
53
|
+
brands = Drinks.brands
|
54
|
+
brands.should_not include "Kaffee"
|
55
|
+
brands.should_not include :kaffee
|
56
|
+
brands.should include false
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
it "::find" do
|
61
|
+
VCR.use_cassette("drinks") do
|
62
|
+
kaffee = Drink.new("Kaffee", "kaffee", false)
|
63
|
+
found = Drinks.find("kaffee")
|
64
|
+
found.should eq kaffee
|
65
|
+
found = Drinks.find(:kaffee)
|
66
|
+
found.should eq kaffee
|
67
|
+
found = Drinks["kaffee"]
|
68
|
+
found.should eq kaffee
|
69
|
+
found = Drinks[:kaffee]
|
70
|
+
found.should eq kaffee
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
it "::search" do
|
75
|
+
VCR.use_cassette("drinks") do
|
76
|
+
kaffee = Drink.new("Kaffee", "kaffee", false)
|
77
|
+
found = Drinks.search("kaff")
|
78
|
+
found.should include kaffee
|
79
|
+
found = Drinks.search(:kaff)
|
80
|
+
found.should include kaffee
|
81
|
+
found = Drinks.search(/kaff/i)
|
82
|
+
found.should include kaffee
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
it "#init with subset" do
|
87
|
+
VCR.use_cassette("drinks") do
|
88
|
+
kaffee = Drink.new("Kaffee", "kaffee", false)
|
89
|
+
clubmate = Drink.new("Club-Mate")
|
90
|
+
drinks = Drinks.new(kaffee)
|
91
|
+
drinks.all.should include kaffee
|
92
|
+
drinks = Drinks.new([kaffee,clubmate])
|
93
|
+
drinks.all.should include kaffee, clubmate
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
it "#init without subset" do
|
98
|
+
VCR.use_cassette("drinks") do
|
99
|
+
drinks = Drinks.new
|
100
|
+
drinks.length.should be > 10
|
101
|
+
kaffee = Drink.new("Kaffee", "kaffee", false)
|
102
|
+
drinks.all.should include kaffee
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
it "#length" do
|
107
|
+
VCR.use_cassette("drinks") do
|
108
|
+
kaffee = Drink.new("Kaffee", "kaffee", false)
|
109
|
+
clubmate = Drink.new("Club-Mate")
|
110
|
+
drinks = Drinks.new([kaffee,clubmate])
|
111
|
+
drinks.length.should be 2
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
it "#add" do
|
116
|
+
VCR.use_cassette("drinks") do
|
117
|
+
kaffee = Drink.new("Kaffee", "kaffee", false)
|
118
|
+
clubmate = Drink.new("Club-Mate")
|
119
|
+
drinks = Drinks.new(kaffee)
|
120
|
+
drinks.add(clubmate)
|
121
|
+
drinks.all.should include kaffee, clubmate
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
it "#<<" do
|
126
|
+
VCR.use_cassette("drinks") do
|
127
|
+
kaffee = Drink.new("Kaffee", "kaffee", false)
|
128
|
+
clubmate = Drink.new("Club-Mate")
|
129
|
+
drinks = Drinks.new(kaffee)
|
130
|
+
drinks << clubmate
|
131
|
+
drinks.all.should include kaffee, clubmate
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
135
|
+
it "#to_s" do
|
136
|
+
VCR.use_cassette("drinks") do
|
137
|
+
kaffee = Drink.new("Kaffee", "kaffee", false)
|
138
|
+
clubmate = Drink.new("Club-Mate", "clubmate")
|
139
|
+
test = Drink.new("testtetetetsetestset", "test")
|
140
|
+
drinks = Drinks.new([kaffee,clubmate,test])
|
141
|
+
drinks.length.should be 3
|
142
|
+
|
143
|
+
output = <<-EOF
|
144
|
+
Drink (key)
|
145
|
+
--------------------------------
|
146
|
+
Kaffee (kaffee)
|
147
|
+
Club-Mate (clubmate)
|
148
|
+
testtetetetsetestset (test)
|
149
|
+
EOF
|
150
|
+
|
151
|
+
drinks.to_s.should eq output.chomp
|
152
|
+
end
|
153
|
+
end
|
154
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require_relative "spec_helper"
|
2
|
+
include SpecHelper
|
3
|
+
|
4
|
+
describe Server::Error do
|
5
|
+
subject { Server::Error.new :wrong, "Something went wrong." }
|
6
|
+
it "#init" do
|
7
|
+
subject.type.should eq :wrong
|
8
|
+
subject.message.should eq "Something went wrong."
|
9
|
+
end
|
10
|
+
|
11
|
+
it "#to_s" do
|
12
|
+
subject.to_s.should eq "wrong"
|
13
|
+
end
|
14
|
+
end
|
data/spec/http_spec.rb
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
require_relative "spec_helper"
|
2
|
+
include SpecHelper
|
3
|
+
|
4
|
+
describe HTTP do
|
5
|
+
it "#create_url" do
|
6
|
+
url = "http://twoffein.com/api/get/drinks/?screen_name=#{SCREEN_NAME}&api_key=#{API_KEY}&encode=xml"
|
7
|
+
HTTP.create_url("get/drinks", PARAMS.merge(:encode => :xml)).should eq url
|
8
|
+
end
|
9
|
+
|
10
|
+
it "#request without parameter" do
|
11
|
+
VCR.use_cassette('drinks') do
|
12
|
+
drinks = HTTP.request(:get, "drinks")
|
13
|
+
drinks.length.should be > 10
|
14
|
+
drink = drinks.first
|
15
|
+
drink.should include(:drink, :key, :brand)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
it "#request as XML" do
|
20
|
+
pending("XML isn't implemented.")
|
21
|
+
VCR.use_cassette('drinks') do
|
22
|
+
drinks = HTTP.request(:get, "drinks", :encode => :xml)
|
23
|
+
drinks.should be_a_kind_of Array
|
24
|
+
drinks.length.should be > 10
|
25
|
+
drink = drinks.first
|
26
|
+
drink.should be_a_kind_of Hash
|
27
|
+
drink.should include(:drink, :key, :brand)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
it "#fetch drinks" do
|
32
|
+
VCR.use_cassette('drinks') do
|
33
|
+
url = "http://twoffein.de/api/get/drinks/?screen_name=#{SCREEN_NAME}&api_key=#{API_KEY}"
|
34
|
+
response = HTTP.fetch(url)
|
35
|
+
example_content = '[{"drink":"Kaffee","key":"kaffee","brand":"0"}'
|
36
|
+
response.body.should include(example_content)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
it "#post_data" do
|
41
|
+
pending("Not required by API")
|
42
|
+
end
|
43
|
+
|
44
|
+
it "#get" do
|
45
|
+
VCR.use_cassette('drinks') do
|
46
|
+
drinks = HTTP.get("drinks")
|
47
|
+
drinks.should be_a_kind_of Array
|
48
|
+
drinks.length.should be > 10
|
49
|
+
drink = drinks.first
|
50
|
+
drink.should be_a_kind_of Hash
|
51
|
+
drink.should include(:drink, :key, :brand)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
it "#get as XML" do
|
56
|
+
pending("XML isn't implemented.")
|
57
|
+
VCR.use_cassette('drinks') do
|
58
|
+
drinks = HTTP.get("drinks", :encode => :xml)
|
59
|
+
drinks.should be_a_kind_of Array
|
60
|
+
drinks.length.should be > 10
|
61
|
+
drink = drinks.first
|
62
|
+
drink.should be_a_kind_of Hash
|
63
|
+
drink.should include(:drink, :key, :brand)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
it "#post" do
|
68
|
+
pending("Not required by API")
|
69
|
+
end
|
70
|
+
|
71
|
+
it "#content_type" do
|
72
|
+
VCR.use_cassette('drinks') do
|
73
|
+
url = "http://twoffein.de/api/get/drinks/?screen_name=#{SCREEN_NAME}&api_key=#{API_KEY}"
|
74
|
+
res = HTTP.fetch(url)
|
75
|
+
HTTP.content_type(res).should match /application/
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
it "#xml?" do
|
80
|
+
VCR.use_cassette('drinks') do
|
81
|
+
url = "http://twoffein.de/api/get/drinks/?screen_name=#{SCREEN_NAME}&api_key=#{API_KEY}&encode=xml"
|
82
|
+
res = HTTP.fetch(url)
|
83
|
+
HTTP.xml?(res).should be_true
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
it "#json?" do
|
88
|
+
VCR.use_cassette('drinks') do
|
89
|
+
url = "http://twoffein.de/api/get/drinks/?screen_name=#{SCREEN_NAME}&api_key=#{API_KEY}"
|
90
|
+
res = HTTP.fetch(url)
|
91
|
+
HTTP.json?(res).should be_true
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|