adn 0.3.6 → 0.3.7

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: 06ed1a97a3c6eb2616915eacbd453390e0a98b12
4
- data.tar.gz: 89dbc49b0cb184307fad22cfa5490d4a31897c99
3
+ metadata.gz: 1b76bc2d8bb0dbd16f14658a2f62f2c0e85aacb8
4
+ data.tar.gz: e347e0817b8494b9ee63f15c63635221ba196e86
5
5
  SHA512:
6
- metadata.gz: 480a7313ea2b43093fd15c8868dd960513ee2a0446c92db23bd2d86bab3eed6c646c7127822eb74d81e5db88f8d4a8be31850ce6c0ca4d68ef0541045c152e9c
7
- data.tar.gz: f0d46825fbadf266d8acd5426e924b92c2f626263c91b4dc81c82af4e82dd9c7062cfc30ad80f5aceb44290dd727cb182b074355c0c51822c38b0fc5cf20ae16
6
+ metadata.gz: f833b54adb6692f0ad6872d63ba2dbcbf0b01c204851d9670f83da23913004c336fe5068750a1189be5a7a754e876135b43871d29178f83e6fff5138b3329085
7
+ data.tar.gz: 2ac0643e86d7166ed3ec9f57c243523931cad2fca878fcf0963c8cc1690235faa4a24c40e04edb2a9d84a928c3835af6687dc83b508afcbe6c8aceed5d3195f5
data/CHANGELOG.md CHANGED
@@ -1,6 +1,9 @@
1
1
  ### Changelog
2
2
 
3
- * **Version 0.3.5** (11 December 2013)
3
+ * **Version 0.3.7** (15 December 2013)
4
+ * Recipes now use the builder pattern
5
+ * Basic file functionality
6
+ * **Version 0.3.6** (11 December 2013)
4
7
  * Added recipe for easy broadcast creation
5
8
  * **Version 0.3.5** (23 October 2012)
6
9
  * Added the unified stream
data/Gemfile.lock CHANGED
@@ -1,12 +1,16 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- adn (0.3.6)
4
+ adn (0.3.7)
5
+ mime-types (~> 2.0)
6
+ multipart-post (~> 1.2.0)
5
7
 
6
8
  GEM
7
9
  remote: https://rubygems.org/
8
10
  specs:
11
+ mime-types (2.0)
9
12
  minitest (5.1.0)
13
+ multipart-post (1.2.0)
10
14
  rake (10.1.0)
11
15
 
12
16
  PLATFORMS
data/adn.gemspec CHANGED
@@ -13,5 +13,7 @@ Gem::Specification.new do |s|
13
13
  s.files = `git ls-files`.split($\)
14
14
  s.test_files = s.files.grep(%r{^(spec)/})
15
15
 
16
+ s.add_runtime_dependency "multipart-post", ["~> 1.2.0"]
17
+ s.add_runtime_dependency "mime-types", ["~> 2.0"]
16
18
  s.required_ruby_version = '>= 1.9.3'
17
19
  end
@@ -0,0 +1,30 @@
1
+ # encoding: UTF-8
2
+ require 'mime/types'
3
+ require 'net/http/post/multipart'
4
+
5
+ module ADN
6
+ module API
7
+ module File
8
+ def self.create(filename, params)
9
+ http_params = {
10
+ "content" => UploadIO.new(filename, MIME::Types.type_for(filename)[0]),
11
+ # make a fake file so we can still pass json
12
+ "metadata" => UploadIO.new(StringIO.new(params.to_json), "application/json", "data"),
13
+ }
14
+ ADN::API.post_multipart("#{ADN::API_ENDPOINT_FILES}", http_params)
15
+ end
16
+
17
+ def self.retrieve(file_id)
18
+ ADN::API.get("#{ADN::API_ENDPOINT_FILES}/#{file_id}")
19
+ end
20
+
21
+ def self.by_id(file_id)
22
+ self.retrieve(file_id)
23
+ end
24
+
25
+ def self.delete(file_id)
26
+ ADN::API.delete("#{ADN::API_ENDPOINT_FILES}/#{file_id}")
27
+ end
28
+ end
29
+ end
30
+ end
data/lib/adn/api.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  # encoding: UTF-8
2
2
 
