Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sumarize all info in hash #37

Merged
merged 1 commit into from
Sep 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions lib/user_agent_parser/device.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,13 @@ def eql?(other)
end

alias == eql?

def to_h
{
family: family,
model: model,
brand: brand
}
end
end
end
7 changes: 7 additions & 0 deletions lib/user_agent_parser/operating_system.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,12 @@ def eql?(other)
end

alias == eql?

def to_h
{
version: version.to_h,
family: family
}
end
end
end
9 changes: 9 additions & 0 deletions lib/user_agent_parser/user_agent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,14 @@ def eql?(other)
end

alias == eql?

def to_h
{
device: device.to_h,
family: family,
os: os.to_h,
version: version.to_h
}
end
end
end
10 changes: 10 additions & 0 deletions lib/user_agent_parser/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,15 @@ def eql?(other)
def segments
@segments ||= version.scan(SEGMENTS_REGEX)
end

def to_h
{
version: version,
major: major,
minor: minor,
patch: patch,
patch_minor: patch_minor
}
end
end
end
14 changes: 14 additions & 0 deletions spec/user_agent_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,18 @@
agent.inspect.must_equal '#<UserAgentParser::UserAgent Mobile Safari 5.0.2 (iOS 4.2.1) (iPhone)>'
end
end

describe '#to_h' do
it 'returns everything' do
browser_version = UserAgentParser::Version.new('5.0.2')
os_version = UserAgentParser::Version.new('4.2.1')
os = UserAgentParser::OperatingSystem.new('iOS', os_version)
device = UserAgentParser::Device.new('iPhone')
agent = UserAgentParser::UserAgent.new('Mobile Safari', browser_version, os, device)
assert_equal(
{:device=>{:family=>"iPhone", :model=>"iPhone", :brand=>nil}, :family=>"Mobile Safari", :os=>{:version=>{:version=>"4.2.1", :major=>"4", :minor=>"2", :patch=>"1", :patch_minor=>nil}, :family=>"iOS"}, :version=>{:version=>"5.0.2", :major=>"5", :minor=>"0", :patch=>"2", :patch_minor=>nil}},
agent.to_h
)
end
end
end