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

[WIP] OH integration #438

Merged
merged 38 commits into from Nov 21, 2017

Conversation

Projects
None yet
4 participants
@gedankenstuecke
Member

gedankenstuecke commented Nov 14, 2017

General API calls to make a connection on our end now works. Some to-dos before merging:

  • Fix upload of our details to Open Humans
  • check whether file on OH needs to be deleted first.
  • Make nicer views
  • Make Hound & CodeClimate happy
  • Add graphics to user profiles and /openhumans/.
  • Add connection to OH as phenotype to our data dumps.
  • Write tests
def self.down
drop_table :open_humans_profiles
end

This comment has been minimized.

@houndci-bot

houndci-bot Nov 14, 2017

Collaborator

Extra empty line detected at class body end.

@@ -0,0 +1,19 @@
# frozen_string_literal: true
class CreateOpenHumansProfiles < ActiveRecord::Migration

This comment has been minimized.

@houndci-bot

houndci-bot Nov 14, 2017

Collaborator

Add an empty line after magic comments.

@@ -0,0 +1,4 @@
# frozen_string_literal: true
class OpenHumansProfile < ActiveRecord::Base

This comment has been minimized.

@houndci-bot

houndci-bot Nov 14, 2017

Collaborator

Add an empty line after magic comments.
Models should subclass ApplicationRecord.

user_id: @user.id,
has_sequence: @user.has_sequence,
user_uri: "https://opensnp.org/users/#{@user.id}"}
return json

This comment has been minimized.

@houndci-bot

houndci-bot Nov 14, 2017

Collaborator

Redundant return detected.

json = {user_name: @user.name,
user_id: @user.id,
has_sequence: @user.has_sequence,
user_uri: "https://opensnp.org/users/#{@user.id}"}

This comment has been minimized.

@houndci-bot

houndci-bot Nov 14, 2017

Collaborator

Space inside } missing.

post_body << "Content-Disposition: form-data; name=\"metadata\"\r\n\r\n"
post_body << metadata.to_json
post_body << "\r\n--#{@boundary}\r\n"
post_body << "Content-Disposition: form-data; name=\"data_file\"; filename=\"#{@user.id}\"\r\n\r\n"

This comment has been minimized.

@houndci-bot

houndci-bot Nov 14, 2017

Collaborator

Line is too long. [103/100]

puts uri
@boundary = "AaB03x"
header = {"Content-Type": "multipart/form-data; boundary=#{@boundary}"}

This comment has been minimized.

@houndci-bot

houndci-bot Nov 14, 2017

Collaborator

Space inside { missing.
Space inside } missing.

uri = URI.parse("https://www.openhumans.org/api/project/upload/?access_token=#{@user.open_humans_profile.access_token}")
puts uri
@boundary = "AaB03x"

This comment has been minimized.

@houndci-bot

houndci-bot Nov 14, 2017

Collaborator

Prefer single-quoted strings when you don't need string interpolation or special symbols.

metadata["description"] = "links to openSNP user #{@user.id}"
uri = URI.parse("https://www.openhumans.org/api/project/upload/?access_token=#{@user.open_humans_profile.access_token}")
puts uri

This comment has been minimized.

@houndci-bot

houndci-bot Nov 14, 2017

Collaborator

Do not write to stdout. Use Rails's logger if you want to log.

metadata["tags"] = ['opensnp']
metadata["description"] = "links to openSNP user #{@user.id}"
uri = URI.parse("https://www.openhumans.org/api/project/upload/?access_token=#{@user.open_humans_profile.access_token}")

This comment has been minimized.

@houndci-bot

houndci-bot Nov 14, 2017

Collaborator

Line is too long. [124/100]

def self.down
drop_table :open_humans_profiles
end

This comment has been minimized.

@houndci-bot

houndci-bot Nov 14, 2017

Collaborator

Extra empty line detected at class body end.

@@ -0,0 +1,19 @@
# frozen_string_literal: true
class CreateOpenHumansProfiles < ActiveRecord::Migration

This comment has been minimized.

@houndci-bot

houndci-bot Nov 14, 2017

Collaborator

Add an empty line after magic comments.

@@ -0,0 +1,4 @@
# frozen_string_literal: true
class OpenHumansProfile < ActiveRecord::Base

This comment has been minimized.

@houndci-bot

houndci-bot Nov 14, 2017

Collaborator

Add an empty line after magic comments.
Models should subclass ApplicationRecord.

user_id: @user.id,
has_sequence: @user.has_sequence,
user_uri: "https://opensnp.org/users/#{@user.id}"}
return json

This comment has been minimized.

@houndci-bot

