5
\$\begingroup\$

I am working on creating an interface gem that works with the Jira-Ruby gem.

I have several questions and would like a general review for both my code and my attempt at creating a gem (I've never written one before). Any advice on where to go next would also be appreciated.

File structure:

jira_interface
   lib
      jira_interface
          config.rb
          version.rb
          app_interface.rb 
      jira_interface.rb
   jira_interface.gemspec
   #...etc files, I used bundle's gem command to set up

My main module (jira_interface.rb) looks like this:

require "jira_interface/version"
require "jira"
require "jira_interface/config"
require "jira_interface/app_interface"

module JiraInterface

  def get_jira_client
      @client = Jira::Client.new(USERINFORMATION)
  end

  def get_project(project_name)
     @client.Project.find(project_name)
  end

end

And my app_interface.rb looks like this:

class AppInterface < JiraInterface
  before_filter :get_jira_client

  def create_issue(issue_desc, project)
    issue = @client.Issue.build
    issue.save({"fields"=>{"summary"=> issue_desc, "project"=> {"id" => project.id}, "issuetype"=> {"id"=> "1"}}})
  end

  def update_issue(issue_id)
    issue = @client.Issue.find(issue_id)

    comment = issue.comments.build
    comment.save({"body"=> "This happened again at #{Date.time}"})
  end

  def get_issues(issue_desc, project_name)
    project = get_project(project_name)

    if project.issues.detect { |issue| issue.fields['summary'].include?(issue_desc) }
      update_issue(issue.id)
    else
      create_issue(issue_desc, project)
    end
  end

end

The goal is pretty simple: call get_issues() from the rails app when an error happens. If the error is already documented, post a comment saying that it happened again. If it's not, then create a new issue.

To the best of my knowledge, this should work because I've been following the examples found online. However, I've been having a heck of a time just testing it. If anyone could suggest a good way of testing this, that would be much appreciated.

\$\endgroup\$
  • \$\begingroup\$ Aside from what I posted in my answer, your approach looks great. Naming could be improved, but like they say, it is hard. Keep the good work up! \$\endgroup\$ – CMW Oct 22 '13 at 21:16
  • \$\begingroup\$ does my answer help at all? If not, I'd very much appreciate any feedback. \$\endgroup\$ – CMW Oct 28 '13 at 20:40
2
\$\begingroup\$

I suggest you take a close look at how the gem you are using is tested. Those tests include mocks for all responses of the API (I assume) and is a good example for testing gems in general.

Notice:

  • helper methods in the support folder including shared examples
  • mock data to simulate responses from the source API
  • custom matchers to make code better readable and reduce repetition of code (DRY-principle)

Those to me are the three most important you should embrace for now.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.