koala 2.0.0 → 2.2.0rc1

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: cee1d15d3263d9cdfffa910b9127b4d6e177f844
4
- data.tar.gz: db07b6f8a9443e34c717671756daa41138c408c0
3
+ metadata.gz: 97051729d27eccc4d97c5140394556293d181446
4
+ data.tar.gz: 0a4fba738c9919c43e570107a54024e23d167b83
5
5
  SHA512:
6
- metadata.gz: ccba6c6fc0b52f5c22582f2438f53968c6e23c458d329be8cf18a997554043a8e433e97f464e03affdfcda6f530cd90b354ac8f6a0cf05d26ff72a23990bf69d
7
- data.tar.gz: 33484fe1e975f2a3808f928a967ff7d054c8495f9185dd4975ce448cb301dbe9002b756d72d4e0a7bc9739e13f90ef42ed4f66254aeec83c0231841dc6df22ef
6
+ metadata.gz: a8083a54eff541caa07259f10a937e5500ae35cdf1323b6085f1f87287e2b9bdfc8f9dab7287ae326d360b570e75219e080b3b17b99fda008db56e3889374517
7
+ data.tar.gz: 3a5d6929a14c0cbcc96384314cadb85c0cd8b4feb1ebfede3bdb427676b990f2c25abb936d61d49eb32de51aa141390c5af000468ebe5bcfa2577d01ef41fdaf
data/.gitignore CHANGED
@@ -5,4 +5,5 @@ Gemfile.lock
5
5
  *.rbc
6
6
  *~
7
7
  .yardoc/
8
- .DS_Store
8
+ .DS_Store
9
+ coverage
@@ -12,3 +12,6 @@ rvm:
12
12
  # jruby
13
13
  - jruby-19mode
14
14
  bundler_args: --without development
15
+ addons:
16
+ code_climate:
17
+ repo_token: 7af99d9225b4c14640f9ec3cb2e24d2f7103ac49417b0bd989188fb6c25f2909
data/Gemfile CHANGED
@@ -15,6 +15,7 @@ group :test do
15
15
  gem "rspec", '~> 3.0.0.beta1'
16
16
  gem "vcr"
17
17
  gem "webmock"
18
+ gem "codeclimate-test-reporter", require: nil
18
19
  end
19
20
 
20
21
  gem "jruby-openssl" if defined? JRUBY_VERSION
@@ -1,3 +1,26 @@
1
+ v2.2.0
2
+ ======
3
+
4
+ Updated features:
5
+
6
+ * You can now specify format: :json in http_options to make Content-Type application/json requests (thanks, adparlor!)
7
+ * Koala now supports uploading videos by URL (thanks, filipegiusti!)
8
+
9
+ Internal Improvements:
10
+
11
+ * Use MultiJson::LoadError instead of the newer ParseError for backward compatibility (thanks, bunshin!)
12
+
13
+ Documentation improvements:
14
+
15
+ * modernize the hash syntax in the readme (thanks, st0012!)
16
+
17
+ v2.1.0
18
+ ======
19
+
20
+ Documentation improvements:
21
+
22
+ * extend/clean up code quality badges (thanks, jbender!)
23
+
1
24
  v2.0.0
2
25
  ======
3
26
 
@@ -37,6 +37,10 @@ module Koala
37
37
  # @param options request-related options for Koala and Faraday.
38
38
  # See https://github.com/arsduo/koala/wiki/HTTP-Services for additional options.
39
39
  # @option options [Symbol] :http_component which part of the response (headers, body, or status) to return
40
+ # @option options [Symbol] :format which request format to use. Currently, :json is supported
41
+ # @option options [Symbol] :preserve_form_arguments preserve arrays in arguments, which are
42
+ # expected by certain FB APIs (see the ads API in particular,
43
+ # https://developers.facebook.com/docs/marketing-api/adgroup/v2.4)
40
44
  # @option options [Boolean] :beta use Facebook's beta tier
41
45
  # @option options [Boolean] :use_ssl force SSL for this request, even if it's tokenless.
42
46
  # (All API requests with access tokens use SSL.)
@@ -61,7 +65,7 @@ module Koala
61
65
  end
62
66
 
63
67
  # Translate any arrays in the params into comma-separated strings
64
- args = sanitize_request_parameters(args)
68
+ args = sanitize_request_parameters(args) unless preserve_form_arguments?(options)
65
69
 
66
70
  # add a leading / if needed...
67
71
  path = "/#{path}" unless path =~ /^\//
@@ -105,8 +109,12 @@ module Koala
105
109
  result.merge(key => value)
106
110
  end
107
111
  end
112
+
113
+ def preserve_form_arguments?(options)
114
+ options[:format] == :json || options[:preserve_form_arguments] || Koala.config.preserve_form_arguments
115
+ end
108
116
  end
109
117
  end
110
118
  end
111
119
 
112
- require 'koala/api/graph_batch_api'
120
+ require 'koala/api/graph_batch_api'
@@ -222,7 +222,8 @@ module Koala
222
222
  put_connections(*parse_media_args(picture_args, "photos"), &block)
223
223
  end
224
224
 
225
- # Upload a video. Functions exactly the same as put_picture.
225
+ # Upload a video. Functions exactly the same as put_picture (URLs supported as of Facebook
226
+ # API version 2.3).
226
227
  # @see #put_picture
227
228
  def put_video(*video_args, &block)
228
229
  args = parse_media_args(video_args, "videos")
@@ -557,7 +558,9 @@ module Koala
557
558
 
558
559
  if url?(media_args.first)
559
560
  # If media_args is a URL, we can upload without UploadableIO
560
- args.merge!(:url => media_args.first)
561
+ # Video: https://developers.facebook.com/docs/graph-api/video-uploads
562
+ fb_expected_arg_name = method == "photos" ? :url : :file_url
563
+ args.merge!(fb_expected_arg_name => media_args.first)
561
564
  else
562
565
  args["source"] = Koala::UploadableIO.new(*media_args.slice(0, 1 + args_offset))
563
566
  end
@@ -74,7 +74,7 @@ module Koala
74
74
  #
75
75
  # @return [Koala::HTTPService::Response] a response object representing the results from Facebook
76
76
  def self.make_request(path, args, verb, options = {})
77
- # if the verb isn't get or post, send it as a post argument
77
+ # if the verb isn't get or post, send it as a post argument with a method param
78
78
  args.merge!({:method => verb}) && verb = "post" if verb != "get" && verb != "post"
79
79
 
80
80
  # turn all the keys to strings (Faraday has issues with symbols under 1.8.7) and resolve UploadableIOs
@@ -101,7 +101,17 @@ module Koala
101
101
  # we have to manually assign params to the URL or the
102
102
  conn = Faraday.new(server(request_options), faraday_options(request_options), &(faraday_middleware || DEFAULT_MIDDLEWARE))
103
103
 
104
- response = conn.send(verb, path, (verb == "post" ? params : {}))
104
+ # remember, all non-GET requests are turned into POSTs -- see the the start of this method
105
+ if verb == "post" && options[:format] == :json
106
+ response = conn.post do |req|
107
+ req.path = path
108
+ req.headers["Content-Type"] = "application/json"
109
+ req.body = params.to_json
110
+ req
111
+ end
112
+ else
113
+ response = conn.send(verb, path, (verb == "post" ? params : {}))
114
+ end
105
115
 
106
116
  # Log URL information
107
117
  Koala::Utils.debug "#{verb.upcase}: #{path} params: #{params.inspect}"
@@ -261,7 +261,7 @@ module Koala
261
261
 
262
262
  def parse_access_token(response_text)
263
263
  MultiJson.load(response_text)
264
- rescue MultiJson::ParseError
264
+ rescue MultiJson::LoadError
265
265
  response_text.split("&").inject({}) do |hash, bit|
266
266
  key, value = bit.split("=")
267
267
  hash.merge!(key => value)
@@ -1,3 +1,3 @@
1
1
  module Koala
2
- VERSION = "2.0.0"
2
+ VERSION = "2.2.0rc1"
3
3
  end
