distimo 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d0df1e14a7e1809e481cadbbf4b7eb268c373ad2
4
- data.tar.gz: 0226224dc68dcf326b06c074a23e7fbab42d2929
3
+ metadata.gz: 382f222edff821a227614f902bc3f38bc0ce39cd
4
+ data.tar.gz: 6a539efab8ef814862cbd7153b6c91bfa9134042
5
5
  SHA512:
6
- metadata.gz: 341e0869cf9891441b5e00314d282f289bbc6dee54bd266fe3a84678af726d2d27028bdc5037d3f1e91d4a63f69ea95a72763521740748305bc2c584f752b032
7
- data.tar.gz: 83fdda0b71a57caa77e691f4f33c69b91cf45b5b77507e37aee05a0eea3b7dfb51d2d7762025b3721729e2a194f57a810af2670225c9971fa90a886969ebe37b
6
+ metadata.gz: db736eddaa448a1946ee4c9865e2e45067b1909c017eab89ef608c0e7ed1652fec848d728c42aa7b9e608df3324f3fc629fd6389c04c35d351dde491e1eb3fab
7
+ data.tar.gz: 54fdcb9594417e7d46624fd682a06c700453840055d7ddde279761a4544b08c86bb9d20000aec2c836680b8fc6c660f768576aad7291d0fa96d334080c0db69f
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Distimo
2
2
 
3
- TODO: Write a gem description
3
+ Simple api wrapper
4
4
 
5
5
  ## Installation
6
6
 
@@ -18,7 +18,16 @@ Or install it yourself as:
18
18
 
19
19
  ## Usage
20
20
 
21
- TODO: Write usage instructions here
21
+ ```ruby
22
+ client = Distimo::Client.new(
23
+ public_key: "your public key",
24
+ private_key: "your private key",
25
+ token: "oauth token",
26
+ username: "your user name",
27
+ password: "your password")
28
+
29
+ puts client.downloads(assets: "1,2,3")
30
+ ```
22
31
 
23
32
  ## Contributing
24
33
 
@@ -1,5 +1,3 @@
1
1
  require "distimo/version"
2
2
  require "distimo/client"
3
- module Distimo
4
- # Your code goes here...
5
- end
3
+
@@ -4,8 +4,7 @@ module Distimo
4
4
 
5
5
  METHODS.each do |name|
6
6
  define_method name do |query = {}|
7
- options = prepare(query)
8
- self.class.get("/#{name}", options)
7
+ get("/#{name}", query)
9
8
  end
10
9
  end
11
10
 
@@ -1,4 +1,5 @@
1
1
  require "distimo/api"
2
+ require "distimo/error"
2
3
  require "httparty"
3
4
  module Distimo
4
5
  class Client
@@ -12,6 +13,13 @@ module Distimo
12
13
  opts.each {|k,v| send("#{k}=",v)}
13
14
  end
14
15
 
16
+ def get path, query
17
+ options = prepare(query)
18
+ response = self.class.get path, options
19
+ raise Error.from_response(response) if response.code >= 400
20
+ response
21
+ end
22
+
15
23
  private
16
24
 
17
25
  def prepare query
@@ -0,0 +1,16 @@
1
+ module Distimo
2
+ class Error < StandardError
3
+ attr_reader :code, :note, :link
4
+
5
+ def initialize code,note,link
6
+ @code = code
7
+ @note = note
8
+ @link = link
9
+ super("Code: #{code}. #{note} See: #{link}")
10
+ end
11
+
12
+ def self.from_response response
13
+ new(response["code"],response["message"],response["link"])
14
+ end
15
+ end
16
+ end
@@ -1,3 +1,3 @@
1
1
  module Distimo
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -28,11 +28,11 @@ describe Distimo::Client do
28
28
  password: "P")
29
29
  }
30
30
  before do
31
- stub_request(:get,"https://analytics.distimo.com/api/v3/downloads" ).
31
+ stub_get("/any").
32
32
  with({ headers: {"Authorization" => "Bearer TOKEN"}, query: hash_including({})})
33
33
  end
34
34
  it "should set auth token" do
35
- client_with_token.downloads({})
35
+ client_with_token.get("/any",{})
36
36
  end