3
- %w{response filter post stream subscription token user message}.each do |f|
3
+ %w{response filter post stream subscription token user message file}.each do |f|
4
4
  require_relative "api/#{f}"
5
5
  end
6
6
 
@@ -41,6 +41,11 @@ module ADN
41
41
  perform(request)
42
42
  end
43
43
 
44
+ def post_multipart(url, *http_args)
45
+ request = Net::HTTP::Post::Multipart.new(url, *http_args)
46
+ perform(request)
47
+ end
48
+
44
49
  def put(url, params = nil)
45
50
  request = construct_request(:put, url)
46
51
  request.add_field('Content-Type', 'application/json')
data/lib/adn/constants.rb CHANGED
@@ -9,6 +9,7 @@ module ADN
9
9
  API_ENDPOINT_USERS = "#{API_ENDPOINT}/users"
10
10
  API_ENDPOINT_TOKEN = "#{API_ENDPOINT}/token"
11
11
  API_ENDPOINT_CHANNELS = "#{API_ENDPOINT}/channels"
12
+ API_ENDPOINT_FILES = "#{API_ENDPOINT}/files"
12
13
  HTTP = Net::HTTP.new(API_HOST, 443)
13
14
 
14
15
  HTTP_ERROR = 400
data/lib/adn/file.rb ADDED
@@ -0,0 +1,63 @@
1
+ # encoding: UTF-8
2
+
3
+ module ADN
4
+ class File
5
+ attr_accessor(
6
+ :id, :file_id, :file_token, :sha1, :name, :source, :url, :kind, :total_size,
7
+ :url_expires, :size, :type, :public, :mime_type, :complete
8
+ )
9
+
10
+ attr_writer :user, :created_at
11
+
12
+ def self.upload_file(filename, params)
13
+ result = ADN::API::File.create(filename, params)
14
+ File.new(result["data"])
15
+ end
16
+
17
+ def self.by_id(id)
18
+ result = ADN::API::File.by_id(id)
19
+ File.new(result["data"])
20
+ end
21
+
22
+ def initialize(raw_file)
23
+ if raw_file.respond_to?(:each_pair)
24
+ set_values(raw_file)
25
+ file_id = id
26
+ else
27
+ file_id = raw_file
28
+ file_details = details
29
+ if file_details.has_key? "data"
30
+ set_values(file_details["data"])
31
+ end
32
+ end
33
+ end
34
+
35
+ def details
36
+ if id
37
+ value = self.instance_variables.map do |i|
38
+ [i.to_s.slice(1..-1), self.instance_variable_get(i)]
39
+ end
40
+ Hash[value]
41
+ else
42
+ ADN::API::File.by_id(file_id)
43
+ end
44
+ end
45
+
46
+ def created_at
47
+ DateTime.parse(@created_at)
48
+ end
49
+
50
+ def user
51
+ ADN::User.new(@user)
52
+ end
53
+
54
+ def delete
55
+ result = ADN::API::File.delete(id)
56
+ ADN.create_instance(result["data"], File)
57
+ end
58
+
59
+ def set_values(values)
60
+ values.each_pair { |k, v| send("#{k}=", v) if respond_to?("#{k}=") }
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,56 @@
1
+ # encoding: UTF-8
2
+
3
+ module ADN
4
+ module Recipes
5
+ class BroadcastMessageBuilder
6
+ attr_accessor(
7
+ :headline, :text, :read_more_link, :channel_id, :parse_links, :parse_markdown_links, :photo, :attachment
8
+ )
9
+
10
+ def initialize(params = {})
11
+ if params.respond_to? :each_pair
12
+ params.each_pair { |k, v| send("#{k}=", v) if respond_to?("#{k}=") }
13
+ end
14
+ yield self if block_given?
15
+ end
16
+
17
+ def annotations
18
+ annotations = [
19
+ {
20
+ type: 'net.app.core.broadcast.message.metadata',
21
+ value: {
22
+ subject: self.headline
23
+ }
24
+ }
25
+ ]
26
+
27
+ if self.read_more_link
28
+ annotations << {
29
+ type: 'net.app.core.crosspost',
30
+ value: {
31
+ canonical_url: self.read_more_link
32
+ }
33
+ }
34
+ end
35
+
36
+ annotations
37
+ end
38
+
39
+ def message
40
+ {
41
+ annotations: self.annotations,
42
+ text: self.text,
43
+ machine_only: (not self.text),
44
+ entities: {
45
+ parse_links: (self.parse_links or self.parse_markdown_links),
46
+ parse_markdown_links: !!self.parse_markdown_links
47
+ }
48
+ }
49
+ end
50
+
51
+ def send
52
+ Message.new ADN::API::Message.create(self.channel_id, self.message)["data"]
53
+ end
54
+ end
55
+ end
56
+ end
data/lib/adn/recipes.rb CHANGED
@@ -1,37 +1,5 @@
1
1
  # encoding: UTF-8
2
2
 
3
- module ADN
4
- module Recipes
5
- def self.send_broadcast(params)
6
- channel_id = params.delete('channel_id')
7
- broadcast = params.delete('broadcast')
8
- text = params.delete('text')
9
- read_more_link = params.delete('read_more_link')
10
-
11
- message = {
12
- annotations: [
13
- {
14
- type: 'net.app.core.broadcast.message.metadata',
15
- value: {
16
- subject: broadcast
17
- }
18
- }
19
- ],
20
- text: text,
21
- machine_only: (not text)
22
- }
23
-
24
- if read_more_link
25
- message[:annotations] << {
26
- type: 'net.app.core.crosspost',
27
- value: {
28
- canonical_url: read_more_link
29
- }
30
- }
31
- end
32
-
33
- result = ADN::API::Message.create(channel_id, message)
34
- Message.new(result["data"])
35
- end
36
- end
3
+ %w{broadcast_message_builder}.each do |f|
4
+ require_relative "recipes/#{f}"
37
5
  end
data/lib/adn/version.rb CHANGED
@@ -3,7 +3,7 @@
3
3
  module ADN
4
4
  MAJOR = 0
5
5
  MINOR = 3
6
- TINY = 6
6
+ TINY = 7
7
7
  VERSION = [MAJOR, MINOR, TINY].join('.')
8
8
  end
9
9
 
@@ -0,0 +1,64 @@
1
+ # encoding: UTF-8
2
+
3
+ require_relative '../../spec_helper'
4
+
5
+ describe ADN::API::File do
6
+ subject { ADN::API::File }
7
+
8
+ let(:base_path) { '/stream/0/files' }
9
+ let(:error_message) {
10
+ 'Call requires authentication: This view requires authentication and no token was provided.'
11
+ }
12
+
13
+ let(:error_response) {
14
+ OpenStruct.new(:body => %Q{ { "meta" : {
15
+ "code" : 401,
16
+ "error_id" : "6f5137beac6c4b9ea8dbec8e50aa9f38$32a85f1c22e98de98ea2ddabaf76c5ae",
17
+ "error_message" : "#{error_message}"
18
+ }} })
19
+ }
20
+
21
+ describe "new" do
22
+ it "passes the file content in params to the API" do
23
+ args(:post_multipart) {
24
+ path, params = subject.create(__FILE__, { type: 'foo' })
25
+
26
+ path.must_equal base_path
27
+ params["content"].local_path.must_equal __FILE__
28
+ params["content"].content_type.must_equal "application/x-ruby"
29
+ params["metadata"].content_type.must_equal "application/json"
30
+ JSON.parse(params["metadata"].read).must_equal({"type" => 'foo'})
31
+ }
32
+ end
33
+
34
+ it 'retrieves error message correctly' do
35
+ ADN::HTTP.stub(:request, error_response) do
36
+ error_call = lambda {subject.create(__FILE__, { foo: 'bar' })}
37
+ error_call.must_raise ADN::API::Error
38
+
39
+ error = error_call.call rescue $!
40
+ error.message.must_equal error_message
41
+ end
42
+ end
43
+ end
44
+
45
+ describe "retrieve" do
46
+ it "retrieves the file" do
47
+ arg(:get) { subject.retrieve(8).must_equal base_path + "/8" }
48
+ end
49
+ end
50
+
51
+ describe "by_id" do
52
+ it "is just an alias for retrieve" do
53
+ subject.stub(:retrieve, 'bar') do
54
+ subject.by_id(4).must_equal 'bar'
55
+ end
56
+ end
57
+ end
58
+
59
+ describe "delete" do
60
+ it "deletes the file" do
61
+ arg(:delete) { subject.delete(5).must_equal base_path + "/5" }
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,89 @@
1
+ # encoding: UTF-8
2
+
3
+ require_relative '../spec_helper'
4
+
5
+ def d(data)
6
+ { 'data' => data }
7
+ end
8
+
9
+ describe ADN::File do
10
+ subject { ADN::File }
11
+
12
+ let(:file) { subject.new(file_data) }
13
+ let(:example_user) { { username: 'peterhellberg' } }
14
+ let(:file_data) {{
15
+ created_at: '1999-12-31T23:59:59Z',
16
+ id: 10001,
17
+ user: example_user,
18
+ file_token: "abc123",
19
+ sha1: "d5e7c7a123108739a28d721eb34133600a70a7fa",
20
+ name: "foo.txt",
21
+ source: { link: "https://alpha.app.net", name: "Alpha" },
22
+ url: "https://adn-uf-01.s3.amazonaws.com/adn-uf-01/really_long_filepath",
23
+ kind: "other",
24
+ total_size: 2248,
25
+ url_expires: "2013-12-14T02:00:00Z",
26
+ size: 2248,
27
+ type: "net.app.testing",
28
+ public: false,
29
+ mime_type: "text/plain",
30
+ complete: true
31
+ }}
32
+
33
+ describe "upload_file" do
34
+ it "creates a message via the API" do
35
+ ADN::API::File.stub(:create, ADN::API::Response.new(d(file_data))) do
36
+ m = subject.upload_file("foo.txt", {type: "net.app.testing"})
37
+ m.type.must_equal 'net.app.testing'
38
+ m.name.must_equal 'foo.txt'
39
+ end
40
+ end
41
+ end
42
+
43
+ describe "initialize" do
44
+ it "populates the accessors using the passed in hash" do
45
+ m = subject.new id: 123
46
+ m.id.must_equal 123
47
+ end
48
+
49
+ it "populates the accessors based on the passed in id" do
50
+ ADN::API::File.stub(:by_id, ->(i){ d({ "total_size" => 1234}) }) do
51
+ f = subject.new 123
52
+ f.total_size.must_equal 1234
53
+ end
54
+ end
55
+ end
56
+
57
+ describe "details" do
58
+ it "returns the details for the file" do
59
+ file.details.keys.must_equal [
60
+ "created_at", "id", "user", "file_token", "sha1", "name", "source", "url", "kind", "total_size",
61
+ "url_expires", "size", "type", "public", "mime_type", "complete"
62
+ ]
63
+ end
64
+ end
65
+
66
+ describe "created_at" do
67
+ it "returns the date and time the message was created" do
68
+ file.created_at.to_s.must_equal '1999-12-31T23:59:59+00:00'
69
+ end
70
+ end
71
+
72
+ describe "user" do
73
+ it "returns the user" do
74
+ file.user.username.must_equal 'peterhellberg'
75
+ end
76
+ end
77
+
78
+ describe "delete" do
79
+ it "deletes the file via the API and returns the file" do
80
+ delete_stub = ->(id){
81
+ ADN::API::Response.new("data" => { "id" => id })
82
+ }
83
+
84
+ ADN::API::File.stub(:delete, delete_stub) do
85
+ file.delete.id.must_equal 10001
86
+ end
87
+ end
88
+ end
89
+ end
@@ -0,0 +1,115 @@
1
+ # encoding: UTF-8
2
+
3
+ require_relative '../../spec_helper'
4
+
5
+ describe ADN::Recipes::BroadcastMessageBuilder do
6
+ subject { ADN::Recipes::BroadcastMessageBuilder.new }
7
+
8
+ describe "annotations" do
9
+ describe "with a read more link" do
10
+ it "generates a crosspost annotation" do
11
+ subject.headline = "foo"
12
+ subject.read_more_link = "http://app.net"
13
+ subject.annotations.must_equal([
14
+ {
15
+ type: "net.app.core.broadcast.message.metadata",
16
+ value: { subject: "foo" }
17
+ },
18
+ {
19
+ type: "net.app.core.crosspost",
20
+ value: { canonical_url: "http://app.net" }
21
+ }
22
+ ])
23
+ end
24
+ end
25
+
26
+ describe "with only a headline" do
27
+ it "generates a message metadata annotation" do
28
+ subject.headline = "foo"
29
+ subject.annotations.must_equal([
30
+ {
31
+ type: "net.app.core.broadcast.message.metadata",
32
+ value: { subject: "foo" }
33
+ },
34
+ ])
35
+ end
36
+ end
37
+ end
38
+
39
+ describe "message" do
40
+ describe "with text" do
41
+ it "includes the extra text" do
42
+ subject.headline = "foo"
43
+ subject.text = "bar"
44
+ subject.message[:text].must_equal("bar")
45
+ subject.message[:machine_only].must_equal(false)
46
+ end
47
+ end
48
+
49
+ describe "without text" do
50
+ it "generates a machine only message" do
51
+ subject.headline = "foo"
52
+ subject.message[:text].must_be_nil
53
+ subject.message[:machine_only].must_equal(true)
54
+ end
55
+ it "includes the annotations" do
56
+ subject.headline = "bar"
57
+ subject.message[:annotations].must_equal([
58
+ {
59
+ type: "net.app.core.broadcast.message.metadata",
60
+ value: { subject: "bar" }
61
+ },
62
+ ])
63
+ end
64
+ end
65
+
66
+ it "allows you to parse links" do
67
+ subject.headline = "foo"
68
+ subject.parse_links = true
69
+ subject.message[:entities].must_equal({
70
+ parse_links: true,
71
+ parse_markdown_links: false
72
+ })
73
+ end
74
+
75
+ it "parses links when you ask for parsing markdown links" do
76
+ subject.headline = "foo"
77
+ subject.parse_markdown_links = true
78
+ subject.message[:entities].must_equal({
79
+ parse_links: true,
80
+ parse_markdown_links: true
81
+ })
82
+ end
83
+ end
84
+
85
+ describe "send" do
86
+ it "calls the api with the message" do
87
+ headline = "foo"
88
+ channel_id = "123"
89
+ response_body = %Q{{
90
+ "meta": {
91
+ "code": 200
92
+ },
93
+ "data": {
94
+ "annotations": [
95
+ {
96
+ "type": "net.app.core.broadcast.message.metadata",
97
+ "value": {"subject": "#{headline}"}
98
+ }
99
+ ],
100
+ "channel_id": "#{channel_id}"
101
+ }
102
+ }}
103
+ response = OpenStruct.new(:body => response_body)
104
+
105
+ ADN::HTTP.stub(:request, response) do
106
+ subject.headline = headline
107
+ subject.channel_id = channel_id
108
+
109
+ message = subject.send
110
+ message.channel_id.must_equal channel_id
111
+ message.annotations[0]["value"]["subject"].must_equal headline
112
+ end
113
+ end
114
+ end
115
+ end
data/spec/adn_spec.rb CHANGED
@@ -19,10 +19,11 @@ describe ADN do
19
19
  ADN::API_HOST.must_equal 'alpha-api.app.net'
20
20
  end
21
21
 
22
- it "has constants containing the API endpoints for posts, users, and channels" do
22
+ it "has constants containing the API endpoints for posts, users, channels, and files" do
23
23
  ADN::API_ENDPOINT_POSTS.must_equal '/stream/0/posts'
24
24
  ADN::API_ENDPOINT_USERS.must_equal '/stream/0/users'
25
25
  ADN::API_ENDPOINT_CHANNELS.must_equal '/stream/0/channels'
26
+ ADN::API_ENDPOINT_FILES.must_equal '/stream/0/files'
26
27
  end
27
28
 
28
29
  it "has constants containing the API endpoints for tokens" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: adn
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.6
4
+ version: 0.3.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kishyr Ramdial
@@ -10,8 +10,36 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2013-12-11 00:00:00.000000000 Z
14
- dependencies: []
13
+ date: 2013-12-15 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: multipart-post
17
+ requirement: !ruby/object:Gem::Requirement
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 1.2.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - ~>
27
+ - !ruby/object:Gem::Version
28
+ version: 1.2.0
29
+ - !ruby/object:Gem::Dependency
30
+ name: mime-types
31
+ requirement: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - ~>
34
+ - !ruby/object:Gem::Version
35
+ version: '2.0'
36
+ type: :runtime
37
+ prerelease: false
38
+ version_requirements: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ~>
41
+ - !ruby/object:Gem::Version
42
+ version: '2.0'
15
43
  description: A simple and easy to use library to interact with App.net's API
16
44
  email:
17
45
  - kishyr@gmail.com
@@ -31,6 +59,7 @@ files:
31
59
  - adn.gemspec
32
60
  - lib/adn.rb
33
61
  - lib/adn/api.rb
62
+ - lib/adn/api/file.rb
34
63
  - lib/adn/api/filter.rb
35
64
  - lib/adn/api/message.rb
36
65
  - lib/adn/api/post.rb
@@ -40,11 +69,14 @@ files:
40
69
  - lib/adn/api/token.rb
41
70
  - lib/adn/api/user.rb
42
71
  - lib/adn/constants.rb
72
+ - lib/adn/file.rb
43
73
  - lib/adn/message.rb
44
74
  - lib/adn/post.rb
45
75
  - lib/adn/recipes.rb
76
+ - lib/adn/recipes/broadcast_message_builder.rb
46
77
  - lib/adn/user.rb
47
78
  - lib/adn/version.rb
79
+ - spec/adn/api/file_spec.rb
48
80
  - spec/adn/api/filter_spec.rb
49
81
  - spec/adn/api/message_spec.rb
50
82
  - spec/adn/api/post_spec.rb
@@ -53,8 +85,10 @@ files:
53
85
  - spec/adn/api/subscription_spec.rb
54
86
  - spec/adn/api/token_spec.rb
55
87
  - spec/adn/api/user_spec.rb
88
+ - spec/adn/file_spec.rb
56
89
  - spec/adn/message_spec.rb
57
90
  - spec/adn/post_spec.rb
91
+ - spec/adn/recipes/broadcast_message_builder_spec.rb
58
92
  - spec/adn/user_spec.rb
59
93
  - spec/adn_spec.rb
60
94
  - spec/fixtures/post.json
@@ -84,6 +118,7 @@ signing_key:
84
118
  specification_version: 4
85
119
  summary: A Ruby library for App.net
86
120
  test_files:
121
+ - spec/adn/api/file_spec.rb
87
122
  - spec/adn/api/filter_spec.rb
88
123
  - spec/adn/api/message_spec.rb
89
124
  - spec/adn/api/post_spec.rb
@@ -92,8 +127,10 @@ test_files:
92
127
  - spec/adn/api/subscription_spec.rb
93
128
  - spec/adn/api/token_spec.rb
94
129
  - spec/adn/api/user_spec.rb
130
+ - spec/adn/file_spec.rb
95
131
  - spec/adn/message_spec.rb
96
132
  - spec/adn/post_spec.rb
133
+ - spec/adn/recipes/broadcast_message_builder_spec.rb
97
134
  - spec/adn/user_spec.rb
98
135
  - spec/adn_spec.rb
99
136
  - spec/fixtures/post.json