data/readme.md CHANGED
@@ -1,7 +1,4 @@
1
- [![Build Status](https://secure.travis-ci.org/arsduo/koala.png)](http://travis-ci.org/arsduo/koala)
2
- [![Code Climate](https://codeclimate.com/github/arsduo/koala.png)](https://codeclimate.com/github/arsduo/koala)
3
-
4
- Koala
1
+ Koala [![Version](https://img.shields.io/gem/v/koala.svg)](https://rubygems.org/gems/koala) [![Dependencies](https://img.shields.io/gemnasium/arsduo/koala.svg)](https://gemnasium.com/arsduo/koala) [![Build Status](https://img.shields.io/travis/arsduo/koala.svg)](http://travis-ci.org/arsduo/koala) [![Code Climate](https://img.shields.io/codeclimate/github/arsduo/koala.svg)](https://codeclimate.com/github/arsduo/koala) [![Code Coverage](https://img.shields.io/codeclimate/coverage/github/arsduo/koala.svg)](https://codeclimate.com/github/arsduo/koala)
5
2
  ====
6
3
  [Koala](http://github.com/arsduo/koala) is a Facebook library for Ruby, supporting the Graph API (including the batch requests and photo uploads), the REST API, realtime updates, test users, and OAuth validation. We wrote Koala with four goals:
7
4
 
@@ -43,14 +40,14 @@ Then, go exploring:
43
40
 
44
41
  profile = @graph.get_object("me")
45
42
  friends = @graph.get_connections("me", "friends")
46
- @graph.put_connections("me", "feed", :message => "I am writing on my wall!")
43
+ @graph.put_connections("me", "feed", message: "I am writing on my wall!")
47
44
 
48
45
  # Three-part queries are easy too!
49
46
  @graph.get_connections("me", "mutualfriends/#{friend_id}")
50
47
 
51
48
  # You can use the Timeline API:
52
49
  # (see https://developers.facebook.com/docs/beta/opengraph/tutorial/)
53
- @graph.put_connections("me", "namespace:action", :object => object_url)
50
+ @graph.put_connections("me", "namespace:action", object: object_url)
54
51
 
55
52
  # For extra security (recommended), you can provide an appsecret parameter,
56
53
  # tying your access tokens to your app secret.
@@ -197,7 +194,7 @@ Sometimes, reaching out to Facebook is a pain -- let it reach out to you instead
197
194
 
198
195
  Koala makes it easy to interact with your applications using the RealtimeUpdates class:
199
196
  ```ruby
200
- @updates = Koala::Facebook::RealtimeUpdates.new(:app_id => app_id, :secret => secret)
197
+ @updates = Koala::Facebook::RealtimeUpdates.new(app_id: app_id, secret: secret)
201
198
  ```
202
199
  You can do just about anything with your real-time update subscriptions using the RealtimeUpdates class:
203
200
  ```ruby
@@ -222,7 +219,7 @@ Test Users
222
219
 
223
220
  We also support the test users API, allowing you to conjure up fake users and command them to do your bidding using the Graph or REST API:
224
221
  ```ruby
225
- @test_users = Koala::Facebook::TestUsers.new(:app_id => id, :secret => secret)
222
+ @test_users = Koala::Facebook::TestUsers.new(app_id: id, secret: secret)
226
223
  user = @test_users.create(is_app_installed, desired_permissions)
227
224
  user_graph_api = Koala::Facebook::API.new(user["access_token"])
228
225
  # or, if you want to make a whole community:
@@ -235,10 +232,10 @@ Koala uses Faraday to make HTTP requests, which means you have complete control
235
232
  ```ruby
236
233
  # Set an SSL certificate to avoid Net::HTTP errors
237
234
  Koala.http_service.http_options = {
238
- :ssl => { :ca_path => "/etc/ssl/certs" }
235
+ ssl: { ca_path: "/etc/ssl/certs" }
239
236
  }
240
237
  # or on a per-request basis
241
- @api.get_object(id, args_hash, { :request => { :timeout => 10 } })
238
+ @api.get_object(id, args_hash, { request: { timeout: 10 } })
242
239
  ```
243
240
  The <a href="https://github.com/arsduo/koala/wiki/HTTP-Services">HTTP Services wiki page</a> has more information on what options are available, as well as on how to configure your own Faraday middleware stack (for instance, to implement request logging).
244
241
 
@@ -75,7 +75,7 @@ describe "Koala::Facebook::API" do
75
75
  expect(@service.api('anything', {}, 'get', :http_component => http_component)).to eq(response)
76
76
  end
77
77
 
78
- it "turns arrays of non-enumerables into comma-separated arguments" do
78
+ it "turns arrays of non-enumerables into comma-separated arguments by default" do
79
79
  args = [12345, {:foo => [1, 2, "3", :four]}]
80
80
  expected = ["/12345", {:foo => "1,2,3,four"}, "get", {}]
81
81
  response = double('Mock KoalaResponse', :body => '', :status => 200)
@@ -83,6 +83,26 @@ describe "Koala::Facebook::API" do
83
83
  @service.api(*args)
84
84
  end
85
85
 
86
+ it "can be configured to leave arrays of non-enumerables as is" do
87
+ Koala.configure do |config|
88
+ config.preserve_form_arguments = true
89
+ end
90
+
91
+ args = [12345, {:foo => [1, 2, "3", :four]}]
92
+ expected = ["/12345", {:foo => [1, 2, "3", :four]}, "get", {}]
93
+ response = double('Mock KoalaResponse', :body => '', :status => 200)
94
+ expect(Koala).to receive(:make_request).with(*expected).and_return(response)
95
+ @service.api(*args)
96
+ end
97
+
98
+ it "can be configured on a per-request basis to leave arrays as is" do
99
+ args = [12345, {foo: [1, 2, "3", :four]}, "get", preserve_form_arguments: true]
100
+ expected = ["/12345", {foo: [1, 2, "3", :four]}, "get", preserve_form_arguments: true]
101
+ response = double('Mock KoalaResponse', :body => '', :status => 200)
102
+ expect(Koala).to receive(:make_request).with(*expected).and_return(response)
103
+ @service.api(*args)
104
+ end
105
+
86
106
  it "doesn't turn arrays containing enumerables into comma-separated strings" do
87
107
  params = {:foo => [1, 2, ["3"], :four]}
88
108
  args = [12345, params]
@@ -95,6 +115,14 @@ describe "Koala::Facebook::API" do
95
115
  @service.api(*args)
96
116
  end
97
117
 
118
+ it "doesn't modify any data if the option format of :json is provided" do
119
+ args = [12345, {:foo => [1, 2, "3", :four]}, 'get', format: :json]
120
+ expected = ["/12345", {:foo => [1, 2, "3", :four]}, 'get', format: :json]
121
+ response = double('Mock KoalaResponse', :body => '', :status => 200)
122
+ expect(Koala).to receive(:make_request).with(*expected).and_return(response)
123
+ @service.api(*args)
124
+ end
125
+
98
126
  it "returns the body of the request as JSON if no http_component is given" do
99
127
  response = double('response', :body => 'body', :status => 200)
100
128
  allow(Koala).to receive(:make_request).and_return(response)
@@ -252,6 +252,30 @@ describe Koala::HTTPService do
252
252
  Koala::HTTPService.make_request("anything", {"access_token" => "foo"}, "get", options)
253
253
  end
254
254
 
255
+ it "calls server with a json object when provided a format option for post requests" do
256
+ # Unstub the now somewhat regrettable stubbing above
257
+ allow(Faraday).to receive(:new).and_call_original
258
+
259
+ mock_request_klass = Class.new do
260
+ attr_accessor :path, :body, :headers, :status
261
+ def initialize
262
+ @headers = {}
263
+ end
264
+ end
265
+
266
+ mock_request = mock_request_klass.new
267
+ allow_any_instance_of(Faraday::Connection).to receive(:post).and_yield(mock_request)
268
+
269
+ path = "California"
270
+ args = {:a => 2, :c => "3"}
271
+
272
+ Koala::HTTPService.make_request(path, args, "post", format: :json)
273
+
274
+ expect(mock_request.path).to eq(path)
275
+ expect(mock_request.headers).to eq("Content-Type" => "application/json")
276
+ expect(mock_request.body).to eq(args.to_json)
277
+ end
278
+
255
279
  it "calls server with the composite options" do
256
280
  options = {:a => 2, :c => "3"}
257
281
  http_options = {:a => :a}
@@ -194,6 +194,14 @@ graph_api:
194
194
  post:
195
195
  <<: *token_required
196
196
  with_token: '{"id": "MOCK_PHOTO"}'
197
+ file_url=http://techslides.com/demos/sample-videos/small.mp4:
198
+ post:
199
+ <<: *token_required
200
+ with_token: '{"id": "MOCK_PHOTO_FROM_URL"}'
201
+ description=my message&file_url=http://techslides.com/demos/sample-videos/small.mp4:
202
+ post:
203
+ <<: *token_required
204
+ with_token: '{"id": "MOCK_PHOTO_FROM_URL"}'
197
205
 
198
206
  /koppel:
199
207
  no_args:
@@ -1,3 +1,7 @@
1
+ # Quantify coverage
2
+ require "codeclimate-test-reporter"
3
+ CodeClimate::TestReporter.start
4
+
1
5
  # load the library
2
6
  require 'koala'
3
7
 
@@ -8,4 +12,4 @@ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
8
12
  # load testing data and (if needed) create test users or validate real users
9
13
  KoalaTest.setup_test_environment!
10
14
 
11
- BEACH_BALL_PATH = File.join(File.dirname(__FILE__), "fixtures", "beach.jpg")
15
+ BEACH_BALL_PATH = File.join(File.dirname(__FILE__), "fixtures", "beach.jpg")
@@ -358,6 +358,25 @@ shared_examples_for "Koala GraphAPI with an access token" do
358
358
  # note: Facebook doesn't post videos immediately to the wall, due to processing time
359
359
  # during which get_object(video_id) will return false
360
360
  # hence we can't do the same verify test we do for photos
361
+
362
+
363
+ describe "using a URL instead of a file" do
364
+ before :each do
365
+ @url = "http://techslides.com/demos/sample-videos/small.mp4"
366
+ end
367
+
368
+ it "can post photo to the user's wall using a URL" do
369
+ result = @api.put_video(@url)
370
+ @temporary_object_id = result["id"]
371
+ expect(@temporary_object_id).not_to be_nil
372
+ end
373
+
374
+ it "can post photo to the user's wall using a URL and an additional param" do
375
+ result = @api.put_video(@url, :description => "my message")
376
+ @temporary_object_id = result["id"]
377
+ expect(@temporary_object_id).not_to be_nil
378
+ end
379
+ end
361
380
  end
362
381
 
363
382
  it "can verify a message with an attachment posted to a feed" do
@@ -225,6 +225,7 @@ module KoalaTest
225
225
  VCR.configure do |c|
226
226
  c.cassette_library_dir = 'spec/fixtures/vcr_cassettes'
227
227
  c.hook_into :webmock # or :fakeweb
228
+ c.ignore_hosts 'codeclimate.com' # Allow test coverage to be reported
228
229
  end
229
230
  end
230
231
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: koala
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 2.2.0rc1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alex Koppel
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-05 00:00:00.000000000 Z
11
+ date: 2015-08-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: multi_json
@@ -141,12 +141,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
141
141
  version: '0'
142
142
  required_rubygems_version: !ruby/object:Gem::Requirement
143
143
  requirements:
144
- - - ">="
144
+ - - ">"
145
145
  - !ruby/object:Gem::Version
146
- version: '0'
146
+ version: 1.3.1
147
147
  requirements: []
148
148
  rubyforge_project:
149
- rubygems_version: 2.2.1
149
+ rubygems_version: 2.4.6
150
150
  signing_key:
151
151
  specification_version: 4
152
152
  summary: A lightweight, flexible library for Facebook with support for the Graph API,