Skip to content

Commit

Permalink
Add methods. Refactor methods. Add to tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
davorg committed Jun 3, 2015
1 parent df81c68 commit bd68eb9
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
5 changes: 4 additions & 1 deletion gettemp
Expand Up @@ -5,6 +5,7 @@ use warnings;
use 5.010;

use lib 'lib';
use Data::Dumper;
use Hive;

my ($user, $pass) = @ARGV;
Expand All @@ -14,4 +15,6 @@ my $hive = Hive->new({
password => $pass,
});

say $hive->get_user;
foreach (qw[get_temperature get_target_temperature]) {
say Dumper $hive->$_;
}
34 changes: 31 additions & 3 deletions lib/Hive.pm
Expand Up @@ -7,6 +7,7 @@ use warnings;
use LWP::UserAgent;
use HTTP::Cookies;
use HTTP::Request::Common;
use JSON;

has username => (
is => 'ro',
Expand Down Expand Up @@ -40,6 +41,18 @@ has base_url => (
default => 'https://api.hivehome.com/v5/',
);

has json => (
is => 'ro',
isa => 'JSON',
lazy_build => 1,
);

sub _build_json {
my $self = shift;
return JSON->new->utf8;
}


sub BUILD {
my $self = shift;

Expand All @@ -48,11 +61,26 @@ sub BUILD {
$self->ua->request($req);
}

sub get_user {
sub get_temperature {
my $self = shift;

return $self->get('/widgets/temperature');
}

sub get_target_temperature {
my $self = shift;

my $req = GET $self->base_url . '/users/'. $self->username;
return $self->ua->request($req)->as_string;
return $self->get('/widgets/climate/targetTemperature');
}

sub get {
my $self = shift;

my $url = $self->base_url . '/users/' . $self->username . shift;

my $req = GET $url;
return $self->json->decode($self->ua->request($req)->content);
}


1;

0 comments on commit bd68eb9

Please sign in to comment.