houndci-bot Nov 14, 2017

Collaborator

Redundant return detected.

json = {user_name: @user.name,
user_id: @user.id,
has_sequence: @user.has_sequence,
user_uri: "https://opensnp.org/users/#{@user.id}"}

This comment has been minimized.

@houndci-bot

houndci-bot Nov 14, 2017

Collaborator

Space inside } missing.

post_body << "Content-Disposition: form-data; name=\"metadata\"\r\n\r\n"
post_body << metadata.to_json
post_body << "\r\n--#{@boundary}\r\n"
post_body << "Content-Disposition: form-data; name=\"data_file\"; filename=\"#{@user.id}\"\r\n\r\n"

This comment has been minimized.

@houndci-bot

houndci-bot Nov 14, 2017

Collaborator

Line is too long. [103/100]

puts uri
@boundary = "AaB03x"
header = {"Content-Type": "multipart/form-data; boundary=#{@boundary}"}

This comment has been minimized.

@houndci-bot

houndci-bot Nov 14, 2017

Collaborator

Space inside { missing.
Space inside } missing.

uri = URI.parse("https://www.openhumans.org/api/project/upload/?access_token=#{@user.open_humans_profile.access_token}")
puts uri
@boundary = "AaB03x"

This comment has been minimized.

@houndci-bot

houndci-bot Nov 14, 2017

Collaborator

Prefer single-quoted strings when you don't need string interpolation or special symbols.

metadata["description"] = "links to openSNP user #{@user.id}"
uri = URI.parse("https://www.openhumans.org/api/project/upload/?access_token=#{@user.open_humans_profile.access_token}")
puts uri

This comment has been minimized.

@houndci-bot

houndci-bot Nov 14, 2017

Collaborator

Do not write to stdout. Use Rails's logger if you want to log.

metadata["tags"] = ['opensnp']
metadata["description"] = "links to openSNP user #{@user.id}"
uri = URI.parse("https://www.openhumans.org/api/project/upload/?access_token=#{@user.open_humans_profile.access_token}")

This comment has been minimized.

@houndci-bot

houndci-bot Nov 14, 2017

Collaborator

Line is too long. [124/100]

@gedankenstuecke

This comment has been minimized.

Member

gedankenstuecke commented Nov 14, 2017

Does Travis fail because I didn't update the db/structure.sql in the repo? @tsujigiri

response = http.request(upload)
end
def generate_json()

This comment has been minimized.

@houndci-bot

houndci-bot Nov 14, 2017

Collaborator

Omit the parentheses in defs when the method doesn't accept any arguments.

upload = Net::HTTP::Post.new(uri.request_uri)
upload.content_type = "multipart/form-data; boundary=#{@boundary}"
upload.body = post_body.join
response = http.request(upload)

This comment has been minimized.

@houndci-bot

houndci-bot Nov 14, 2017

Collaborator

Useless assignment to variable - response.

post_body << "\r\n\r\n--#{@boundary}--\r\n"
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = (uri.scheme == "https")

This comment has been minimized.

@houndci-bot

houndci-bot Nov 14, 2017

Collaborator

Prefer single-quoted strings when you don't need string interpolation or special symbols.

post_body << "Content-Disposition: form-data; name=\"metadata\"\r\n\r\n"
post_body << metadata.to_json
post_body << "\r\n--#{@boundary}\r\n"
post_body << "Content-Disposition: form-data; name=\"data_file\"; filename=\"#{@user.id}.json\"\r\n\r\n"

This comment has been minimized.

@houndci-bot

houndci-bot Nov 14, 2017

Collaborator

Line is too long. [108/100]

metadata["description"] = "links to openSNP user #{@user.id}"
uri = URI.parse("https://www.openhumans.org/api/direct-sharing/project/files/upload/?access_token=#{@user.open_humans_profile.access_token}")
@boundary = "111222XXX222111"

This comment has been minimized.

@houndci-bot

houndci-bot Nov 14, 2017

Collaborator

Prefer single-quoted strings when you don't need string interpolation or special symbols.

metadata["tags"] = ['opensnp']
metadata["description"] = "links to openSNP user #{@user.id}"
uri = URI.parse("https://www.openhumans.org/api/direct-sharing/project/files/upload/?access_token=#{@user.open_humans_profile.access_token}")

This comment has been minimized.

@houndci-bot

houndci-bot Nov 14, 2017

Collaborator

Line is too long. [145/100]

user_info = generate_json
metadata = user_info
metadata["tags"] = ['opensnp']
metadata["description"] = "links to openSNP user #{@user.id}"

This comment has been minimized.

@houndci-bot

