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 #212

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
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
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