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

Faraday connection updated #248

Open
wants to merge 25 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
bc786ab
First stab at bringing config connection client
hexgnu Jan 30, 2014
1adc6ef
Verify not verity
hexgnu Jan 30, 2014
2c6c0b4
Caching locally the connection variable
hexgnu Jan 30, 2014
7348501
Move connection into client
hexgnu Jan 30, 2014
8a1908d
WIP Faraday connection
hexgnu Jan 31, 2014
195b64d
Proof of Concept done minus the weird fields issue
hexgnu Jan 31, 2014
45c22b0
Take out puts
hexgnu Feb 3, 2014
3202628
All things passing
hexgnu Feb 3, 2014
ef014d3
Finishing up and giving the ability to add new middleware
hexgnu Feb 3, 2014
40f7823
Take out to_query and to_uri helpers
hexgnu Feb 3, 2014
80222d1
Refactor error reporting
hexgnu Feb 3, 2014
7448309
Use mashify instead of Mash.new
hexgnu Feb 3, 2014
1043769
Take out whitespace
hexgnu Feb 21, 2014
0064f3a
Cleanup brackets on spec
hexgnu Feb 21, 2014
065e0a3
Remove failfast option
hundredwatt Feb 4, 2014
44362bf
Raise multi_json to ~>1.8
hexgnu Feb 21, 2014
4c0a239
Fix SimpleOAuth header generation
hundredwatt Nov 12, 2014
301f1bb
Match class/file naming convention
hundredwatt Nov 12, 2014
95b1131
Update http stubs for #224
hundredwatt Nov 12, 2014
7294454
More updates to http stubs and fix Company API cassette
hundredwatt Nov 12, 2014
60ab359
Build against rbx-2 to match Addressable gem
hundredwatt Nov 12, 2014
1db6a0e
Revert "Build against rbx-2 to match Addressable gem" - didn't fix build
hundredwatt Nov 12, 2014
617ad8d
Let's try and use a specific version of rbx
hexgnu Nov 15, 2014
d839efa
Bump again
hexgnu Nov 15, 2014
e669cc6
Drop rbx
hundredwatt Nov 22, 2014
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
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,3 @@ rvm:
- 2.1.0
- 1.9.3
- jruby-19mode
- rbx
36 changes: 36 additions & 0 deletions lib/linked_in.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
require 'oauth'
require 'linked_in/configuration'

module LinkedIn
extend Configuration
class << self
attr_accessor :token, :secret, :default_profile_fields

# config/initializers/linkedin.rb (for instance)
#
# ```ruby
# LinkedIn.configure do |config|
# config.token = 'consumer_token'
# config.secret = 'consumer_secret'
# config.default_profile_fields = ['educations', 'positions']
# end
# ```
# elsewhere
#
# ```ruby
# client = LinkedIn::Client.new
# ```
def configure
yield self
true
end
end

autoload :Api, "linked_in/api"
autoload :Client, "linked_in/client"
autoload :Mash, "linked_in/mash"
autoload :Errors, "linked_in/errors"
autoload :Helpers, "linked_in/helpers"
autoload :Search, "linked_in/search"
autoload :Version, "linked_in/version"
end
2 changes: 1 addition & 1 deletion lib/linked_in/api/communications.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ module Communications
# message
# @return [void]
def send_message(subject, body, recipient_paths)
path = "/people/~/mailbox"
path = "people/~/mailbox"

message = {
'subject' => subject,
Expand Down
6 changes: 3 additions & 3 deletions lib/linked_in/api/companies.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def company_updates_likes(update_key, options={})
# @macro share_input_fields
# @return [void]
def add_company_share(company_id, share)
path = "/companies/#{company_id}/shares"
path = "companies/#{company_id}/shares"
defaults = {:visibility => {:code => "anyone"}}
post(path, MultiJson.dump(defaults.merge(share)), "Content-Type" => "application/json")
end
Expand All @@ -108,7 +108,7 @@ def add_company_share(company_id, share)
# @param [String] company_id Company ID
# @return [void]
def follow_company(company_id)
path = "/people/~/following/companies"
path = "people/~/following/companies"
body = {:id => company_id }
post(path, MultiJson.dump(body), "Content-Type" => "application/json")
end
Expand All @@ -120,7 +120,7 @@ def follow_company(company_id)
# @param [String] company_id Company ID
# @return [void]
def unfollow_company(company_id)
path = "/people/~/following/companies/id=#{company_id}"
path = "people/~/following/companies/id=#{company_id}"
delete(path)
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/linked_in/api/groups.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def post_group_discussion(group_id, discussion)
# @macro share_input_fields
# @return [void]
def add_group_share(group_id, share)
path = "/groups/#{group_id}/posts"
path = "groups/#{group_id}/posts"
post(path, MultiJson.dump(share), "Content-Type" => "application/json")
end

Expand All @@ -106,7 +106,7 @@ def add_group_share(group_id, share)
# @param [String] group_id Group ID
# @return [void]
def join_group(group_id)
path = "/people/~/group-memberships/#{group_id}"
path = "people/~/group-memberships/#{group_id}"
body = {'membership-state' => {'code' => 'member' }}
put(path, MultiJson.dump(body), "Content-Type" => "application/json")
end
Expand Down
2 changes: 1 addition & 1 deletion lib/linked_in/api/jobs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def job_suggestions(options = {})
# @param [String] job_id Job ID
# @return [void]
def add_job_bookmark(job_id)
path = "/people/~/job-bookmarks"
path = "people/~/job-bookmarks"
body = {'job' => {'id' => job_id}}
post(path, MultiJson.dump(body), "Content-Type" => "application/json")
end
Expand Down
14 changes: 7 additions & 7 deletions lib/linked_in/api/query_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module QueryHelpers
private

def group_path(options)
path = "/groups"
path = "groups"
if id = options.delete(:id)
path += "/#{id}"
end
Expand All @@ -21,10 +21,10 @@ def simple_query(path, options={})
end

headers = options.delete(:headers) || {}
params = to_query(options)
path += "#{path.include?("?") ? "&" : "?"}#{params}" if !params.empty?
# params = to_query(options)
# path += "#{path.include?("?") ? "&" : "?"}#{params}" if !params.empty?

Mash.from_json(get(path, headers))
get(path, options, headers)
end

def build_fields_params(fields)
Expand All @@ -38,7 +38,7 @@ def build_fields_params(fields)
end

def person_path(options)
path = "/people"
path = "people"
if id = options.delete(:id)
path += "/id=#{id}"
elsif url = options.delete(:url)
Expand All @@ -51,7 +51,7 @@ def person_path(options)
end

def company_path(options)
path = "/companies"
path = "companies"

if domain = options.delete(:domain)
path += "?email-domain=#{CGI.escape(domain)}"
Expand All @@ -74,7 +74,7 @@ def picture_urls_path(options)
end

def jobs_path(options)
path = "/jobs"
path = "jobs"
if id = options.delete(:id)
path += "/id=#{id}"
else
Expand Down
8 changes: 4 additions & 4 deletions lib/linked_in/api/share_and_social_stream.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def share_likes(update_key, options={})
# @macro share_input_fields
# @return [void]
def add_share(share)
path = "/people/~/shares"
path = "people/~/shares"
defaults = {:visibility => {:code => "anyone"}}
post(path, MultiJson.dump(defaults.merge(share)), "Content-Type" => "application/json")
end
Expand All @@ -99,7 +99,7 @@ def add_share(share)
# @param [String] comment The text of the comment
# @return [void]
def update_comment(update_key, comment)
path = "/people/~/network/updates/key=#{update_key}/update-comments"
path = "people/~/network/updates/key=#{update_key}/update-comments"
body = {'comment' => comment}
post(path, MultiJson.dump(body), "Content-Type" => "application/json")
end
Expand All @@ -112,7 +112,7 @@ def update_comment(update_key, comment)
# particular network update
# @return [void]
def like_share(update_key)
path = "/people/~/network/updates/key=#{update_key}/is-liked"
path = "people/~/network/updates/key=#{update_key}/is-liked"
put(path, 'true', "Content-Type" => "application/json")
end

Expand All @@ -125,7 +125,7 @@ def like_share(update_key)
# particular network update
# @return [void]
def unlike_share(update_key)
path = "/people/~/network/updates/key=#{update_key}/is-liked"
path = "people/~/network/updates/key=#{update_key}/is-liked"
put(path, 'false', "Content-Type" => "application/json")
end
end
Expand Down
68 changes: 45 additions & 23 deletions lib/linked_in/client.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
require 'cgi'
require 'faraday'
require 'faraday_middleware'

module LinkedIn

Expand All @@ -21,30 +23,50 @@ def initialize(ctoken=LinkedIn.token, csecret=LinkedIn.secret, options={})
@consumer_secret = csecret
@consumer_options = options
end
private

#
# def current_status
# path = "/people/~/current-status"
# Crack::XML.parse(get(path))['current_status']
# end
#
# def network_statuses(options={})
# options[:type] = 'STAT'
# network_updates(options)
# end
#
# def network_updates(options={})
# path = "/people/~/network"
# Network.from_xml(get(to_uri(path, options)))
# end
#
# # helpful in making authenticated calls and writing the
# # raw xml to a fixture file
# def write_fixture(path, filename)
# file = File.new("test/fixtures/#{filename}", "w")
# file.puts(access_token.get(path).body)
# file.close
# end
def authentication
{
:consumer_key => consumer_token,
:consumer_secret => consumer_secret,
:token => @auth_token,
:token_secret => @auth_secret,
}
end

def authenticated?
authentication.values.all?
end

def connection_options
LinkedIn.faraday_options.merge({
:headers => {
'Accept' => "application/#{LinkedIn.format}",
'User-Agent' => LinkedIn.user_agent,
'x-li-format' => 'json'
},
:proxy => LinkedIn.proxy,
:ssl => {:verify => false},
:url => LinkedIn.endpoint
})
end

def connection
raise "Please authenticate first" unless authenticated?

@connection ||= Faraday.new(connection_options) do |builder|
builder.use ::Faraday::Request::OAuth, authentication.merge(:ignore_extra_keys => true)
builder.use ::Faraday::Request::UrlEncoded
builder.use ::FaradayMiddleware::Mashify, :mash_class => LinkedIn::Mash
builder.use ::Faraday::Response::ParseJson

LinkedIn.middleware.each do |middle|
builder.use middle
end

builder.adapter(LinkedIn.adapter)
end
end

end

Expand Down
104 changes: 104 additions & 0 deletions lib/linked_in/configuration.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
require 'linked_in/version'
require 'faraday'
require 'faraday_middleware'
require 'linked_in/middleware/error_reporter'

module LinkedIn
# Defines constants and methods related to configuration
module Configuration
# An array of valid keys in the options hash when configuring a {LinkedIn::API}
VALID_OPTIONS_KEYS = [
:adapter,
:consumer_key,
:consumer_secret,
:default_profile_fields,
:endpoint,
:format,
:gateway,
:oauth_token,
:oauth_token_secret,
:proxy,
:user_agent,
:faraday_options,
:middleware].freeze

# The adapter that will be used to connect if none is set
DEFAULT_ADAPTER = :net_http

# By default, don't set an application key
DEFAULT_CONSUMER_KEY = nil

# By default, don't set an application secret
DEFAULT_CONSUMER_SECRET = nil

# The endpoint that will be used to connect if none is set
#
DEFAULT_ENDPOINT = 'https://api.linkedin.com/'.freeze

# The response format appended to the path and sent in the 'Accept' header if none is set
#
# @note JSON is preferred over XML because it is more concise and faster to parse.
DEFAULT_FORMAT = :json

# By default, don't set a user oauth token
DEFAULT_OAUTH_TOKEN = nil

# By default, don't set a user oauth secret
DEFAULT_OAUTH_TOKEN_SECRET = nil

# By default, don't set any profile fields
DEFAULT_PROFILE_FIELDS = nil

# By default, don't use a proxy server
DEFAULT_PROXY = nil

# The value sent in the 'User-Agent' header if none is set
DEFAULT_USER_AGENT = "LinkedIn Ruby Gem #{LinkedIn::VERSION}".freeze

DEFAULT_GATEWAY = nil

DEFAULT_FARADAY_OPTIONS = {}.freeze

DEFAULT_MIDDLEWARE = [
::LinkedIn::ErrorReporter
]

# @private
attr_accessor *VALID_OPTIONS_KEYS

# When this module is extended, set all configuration options to their default values
def self.extended(base)
base.reset
end

# Convenience method to allow configuration options to be set in a block
def configure
yield self
end

# Create a hash of options and their values
def options
options = {}
VALID_OPTIONS_KEYS.each{|k| options[k] = send(k)}
options
end

# Reset all configuration options to defaults
def reset
self.adapter = DEFAULT_ADAPTER
self.consumer_key = DEFAULT_CONSUMER_KEY
self.consumer_secret = DEFAULT_CONSUMER_SECRET
self.default_profile_fields = DEFAULT_PROFILE_FIELDS
self.endpoint = DEFAULT_ENDPOINT
self.format = DEFAULT_FORMAT
self.oauth_token = DEFAULT_OAUTH_TOKEN
self.oauth_token_secret = DEFAULT_OAUTH_TOKEN_SECRET
self.proxy = DEFAULT_PROXY
self.user_agent = DEFAULT_USER_AGENT
self.gateway = DEFAULT_GATEWAY
self.faraday_options = DEFAULT_FARADAY_OPTIONS
self.middleware = DEFAULT_MIDDLEWARE
self
end
end
end
8 changes: 1 addition & 7 deletions lib/linked_in/errors.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
module LinkedIn
module Errors
class LinkedInError < StandardError
attr_reader :data
def initialize(data)
@data = data
super
end
end
class LinkedInError < StandardError; end

# Raised when a 401 response status code is received
class UnauthorizedError < LinkedInError; end
Expand Down
Loading