houndci-bot Nov 14, 2017

Collaborator

Prefer single-quoted strings when you don't need string interpolation or special symbols.

@user = current_user
user_info = generate_json
metadata = user_info
metadata["tags"] = ['opensnp']

This comment has been minimized.

@houndci-bot

houndci-bot Nov 14, 2017

Collaborator

Prefer single-quoted strings when you don't need string interpolation or special symbols.

uri.query = URI.encode_www_form(uri_params)
res = Net::HTTP.get_response(uri)
res_json = JSON.parse(res.body)
return res_json

This comment has been minimized.

@houndci-bot

houndci-bot Nov 14, 2017

Collaborator

Redundant return detected.

def get_open_humans_ids(access_token)
uri = URI.parse('https://www.openhumans.org/api/direct-sharing/project/exchange-member/')
uri_params = {:access_token => access_token}

This comment has been minimized.

@houndci-bot

houndci-bot Nov 14, 2017

Collaborator

Space inside { missing.
Use the new Ruby 1.9 hash syntax.
Space inside } missing.

response = http.request(upload)
end
def generate_json()

This comment has been minimized.

@houndci-bot

houndci-bot Nov 14, 2017

Collaborator

Omit the parentheses in defs when the method doesn't accept any arguments.

upload = Net::HTTP::Post.new(uri.request_uri)
upload.content_type = "multipart/form-data; boundary=#{@boundary}"
upload.body = post_body.join
response = http.request(upload)

This comment has been minimized.

@houndci-bot

houndci-bot Nov 14, 2017

Collaborator

Useless assignment to variable - response.

post_body << "\r\n\r\n--#{@boundary}--\r\n"
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = (uri.scheme == "https")

This comment has been minimized.

@houndci-bot

houndci-bot Nov 14, 2017

Collaborator

Prefer single-quoted strings when you don't need string interpolation or special symbols.

post_body << "Content-Disposition: form-data; name=\"metadata\"\r\n\r\n"
post_body << metadata.to_json
post_body << "\r\n--#{@boundary}\r\n"
post_body << "Content-Disposition: form-data; name=\"data_file\"; filename=\"#{@user.id}.json\"\r\n\r\n"

This comment has been minimized.

@houndci-bot

houndci-bot Nov 14, 2017

Collaborator

Line is too long. [108/100]

metadata["description"] = "links to openSNP user #{@user.id}"
uri = URI.parse("https://www.openhumans.org/api/direct-sharing/project/files/upload/?access_token=#{@user.open_humans_profile.access_token}")
@boundary = "111222XXX222111"

This comment has been minimized.

@houndci-bot

houndci-bot Nov 14, 2017

Collaborator

Prefer single-quoted strings when you don't need string interpolation or special symbols.

metadata["tags"] = ['opensnp']
metadata["description"] = "links to openSNP user #{@user.id}"
uri = URI.parse("https://www.openhumans.org/api/direct-sharing/project/files/upload/?access_token=#{@user.open_humans_profile.access_token}")

This comment has been minimized.

@houndci-bot

houndci-bot Nov 14, 2017

Collaborator

Line is too long. [145/100]

user_info = generate_json
metadata = user_info
metadata["tags"] = ['opensnp']
metadata["description"] = "links to openSNP user #{@user.id}"

This comment has been minimized.

@houndci-bot

houndci-bot Nov 14, 2017

Collaborator

Prefer single-quoted strings when you don't need string interpolation or special symbols.

@user = current_user
user_info = generate_json
metadata = user_info
metadata["tags"] = ['opensnp']

This comment has been minimized.

@houndci-bot

houndci-bot Nov 14, 2017

Collaborator

Prefer single-quoted strings when you don't need string interpolation or special symbols.

uri.query = URI.encode_www_form(uri_params)
res = Net::HTTP.get_response(uri)
res_json = JSON.parse(res.body)
return res_json

This comment has been minimized.

@houndci-bot

houndci-bot Nov 14, 2017

Collaborator

Redundant return detected.

def get_open_humans_ids(access_token)
uri = URI.parse('https://www.openhumans.org/api/direct-sharing/project/exchange-member/')
uri_params = {:access_token => access_token}

This comment has been minimized.

@houndci-bot

houndci-bot Nov 14, 2017

Collaborator

Space inside { missing.
Use the new Ruby 1.9 hash syntax.
Space inside } missing.

@@ -0,0 +1,5 @@
# frozen_string_literal: true
class OpenHumansProfile < ActiveRecord::Base

This comment has been minimized.

@houndci-bot

houndci-bot Nov 14, 2017

Collaborator

Models should subclass ApplicationRecord.

This comment has been minimized.

@tsujigiri

tsujigiri Nov 14, 2017

Collaborator

For that we'd need Rails5, first... 🐩

This comment has been minimized.

@gedankenstuecke

gedankenstuecke Nov 14, 2017

Member

What exactly do you mean here? Follows the same convention as all other models?

This comment has been minimized.

@tsujigiri

tsujigiri Nov 14, 2017

Collaborator

Rubocop wants the models to subclass a common ApplicationRecord class. I tried doing this in another project using Rails 4 and it didn't work. But, thinking about it, this may have been due to some gem we are using. Long story short, just ignore it for now. We can do this separately.

This comment has been minimized.

@gedankenstuecke
def generate_json
@user = current_user
json = {user_name: @user.name,

This comment has been minimized.

@houndci-bot

houndci-bot Nov 14, 2017

Collaborator

Space inside { missing.

metadata['tags'] = ['opensnp']
metadata['description'] = "links to openSNP user #{@user.id}"
uri = URI.parse("https://www.openhumans.org/api/direct-sharing/project/files/upload/?access_token=#{@user.open_humans_profile.access_token}")

This comment has been minimized.

@houndci-bot

houndci-bot Nov 14, 2017

Collaborator

Line is too long. [145/100]

http.use_ssl = (url.scheme == 'https')
response = http.request(req)
response_json = JSON.parse(response.body)
return response_json

This comment has been minimized.

@houndci-bot

houndci-bot Nov 14, 2017

Collaborator

Redundant return detected.

req.set_form_data({'grant_type' => 'authorization_code',
'code' => code,
'redirect_uri' => "http://localhost:3000/openhumans/authorize"
})

This comment has been minimized.

@houndci-bot

houndci-bot Nov 14, 2017

Collaborator

Closing hash brace must be on the same line as the last hash element when opening brace is on the same line as the first hash element.

req.basic_auth ENV.fetch('OH_client_id'), ENV.fetch('OH_client_secret')
req.set_form_data({'grant_type' => 'authorization_code',
'code' => code,
'redirect_uri' => "http://localhost:3000/openhumans/authorize"

This comment has been minimized.

@houndci-bot

houndci-bot Nov 14, 2017

Collaborator

Prefer single-quoted strings when you don't need string interpolation or special symbols.

url = URI.parse('https://www.openhumans.org/oauth2/token/')
req = Net::HTTP::Post.new(url.request_uri)
req.basic_auth ENV.fetch('OH_client_id'), ENV.fetch('OH_client_secret')
req.set_form_data({'grant_type' => 'authorization_code',

This comment has been minimized.

@houndci-bot

houndci-bot Nov 14, 2017

Collaborator

Space inside { missing.
Redundant curly braces around a hash parameter.

@oh_profile.open_humans_user_id = user_ids["username"]
@oh_profile.save
upload_opensnp_id

This comment has been minimized.

@houndci-bot

houndci-bot Nov 14, 2017

Collaborator

Extra empty line detected at method body end.

user_ids = get_open_humans_ids(@oh_profile.access_token)
@oh_profile.project_member_id = user_ids["project_member_id"]
@oh_profile.open_humans_user_id = user_ids["username"]

This comment has been minimized.

@houndci-bot

houndci-bot Nov 14, 2017

Collaborator

Prefer single-quoted strings when you don't need string interpolation or special symbols.

@oh_profile.save
upload_opensnp_id
end

This comment has been minimized.

@tsujigiri

tsujigiri Nov 14, 2017

Collaborator

You don't need instance variables if you don't use them outside of the method. Also, since this is OAuth, why not use one of the numerous OAuth client libs instead of hand-rolling it? Also... 😉 It's good practice to put most of the logic of, like, everything in service classes instead of stuffing the controllers like a christmas goose. 🦆

This comment has been minimized.

@gedankenstuecke

gedankenstuecke Nov 14, 2017

Member

Do you have a recommendation for an OAuth client lib? The ones I saw while googling all felt a bit weird to me somehow, that's why I ended up writing it myself.

But the service classes are a good idea. I'll refactor accordingly 👍

This comment has been minimized.

@tsujigiri

tsujigiri Nov 14, 2017

Collaborator

I haven't looked into it for a while, so I'm not sure. It just seems like re-inventing the wheel.

@@ -0,0 +1,5 @@
# frozen_string_literal: true
class OpenHumansProfile < ActiveRecord::Base

This comment has been minimized.

@tsujigiri

tsujigiri Nov 14, 2017

Collaborator

For that we'd need Rails5, first... 🐩

@tsujigiri

This comment has been minimized.

Collaborator

tsujigiri commented Nov 14, 2017

@gedankenstuecke Yes, you need to commit the changed db/structure.sql. There's also this, but I don't know if it's a problem.

post_body << "Content-Disposition: form-data; name=\"metadata\"\r\n\r\n"
post_body << metadata.to_json
post_body << "\r\n--#{boundary}\r\n"
post_body << "Content-Disposition: form-data; name=\"data_file\"; filename=\"#{user.id}.json\"\r\n\r\n"

This comment has been minimized.

@houndci-bot

houndci-bot Nov 15, 2017

Collaborator

Line is too long. [107/100]

return metadata
end
def generate_form_body(user,boundary)

This comment has been minimized.

@houndci-bot

houndci-bot Nov 15, 2017

Collaborator

Space missing after comma.

metadata = generate_json(user)
metadata['tags'] = ['opensnp']
metadata['description'] = "links to openSNP user #{user.id}"
return metadata

This comment has been minimized.

@houndci-bot

houndci-bot Nov 15, 2017

Collaborator

Redundant return detected.

This comment has been minimized.

@gedankenstuecke

gedankenstuecke Nov 15, 2017

Member

this is actually not redundant as it otherwise breaks the hash…

This comment has been minimized.

@tsujigiri

tsujigiri Nov 15, 2017

Collaborator

This seems to be pretty redundant, as it is the last expression in the method. Am I missing something?

This comment has been minimized.

@gedankenstuecke

gedankenstuecke Nov 15, 2017

Member

I don't know. If I'm not running the return it only returns "links to openSNP user #{user.id}" instead of the whole hash. That's why I put it back in, otherwise the OH API doesn't accept our upload.

This comment has been minimized.

@tsujigiri

tsujigiri Nov 16, 2017

Collaborator

Aaaaah! Now I get it. 💡 You don't need the return, but you still need the metadata. Without the metadata in the end, it returns the return value of the assignment above it, which is the value you assign.

user_id: user.id,
has_sequence: user.has_sequence,
user_uri: "https://opensnp.org/users/#{user.id}"
}

This comment has been minimized.

@houndci-bot

houndci-bot Nov 15, 2017

Collaborator

Indent the right brace the same as the start of the line where the left brace is.

def generate_json(user)
json = {
user_name: user.name,

This comment has been minimized.

@houndci-bot

houndci-bot Nov 15, 2017

Collaborator

Use 2 spaces for indentation in a hash, relative to the start of the line where the left curly brace is.

private
def update_tokens(oh_profile,tokens)

This comment has been minimized.

@houndci-bot

houndci-bot Nov 15, 2017

Collaborator

Space missing after comma.

req.set_form_data({
project_member_id: oh_profile.project_member_id,
all_files: true
})

This comment has been minimized.

@houndci-bot

houndci-bot Nov 15, 2017

Collaborator

Indent the right brace the same as the first position after the preceding left parenthesis.

req = Net::HTTP::Post.new(url.request_uri)
oh_profile = user.open_humans_profile
req.set_form_data({
project_member_id: oh_profile.project_member_id,

This comment has been minimized.

@houndci-bot

houndci-bot Nov 15, 2017

Collaborator

Use 2 spaces for indentation in a hash, relative to the first position after the preceding left parenthesis.

url = URI.parse("https://www.openhumans.org/api/direct-sharing/project/files/delete/?access_token=#{user.open_humans_profile.access_token}")
req = Net::HTTP::Post.new(url.request_uri)
oh_profile = user.open_humans_profile
req.set_form_data({

This comment has been minimized.

@houndci-bot

houndci-bot Nov 15, 2017

Collaborator

Redundant curly braces around a hash parameter.

end
def delete_opensnp_id(user)
url = URI.parse("https://www.openhumans.org/api/direct-sharing/project/files/delete/?access_token=#{user.open_humans_profile.access_token}")

This comment has been minimized.

@houndci-bot

houndci-bot Nov 15, 2017

Collaborator

Line is too long. [144/100]

post_body << "Content-Disposition: form-data; name=\"metadata\"\r\n\r\n"
post_body << metadata.to_json
post_body << "\r\n--#{boundary}\r\n"
post_body << "Content-Disposition: form-data; "

This comment has been minimized.

@houndci-bot

houndci-bot Nov 15, 2017

Collaborator

Prefer single-quoted strings when you don't need string interpolation or special symbols.

This comment has been minimized.

@gedankenstuecke

gedankenstuecke Nov 15, 2017

Member

breaks the form if i do that…

This comment has been minimized.

@tsujigiri

tsujigiri Nov 15, 2017

Collaborator

Are you sure this is the only format the API accepts? This is usually what the browser generates from a form. If so, maybe you could put the whole thing in some form of template or a heredoc:

def generate_form_body(user, boundary)
  metadata = generate_metadata(user)
  file_content = generate_json(user)
  <<-POST_BODY.strip_heredoc
    --#{boundary}\r
    Content-Disposition: form-data; name="project_member_id"\r\n\r
    #{user.open_humans_profile.project_member_id}\r
    --#{boundary}\r
    Content-Disposition: form-data; name="metadata"\r\n\r
    #{metadata.to_json}\r
    --#{boundary}\r
    Content-Disposition: form-data; name="data_file"; filename="#{user.id}.json"\r\n\r
    #{file_content.to_json}\r
    --#{boundary}--\r\n\r
  POST_BODY
end

This comment has been minimized.

@gedankenstuecke

gedankenstuecke Nov 15, 2017

Member

you're right on this, it works! Thanks! 😍

user_id: user.id,
has_sequence: user.has_sequence,
user_uri: "https://opensnp.org/users/#{user.id}"
}

This comment has been minimized.

@houndci-bot

houndci-bot Nov 15, 2017

Collaborator

Closing hash brace must be on the same line as the last hash element when opening brace is on the same line as the first hash element.

end
def generate_json(user)
json = { user_name: user.name,

This comment has been minimized.

@houndci-bot

houndci-bot Nov 15, 2017

Collaborator

Useless assignment to variable - json.

url = URI.parse(base_url + user.open_humans_profile.access_token)
req = Net::HTTP::Post.new(url.request_uri)
oh_profile = user.open_humans_profile
req.set_form_data({

This comment has been minimized.

@houndci-bot

houndci-bot Nov 15, 2017

Collaborator

Redundant curly braces around a hash parameter.

upload.content_type = "multipart/form-data; boundary=#{boundary}"
post_body = generate_form_body(user,boundary)
upload.body = post_body.join
puts post_body.join

This comment has been minimized.

@houndci-bot

houndci-bot Nov 15, 2017

Collaborator

Do not write to stdout. Use Rails's logger if you want to log.

end
def set_open_humans_ids(oh_profile)

This comment has been minimized.

@houndci-bot

houndci-bot Nov 15, 2017

Collaborator

Use empty lines between method definitions.

update_tokens(oh_profile,response_json)
end

This comment has been minimized.

@houndci-bot

houndci-bot Nov 15, 2017

Collaborator

Extra blank line detected.

http.use_ssl = (url.scheme == 'https')
response = http.request(req)
response_json = JSON.parse(response.body)
update_tokens(oh_profile,response_json)

This comment has been minimized.

@houndci-bot

houndci-bot Nov 15, 2017

Collaborator

Space missing after comma.

req = Net::HTTP::Post.new(url.request_uri)
req.set_form_data({'grant_type' => 'refresh_token',
'refresh_token' => oh_profile.refresh_token
})

This comment has been minimized.

@houndci-bot

houndci-bot Nov 15, 2017

Collaborator

Closing hash brace must be on the same line as the last hash element when opening brace is on the same line as the first hash element.

oh_profile = user.open_humans_profile
url = URI.parse('https://www.openhumans.org/oauth2/token/')
req = Net::HTTP::Post.new(url.request_uri)
req.set_form_data({'grant_type' => 'refresh_token',

This comment has been minimized.

@houndci-bot

houndci-bot Nov 15, 2017

Collaborator

Space inside { missing.
Redundant curly braces around a hash parameter.

post_body << "Content-Disposition: form-data; name=\"metadata\"\r\n\r\n"
post_body << metadata.to_json
post_body << "\r\n--#{boundary}\r\n"
post_body << "Content-Disposition: form-data; "

This comment has been minimized.

@houndci-bot

houndci-bot Nov 15, 2017

Collaborator

Prefer single-quoted strings when you don't need string interpolation or special symbols.

This comment has been minimized.

@gedankenstuecke

gedankenstuecke Nov 15, 2017

Member

breaks the form if i do that…

user_id: user.id,
has_sequence: user.has_sequence,
user_uri: "https://opensnp.org/users/#{user.id}"
}

This comment has been minimized.

@houndci-bot

houndci-bot Nov 15, 2017

Collaborator

Closing hash brace must be on the same line as the last hash element when opening brace is on the same line as the first hash element.

end
def generate_json(user)
json = { user_name: user.name,

This comment has been minimized.

@houndci-bot

houndci-bot Nov 15, 2017

Collaborator

Useless assignment to variable - json.

url = URI.parse(base_url + user.open_humans_profile.access_token)
req = Net::HTTP::Post.new(url.request_uri)
oh_profile = user.open_humans_profile
req.set_form_data({

This comment has been minimized.

@houndci-bot

houndci-bot Nov 15, 2017

Collaborator

Redundant curly braces around a hash parameter.

upload.content_type = "multipart/form-data; boundary=#{boundary}"
post_body = generate_form_body(user,boundary)
upload.body = post_body.join
puts post_body.join

This comment has been minimized.

@houndci-bot

houndci-bot Nov 15, 2017

Collaborator

Do not write to stdout. Use Rails's logger if you want to log.

end
def set_open_humans_ids(oh_profile)

This comment has been minimized.

@houndci-bot

houndci-bot Nov 15, 2017

Collaborator

Use empty lines between method definitions.

update_tokens(oh_profile,response_json)
end

This comment has been minimized.

@houndci-bot

houndci-bot Nov 15, 2017

Collaborator

Extra blank line detected.

http.use_ssl = (url.scheme == 'https')
response = http.request(req)
response_json = JSON.parse(response.body)
update_tokens(oh_profile,response_json)

This comment has been minimized.

@houndci-bot

houndci-bot Nov 15, 2017

Collaborator

Space missing after comma.

req = Net::HTTP::Post.new(url.request_uri)
req.set_form_data({'grant_type' => 'refresh_token',
'refresh_token' => oh_profile.refresh_token
})

This comment has been minimized.

@houndci-bot

houndci-bot Nov 15, 2017

Collaborator

Closing hash brace must be on the same line as the last hash element when opening brace is on the same line as the first hash element.

oh_profile = user.open_humans_profile
url = URI.parse('https://www.openhumans.org/oauth2/token/')
req = Net::HTTP::Post.new(url.request_uri)
req.set_form_data({'grant_type' => 'refresh_token',

This comment has been minimized.

@houndci-bot

houndci-bot Nov 15, 2017

Collaborator

Space inside { missing.
Redundant curly braces around a hash parameter.

})
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = (url.scheme == 'https')
#http.set_debug_output($stdout)

This comment has been minimized.

@houndci-bot

houndci-bot Nov 15, 2017

Collaborator

Missing space after #.

end
def set_open_humans_ids(oh_profile)

This comment has been minimized.

@houndci-bot

houndci-bot Nov 15, 2017

Collaborator

Use empty lines between method definitions.

update_tokens(oh_profile, response_json)
end

This comment has been minimized.

@houndci-bot

houndci-bot Nov 15, 2017

Collaborator

Extra blank line detected.

oh_profile = user.open_humans_profile
url = URI.parse('https://www.openhumans.org/oauth2/token/')
req = Net::HTTP::Post.new(url.request_uri)
req.set_form_data({ 'grant_type' => 'refresh_token',

This comment has been minimized.

@houndci-bot

houndci-bot Nov 15, 2017

Collaborator

Redundant curly braces around a hash parameter.

req.basic_auth ENV.fetch('OH_client_id'), ENV.fetch('OH_client_secret')
req.set_form_data({ 'grant_type' => 'authorization_code',
'code' => code,
'redirect_uri' => "http://localhost:3000/openhumans/authorize" })

This comment has been minimized.

@houndci-bot

houndci-bot Nov 15, 2017

Collaborator

Prefer single-quoted strings when you don't need string interpolation or special symbols.

url = URI.parse('https://www.openhumans.org/oauth2/token/')
req = Net::HTTP::Post.new(url.request_uri)
req.basic_auth ENV.fetch('OH_client_id'), ENV.fetch('OH_client_secret')
req.set_form_data({ 'grant_type' => 'authorization_code',

This comment has been minimized.

@houndci-bot

houndci-bot Nov 15, 2017

Collaborator

Redundant curly braces around a hash parameter.

class OpenHumansService
# used to do the interfacing w/ Open Humans
def get_access_tokens(user,code)

This comment has been minimized.

@houndci-bot

houndci-bot Nov 15, 2017

Collaborator

Space missing after comma.

@@ -0,0 +1,115 @@
class OpenHumansService

This comment has been minimized.

@houndci-bot

houndci-bot Nov 15, 2017

Collaborator

Missing magic comment # frozen_string_literal: true.

This comment has been minimized.

@gedankenstuecke

gedankenstuecke Nov 15, 2017

Member

is fixed…

def set_open_humans_ids(oh_profile)
uri = URI.parse('https://www.openhumans.org/api/direct-sharing/project/exchange-member/')
uri_params = {:access_token => oh_profile.access_token}

This comment has been minimized.

@houndci-bot

houndci-bot Nov 15, 2017

Collaborator

Space inside { missing.
Use the new Ruby 1.9 hash syntax.
Space inside } missing.

@user.save
end
# lets convert
oh_service.get_access_tokens(@user,@code)

This comment has been minimized.

@houndci-bot

houndci-bot Nov 15, 2017

Collaborator

Space missing after comma.

oh_service = OpenHumansService.new()
# does the user have an OH profile on openSNP? if not, create one
if @user.open_humans_profile == nil

This comment has been minimized.

@houndci-bot

houndci-bot Nov 15, 2017

Collaborator

Prefer the use of the nil? predicate.

# let's get the current user and their code
@user = current_user
@code = params[:code]
oh_service = OpenHumansService.new()

This comment has been minimized.

@houndci-bot

houndci-bot Nov 15, 2017

Collaborator

Do not use parentheses for method calls with no arguments.

# there's little to do for the start. Just read the .env for our client_id
# then lead ppl to OH.org to give us a key
client_id = ENV.fetch('OH_client_id')
redirect_to "https://www.openhumans.org/direct-sharing/projects/oauth2/authorize/?client_id=#{client_id}&response_type=code"

This comment has been minimized.

@houndci-bot

houndci-bot Nov 15, 2017

Collaborator

Line is too long. [128/100]

# frozen_string_literal: true
class OpenHumansProfilesController < ApplicationController
before_filter :require_user, except: [:index]

This comment has been minimized.

@houndci-bot

houndci-bot Nov 15, 2017

Collaborator

Prefer before_action over before_filter.

def set_open_humans_ids(oh_profile)
uri = URI.parse('https://www.openhumans.org/api/direct-sharing/project/exchange-member/')
uri_params = {:access_token => oh_profile.access_token}

This comment has been minimized.

@houndci-bot

houndci-bot Nov 15, 2017

Collaborator

Space inside { missing.
Use the new Ruby 1.9 hash syntax.
Space inside } missing.

@user.save
end
# lets convert
oh_service.get_access_tokens(@user,@code)

This comment has been minimized.

@houndci-bot

houndci-bot Nov 15, 2017

Collaborator

Space missing after comma.

oh_service = OpenHumansService.new()
# does the user have an OH profile on openSNP? if not, create one
if @user.open_humans_profile == nil

This comment has been minimized.

@houndci-bot

houndci-bot Nov 15, 2017

Collaborator

Prefer the use of the nil? predicate.

# let's get the current user and their code
@user = current_user
@code = params[:code]
oh_service = OpenHumansService.new()

This comment has been minimized.

@houndci-bot

houndci-bot Nov 15, 2017

Collaborator

Do not use parentheses for method calls with no arguments.

# there's little to do for the start. Just read the .env for our client_id
# then lead ppl to OH.org to give us a key
client_id = ENV.fetch('OH_client_id')
redirect_to "https://www.openhumans.org/direct-sharing/projects/oauth2/authorize/?client_id=#{client_id}&response_type=code"

This comment has been minimized.

@houndci-bot

houndci-bot Nov 15, 2017

Collaborator

Line is too long. [128/100]

# frozen_string_literal: true
class OpenHumansProfilesController < ApplicationController
before_filter :require_user, except: [:index]

This comment has been minimized.

@houndci-bot

houndci-bot Nov 15, 2017

Collaborator

Prefer before_action over before_filter.

req.basic_auth ENV.fetch('OH_client_id'), ENV.fetch('OH_client_secret')
req.set_form_data('grant_type' => 'authorization_code',
'code' => code,
'redirect_uri' => "http://localhost:3000/openhumans/authorize")

This comment has been minimized.

@houndci-bot

houndci-bot Nov 15, 2017

Collaborator

Prefer single-quoted strings when you don't need string interpolation or special symbols.

@gedankenstuecke

This comment has been minimized.

Member

gedankenstuecke commented Nov 15, 2017

Okay, the hound is basically happy now. I also followed the advice of @tsujigiri and put all the logic into an OpenHumansService, which makes the controller much nicer to look at.

I also figured out how to avoid duplicate data entries on side of @OpenHumans. Slowly getting there 😄

class OpenHumansService
# used to do the interfacing w/ Open Humans
def get_access_tokens(user,code)
def get_access_tokens(user, code)
# authenticate w/ our client id/secret against API
# post with the key the user provided us with.
url = URI.parse('https://www.openhumans.org/oauth2/token/')
req = Net::HTTP::Post.new(url.request_uri)
req.basic_auth ENV.fetch('OH_client_id'), ENV.fetch('OH_client_secret')

This comment has been minimized.

@tsujigiri