37
37
  end
38
38
 
@@ -44,16 +44,52 @@ describe Distimo::Client do
44
44
  password: "pass")
45
45
  }
46
46
  before do
47
- stub_request(:get,"https://user:pass@analytics.distimo.com/api/v3/downloads" ).
47
+ stub_request(:get,"https://user:pass@analytics.distimo.com/api/v3/any" ).
48
48
  with({ headers: {}, query: hash_including({})})
49
49
  end
50
- it "should set auth token" do
51
- client_without_token.downloads({})
50
+ it "should use basic auth" do
51
+ client_without_token.get("/any",{})
52
52
  end
53
53
  end
54
+ end
54
55
 
56
+ describe "#get" do
57
+ let(:client){
58
+ Distimo::Client.new(private_key: "PrivateKey",
59
+ public_key: "PublicKey",
60
+ token: "TOKEN")
61
+ }
62
+ let(:time){ Time.utc(2000) }
63
+ before do
64
+ Timecop.freeze(time)
65
+ end
55
66
 
56
- end
67
+ after do
68
+ Timecop.return
69
+ end
70
+
71
+ it "has apikey in query" do
72
+ stub_get("/any").with(query: hash_including({apikey: "PublicKey"}))
73
+ client.get("/any",{})
74
+ end
75
+ it "has time in query" do
76
+ stub_get("/any").with(query: hash_including({t: time.to_i.to_s}))
77
+ client.get("/any",{})
78
+ end
79
+ it "has time in query" do
80
+ stub_get("/any").with(query: hash_including(
81
+ foo: "bar",
82
+ hash: OpenSSL::HMAC.hexdigest('sha1', "PrivateKey", "foo=bar" + time.to_i.to_s)))
83
+ client.get("/any",{foo: "bar"})
84
+ end
57
85
 
86
+ context "with error" do
87
+ before do
88
+ stub_get("/any").with(query: hash_including({apikey: "PublicKey"})).
89
+ to_return(:status => 400)
90
+ end
58
91
 
92
+ it { expect{ client.get("/any",{})}.to raise_error(Distimo::Error)}
93
+ end
94
+ end
59
95
  end
@@ -0,0 +1,18 @@
1
+ require "helper"
2
+
3
+ describe Distimo::Error do
4
+ describe ".from_response" do
5
+ subject {
6
+ Distimo::Error.from_response({"http_code"=>401,
7
+ "code"=>201,
8
+ "message"=>"Please authenticate API keys.",
9
+ "link"=>"https://analytics.distimo.com/support/api/v3/code/201"})
10
+ }
11
+ its(:code){ should eq(201)}
12
+ its(:note){ should eq("Please authenticate API keys.")}
13
+ its(:message){ should eq("Code: 201. Please authenticate API keys. See: https://analytics.distimo.com/support/api/v3/code/201")}
14
+ its(:link){ should eq("https://analytics.distimo.com/support/api/v3/code/201")}
15
+ its(:class){ should <= StandardError }
16
+ end
17
+
18
+ end
@@ -15,3 +15,8 @@ RSpec.configure do |config|
15
15
  c.syntax = :expect
16
16
  end
17
17
  end
18
+
19
+
20
+ def stub_get(path)
21
+ stub_request(:get, Distimo::Client.base_uri + path)
22
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: distimo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yuri Barbashov
@@ -68,9 +68,11 @@ files:
68
68
  - lib/distimo.rb
69
69
  - lib/distimo/api.rb
70
70
  - lib/distimo/client.rb
71
+ - lib/distimo/error.rb
71
72
  - lib/distimo/version.rb
72
73
  - spec/distimo/api_spec.rb
73
74
  - spec/distimo/client_spec.rb
75
+ - spec/distimo/error_spec.rb
74
76
  - spec/helper.rb
75
77
  homepage: https://github.com/playa/distimo
76
78
  licenses:
@@ -99,5 +101,6 @@ summary: Distimo API wrapper gem
99
101
  test_files:
100
102
  - spec/distimo/api_spec.rb
101
103
  - spec/distimo/client_spec.rb
104
+ - spec/distimo/error_spec.rb
102
105
  - spec/helper.rb
103
106
  has_rdoc: